1 import { existsSync
} from
'node:fs'
2 import { isPlainObject
} from
'../utils'
4 WorkerChoiceStrategies
,
5 type WorkerChoiceStrategy
6 } from
'./selection-strategies/selection-strategies-types'
7 import type { TasksQueueOptions
} from
'./pool'
9 export const checkFilePath
= (filePath
: string): void => {
12 typeof filePath
!== 'string' ||
13 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
15 throw new Error('Please specify a file with a worker implementation')
17 if (!existsSync(filePath
)) {
18 throw new Error(`Cannot find the worker file '${filePath}'`)
22 export const checkDynamicPoolSize
= (min
: number, max
: number): void => {
25 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
27 } else if (!Number.isSafeInteger(max
)) {
29 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
31 } else if (min
> max
) {
33 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
35 } else if (max
=== 0) {
37 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
39 } else if (min
=== max
) {
41 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
46 export const checkValidWorkerChoiceStrategy
= (
47 workerChoiceStrategy
: WorkerChoiceStrategy
50 workerChoiceStrategy
!= null &&
51 !Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)
53 throw new Error(`Invalid worker choice strategy '${workerChoiceStrategy}'`)
57 export const checkValidTasksQueueOptions
= (
58 tasksQueueOptions
: TasksQueueOptions
60 if (tasksQueueOptions
!= null && !isPlainObject(tasksQueueOptions
)) {
61 throw new TypeError('Invalid tasks queue options: must be a plain object')
64 tasksQueueOptions
?.concurrency
!= null &&
65 !Number.isSafeInteger(tasksQueueOptions
.concurrency
)
68 'Invalid worker node tasks concurrency: must be an integer'
72 tasksQueueOptions
?.concurrency
!= null &&
73 tasksQueueOptions
.concurrency
<= 0
76 `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
80 tasksQueueOptions
?.size
!= null &&
81 !Number.isSafeInteger(tasksQueueOptions
.size
)
84 'Invalid worker node tasks queue size: must be an integer'
87 if (tasksQueueOptions
?.size
!= null && tasksQueueOptions
.size
<= 0) {
89 `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`