Skip to content

Commit

Permalink
Fix NodeModule.process() again for esm.sh bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Apr 2, 2024
1 parent 17df773 commit 09b902f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/protocols/workers/internal/processes/ProcessChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { NodeModule } from "../../../../utils/internal/NodeModule";
*/
export class ProcessChannel {
public static postMessage(message: any): void {
NodeModule.process.get().send!(message);
NodeModule.process().send!(message);
}

public static close(): void {
NodeModule.process.get().exit();
NodeModule.process().exit();
}

public static set onmessage(listener: (event: MessageEvent) => void) {
NodeModule.process.get().on("message", (msg) => {
NodeModule.process().on("message", (msg) => {
listener({ data: msg } as MessageEvent);
});
}

public static is_worker_server(): boolean {
return !!NodeModule.process.get().send;
return !!NodeModule.process().send;
}
}
2 changes: 1 addition & 1 deletion src/protocols/workers/internal/threads/ThreadPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.get();
const process = NodeModule.process();
class ThreadPort {
public static postMessage(message: any): void {
parentPort!.postMessage(message);
Expand Down
9 changes: 6 additions & 3 deletions src/utils/internal/NodeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export namespace NodeModule {
export const ws: Singleton<Promise<typeof __ws>> = new Singleton(
() => import("ws"),
);
export const process: Singleton<typeof __process> = new Singleton(() =>
is_node() ? global.process : undefined!,
);
export const process = () => {
if (__global === undefined) throw new Error("Not a node environment");
return __global.process;
};
}

const __global = is_node() ? global : undefined;

0 comments on commit 09b902f

Please sign in to comment.