Skip to content

implement test-worker-type-check #19194

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

Closed
wants to merge 1 commit into from
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
17 changes: 11 additions & 6 deletions src/js/node/worker_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const SHARE_ENV = Symbol("nodejs.worker_threads.SHARE_ENV");
const isMainThread = Bun.isMainThread;
const { 0: _workerData, 1: _threadId, 2: _receiveMessageOnPort } = $cpp("Worker.cpp", "createNodeWorkerThreadsBinding");

type NodeWorkerOptions = import("node:worker_threads").WorkerOptions;

// Used to ensure that Blobs created to hold the source code for `eval: true` Workers get cleaned up
// after their Worker exits
let urlRevokeRegistry: FinalizationRegistry<string> | undefined = undefined;
Expand Down Expand Up @@ -212,29 +210,36 @@ class Worker extends EventEmitter {
#urlToRevoke = "";
#isRunning = false;

constructor(filename: string, options: NodeWorkerOptions = {}) {
constructor(filename: string | URL, options: import("node:worker_threads").WorkerOptions = {}) {
super();

if (typeof filename !== "string" && !(filename instanceof URL)) {
throw $ERR_INVALID_ARG_TYPE("filename", ["string", "an instance of URL"], filename);
}

for (const key of unsupportedOptions) {
if (key in options && options[key] != null) {
warnNotImplementedOnce(`worker_threads.Worker option "${key}"`);
}
}

const filenameAsString = filename instanceof URL ? filename.toString() : filename;

const builtinsGeneratorHatesEval = "ev" + "a" + "l"[0];
if (options && builtinsGeneratorHatesEval in options) {
if (options[builtinsGeneratorHatesEval]) {
// TODO: consider doing this step in native code and letting the Blob be cleaned up by the
// C++ Worker object's destructor
const blob = new Blob([filename], { type: "" });
const blob = new Blob([filenameAsString], { type: "" });
this.#urlToRevoke = filename = URL.createObjectURL(blob);
} else {
// if options.eval = false, allow the constructor below to fail, if
// we convert the code to a blob, it will succeed.
this.#urlToRevoke = filename;
this.#urlToRevoke = filenameAsString;
}
}
try {
this.#worker = new WebWorker(filename, options);
this.#worker = new WebWorker(filenameAsString, options);
} catch (e) {
if (this.#urlToRevoke) {
URL.revokeObjectURL(this.#urlToRevoke);
Expand Down
29 changes: 29 additions & 0 deletions test/js/node/test/parallel/test-worker-type-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');

{
[
undefined,
null,
false,
0,
Symbol('test'),
{},
[],
() => {},
].forEach((val) => {
assert.throws(
() => new Worker(val),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "filename" argument must be of type string ' +
'or an instance of URL.' +
common.invalidArgTypeHelper(val)
}
);
});
}