X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-cluster%2Fsrc%2Fworker.ts;h=e491fa8f4b05c3ee4712740e1e669a102775108e;hb=d3a9c958dcc326062e978f22d88747c00522032d;hp=2f6c8f6dec1fcf405c8ce5bdb78a51c84c7c1915;hpb=8538ea4ccb5672b3d4b846db1bedf80135c1df8b;p=poolifier.git diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts index 2f6c8f6d..e491fa8f 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -1,22 +1,30 @@ import type { AddressInfo } from 'node:net' -import { ClusterWorker } from 'poolifier' + import Fastify, { type FastifyInstance } from 'fastify' +import { ClusterWorker } from 'poolifier' + import type { WorkerData, WorkerResponse } from './types.js' class FastifyWorker extends ClusterWorker { private static fastify: FastifyInstance - private static readonly factorial = (n: number): number => { - if (n === 0) { - return 1 + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial } - return FastifyWorker.factorial(n - 1) * n } private static readonly startFastify = async ( workerData?: WorkerData ): Promise => { - const { port } = workerData as WorkerData + const { port } = workerData! FastifyWorker.fastify = Fastify({ logger: true @@ -30,13 +38,13 @@ class FastifyWorker extends ClusterWorker { Params: { number: number } }>('/api/factorial/:number', request => { const { number } = request.params - return { number: FastifyWorker.factorial(number) } + return { number: FastifyWorker.factorial(number).toString() } }) await FastifyWorker.fastify.listen({ port }) return { status: true, - port: (FastifyWorker.fastify.server.address() as AddressInfo)?.port + port: (FastifyWorker.fastify.server.address() as AddressInfo).port } }