// /* eslint-disable no-console */ // import { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js"; // import cluster from "cluster"; // import { existsSync, unlinkSync } from "fs"; // import { platform } from "os"; // import { isMainThread } from "worker_threads"; // import { WebSocketServer, WebSocket, } from "ws"; // const socketPath = /win32/i.test(platform()) ? `\\\\.\\pipe\\social-mail-ipc-sock.sock` : `/tmp/social-mail-ipc-sock.sock`; // let pid = 1; // @RegisterSingleton // export default class GlobalEvents { // // eslint-disable-next-line @typescript-eslint/ban-types // private listeners = new Map(); // private client: Promise; // public on(m: "refresh-dns", fx: (m1: "refresh-dns", value: { serverID }) => any); // public on(m: "dkim-added", fx: (m1: "dkim-added", value: { serverID, host, publicKey, name, keyType }) => any); // public on(m: "host-added", fx: (m1: "host-added", value: { serverID, host: string }) => any); // public on(m: "html-to-image" | "html-to-image-result", value: any); // public on(message: any, value: (msg: any, data:any) => any) { // let list = this.listeners.get(message); // if(!list) { // list = []; // this.listeners.set(message, list); // } // list.push(value); // this.connectClient().catch(console.error); // return { // [Symbol.dispose]: () => { // const index = list.indexOf(value); // if (index !== -1) { // list.splice(index, 1); // } // } // }; // } // public onPost(m: "html-to-image", fx: (v) => Promise) { // this.on(m, (m1, v) => { // const { id } = v; // try { // fx(v).then( // (value) => this.notify(id, { value } as any), // (error) => this.notify(id, { error } as any)); // } catch (e) { // this.notify(id, { error: e} as any); // } // }); // } // public post(m: "html-to-image", value: any): Promise; // public async post(m: any, p: any): Promise { // const id = `${m}-${pid++}`; // return new Promise((resolve, reject) => { // const d = this.on("html-to-image", ({ value, error }) => { // d[Symbol.dispose](); // if (error) { // reject(error); // } else { // resolve(value); // } // }); // p.id = id; // this.notify(m, p); // }); // } // public notify(m: "refresh-dns", value: { serverID }); // public notify(m: "dkim-added", value: { serverID, host, publicKey, name, keyType }); // public notify(m: "host-added", value: { serverID, host: string }); // public notify(message: any, value: any) { // this.notifyInternal(message, value).catch(console.error); // } // startListener() { // if(!cluster.isPrimary) { // return; // } // if(!isMainThread) { // return; // } // this.startListenerInternal().catch(console.error); // } // private async notifyInternal(message: string, value: string) { // try { // const client = await this.connectClient(); // const json = JSON.stringify({ message, value }); // const data = Buffer.from(json, "utf-8"); // client.send(data, { binary: true }, (error) => { // if (error) { // try { // client.close(); // } catch { // // do nothing // } // this.client = null; // return; // } // }); // // console.log(`Notify success ${json}`); // } catch (error) { // console.error(error); // this.client?.then((c) => { // try { // c.close(); // } catch { // // do nothing // } // this.client = null; // }).catch(console.error); // this.client = null; // } // } // private connectClient() { // this.client ??= (async () => { // let client: WebSocket; // try { // client = await new Promise((resolve, reject) => { // const s = new WebSocket(`ws+unix://${socketPath}`); // s.on("open", () => resolve(s)); // s.on("error", reject); // }); // client.on("error", (err) => { // console.error(err); // this.client = null; // }); // client.on("end", () => { // this.client = null; // }); // client.on("close", () => { // this.client = null; // }); // } catch (error) { // console.error(`No socket is listening on ${socketPath}`); // console.error(error.stack ?? error.toString()); // this.client = null; // } // // start listening... // client.on("message", (data: Buffer, isBinary ) => { // const text = data.toString("utf-8"); // // console.log(`Received ${text}`); // const { message, value } = JSON.parse(text); // const list = this.listeners.get(message); // if (!list) { // return; // } // for (const iterator of list) { // iterator(message, value)?.catch(console.error); // } // }); // return client; // })(); // return this.client; // } // private async startListenerInternal() { // try { // if(existsSync(socketPath)) { // unlinkSync(socketPath); // } // const server = new WebSocketServer({ // port: socketPath as any, // }, () => { // console.log(`Global Events listening on ${socketPath}`); // }); // server.on("connection", (socket) => { // socket.on("message", (data, binary) => { // for (const iterator of server.clients) { // iterator.send(data, { binary }, (error) => { // if (error) { // try { iterator.close(); } catch { // // so nothing // } // } // }); // } // }); // }); // server.on("error", console.error); // } catch (error) { // console.error(error); // } // } // // private async writeToClient(key: Socket,text: Buffer, promise: Promise): Promise { // // await promise; // // try { // // await new Promise((resolve, reject) => { // // key.write(text, (error) => { // // if (error) { // // reject(error); // // return; // // } // // resolve(); // // }); // // }); // // } catch (error) { // // try { // // key.end(); // // } catch { // // // closing errored socket... // // } // // this.serverClients.delete(key); // // } // // } // } // export class TestGlobalEvents extends GlobalEvents { // // eslint-disable-next-line @typescript-eslint/ban-types // map = new Map(); // on(m, fx){ // const { map } = this; // let list = map.get(m); // if(!list) { // list = []; // map.set(m, list); // } // list.push(fx); // return { // [Symbol.dispose]() { // // do nothing // } // }; // } // notify(m, v) { // const { map } = this; // const list = map.get(m); // if(!list) { // return list; // } // for (const iterator of list) { // iterator(m, v)?.catch(console.error); // } // } // startListener() { // return Promise.resolve(); // } // }