1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(node): Worker constructor doesn't check type: module of package.json (#19480)

This commit is contained in:
Vedant Pandey 2023-06-15 20:30:30 +05:30 committed by Bartek Iwańczuk
parent c966697cc8
commit 8e720515d3
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
5 changed files with 31 additions and 1 deletions

View file

@ -0,0 +1,3 @@
import { myFunction } from "./other_file.js";
myFunction().then(() => {});

View file

@ -0,0 +1,3 @@
export async function myFunction() {
await new Promise((resolve) => setTimeout(resolve, 100));
}

View file

@ -0,0 +1,4 @@
{
"name": "foo",
"type": "module"
}

View file

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

View file

@ -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}");})();`;