From c2b6edf9ab81794ef7523f4469ac0ec3231aef44 Mon Sep 17 00:00:00 2001 From: Bruce MacNaughton Date: Tue, 5 Dec 2023 10:54:01 -0800 Subject: [PATCH] esm: fix hook name in error message PR-URL: https://github.com/nodejs/node/pull/50466 Reviewed-By: Geoffrey Booth Reviewed-By: Jacob Smith Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- lib/internal/errors.js | 4 ++-- lib/internal/modules/esm/translators.js | 2 +- test/es-module/test-esm-loader.mjs | 5 +++++ test/fixtures/es-module-loaders/hooks-custom.mjs | 8 ++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 39e512762a470c..ad151cc2bdd227 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1475,11 +1475,11 @@ E('ERR_INVALID_REPL_EVAL_CONFIG', E('ERR_INVALID_REPL_INPUT', '%s', TypeError); E('ERR_INVALID_RETURN_PROPERTY', (input, name, prop, value) => { return `Expected a valid ${input} to be returned for the "${prop}" from the` + - ` "${name}" function but got ${determineSpecificType(value)}.`; + ` "${name}" hook but got ${determineSpecificType(value)}.`; }, TypeError); E('ERR_INVALID_RETURN_PROPERTY_VALUE', (input, name, prop, value) => { return `Expected ${input} to be returned for the "${prop}" from the` + - ` "${name}" function but got ${determineSpecificType(value)}.`; + ` "${name}" hook but got ${determineSpecificType(value)}.`; }, TypeError); E('ERR_INVALID_RETURN_VALUE', (input, name, value) => { const type = determineSpecificType(value); diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 9976b819f266c6..949dbab7f6ad6b 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -128,7 +128,7 @@ function assertBufferSource(body, allowString, hookName) { */ function stringify(body) { if (typeof body === 'string') { return body; } - assertBufferSource(body, false, 'transformSource'); + assertBufferSource(body, false, 'load'); const { TextDecoder } = require('internal/encoding'); DECODER = DECODER === null ? new TextDecoder() : DECODER; return DECODER.decode(body); diff --git a/test/es-module/test-esm-loader.mjs b/test/es-module/test-esm-loader.mjs index e19a38cc4231b0..de34814324818c 100644 --- a/test/es-module/test-esm-loader.mjs +++ b/test/es-module/test-esm-loader.mjs @@ -43,6 +43,11 @@ await assert.rejects( { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, ); +await assert.rejects(import('esmHook/commonJsNullSource.mjs'), { + code: 'ERR_INVALID_RETURN_PROPERTY_VALUE', + message: /"source".*'load'.*got type bigint/, +}); + await import('../fixtures/es-module-loaders/js-as-esm.js') .then((parsedModule) => { assert.strictEqual(typeof parsedModule, 'object'); diff --git a/test/fixtures/es-module-loaders/hooks-custom.mjs b/test/fixtures/es-module-loaders/hooks-custom.mjs index 5656f95232b856..3c38649a88794f 100644 --- a/test/fixtures/es-module-loaders/hooks-custom.mjs +++ b/test/fixtures/es-module-loaders/hooks-custom.mjs @@ -97,5 +97,13 @@ export function load(url, context, next) { }; } + if (url.endsWith('esmHook/commonJsNullSource.mjs')) { + return { + format: 'commonjs', + shortCircuit: true, + source: 1n, + }; + } + return next(url); }