|
| 1 | +import EventEmitter from "node:events" |
| 2 | +import * as crypto from "node:crypto" |
| 3 | + |
| 4 | +import ipc from "node-ipc" |
| 5 | + |
| 6 | +import { IpcOrigin, IpcMessageType, IpcMessage, ipcMessageSchema, TaskCommand, TaskEvent } from "@benchmark/types" |
| 7 | + |
| 8 | +export type IpcClientEvents = { |
| 9 | + [IpcMessageType.Connect]: [] |
| 10 | + [IpcMessageType.Disconnect]: [] |
| 11 | + [IpcMessageType.Ack]: [clientId: string] |
| 12 | + [IpcMessageType.TaskCommand]: [data: TaskCommand] |
| 13 | + [IpcMessageType.TaskEvent]: [data: TaskEvent] |
| 14 | +} |
| 15 | + |
| 16 | +export class IpcClient extends EventEmitter<IpcClientEvents> { |
| 17 | + private readonly _socketPath: string |
| 18 | + private readonly _id: string |
| 19 | + private readonly _log: (...args: unknown[]) => void |
| 20 | + private _isConnected = false |
| 21 | + private _clientId?: string |
| 22 | + |
| 23 | + constructor(socketPath: string, log = console.log) { |
| 24 | + super() |
| 25 | + |
| 26 | + this._socketPath = socketPath |
| 27 | + this._id = `benchmark-${crypto.randomBytes(6).toString("hex")}` |
| 28 | + this._log = log |
| 29 | + |
| 30 | + ipc.config.silent = true |
| 31 | + |
| 32 | + ipc.connectTo(this._id, this.socketPath, () => { |
| 33 | + ipc.of[this._id]?.on("connect", () => this.onConnect()) |
| 34 | + ipc.of[this._id]?.on("disconnect", () => this.onDisconnect()) |
| 35 | + ipc.of[this._id]?.on("message", (data) => this.onMessage(data)) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + private onConnect() { |
| 40 | + if (this._isConnected) { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + this.log("[client#onConnect]") |
| 45 | + this._isConnected = true |
| 46 | + this.emit(IpcMessageType.Connect) |
| 47 | + } |
| 48 | + |
| 49 | + private onDisconnect() { |
| 50 | + if (!this._isConnected) { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + this.log("[client#onDisconnect]") |
| 55 | + this._isConnected = false |
| 56 | + this.emit(IpcMessageType.Disconnect) |
| 57 | + } |
| 58 | + |
| 59 | + private onMessage(data: unknown) { |
| 60 | + if (typeof data !== "object") { |
| 61 | + this._log("[client#onMessage] invalid data", data) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + const result = ipcMessageSchema.safeParse(data) |
| 66 | + |
| 67 | + if (!result.success) { |
| 68 | + this.log("[client#onMessage] invalid payload", data) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + const payload = result.data |
| 73 | + |
| 74 | + if (payload.origin === IpcOrigin.Server) { |
| 75 | + switch (payload.type) { |
| 76 | + case IpcMessageType.Ack: |
| 77 | + this._clientId = payload.data.clientId |
| 78 | + this.emit(IpcMessageType.Ack, payload.data.clientId) |
| 79 | + break |
| 80 | + case IpcMessageType.TaskEvent: |
| 81 | + this.emit(IpcMessageType.TaskEvent, payload.data) |
| 82 | + break |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private log(...args: unknown[]) { |
| 88 | + this._log(...args) |
| 89 | + } |
| 90 | + |
| 91 | + public sendMessage(message: IpcMessage) { |
| 92 | + ipc.of[this._id]?.emit("message", message) |
| 93 | + } |
| 94 | + |
| 95 | + public disconnect() { |
| 96 | + try { |
| 97 | + ipc.disconnect(this._id) |
| 98 | + // @TODO: Should we set _disconnect here? |
| 99 | + } catch (error) { |
| 100 | + this.log("[client#disconnect] error disconnecting", error) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public get socketPath() { |
| 105 | + return this._socketPath |
| 106 | + } |
| 107 | + |
| 108 | + public get clientId() { |
| 109 | + return this._clientId |
| 110 | + } |
| 111 | + |
| 112 | + public get isConnected() { |
| 113 | + return this._isConnected |
| 114 | + } |
| 115 | + |
| 116 | + public get isReady() { |
| 117 | + return this._isConnected && this._clientId !== undefined |
| 118 | + } |
| 119 | +} |
0 commit comments