From 84b0ead758872d88df950a05b26d40a497f5b630 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 9fb943e3f58252..2f3549ee04a021 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1354,11 +1354,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 d1bc2754fc3ce3..2c5efa61a6936c 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -113,7 +113,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); }