From e8d731c05f205654f7d75c0a37267dcd2c615fb0 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 5 Dec 2024 14:30:43 +0100 Subject: [PATCH] fix(node/worker_threads): data url not encoded properly with eval (#27184) 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 https://github.com/denoland/deno/issues/27167 --- ext/node/polyfills/worker_threads.ts | 7 ++++++- tests/unit_node/worker_threads_test.ts | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index d4b75fb30c..1b175fb1dd 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -42,6 +42,7 @@ const { SafeWeakMap, SafeMap, TypeError, + encodeURIComponent, } = primordials; const debugWorkerThreads = false; @@ -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:") ) { diff --git a/tests/unit_node/worker_threads_test.ts b/tests/unit_node/worker_threads_test.ts index 24a9107898..808fd6116e 100644 --- a/tests/unit_node/worker_threads_test.ts +++ b/tests/unit_node/worker_threads_test.ts @@ -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() {