8e4713cfae2df7e3a158c6191a3ec57fc3d8060f
1 import type { AddressInfo
} from
'node:net'
3 import Fastify
, { type FastifyInstance
} from
'fastify'
4 import { ClusterWorker
} from
'poolifier'
6 import { fastifyPoolifier
} from
'./fastify-poolifier.js'
7 import type { ClusterWorkerData
, ClusterWorkerResponse
} from
'./types.js'
9 class FastifyWorker
extends ClusterWorker
<
13 private static fastify
: FastifyInstance
15 private static readonly startFastify
= async (
16 workerData
?: ClusterWorkerData
17 ): Promise
<ClusterWorkerResponse
> => {
18 const { port
, ...fastifyPoolifierOptions
} = workerData
!
20 FastifyWorker
.fastify
= Fastify({
24 await FastifyWorker
.fastify
.register(
26 fastifyPoolifierOptions
29 FastifyWorker
.fastify
.all('/api/echo', async request
=> {
31 await FastifyWorker
.fastify
.execute({ data
: request
.body
}, 'echo')
35 FastifyWorker
.fastify
.get
<{
36 Params
: { number: number }
37 }>('/api/factorial/:number', async request
=> {
38 const { number } = request
.params
40 await FastifyWorker
.fastify
.execute({ data
: { number } }, 'factorial')
44 await FastifyWorker
.fastify
.listen({ port
})
47 port
: (FastifyWorker
.fastify
.server
.address() as AddressInfo
).port
51 public constructor () {
52 super(FastifyWorker
.startFastify
, {
53 killHandler
: async () => {
54 await FastifyWorker
.fastify
.pool
.destroy()
55 await FastifyWorker
.fastify
.close()
61 export const fastifyWorker
= new FastifyWorker()