Skip to content

Commit

Permalink
fix(node/worker_threads): data url not encoded properly with eval (#2…
Browse files Browse the repository at this point in the history
…7184)

When using the `eval` option on Node's `worker_threads` the code is
passed as a `data:` URL. But we didn't encode the actual code for that,
which lead to syntax errors when including characters not allowed in an
URL.

Fixes a part of #27167
  • Loading branch information
marvinhagemeister authored and bartlomieju committed Dec 5, 2024
1 parent f71fe37 commit 2a0658a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ext/node/polyfills/worker_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
SafeWeakMap,
SafeMap,
TypeError,
encodeURIComponent,
} = primordials;

const debugWorkerThreads = false;
Expand Down Expand Up @@ -123,7 +124,11 @@ class NodeWorker extends EventEmitter {
);
}
if (options?.eval) {
specifier = `data:text/javascript,${specifier}`;
const code = typeof specifier === "string"
? encodeURIComponent(specifier)
// deno-lint-ignore prefer-primordials
: specifier.toString();
specifier = `data:text/javascript,${code}`;
} else if (
!(typeof specifier === "object" && specifier.protocol === "data:")
) {
Expand Down
19 changes: 19 additions & 0 deletions tests/unit_node/worker_threads_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ Deno.test({
},
});

Deno.test({
name: "[node/worker_threads] Worker eval",
async fn() {
// Check that newlines are encoded properly
const worker = new workerThreads.Worker(
`
import { parentPort } from "node:worker_threads"
console.log("hey, foo") // comment
parentPort.postMessage("It works!");
`,
{
eval: true,
},
);
assertEquals((await once(worker, "message"))[0], "It works!");
worker.terminate();
},
});

Deno.test({
name: "[node/worker_threads] worker thread with type module",
async fn() {
Expand Down

0 comments on commit 2a0658a

Please sign in to comment.