Skip to content

Commit

Permalink
Merge pull request #60 from samchon/features/refactoring
Browse files Browse the repository at this point in the history
Compatible with https://esm.sh
  • Loading branch information
samchon authored Mar 27, 2024
2 parents 80cc914 + cf17fef commit 2881f43
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tgrid",
"version": "0.9.3",
"version": "0.9.7",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"description": "Grid Computing Framework for TypeScript",
Expand All @@ -24,16 +24,14 @@
"dependencies": {
"import2": "^1.0.3",
"serialize-error": "^4.1.0",
"tstl": "^2.5.13",
"uuid": "^9.0.1",
"tstl": "^2.5.16",
"ws": "^7.5.3"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/browserify": "^12.0.40",
"@types/node": "^20.11.26",
"@types/puppeteer": "^7.0.4",
"@types/uuid": "^9.0.8",
"@types/ws": "^7.4.7",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
Expand Down
10 changes: 8 additions & 2 deletions src/protocols/workers/internal/NodeWorkerCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { v4 } from "uuid";
import { NodeModule } from "../../../utils/internal/NodeModule";

import { FileSystem } from "./FileSystem";
Expand All @@ -23,7 +22,7 @@ export const NodeWorkerCompiler = async (
if ((await FileSystem.exists(path)) === false) await FileSystem.mkdir(path);

while (true) {
const myPath: string = `${path}/${v4()}.js`;
const myPath: string = `${path}/${uuid()}.js`;
if ((await FileSystem.exists(myPath)) === false) {
path = myPath;
break;
Expand All @@ -43,3 +42,10 @@ export const NodeWorkerCompiler = async (
// return new this.factory_(jsFile, execArgv) as any;
// }
});

const uuid = () =>
"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
10 changes: 6 additions & 4 deletions src/protocols/workers/internal/processes/ProcessChannel.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { NodeModule } from "../../../../utils/internal/NodeModule";

/**
* @hidden
*/
export class ProcessChannel {
public static postMessage(message: any): void {
(global.process as Required<NodeJS.Process>).send(message);
NodeModule.process().send!(message);
}

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

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

public static is_worker_server(): boolean {
return !!global.process.send;
return !!NodeModule.process().send;
}
}
3 changes: 2 additions & 1 deletion src/protocols/workers/internal/threads/ThreadPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { NodeModule } from "../../../../utils/internal/NodeModule";
*/
export async function ThreadPort() {
const { parentPort } = await NodeModule.thread.get();
const process = NodeModule.process();
return {
postMessage: (message: any) => parentPort!.postMessage(message),
close: () => global.process.exit(0),
close: () => process.exit(0),
onmessage: (listener: (event: MessageEvent) => void) =>
parentPort!.on("message", (message) =>
listener({ data: message } as MessageEvent),
Expand Down
7 changes: 7 additions & 0 deletions src/utils/internal/NodeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type * as __https from "https";
import type * as __os from "os";
import type * as __thread from "worker_threads";
import type * as __ws from "ws";
import type * as __process from "process";

import import2 from "import2";
import { Singleton } from "tstl/thread/Singleton";
Expand All @@ -31,4 +32,10 @@ export namespace NodeModule {
export const ws: Singleton<Promise<typeof __ws>> = new Singleton(
() => import("ws"),
);
export const process = () => __global.process;
}

/**
* @internal
*/
const __global = global;

0 comments on commit 2881f43

Please sign in to comment.