Skip to content

Commit 76b80b1

Browse files
worker: add eval ts input
PR-URL: nodejs#56394 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Pietro Marchini <[email protected]>
1 parent 639db21 commit 76b80b1

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

lib/internal/main/worker_thread.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ const { setupMainThreadPort } = require('internal/worker/messaging');
4949
const {
5050
onGlobalUncaughtException,
5151
evalScript,
52+
evalTypeScript,
5253
evalModuleEntryPoint,
54+
parseAndEvalCommonjsTypeScript,
55+
parseAndEvalModuleTypeScript,
5356
} = require('internal/process/execution');
5457

5558
let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
@@ -166,7 +169,29 @@ port.on('message', (message) => {
166169
value: filename,
167170
});
168171
ArrayPrototypeSplice(process.argv, 1, 0, name);
169-
evalScript(name, filename);
172+
const tsEnabled = getOptionValue('--experimental-strip-types');
173+
const inputType = getOptionValue('--input-type');
174+
175+
if (inputType === 'module-typescript' && tsEnabled) {
176+
// This is a special case where we want to parse and eval the
177+
// TypeScript code as a module
178+
parseAndEvalModuleTypeScript(filename, false);
179+
break;
180+
}
181+
182+
let evalFunction;
183+
if (inputType === 'commonjs') {
184+
evalFunction = evalScript;
185+
} else if (inputType === 'commonjs-typescript' && tsEnabled) {
186+
evalFunction = parseAndEvalCommonjsTypeScript;
187+
} else if (tsEnabled) {
188+
evalFunction = evalTypeScript;
189+
} else {
190+
// Default to commonjs.
191+
evalFunction = evalScript;
192+
}
193+
194+
evalFunction(name, filename);
170195
break;
171196
}
172197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
const { test } = require('node:test');
6+
const { once } = require('events');
7+
8+
const esmHelloWorld = `
9+
import worker from 'worker_threads';
10+
const foo: string = 'Hello, World!';
11+
worker.parentPort.postMessage(foo);
12+
`;
13+
14+
const cjsHelloWorld = `
15+
const { parentPort } = require('worker_threads');
16+
const foo: string = 'Hello, World!';
17+
parentPort.postMessage(foo);
18+
`;
19+
20+
const disableTypeScriptWarningFlag = '--disable-warning=ExperimentalWarning';
21+
22+
test('Worker eval module typescript without input-type', async () => {
23+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: [disableTypeScriptWarningFlag] });
24+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
25+
});
26+
27+
test('Worker eval module typescript with --input-type=module-typescript', async () => {
28+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=module-typescript',
29+
disableTypeScriptWarningFlag] });
30+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
31+
});
32+
33+
test('Worker eval module typescript with --input-type=commonjs-typescript', async () => {
34+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=commonjs-typescript',
35+
disableTypeScriptWarningFlag] });
36+
37+
const [err] = await once(w, 'error');
38+
assert.strictEqual(err.name, 'SyntaxError');
39+
assert.match(err.message, /Cannot use import statement outside a module/);
40+
});
41+
42+
test('Worker eval module typescript with --input-type=module', async () => {
43+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=module',
44+
disableTypeScriptWarningFlag] });
45+
const [err] = await once(w, 'error');
46+
assert.strictEqual(err.name, 'SyntaxError');
47+
assert.match(err.message, /Missing initializer in const declaration/);
48+
});
49+
50+
test('Worker eval commonjs typescript without input-type', async () => {
51+
const w = new Worker(cjsHelloWorld, { eval: true, execArgv: [disableTypeScriptWarningFlag] });
52+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
53+
});
54+
55+
test('Worker eval commonjs typescript with --input-type=commonjs-typescript', async () => {
56+
const w = new Worker(cjsHelloWorld, { eval: true, execArgv: ['--input-type=commonjs-typescript',
57+
disableTypeScriptWarningFlag] });
58+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
59+
});
60+
61+
test('Worker eval commonjs typescript with --input-type=module-typescript', async () => {
62+
const w = new Worker(cjsHelloWorld, { eval: true, execArgv: ['--input-type=module-typescript',
63+
disableTypeScriptWarningFlag] });
64+
const [err] = await once(w, 'error');
65+
assert.strictEqual(err.name, 'ReferenceError');
66+
assert.match(err.message, /require is not defined in ES module scope, you can use import instead/);
67+
});

0 commit comments

Comments
 (0)