import { type RawData, WebSocketServer } from 'ws'
-import { type DataPayload, type MessagePayload, MessageType } from './types.js'
+
import { requestHandlerPool } from './pool.js'
+import { type DataPayload, type MessagePayload, MessageType } from './types.js'
const port = 8080
const wss = new WebSocketServer({ port }, () => {
console.info(
- `⚡️[ws server]: WebSocket server is started at ws://localhost:${port}/`
+ `⚡️[ws server]: WebSocket server is started at ws://localhost:${port.toString()}/`
)
})
const emptyFunction = (): void => {
- /** Intentional */
+ /* Intentional */
}
-wss.on('connection', (ws) => {
+wss.on('connection', ws => {
ws.on('error', console.error)
ws.on('message', (message: RawData) => {
const { type, data } = JSON.parse(
case MessageType.echo:
requestHandlerPool
.execute({ data }, 'echo')
- .then((response) => {
+ .then(response => {
ws.send(
JSON.stringify({
type: MessageType.echo,
- data: response.data
+ data: response.data,
})
)
- return null
+ return undefined
})
.catch(emptyFunction)
break
case MessageType.factorial:
requestHandlerPool
.execute({ data }, 'factorial')
- .then((response) => {
+ .then(response => {
ws.send(
- JSON.stringify({
- type: MessageType.factorial,
- data: response.data
- })
+ JSON.stringify(
+ {
+ type: MessageType.factorial,
+ data: response.data,
+ },
+ (_, v: unknown) => (typeof v === 'bigint' ? v.toString() : v)
+ )
)
- return null
+ return undefined
})
.catch(emptyFunction)
break