Commit | Line | Data |
---|---|---|
8ff2efc7 | 1 | import { |
ded253e2 | 2 | availableParallelism, |
8ff2efc7 | 3 | ClusterWorker, |
3a502712 | 4 | DynamicThreadPool, |
8ff2efc7 | 5 | } from 'poolifier' |
02999424 | 6 | import { type RawData, WebSocketServer } from 'ws' |
ded253e2 | 7 | |
02999424 JB |
8 | import { |
9 | type ClusterWorkerData, | |
10 | type ClusterWorkerResponse, | |
11 | type DataPayload, | |
12 | type MessagePayload, | |
8ff2efc7 JB |
13 | MessageType, |
14 | type ThreadWorkerData, | |
3a502712 | 15 | type ThreadWorkerResponse, |
02999424 | 16 | } from './types.js' |
02999424 JB |
17 | |
18 | const emptyFunction = (): void => { | |
8ea47589 | 19 | /* Intentional */ |
02999424 JB |
20 | } |
21 | ||
02999424 | 22 | class WebSocketServerWorker extends ClusterWorker< |
3a502712 JB |
23 | ClusterWorkerData, |
24 | ClusterWorkerResponse | |
02999424 | 25 | > { |
3d49c6d2 JB |
26 | private static wss: WebSocketServer |
27 | private static requestHandlerPool: DynamicThreadPool< | |
3a502712 JB |
28 | ThreadWorkerData<DataPayload>, |
29 | ThreadWorkerResponse<DataPayload> | |
3d49c6d2 JB |
30 | > |
31 | ||
8ff2efc7 JB |
32 | private static readonly startWebSocketServer = ( |
33 | workerData?: ClusterWorkerData | |
34 | ): ClusterWorkerResponse => { | |
7d9756e3 | 35 | const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } = |
3a502712 | 36 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
67f3f2d6 | 37 | workerData! |
8ff2efc7 | 38 | |
63ff88a9 | 39 | WebSocketServerWorker.requestHandlerPool = new DynamicThreadPool< |
3a502712 JB |
40 | ThreadWorkerData<DataPayload>, |
41 | ThreadWorkerResponse<DataPayload> | |
63ff88a9 | 42 | >( |
d0ed34c9 JB |
43 | minWorkers ?? 1, |
44 | maxWorkers ?? availableParallelism(), | |
45 | workerFile, | |
7d9756e3 | 46 | poolOptions |
63ff88a9 JB |
47 | ) |
48 | ||
d0ed34c9 JB |
49 | WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { |
50 | console.info( | |
6e5d7052 | 51 | `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port.toString()}/` |
d0ed34c9 JB |
52 | ) |
53 | }) | |
54 | ||
041dc05b | 55 | WebSocketServerWorker.wss.on('connection', ws => { |
8ff2efc7 JB |
56 | ws.on('error', console.error) |
57 | ws.on('message', (message: RawData) => { | |
58 | const { type, data } = JSON.parse( | |
6e5d7052 | 59 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
8ff2efc7 JB |
60 | message.toString() |
61 | ) as MessagePayload<DataPayload> | |
62 | switch (type) { | |
63 | case MessageType.echo: | |
63ff88a9 | 64 | WebSocketServerWorker.requestHandlerPool |
8ff2efc7 | 65 | .execute({ data }, 'echo') |
041dc05b | 66 | .then(response => { |
8ff2efc7 JB |
67 | ws.send( |
68 | JSON.stringify({ | |
69 | type: MessageType.echo, | |
3a502712 | 70 | data: response.data, |
8ff2efc7 JB |
71 | }) |
72 | ) | |
fefd3cef | 73 | return undefined |
8ff2efc7 JB |
74 | }) |
75 | .catch(emptyFunction) | |
76 | break | |
77 | case MessageType.factorial: | |
63ff88a9 | 78 | WebSocketServerWorker.requestHandlerPool |
8ff2efc7 | 79 | .execute({ data }, 'factorial') |
041dc05b | 80 | .then(response => { |
8ff2efc7 | 81 | ws.send( |
66f0c14c JB |
82 | JSON.stringify( |
83 | { | |
84 | type: MessageType.factorial, | |
3a502712 | 85 | data: response.data, |
66f0c14c | 86 | }, |
6e5d7052 JB |
87 | (_, v: unknown) => |
88 | typeof v === 'bigint' ? v.toString() : v | |
66f0c14c | 89 | ) |
8ff2efc7 | 90 | ) |
fefd3cef | 91 | return undefined |
8ff2efc7 JB |
92 | }) |
93 | .catch(emptyFunction) | |
94 | break | |
95 | } | |
96 | }) | |
97 | }) | |
98 | return { | |
99 | status: true, | |
3a502712 | 100 | port: WebSocketServerWorker.wss.options.port, |
8ff2efc7 JB |
101 | } |
102 | } | |
103 | ||
02999424 | 104 | public constructor () { |
5daad283 | 105 | super(WebSocketServerWorker.startWebSocketServer, { |
cb5d91cf | 106 | killHandler: async () => { |
cb5d91cf | 107 | await WebSocketServerWorker.requestHandlerPool.destroy() |
93b097ac | 108 | WebSocketServerWorker.wss.close() |
3a502712 | 109 | }, |
5daad283 | 110 | }) |
02999424 JB |
111 | } |
112 | } | |
113 | ||
114 | export const webSocketServerWorker = new WebSocketServerWorker() |