From 0c50c39c35f1c92bbb96bc3e101e2c446256cb7b Mon Sep 17 00:00:00 2001 From: Vedant Pandey Date: Thu, 15 Jun 2023 20:30:30 +0530 Subject: fix(node): Worker constructor doesn't check type: module of package.json (#19480) --- cli/tests/unit_node/testdata/worker_module/index.js | 3 +++ cli/tests/unit_node/testdata/worker_module/other_file.js | 3 +++ cli/tests/unit_node/testdata/worker_module/package.json | 4 ++++ cli/tests/unit_node/worker_threads_test.ts | 10 ++++++++++ ext/node/polyfills/worker_threads.ts | 12 +++++++++++- 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 cli/tests/unit_node/testdata/worker_module/index.js create mode 100644 cli/tests/unit_node/testdata/worker_module/other_file.js create mode 100644 cli/tests/unit_node/testdata/worker_module/package.json diff --git a/cli/tests/unit_node/testdata/worker_module/index.js b/cli/tests/unit_node/testdata/worker_module/index.js new file mode 100644 index 000000000..a3e976b65 --- /dev/null +++ b/cli/tests/unit_node/testdata/worker_module/index.js @@ -0,0 +1,3 @@ +import { myFunction } from "./other_file.js"; + +myFunction().then(() => {}); diff --git a/cli/tests/unit_node/testdata/worker_module/other_file.js b/cli/tests/unit_node/testdata/worker_module/other_file.js new file mode 100644 index 000000000..41789dfe8 --- /dev/null +++ b/cli/tests/unit_node/testdata/worker_module/other_file.js @@ -0,0 +1,3 @@ +export async function myFunction() { + await new Promise((resolve) => setTimeout(resolve, 100)); +} diff --git a/cli/tests/unit_node/testdata/worker_module/package.json b/cli/tests/unit_node/testdata/worker_module/package.json new file mode 100644 index 000000000..486d2f82b --- /dev/null +++ b/cli/tests/unit_node/testdata/worker_module/package.json @@ -0,0 +1,4 @@ +{ + "name": "foo", + "type": "module" +} diff --git a/cli/tests/unit_node/worker_threads_test.ts b/cli/tests/unit_node/worker_threads_test.ts index f53b1e692..c46df057e 100644 --- a/cli/tests/unit_node/worker_threads_test.ts +++ b/cli/tests/unit_node/worker_threads_test.ts @@ -133,6 +133,16 @@ Deno.test({ }, }); +Deno.test({ + name: "[worker_threads] worker thread with type module", + fn() { + const worker = new workerThreads.Worker( + new URL("./testdata/worker_module/index.js", import.meta.url), + ); + worker.terminate(); + }, +}); + Deno.test({ name: "[worker_threads] inheritences", async fn() { diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index 8005506bb..32f9885af 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -9,6 +9,7 @@ import { MessageChannel, MessagePort } from "ext:deno_web/13_message_port.js"; let environmentData = new Map(); let threads = 0; +const { core } = globalThis.__bootstrap; export interface WorkerOptions { // only for typings @@ -53,7 +54,16 @@ class _Worker extends EventEmitter { specifier = `data:text/javascript,${specifier}`; } else if (typeof specifier === "string") { specifier = resolve(specifier); - if (!specifier.toString().endsWith(".mjs")) { + let pkg; + try { + pkg = core.ops.op_require_read_closest_package_json(specifier); + } catch (_) { + // empty catch block when package json might not be present + } + if ( + !(specifier.toString().endsWith(".mjs") || + (pkg && pkg.exists && pkg.typ == "module")) + ) { const cwdFileUrl = toFileUrl(Deno.cwd()); specifier = `data:text/javascript,(async function() {const { createRequire } = await import("node:module");const require = createRequire("${cwdFileUrl}");require("${specifier}");})();`; -- cgit v1.2.3