Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type option to Worker constructor #31760

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,15 @@ if (isMainThread) {
<!-- YAML
added: v10.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/31760
description: The `type` option was introduced.
- version:
- v13.13.0
- v12.17.0
pr-url: https://github.com/nodejs/node/pull/32278
description: The `transferList` option was introduced.
- version:
- v13.12.0
- v12.17.0
- version: v13.12.0
pr-url: https://github.com/nodejs/node/pull/31664
description: The `filename` parameter can be a WHATWG `URL` object using
`file:` protocol.
Expand All @@ -587,7 +588,8 @@ changes:
* `filename` {string|URL} The path to the Worker’s main script or module. Must
be either an absolute path or a relative path (i.e. relative to the
current working directory) starting with `./` or `../`, or a WHATWG `URL`
object using `file:` protocol.
object using `file:` or `data:` protocol.
When using a [`data:` URL][], you must set the `type` option to `"module"`.
If `options.eval` is `true`, this is a string containing JavaScript code
rather than a path.
* `options` {Object}
Expand All @@ -600,9 +602,13 @@ changes:
to specify that the parent thread and the child thread should share their
environment variables; in that case, changes to one thread’s `process.env`
object will affect the other thread as well. **Default:** `process.env`.
* `eval` {boolean} If `true` and the first argument is a `string`, interpret
the first argument to the constructor as a script that is executed once the
worker is online.
* `eval` {boolean} If `true`, interprets the first argument to the constructor
as a script (or a module if `type` is set to `module`) that is executed once
the worker is online.
* `type` {string} If `"module"`, interprets the first argument as an
ECMAScript 2015 module instead of a script. The default value is
`"classic"`. Doesn't have any effect when `filename` refers to a file, in
which case the type is inferred from the file extension.
* `execArgv` {string[]} List of node CLI options passed to the worker.
V8 options (such as `--max-old-space-size`) and options that affect the
process (such as `--title`) are not supported. If set, this will be provided
Expand Down Expand Up @@ -865,3 +871,4 @@ active handle in the event system. If the worker is already `unref()`ed calling
[child processes]: child_process.html
[contextified]: vm.html#vm_what_does_it_mean_to_contextify_an_object
[v8.serdes]: v8.html#v8_serialization_api
[`data:` URL]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
4 changes: 2 additions & 2 deletions lib/internal/main/eval_stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
const { getOptionValue } = require('internal/options');

const {
evalModule,
evalModuleOrCrash,
evalScript,
readStdin
} = require('internal/process/execution');
Expand All @@ -24,7 +24,7 @@ readStdin((code) => {

const print = getOptionValue('--print');
if (getOptionValue('--input-type') === 'module')
evalModule(code, print);
evalModuleOrCrash(code, print);
else
evalScript('[stdin]',
code,
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');
const { evalModule, evalScript } = require('internal/process/execution');
const { evalModuleOrCrash, evalScript } = require('internal/process/execution');
const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');

const { getOptionValue } = require('internal/options');
Expand All @@ -18,7 +18,7 @@ markBootstrapComplete();
const source = getOptionValue('--eval');
const print = getOptionValue('--print');
if (getOptionValue('--input-type') === 'module')
evalModule(source, print);
evalModuleOrCrash(source, print);
else
evalScript('[eval]',
source,
Expand Down
31 changes: 20 additions & 11 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// message port.

const {
ArrayPrototypeSplice,
ObjectDefineProperty,
} = primordials;

Expand Down Expand Up @@ -98,6 +99,7 @@ port.on('message', (message) => {
cwdCounter,
filename,
doEval,
workerType,
workerData,
publicPort,
manifestSrc,
Expand Down Expand Up @@ -148,17 +150,24 @@ port.on('message', (message) => {
`(eval = ${eval}) at cwd = ${process.cwd()}`);
port.postMessage({ type: UP_AND_RUNNING });
if (doEval) {
const { evalScript } = require('internal/process/execution');
const name = '[worker eval]';
// This is necessary for CJS module compilation.
// TODO: pass this with something really internal.
ObjectDefineProperty(process, '_eval', {
configurable: true,
enumerable: true,
value: filename,
});
process.argv.splice(1, 0, name);
evalScript(name, filename);
if (workerType === 'module') {
const { evalModule } = require('internal/process/execution');
evalModule(filename).catch((e) => {
workerOnGlobalUncaughtException(e, true);
});
} else {
const { evalScript } = require('internal/process/execution');
const name = '[worker eval]';
// This is necessary for CJS module compilation.
// TODO: pass this with something really internal.
ObjectDefineProperty(process, '_eval', {
configurable: true,
enumerable: true,
value: filename,
});
ArrayPrototypeSplice(process.argv, 1, 0, name);
evalScript(name, filename);
}
} else {
// script filename
// runMain here might be monkey-patched by users in --require.
Expand Down
16 changes: 11 additions & 5 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
FunctionPrototypeApply,
JSONStringify,
PromiseResolve,
} = primordials;
Expand Down Expand Up @@ -43,16 +44,20 @@ function evalModule(source, print) {
if (print) {
throw new ERR_EVAL_ESM_CANNOT_PRINT();
}
const { log, error } = require('internal/console/global');
const { decorateErrorStack } = require('internal/util');
const { log } = require('internal/console/global');
const asyncESM = require('internal/process/esm_loader');
PromiseResolve(asyncESM.ESMLoader).then(async (loader) => {
return PromiseResolve(asyncESM.ESMLoader).then(async (loader) => {
const { result } = await loader.eval(source);
if (print) {
log(result);
}
})
.catch((e) => {
});
}

function evalModuleOrCrash() {
const { error } = require('internal/console/global');
const { decorateErrorStack } = require('internal/util');
FunctionPrototypeApply(evalModule, this, arguments).catch((e) => {
decorateErrorStack(e);
error(e);
process.exit(1);
Expand Down Expand Up @@ -216,6 +221,7 @@ module.exports = {
readStdin,
tryGetCwd,
evalModule,
evalModuleOrCrash,
evalScript,
onGlobalUncaughtException: createOnGlobalUncaughtException(),
setUncaughtExceptionCaptureCallback,
Expand Down
30 changes: 28 additions & 2 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const {
ArrayIsArray,
JSONStringify,
MathMax,
ObjectCreate,
ObjectEntries,
Expand Down Expand Up @@ -44,6 +45,7 @@ const {
} = workerIo;
const { deserializeError } = require('internal/error-serdes');
const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url');
const { validateString } = require('internal/validators');

const {
ownsProcessState,
Expand Down Expand Up @@ -98,7 +100,7 @@ class Worker extends EventEmitter {
argv = options.argv.map(String);
}

let url;
let url, doEval;
if (options.eval) {
if (typeof filename !== 'string') {
throw new ERR_INVALID_ARG_VALUE(
Expand All @@ -108,7 +110,20 @@ class Worker extends EventEmitter {
);
}
url = null;
doEval = true;
} else if (filename?.protocol === 'data:') {
if (options.type !== 'module') {
throw new ERR_INVALID_ARG_VALUE(
'options.type',
options.type,
'must be set to "module" when filename is a data: URL'
);
}
url = null;
doEval = true;
filename = `import ${JSONStringify(filename)}`;
} else {
doEval = false;
if (isURLInstance(filename)) {
url = filename;
filename = fileURLToPath(filename);
Expand All @@ -130,6 +145,16 @@ class Worker extends EventEmitter {
throw new ERR_WORKER_UNSUPPORTED_EXTENSION(ext);
}
}
if (options.type) {
validateString(options.type, 'options.type');
if (options.type !== 'module' && options.type !== 'classic') {
throw new ERR_INVALID_ARG_VALUE(
'options.type',
options.type,
'must be either "module" or "classic"'
);
}
}

let env;
if (typeof options.env === 'object' && options.env !== null) {
Expand Down Expand Up @@ -196,7 +221,8 @@ class Worker extends EventEmitter {
argv,
type: messageTypes.LOAD_SCRIPT,
filename,
doEval: !!options.eval,
doEval,
workerType: options.type,
cwdCounter: cwdCounter || workerIo.sharedCwdCounter,
workerData: options.workerData,
publicPort: port2,
Expand Down
62 changes: 62 additions & 0 deletions test/parallel/test-worker-eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';
require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');

const moduleString = 'export default import.meta.url;';
const cjsString = 'module.exports=__filename;';

const cjsLoadingESMError = /Unexpected token 'export'/;
const esmLoadingCJSError = /module is not defined/;
const invalidValueError =
/The argument 'options\.type' must be either "module" or "classic"/;
const invalidValueError2 =
/The argument 'options\.type' must be set to "module" when filename is a data: URL/;

function runInWorker(evalString, options) {
return new Promise((resolve, reject) => {
const worker = new Worker(evalString, options);
worker.on('error', reject);
worker.on('exit', resolve);
});
}

{
const options = { eval: true };
assert.rejects(runInWorker(moduleString, options), cjsLoadingESMError);
runInWorker(cjsString, options); // does not reject
}

{
const options = { eval: true, type: 'classic' };
assert.rejects(runInWorker(moduleString, options), cjsLoadingESMError);
runInWorker(cjsString, options); // does not reject
}

{
const options = { eval: true, type: 'module' };
runInWorker(moduleString, options); // does not reject
assert.rejects(runInWorker(cjsString, options), esmLoadingCJSError);
}

{
const options = { eval: false, type: 'module' };
runInWorker(new URL(`data:text/javascript,${moduleString}`),
options); // does not reject
assert.rejects(runInWorker(new URL(`data:text/javascript,${cjsString}`),
options), esmLoadingCJSError);
}

{
const options = { eval: true, type: 'wasm' };
assert.rejects(runInWorker(moduleString, options), invalidValueError);
assert.rejects(runInWorker(cjsString, options), invalidValueError);
}

{
const options = { type: 'classic' };
assert.rejects(runInWorker(new URL(`data:text/javascript,${moduleString}`),
options), invalidValueError2);
assert.rejects(runInWorker(new URL(`data:text/javascript,${cjsString}`),
options), invalidValueError2);
}
19 changes: 19 additions & 0 deletions test/parallel/test-worker-type-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@ const { Worker } = require('worker_threads');
);
});
}

{
[
Symbol('test'),
{},
[],
() => {}
].forEach((type) => {
assert.throws(
() => new Worker('', { type, eval: true }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.type" property must be of type string.' +
common.invalidArgTypeHelper(type)
}
);
});
}
2 changes: 0 additions & 2 deletions test/parallel/test-worker-unsupported-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,4 @@ const { Worker } = require('worker_threads');
};
assert.throws(() => { new Worker(new URL('https://www.url.com')); },
expectedErr);
assert.throws(() => { new Worker(new URL('data:application/javascript,')); },
expectedErr);
}