+import { existsSync } from 'node:fs'
+import { isPlainObject } from '../utils'
+import {
+ WorkerChoiceStrategies,
+ type WorkerChoiceStrategy
+} from './selection-strategies/selection-strategies-types'
+import type { TasksQueueOptions } from './pool'
+
+export const checkFilePath = (filePath: string): void => {
+ if (
+ filePath == null ||
+ typeof filePath !== 'string' ||
+ (typeof filePath === 'string' && filePath.trim().length === 0)
+ ) {
+ throw new Error('Please specify a file with a worker implementation')
+ }
+ if (!existsSync(filePath)) {
+ throw new Error(`Cannot find the worker file '${filePath}'`)
+ }
+}
+
+export const checkDynamicPoolSize = (min: number, max: number): void => {
+ if (max == null) {
+ throw new TypeError(
+ 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
+ )
+ } else if (!Number.isSafeInteger(max)) {
+ throw new TypeError(
+ 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
+ )
+ } else if (min > max) {
+ throw new RangeError(
+ 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
+ )
+ } else if (max === 0) {
+ throw new RangeError(
+ 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
+ )
+ } else if (min === max) {
+ throw new RangeError(
+ 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
+ )
+ }
+}
+
+export const checkValidWorkerChoiceStrategy = (
+ workerChoiceStrategy: WorkerChoiceStrategy
+): void => {
+ if (
+ workerChoiceStrategy != null &&
+ !Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)
+ ) {
+ throw new Error(`Invalid worker choice strategy '${workerChoiceStrategy}'`)
+ }
+}
+
+export const checkValidTasksQueueOptions = (
+ tasksQueueOptions: TasksQueueOptions
+): void => {
+ if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
+ throw new TypeError('Invalid tasks queue options: must be a plain object')
+ }
+ if (
+ tasksQueueOptions?.concurrency != null &&
+ !Number.isSafeInteger(tasksQueueOptions.concurrency)
+ ) {
+ throw new TypeError(
+ 'Invalid worker node tasks concurrency: must be an integer'
+ )
+ }
+ if (
+ tasksQueueOptions?.concurrency != null &&
+ tasksQueueOptions.concurrency <= 0
+ ) {
+ throw new RangeError(
+ `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
+ )
+ }
+ if (
+ tasksQueueOptions?.size != null &&
+ !Number.isSafeInteger(tasksQueueOptions.size)
+ ) {
+ throw new TypeError(
+ 'Invalid worker node tasks queue size: must be an integer'
+ )
+ }
+ if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
+ throw new RangeError(
+ `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
+ )
+ }
+}