Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-worker_thr...
[poolifier.git] / src / pools / utils.ts
1 import { existsSync } from 'node:fs'
2 import { isPlainObject } from '../utils'
3 import {
4 WorkerChoiceStrategies,
5 type WorkerChoiceStrategy
6 } from './selection-strategies/selection-strategies-types'
7 import type { TasksQueueOptions } from './pool'
8
9 export const checkFilePath = (filePath: string): void => {
10 if (
11 filePath == null ||
12 typeof filePath !== 'string' ||
13 (typeof filePath === 'string' && filePath.trim().length === 0)
14 ) {
15 throw new Error('Please specify a file with a worker implementation')
16 }
17 if (!existsSync(filePath)) {
18 throw new Error(`Cannot find the worker file '${filePath}'`)
19 }
20 }
21
22 export const checkDynamicPoolSize = (min: number, max: number): void => {
23 if (max == null) {
24 throw new TypeError(
25 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
26 )
27 } else if (!Number.isSafeInteger(max)) {
28 throw new TypeError(
29 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
30 )
31 } else if (min > max) {
32 throw new RangeError(
33 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
34 )
35 } else if (max === 0) {
36 throw new RangeError(
37 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
38 )
39 } else if (min === max) {
40 throw new RangeError(
41 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
42 )
43 }
44 }
45
46 export const checkValidWorkerChoiceStrategy = (
47 workerChoiceStrategy: WorkerChoiceStrategy
48 ): void => {
49 if (
50 workerChoiceStrategy != null &&
51 !Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)
52 ) {
53 throw new Error(`Invalid worker choice strategy '${workerChoiceStrategy}'`)
54 }
55 }
56
57 export const checkValidTasksQueueOptions = (
58 tasksQueueOptions: TasksQueueOptions
59 ): void => {
60 if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
61 throw new TypeError('Invalid tasks queue options: must be a plain object')
62 }
63 if (
64 tasksQueueOptions?.concurrency != null &&
65 !Number.isSafeInteger(tasksQueueOptions.concurrency)
66 ) {
67 throw new TypeError(
68 'Invalid worker node tasks concurrency: must be an integer'
69 )
70 }
71 if (
72 tasksQueueOptions?.concurrency != null &&
73 tasksQueueOptions.concurrency <= 0
74 ) {
75 throw new RangeError(
76 `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
77 )
78 }
79 if (
80 tasksQueueOptions?.size != null &&
81 !Number.isSafeInteger(tasksQueueOptions.size)
82 ) {
83 throw new TypeError(
84 'Invalid worker node tasks queue size: must be an integer'
85 )
86 }
87 if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
88 throw new RangeError(
89 `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
90 )
91 }
92 }