mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 00:21:05 -05:00
fix(ext/node): Support env
option in worker_thread (#23462)
Fixes https://github.com/denoland/deno/issues/23455
This commit is contained in:
parent
9425dce6db
commit
e0554ac4a2
2 changed files with 32 additions and 0 deletions
|
@ -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 {
|
||||
|
@ -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(
|
||||
{
|
||||
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue