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'
8 import type { IWorker
} from
'./worker'
10 export const checkFilePath
= (filePath
: string): void => {
13 typeof filePath
!== 'string' ||
14 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
16 throw new Error('Please specify a file with a worker implementation')
18 if (!existsSync(filePath
)) {
19 throw new Error(`Cannot find the worker file '${filePath}'`)
23 export const checkDynamicPoolSize
= (min
: number, max
: number): void => {
26 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
28 } else if (!Number.isSafeInteger(max
)) {
30 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
32 } else if (min
> max
) {
34 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
36 } else if (max
=== 0) {
38 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
40 } else if (min
=== max
) {
42 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
47 export const checkValidWorkerChoiceStrategy
= (
48 workerChoiceStrategy
: WorkerChoiceStrategy
51 workerChoiceStrategy
!= null &&
52 !Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)
54 throw new Error(`Invalid worker choice strategy '${workerChoiceStrategy}'`)
58 export const checkValidTasksQueueOptions
= (
59 tasksQueueOptions
: TasksQueueOptions
61 if (tasksQueueOptions
!= null && !isPlainObject(tasksQueueOptions
)) {
62 throw new TypeError('Invalid tasks queue options: must be a plain object')
65 tasksQueueOptions
?.concurrency
!= null &&
66 !Number.isSafeInteger(tasksQueueOptions
.concurrency
)
69 'Invalid worker node tasks concurrency: must be an integer'
73 tasksQueueOptions
?.concurrency
!= null &&
74 tasksQueueOptions
.concurrency
<= 0
77 `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
81 tasksQueueOptions
?.size
!= null &&
82 !Number.isSafeInteger(tasksQueueOptions
.size
)
85 'Invalid worker node tasks queue size: must be an integer'
88 if (tasksQueueOptions
?.size
!= null && tasksQueueOptions
.size
<= 0) {
90 `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
94 export const checkWorkerNodeArguments
= <Worker
extends IWorker
>(
96 tasksQueueBackPressureSize
: number
99 throw new TypeError('Cannot construct a worker node without a worker')
101 if (tasksQueueBackPressureSize
== null) {
103 'Cannot construct a worker node without a tasks queue back pressure size'
106 if (!Number.isSafeInteger(tasksQueueBackPressureSize
)) {
108 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
111 if (tasksQueueBackPressureSize
<= 0) {
112 throw new RangeError(
113 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'