From b424959d3e2554a83dd6a7a9c8837805a3d9ae65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 24 Apr 2024 17:17:28 +0100 Subject: fix(ext/node): worker_threads copies env object (#23536) Most common argument to `env` option for `worker_threads.Worker` will be `process.env`. In Deno `process.env` is a `Proxy` which can't be cloned using structured clone algorithm. So to be safe, I'm creating a copy of actual object before it's sent to the worker thread. Ref #23522 --- tests/unit_node/worker_threads_test.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'tests/unit_node') diff --git a/tests/unit_node/worker_threads_test.ts b/tests/unit_node/worker_threads_test.ts index a96896ce5..bc2becd66 100644 --- a/tests/unit_node/worker_threads_test.ts +++ b/tests/unit_node/worker_threads_test.ts @@ -10,6 +10,7 @@ import { import { fromFileUrl, relative, SEPARATOR } from "@std/path/mod.ts"; import * as workerThreads from "node:worker_threads"; import { EventEmitter, once } from "node:events"; +import process from "node:process"; Deno.test("[node/worker_threads] BroadcastChannel is exported", () => { assertEquals(workerThreads.BroadcastChannel, BroadcastChannel); @@ -486,3 +487,31 @@ Deno.test({ await worker.terminate(); }, }); + +Deno.test({ + name: "[node/worker_threads] Worker env using process.env", + async fn() { + const deferred = Promise.withResolvers(); + const worker = new workerThreads.Worker( + ` + import { parentPort } from "node:worker_threads"; + import process from "node:process"; + parentPort.postMessage("ok"); + `, + { + eval: true, + // Make sure this doesn't throw `DataCloneError`. + // See https://github.com/denoland/deno/issues/23522. + env: process.env, + }, + ); + + worker.on("message", (data) => { + assertEquals(data, "ok"); + deferred.resolve(); + }); + + await deferred.promise; + await worker.terminate(); + }, +}); -- cgit v1.2.3