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:
parent
a26b873a7d
commit
e8d731c05f
2 changed files with 25 additions and 1 deletions
|
@ -42,6 +42,7 @@ const {
|
||||||
SafeWeakMap,
|
SafeWeakMap,
|
||||||
SafeMap,
|
SafeMap,
|
||||||
TypeError,
|
TypeError,
|
||||||
|
encodeURIComponent,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
const debugWorkerThreads = false;
|
const debugWorkerThreads = false;
|
||||||
|
@ -123,7 +124,11 @@ class NodeWorker extends EventEmitter {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (options?.eval) {
|
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 (
|
} else if (
|
||||||
!(typeof specifier === "object" && specifier.protocol === "data:")
|
!(typeof specifier === "object" && specifier.protocol === "data:")
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -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({
|
Deno.test({
|
||||||
name: "[node/worker_threads] worker thread with type module",
|
name: "[node/worker_threads] worker thread with type module",
|
||||||
async fn() {
|
async fn() {
|
||||||
|
|
Loading…
Reference in a new issue