From fe729ec354faeb09c789704ac126ff14c3b1ba93 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 02:40:12 +0200 Subject: [PATCH] esm: refactor test-esm-loader-resolve-type PR-URL: https://github.com/nodejs/node/pull/49493 Backport-PR-URL: https://github.com/nodejs/node/pull/50669 Reviewed-By: Jacob Smith Reviewed-By: Antoine du Hamel --- .../test-esm-loader-resolve-type.mjs | 10 ++-- .../hook-resolve-type-loader.mjs | 18 ++++++ .../es-module-loaders/hook-resolve-type.mjs | 60 +++++++------------ 3 files changed, 47 insertions(+), 41 deletions(-) create mode 100644 graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs diff --git a/graal-nodejs/test/es-module/test-esm-loader-resolve-type.mjs b/graal-nodejs/test/es-module/test-esm-loader-resolve-type.mjs index 05a41dd5158..4c5e7aede8f 100644 --- a/graal-nodejs/test/es-module/test-esm-loader-resolve-type.mjs +++ b/graal-nodejs/test/es-module/test-esm-loader-resolve-type.mjs @@ -16,7 +16,7 @@ try { { recursive: true } ); - deepStrictEqual(await spawnPromisified( + const output = await spawnPromisified( execPath, [ '--no-warnings', @@ -29,15 +29,17 @@ try { console.log(JSON.stringify({ before, after }));`, ], { cwd: base }, - ), { + ); + + deepStrictEqual(output, { + code: 0, + signal: null, stderr: '', stdout: JSON.stringify({ before: { importedESM: 0, importedCJS: 0 }, // Dynamic import in the eval script should increment ESM counter but not CJS counter after: { importedESM: 1, importedCJS: 0 }, }) + '\n', - code: 0, - signal: null, }); } finally { await rm(base, { recursive: true, force: true }); diff --git a/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs new file mode 100644 index 00000000000..f2dc0aba5ca --- /dev/null +++ b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs @@ -0,0 +1,18 @@ +/** @type {MessagePort} */ +let port; +export function initialize(data) { + port = data.port; +} + +export async function resolve(specifier, context, next) { + const nextResult = await next(specifier, context); + const { format } = nextResult; + + if (format === 'module' || specifier.endsWith('.mjs')) { + port.postMessage({ type: 'module' }); + } else if (format == null || format === 'commonjs') { + port.postMessage({ type: 'commonjs' }); + } + + return nextResult; +} diff --git a/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs index a4d87938ad8..6fa7a553b8e 100644 --- a/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs +++ b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs @@ -1,44 +1,30 @@ +import * as fixtures from '../../common/fixtures.mjs'; +import { register } from 'node:module'; +import { MessageChannel } from 'node:worker_threads'; + let importedESM = 0; let importedCJS = 0; +export function getModuleTypeStats() { + return { importedESM, importedCJS }; +}; -export function globalPreload({ port }) { - port.on('message', (int32) => { - port.postMessage({ importedESM, importedCJS }); - Atomics.store(int32, 0, 1); - Atomics.notify(int32, 0); - }); - port.unref(); - return ` - const { receiveMessageOnPort } = getBuiltin('worker_threads'); - global.getModuleTypeStats = async function getModuleTypeStats() { - const sab = new SharedArrayBuffer(4); - const int32 = new Int32Array(sab); - port.postMessage(int32); - // Artificial timeout to keep the event loop alive. - // https://bugs.chromium.org/p/v8/issues/detail?id=13238 - // TODO(targos) Remove when V8 issue is resolved. - const timeout = setTimeout(() => { throw new Error('timeout'); }, 1_000); - await Atomics.waitAsync(int32, 0, 0).value; - clearTimeout(timeout); - return receiveMessageOnPort(port).message; - }; - `; -} - -export async function load(url, context, next) { - return next(url); -} +const { port1, port2 } = new MessageChannel(); -export async function resolve(specifier, context, next) { - const nextResult = await next(specifier, context); - const { format } = nextResult; +register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), { + data: { port: port2 }, + transferList: [port2], +}); - if (format === 'module' || specifier.endsWith('.mjs')) { - importedESM++; - } else if (format == null || format === 'commonjs') { - importedCJS++; +port1.on('message', ({ type }) => { + switch (type) { + case 'module': + importedESM++; + break; + case 'commonjs': + importedCJS++; + break; } +}); - return nextResult; -} - +port1.unref(); +port2.unref();