From 655111fa00c800568838ae6a901decbb995bfcf0 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 30 May 2023 11:56:29 +0200 Subject: [PATCH] esm: handle `globalPreload` hook returning a nullish value PR-URL: https://github.com/nodejs/node/pull/48249 Backport-PR-URL: https://github.com/nodejs/node/pull/50669 Fixes: https://github.com/nodejs/node/issues/48240 Reviewed-By: Geoffrey Booth Reviewed-By: Jacob Smith --- lib/internal/modules/esm/hooks.js | 2 +- test/es-module/test-esm-loader-hooks.mjs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index d7f8528d5f3..982a5c6dbf3 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -168,7 +168,7 @@ class Hooks { port: insideLoader, }); - if (preloaded == null) { return; } + if (preloaded == null) { continue; } if (typeof preloaded !== 'string') { // [2] throw new ERR_INVALID_RETURN_VALUE( diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index e36a6f04258..97f25f2eaa1 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -348,6 +348,20 @@ describe('Loader hooks', { concurrency: true }, () => { }); }); + it('should handle globalPreload returning undefined', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-loader', + 'data:text/javascript,export function globalPreload(){}', + fixtures.path('empty.js'), + ]); + + assert.strictEqual(stderr, ''); + assert.strictEqual(stdout, ''); + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + }); + it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => { const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ '--no-warnings',