Skip to content

Commit

Permalink
fix (worker-eval): fail worker with source when eval = false (#13062)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Johnston <[email protected]>
Co-authored-by: Jarred Sumner <[email protected]>
  • Loading branch information
3 people authored Aug 4, 2024
1 parent 6fbe3d8 commit 483af7c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/js/node/worker_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,15 @@ class Worker extends EventEmitter {

const builtinsGeneratorHatesEval = "ev" + "a" + "l"[0];
if (options && builtinsGeneratorHatesEval in options) {
if (options[builtinsGeneratorHatesEval]) {
const blob = new Blob([filename], { 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;
}
delete options[builtinsGeneratorHatesEval];
const blob = new Blob([filename], { type: "" });
this.#urlToRevoke = filename = URL.createObjectURL(blob);
}
try {
this.#worker = new WebWorker(filename, options);
Expand Down
27 changes: 27 additions & 0 deletions test/js/web/workers/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,31 @@ describe("worker_threads", () => {
expect(process.argv).toEqual(original_argv);
expect(process.execArgv).toEqual(original_execArgv);
});

test("worker with eval = false fails with code", async () => {
let has_error = false;
try {
const worker = new wt.Worker("console.log('this should not get printed')", { eval: false });
} catch (err) {
expect(err.constructor.name).toEqual("TypeError");
expect(err.message).toMatch(/BuildMessage: ModuleNotFound.+/);
has_error = true;
}
expect(has_error).toBe(true);
});

test("worker with eval = true succeeds with valid code", async () => {
let message;
const worker = new wt.Worker("postMessage('hello')", { eval: true });
worker.on('message', e => {
message = e;
});
const p = new Promise((resolve, reject) => {
worker.on('error', reject);
worker.on('exit', resolve);
})
await p;
expect(message).toEqual("hello");
});

});

0 comments on commit 483af7c

Please sign in to comment.