From fc90d7c63aad09c4cb69e9343efea257a55bad8e Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 19 Sep 2024 12:08:50 +0200 Subject: [PATCH] worker: handle `--input-type` more consistently MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/54979 Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca Reviewed-By: Marco Ippolito --- lib/internal/main/worker_thread.js | 4 +++- test/parallel/test-worker-cli-options.js | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index fe516ae38297ce..ab244293667bce 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -55,6 +55,7 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => { }); const assert = require('internal/assert'); +const { getOptionValue } = require('internal/options'); const { exitCodes: { kGenericUserError } } = internalBinding('errors'); prepareWorkerThreadExecution(); @@ -152,7 +153,7 @@ port.on('message', (message) => { break; } - case 'classic': { + case 'classic': if (getOptionValue('--input-type') !== 'module') { const { evalScript } = require('internal/process/execution'); const name = '[worker eval]'; // This is necessary for CJS module compilation. @@ -168,6 +169,7 @@ port.on('message', (message) => { break; } + // eslint-disable-next-line no-fallthrough case 'module': { const { evalModuleEntryPoint } = require('internal/process/execution'); PromisePrototypeThen(evalModuleEntryPoint(filename), undefined, (e) => { diff --git a/test/parallel/test-worker-cli-options.js b/test/parallel/test-worker-cli-options.js index ae59dcb6e62d48..0c243d251e97bc 100644 --- a/test/parallel/test-worker-cli-options.js +++ b/test/parallel/test-worker-cli-options.js @@ -1,6 +1,6 @@ // Flags: --expose-internals --expose-gc 'use strict'; -require('../common'); +const common = require('../common'); const { Worker } = require('worker_threads'); const assert = require('assert'); @@ -29,3 +29,11 @@ new Worker(CODE, { eval: true, env: process.env, execArgv: ['--expose-internals' assert.throws(() => { new Worker(CODE, { eval: true, execArgv: ['--expose-gc'] }); }, /ERR_WORKER_INVALID_EXEC_ARGV/); + +// Test ESM eval +new Worker('export {}', { eval: true, execArgv: ['--input-type=module'] }); +new Worker('export {}', { eval: true, execArgv: ['--input-type=commonjs'] }) + .once('error', common.expectsError({ name: 'SyntaxError' })); +new Worker('export {}', { eval: true, execArgv: ['--experimental-detect-module'] }); +new Worker('export {}', { eval: true, execArgv: ['--no-experimental-detect-module'] }) + .once('error', common.expectsError({ name: 'SyntaxError' }));