1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 05:14:21 -05:00

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
This commit is contained in:
Marvin Hagemeister 2024-12-05 14:30:43 +01:00 committed by GitHub
parent a26b873a7d
commit e8d731c05f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View file

@ -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:")
) {

View file

@ -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() {