worker: strip types in Web Worker module entries · nodejs/node@19732fc · GitHub
Skip to content

Commit 19732fc

Browse files
osztenkurdenaduh95
authored andcommitted
worker: strip types in Web Worker module entries
Strip TypeScript from file-backed module worker entries before evaluating the fetched source. Add regression coverage and document the supported entry types. Assisted-by: Codex Signed-off-by: Hubert Walczak <hubertwalczak8@gmail.com> PR-URL: #66085 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 6c927c0 commit 19732fc

7 files changed

Lines changed: 111 additions & 3 deletions

File tree

‎doc/api/globals.md‎

Lines changed: 6 additions & 0 deletions

‎lib/internal/webworker.js‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const {
2525
globalThis,
2626
} = primordials;
2727

28+
const { extname } = require('path');
29+
2830
const {
2931
ERR_ILLEGAL_CONSTRUCTOR,
3032
ERR_INVALID_STATE,
@@ -37,6 +39,8 @@ const {
3739
initEventTarget,
3840
} = require('internal/event_target');
3941

42+
const { getOptionValue } = require('internal/options');
43+
4044
const {
4145
assignFunctionName,
4246
defineOperation,
@@ -89,6 +93,7 @@ const {
8993
const {
9094
URL,
9195
URLParse,
96+
fileURLToPath,
9297
} = require('internal/url');
9398

9499
const {
@@ -117,6 +122,9 @@ let scopeBaseURL = null;
117122
const lazyErrorEvent =
118123
getLazy(() => require('internal/deps/undici/undici').ErrorEvent);
119124

125+
const lazyStripTypeScriptModuleTypes = getLazy(
126+
() => require('internal/modules/typescript').stripTypeScriptModuleTypes);
127+
120128
function createErrorEvent(init) {
121129
const ErrorEvent = lazyErrorEvent();
122130
return new ErrorEvent('error', init);
@@ -253,10 +261,20 @@ function runClassicScriptSource(source, url) {
253261
* @returns {Promise}
254262
*/
255263
function runModuleScriptSource(source, url) {
256-
// Necessary to reset RegExp statics before user code runs.
257-
RegExpPrototypeExec(/^/, '');
258264
return require('internal/modules/run_main').runEntryPointWithESMLoader(
259-
(loader) => loader.eval(source, url, true),
265+
(loader) => {
266+
const parsedURL = new URL(url);
267+
if (parsedURL.protocol === 'file:' && getOptionValue('--strip-types')) {
268+
const filename = fileURLToPath(parsedURL);
269+
const extension = extname(filename);
270+
if (extension === '.ts' || extension === '.mts' || extension === '.cts') {
271+
source = lazyStripTypeScriptModuleTypes()(source, filename, url);
272+
}
273+
}
274+
// Necessary to reset RegExp statics before user code runs.
275+
RegExpPrototypeExec(/^/, '');
276+
return loader.eval(source, url, true);
277+
},
260278
);
261279
}
262280

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value: number = 42;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const value: number = 1;
2+
postMessage(value);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { value } from './dependency.ts';
2+
3+
const result: number = value;
4+
postMessage(result);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Flags: --experimental-web-worker --no-strip-types
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('node:assert');
6+
const fixtures = require('../common/fixtures');
7+
8+
// Without type stripping, annotated `.ts` entries fail to parse.
9+
const worker = new Worker(fixtures.fileURL('web-worker', 'typescript', 'entry.ts'), { type: 'module' });
10+
worker.onmessage = common.mustNotCall('types must not be stripped');
11+
worker.onerror = common.mustCall(({ error }) => {
12+
assert.strictEqual(error.name, 'SyntaxError');
13+
});
Lines changed: 64 additions & 0 deletions

0 commit comments

Comments
 (0)