Skip to content

Commit ff3ec1d

Browse files
committedDec 29, 2024
worker: add eval ts input
1 parent 67b647e commit ff3ec1d

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed
 

Diff for: ‎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

Diff for: ‎test/parallel/test-worker-eval-typescript.js

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

0 commit comments

Comments
 (0)