From 17df773507ce20429f5d5bdb1e405db671f25e99 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Tue, 2 Apr 2024 20:41:32 +0900 Subject: [PATCH] Avoid direct `global` accessment. --- package.json | 2 +- .../workers/internal/processes/ProcessChannel.ts | 8 ++++---- src/protocols/workers/internal/threads/ThreadPort.ts | 2 +- src/utils/internal/NodeModule.ts | 11 ++++------- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 6e3d997..3975517 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tgrid", - "version": "0.10.0", + "version": "0.10.1", "main": "lib/index.js", "typings": "lib/index.d.ts", "exports": { diff --git a/src/protocols/workers/internal/processes/ProcessChannel.ts b/src/protocols/workers/internal/processes/ProcessChannel.ts index 72cb5fb..820ffda 100644 --- a/src/protocols/workers/internal/processes/ProcessChannel.ts +++ b/src/protocols/workers/internal/processes/ProcessChannel.ts @@ -5,20 +5,20 @@ import { NodeModule } from "../../../../utils/internal/NodeModule"; */ export class ProcessChannel { public static postMessage(message: any): void { - NodeModule.process().send!(message); + NodeModule.process.get().send!(message); } public static close(): void { - NodeModule.process().exit(); + NodeModule.process.get().exit(); } public static set onmessage(listener: (event: MessageEvent) => void) { - NodeModule.process().on("message", (msg) => { + NodeModule.process.get().on("message", (msg) => { listener({ data: msg } as MessageEvent); }); } public static is_worker_server(): boolean { - return !!NodeModule.process().send; + return !!NodeModule.process.get().send; } } diff --git a/src/protocols/workers/internal/threads/ThreadPort.ts b/src/protocols/workers/internal/threads/ThreadPort.ts index 6598228..124eebd 100644 --- a/src/protocols/workers/internal/threads/ThreadPort.ts +++ b/src/protocols/workers/internal/threads/ThreadPort.ts @@ -12,7 +12,7 @@ export async function ThreadPort() { const { parentPort } = await NodeModule.thread.get(); if (!parentPort) throw new Error("This is not a worker thread."); - const process = NodeModule.process(); + const process = NodeModule.process.get(); class ThreadPort { public static postMessage(message: any): void { parentPort!.postMessage(message); diff --git a/src/utils/internal/NodeModule.ts b/src/utils/internal/NodeModule.ts index 833af79..e013320 100644 --- a/src/utils/internal/NodeModule.ts +++ b/src/utils/internal/NodeModule.ts @@ -5,7 +5,7 @@ import type * as __https from "https"; import import2 from "import2"; import type * as __os from "os"; import type * as __process from "process"; -import { Singleton } from "tstl"; +import { Singleton, is_node } from "tstl"; import type * as __thread from "worker_threads"; import type * as __ws from "ws"; @@ -31,10 +31,7 @@ export namespace NodeModule { export const ws: Singleton> = new Singleton( () => import("ws"), ); - export const process = () => __global.process; + export const process: Singleton = new Singleton(() => + is_node() ? global.process : undefined!, + ); } - -/** - * @internal - */ -const __global = global;