Merge dependabot/npm_and_yarn/examples/typescript/websocket-server-pool/ws-cluster...
[poolifier.git] / src / worker / utils.ts
CommitLineData
9a38f99e
JB
1import { isPlainObject } from '../utils'
2import type { TaskFunction } from './task-functions'
3import { KillBehaviors, type WorkerOptions } from './worker-options'
4
5export const checkValidWorkerOptions = (opts: WorkerOptions): void => {
6 if (opts != null && !isPlainObject(opts)) {
7 throw new TypeError('opts worker options parameter is not a plain object')
8 }
9 if (
10 opts?.killBehavior != null &&
11 !Object.values(KillBehaviors).includes(opts.killBehavior)
12 ) {
13 throw new TypeError(
14 `killBehavior option '${opts.killBehavior}' is not valid`
15 )
16 }
17 if (
18 opts?.maxInactiveTime != null &&
19 !Number.isSafeInteger(opts.maxInactiveTime)
20 ) {
21 throw new TypeError('maxInactiveTime option is not an integer')
22 }
23 if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) {
24 throw new TypeError(
25 'maxInactiveTime option is not a positive integer greater or equal than 5'
26 )
27 }
28 if (opts?.killHandler != null && typeof opts.killHandler !== 'function') {
29 throw new TypeError('killHandler option is not a function')
30 }
31 if (opts?.async != null) {
32 throw new Error('async option is deprecated')
33 }
34}
35
36export const checkValidTaskFunctionEntry = <Data = unknown, Response = unknown>(
37 name: string,
38 fn: TaskFunction<Data, Response>
39): void => {
40 if (typeof name !== 'string') {
41 throw new TypeError('A taskFunctions parameter object key is not a string')
42 }
43 if (typeof name === 'string' && name.trim().length === 0) {
44 throw new TypeError(
45 'A taskFunctions parameter object key is an empty string'
46 )
47 }
48 if (typeof fn !== 'function') {
49 throw new TypeError(
50 'A taskFunctions parameter object value is not a function'
51 )
52 }
53}
54
55export const checkTaskFunctionName = (name: string): void => {
56 if (typeof name !== 'string') {
57 throw new TypeError('name parameter is not a string')
58 }
59 if (typeof name === 'string' && name.trim().length === 0) {
60 throw new TypeError('name parameter is an empty string')
61 }
62}