From e927df1f2af571aa4511f05e4d26d6ae5d09abb0 Mon Sep 17 00:00:00 2001 From: bmacnaughton Date: Mon, 30 Oct 2023 05:03:49 -0700 Subject: [PATCH 1/7] esm: change transformSource to load for error --- lib/internal/modules/esm/translators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 7a62615cfe4210..4d652d08fcdd09 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -116,7 +116,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); From 4939e5ab3956bfd4ede8d94c5aecf4e05e828d5e Mon Sep 17 00:00:00 2001 From: bmacnaughton Date: Sun, 5 Nov 2023 03:28:55 -0800 Subject: [PATCH 2/7] add test for "load" name --- test/es-module/test-esm-loader.mjs | 8 ++++++++ test/fixtures/es-module-loaders/hooks-custom.mjs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/test/es-module/test-esm-loader.mjs b/test/es-module/test-esm-loader.mjs index e19a38cc4231b0..f1729b69ef8293 100644 --- a/test/es-module/test-esm-loader.mjs +++ b/test/es-module/test-esm-loader.mjs @@ -43,6 +43,14 @@ await assert.rejects( { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, ); +await assert.rejects( + import('esmHook/commonJsNullSource.mjs'), + { + code: 'ERR_INVALID_RETURN_PROPERTY_VALUE', + message: 'Expected array buffer, or typed array to be returned for the "source" from the "load" function but got null.' + }, +); + 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..b8ca0b972f4c05 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: null, + }; + } + return next(url); } From c84b64cef3d98fe8652dc516615594e40a1ebd05 Mon Sep 17 00:00:00 2001 From: bmacnaughton Date: Sun, 5 Nov 2023 03:59:49 -0800 Subject: [PATCH 3/7] shorten line with regex --- test/es-module/test-esm-loader.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/es-module/test-esm-loader.mjs b/test/es-module/test-esm-loader.mjs index f1729b69ef8293..8db2180b47251b 100644 --- a/test/es-module/test-esm-loader.mjs +++ b/test/es-module/test-esm-loader.mjs @@ -47,7 +47,7 @@ await assert.rejects( import('esmHook/commonJsNullSource.mjs'), { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE', - message: 'Expected array buffer, or typed array to be returned for the "source" from the "load" function but got null.' + message: /for the "source" from the "load" function but got null/, }, ); From 1a44c89b52e3234e9344f5b4fe352c6d5fa9a85e Mon Sep 17 00:00:00 2001 From: bmacnaughton Date: Sun, 5 Nov 2023 17:28:49 -0800 Subject: [PATCH 4/7] address PR feedback --- lib/internal/errors.js | 4 ++-- test/es-module/test-esm-loader.mjs | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 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/test/es-module/test-esm-loader.mjs b/test/es-module/test-esm-loader.mjs index 8db2180b47251b..f6e1b02302a7d1 100644 --- a/test/es-module/test-esm-loader.mjs +++ b/test/es-module/test-esm-loader.mjs @@ -43,13 +43,14 @@ await assert.rejects( { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, ); -await assert.rejects( - import('esmHook/commonJsNullSource.mjs'), - { - code: 'ERR_INVALID_RETURN_PROPERTY_VALUE', - message: /for the "source" from the "load" function but got null/, - }, -); +try { + await import('esmHook/commonJsNullSource.mjs'); +} catch (e) { + assert.strictEqual(e.code, 'ERR_INVALID_RETURN_PROPERTY_VALUE'); + assert(e.message.includes('"source"')); + assert(e.message.includes('"load"')); + assert(e.message.includes('got null')); +} await import('../fixtures/es-module-loaders/js-as-esm.js') .then((parsedModule) => { From e71c1ee36f372500e76c5d40924607328b246118 Mon Sep 17 00:00:00 2001 From: bmacnaughton Date: Sun, 5 Nov 2023 20:01:07 -0800 Subject: [PATCH 5/7] change to assert.match() --- test/es-module/test-esm-loader.mjs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/es-module/test-esm-loader.mjs b/test/es-module/test-esm-loader.mjs index f6e1b02302a7d1..6472d55bce6f6c 100644 --- a/test/es-module/test-esm-loader.mjs +++ b/test/es-module/test-esm-loader.mjs @@ -45,11 +45,11 @@ await assert.rejects( try { await import('esmHook/commonJsNullSource.mjs'); -} catch (e) { - assert.strictEqual(e.code, 'ERR_INVALID_RETURN_PROPERTY_VALUE'); - assert(e.message.includes('"source"')); - assert(e.message.includes('"load"')); - assert(e.message.includes('got null')); +} catch ({ code, message }) { + assert.strictEqual(code, 'ERR_INVALID_RETURN_PROPERTY_VALUE'); + assert.match(message, /"source"/); + assert.match(message, /"load"/); + assert.match(message, /got null/); } await import('../fixtures/es-module-loaders/js-as-esm.js') From 91f780cd85285260c7b7b0bdb4e79030db276399 Mon Sep 17 00:00:00 2001 From: bmacnaughton Date: Thu, 30 Nov 2023 03:20:32 -0800 Subject: [PATCH 6/7] fix test --- test/es-module/test-esm-loader.mjs | 4 ++-- test/fixtures/es-module-loaders/hooks-custom.mjs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/es-module/test-esm-loader.mjs b/test/es-module/test-esm-loader.mjs index 6472d55bce6f6c..d5c7816fdda124 100644 --- a/test/es-module/test-esm-loader.mjs +++ b/test/es-module/test-esm-loader.mjs @@ -48,8 +48,8 @@ try { } catch ({ code, message }) { assert.strictEqual(code, 'ERR_INVALID_RETURN_PROPERTY_VALUE'); assert.match(message, /"source"/); - assert.match(message, /"load"/); - assert.match(message, /got null/); + assert.match(message, /'load'/); + assert.match(message, /got type bigint/); } await import('../fixtures/es-module-loaders/js-as-esm.js') diff --git a/test/fixtures/es-module-loaders/hooks-custom.mjs b/test/fixtures/es-module-loaders/hooks-custom.mjs index b8ca0b972f4c05..3c38649a88794f 100644 --- a/test/fixtures/es-module-loaders/hooks-custom.mjs +++ b/test/fixtures/es-module-loaders/hooks-custom.mjs @@ -101,7 +101,7 @@ export function load(url, context, next) { return { format: 'commonjs', shortCircuit: true, - source: null, + source: 1n, }; } From 934406adfc1273561897cfc808993faf87781c61 Mon Sep 17 00:00:00 2001 From: Bruce MacNaughton Date: Thu, 30 Nov 2023 04:48:53 -0800 Subject: [PATCH 7/7] Update test/es-module/test-esm-loader.mjs Co-authored-by: Antoine du Hamel --- test/es-module/test-esm-loader.mjs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/es-module/test-esm-loader.mjs b/test/es-module/test-esm-loader.mjs index d5c7816fdda124..de34814324818c 100644 --- a/test/es-module/test-esm-loader.mjs +++ b/test/es-module/test-esm-loader.mjs @@ -43,14 +43,10 @@ await assert.rejects( { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, ); -try { - await import('esmHook/commonJsNullSource.mjs'); -} catch ({ code, message }) { - assert.strictEqual(code, 'ERR_INVALID_RETURN_PROPERTY_VALUE'); - assert.match(message, /"source"/); - assert.match(message, /'load'/); - assert.match(message, /got type bigint/); -} +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) => {