Skip to content

Commit

Permalink
fix(ext/node): Support env option in worker_thread (#23462)
Browse files Browse the repository at this point in the history
Fixes #23455
  • Loading branch information
littledivy authored Apr 20, 2024
1 parent 9425dce commit e0554ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ext/node/polyfills/worker_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as webidl from "ext:deno_webidl/00_webidl.js";
import { notImplemented } from "ext:deno_node/_utils.ts";
import { EventEmitter } from "node:events";
import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js";
import process from "node:process";

const { ObjectPrototypeIsPrototypeOf } = primordials;
const {
Expand Down Expand Up @@ -128,6 +129,7 @@ class NodeWorker extends EventEmitter {
const serializedWorkerMetadata = serializeJsMessageData({
workerData: options?.workerData,
environmentData: environmentData,
env: options?.env,
}, options?.transferList ?? []);
const id = op_create_worker(
{
Expand Down Expand Up @@ -348,6 +350,10 @@ internals.__initWorkerThreads = (
const { 0: metadata, 1: _ } = maybeWorkerMetadata;
workerData = metadata.workerData;
environmentData = metadata.environmentData;
const env = metadata.env;
if (env) {
process.env = env;
}
}
defaultExport.workerData = workerData;
defaultExport.parentPort = parentPort;
Expand Down
26 changes: 26 additions & 0 deletions tests/unit_node/worker_threads_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,29 @@ Deno.test("[node/worker_threads] receiveMessageOnPort works if there's pending r
port5.close();
port6.close();
});

Deno.test({
name: "[node/worker_threads] Worker env",
async fn() {
const deferred = Promise.withResolvers<void>();
const worker = new workerThreads.Worker(
`
import { parentPort } from "node:worker_threads";
import process from "node:process";
parentPort.postMessage(process.env.TEST_ENV);
`,
{
eval: true,
env: { TEST_ENV: "test" },
},
);

worker.on("message", (data) => {
assertEquals(data, "test");
deferred.resolve();
});

await deferred.promise;
await worker.terminate();
},
});

0 comments on commit e0554ac

Please sign in to comment.