diff --git a/package.json b/package.json index a959f52..ca49c99 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tgrid", - "version": "1.0.0", + "version": "1.0.1", "main": "lib/index.js", "typings": "lib/index.d.ts", "exports": { diff --git a/src/protocols/workers/WorkerConnector.ts b/src/protocols/workers/WorkerConnector.ts index c5fc236..3931fb0 100644 --- a/src/protocols/workers/WorkerConnector.ts +++ b/src/protocols/workers/WorkerConnector.ts @@ -200,7 +200,7 @@ export class WorkerConnector< const compiler: IWorkerCompiler = await this.compiler_.get(); this.worker_ = await compiler.execute( jsFile, - is_node() === true ? options.execArgv : undefined, + is_node() === true ? options : undefined, ); // WAIT THE WORKER TO BE READY @@ -348,5 +348,12 @@ export namespace WorkerConnector { * Arguments only for the NodeJS environments. */ execArgv: string[]; + + /** + * Whether to redirect the standard input to the worker server. + * + * Available only in the NodeJS + Process environments. + */ + stdio: "overlapped" | "pipe" | "ignore" | "inherit"; } } diff --git a/src/protocols/workers/internal/IWorkerCompiler.ts b/src/protocols/workers/internal/IWorkerCompiler.ts index f53776d..49d3ef1 100644 --- a/src/protocols/workers/internal/IWorkerCompiler.ts +++ b/src/protocols/workers/internal/IWorkerCompiler.ts @@ -1,10 +1,15 @@ +import { WorkerConnector } from "../WorkerConnector"; + /** * @internal */ export interface IWorkerCompiler { compile(content: string): Promise; remove(path: string): Promise; - execute(jsFile: string, argv: string[] | undefined): Promise; + execute( + jsFile: string, + options?: Partial, + ): Promise; } /** @@ -12,6 +17,9 @@ export interface IWorkerCompiler { */ export namespace IWorkerCompiler { export type Creator = { - new (jsFile: string, execArgv: string[] | undefined): IWorkerCompiler; + new ( + jsFile: string, + options?: Partial, + ): IWorkerCompiler; }; } diff --git a/src/protocols/workers/internal/NodeWorkerCompiler.ts b/src/protocols/workers/internal/NodeWorkerCompiler.ts index 551912c..dc90281 100644 --- a/src/protocols/workers/internal/NodeWorkerCompiler.ts +++ b/src/protocols/workers/internal/NodeWorkerCompiler.ts @@ -10,10 +10,10 @@ import { ThreadWorker } from "./threads/ThreadWorker"; export const NodeWorkerCompiler = async ( type: "process" | "thread", ): Promise => ({ - execute: async (jsFile, execArg) => { + execute: async (jsFile, options) => { const factory = type === "process" ? await ProcessWorker() : await ThreadWorker(); - return (new factory(jsFile, execArg)) as Worker; + return (new factory(jsFile, options)) as Worker; }, compile: async (content) => { const os = await NodeModule.os.get(); diff --git a/src/protocols/workers/internal/processes/ProcessWorker.ts b/src/protocols/workers/internal/processes/ProcessWorker.ts index c6a7453..4469ca2 100644 --- a/src/protocols/workers/internal/processes/ProcessWorker.ts +++ b/src/protocols/workers/internal/processes/ProcessWorker.ts @@ -1,6 +1,7 @@ import type cp from "child_process"; import { NodeModule } from "../../../../utils/internal/NodeModule"; +import { WorkerConnector } from "../../WorkerConnector"; import { IWorkerCompiler } from "../IWorkerCompiler"; /** @@ -12,8 +13,14 @@ export async function ProcessWorker(): Promise { class ProcessWorker { private process_: cp.ChildProcess; - public constructor(jsFile: string, execArgv: string[] | undefined) { - this.process_ = fork(jsFile, { execArgv }); + public constructor( + jsFile: string, + options?: Partial, + ) { + this.process_ = fork(jsFile, { + execArgv: options?.execArgv, + stdio: options?.stdio, + }); } public terminate(): void { diff --git a/src/protocols/workers/internal/threads/ThreadWorker.ts b/src/protocols/workers/internal/threads/ThreadWorker.ts index 3f985e2..21df9ab 100644 --- a/src/protocols/workers/internal/threads/ThreadWorker.ts +++ b/src/protocols/workers/internal/threads/ThreadWorker.ts @@ -1,6 +1,7 @@ import type thread from "worker_threads"; import { NodeModule } from "../../../../utils/internal/NodeModule"; +import { WorkerConnector } from "../../WorkerConnector"; import { IWorkerCompiler } from "../IWorkerCompiler"; /** @@ -11,8 +12,13 @@ export async function ThreadWorker(): Promise { class ThreadWorker { private readonly worker_: thread.Worker; - public constructor(jsFile: string, execArgv: string[] | undefined) { - this.worker_ = new Worker(jsFile, { execArgv }); + public constructor( + jsFile: string, + arg?: Partial, + ) { + this.worker_ = new Worker(jsFile, { + execArgv: arg?.execArgv, + }); } public terminate(): void { diff --git a/test/node/protocols/workers/internal/loud.ts b/test/node/protocols/workers/internal/loud.ts new file mode 100644 index 0000000..90d04d4 --- /dev/null +++ b/test/node/protocols/workers/internal/loud.ts @@ -0,0 +1,27 @@ +import { WorkerServer } from "tgrid"; + +import { IScientific } from "../../../../controllers/ICalculator"; + +class ScientificCalculator implements IScientific { + public pow(x: number, y: number): number { + console.log("pow", x, y); + return Math.pow(x, y); + } + public sqrt(x: number): number { + console.log("sqrt", x); + return Math.sqrt(x); + } + public log(x: number, y: number): number { + console.log("log", x, y); + return Math.log(x) / Math.log(y); + } +} + +async function main(): Promise { + const server = new WorkerServer(); + await server.open(new ScientificCalculator()); +} +main().catch((exp) => { + console.log(exp); + process.exit(-1); +}); diff --git a/test/node/protocols/workers/test_worker_stdio.ts b/test/node/protocols/workers/test_worker_stdio.ts new file mode 100644 index 0000000..ef03cf4 --- /dev/null +++ b/test/node/protocols/workers/test_worker_stdio.ts @@ -0,0 +1,16 @@ +import { Driver, WorkerConnector } from "tgrid"; + +import { IScientific } from "../../../controllers/ICalculator"; + +export async function test_worker_stdio(): Promise { + const connector = new WorkerConnector(null, null, "process"); + await connector.connect(`${__dirname}/internal/loud.js`, { + stdio: "ignore", + }); + + const driver: Driver = connector.getDriver(); + await driver.pow(2, 4); + await driver.sqrt(16); + + await connector.close(); +}