1 import { isPlainObject
} from
'../utils'
2 import type { TaskFunction
} from
'./task-functions'
3 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options'
5 export const checkValidWorkerOptions
= (opts
: WorkerOptions
): void => {
6 if (opts
!= null && !isPlainObject(opts
)) {
7 throw new TypeError('opts worker options parameter is not a plain object')
10 opts
?.killBehavior
!= null &&
11 !Object.values(KillBehaviors
).includes(opts
.killBehavior
)
14 `killBehavior option '${opts.killBehavior}' is not valid`
18 opts
?.maxInactiveTime
!= null &&
19 !Number.isSafeInteger(opts
.maxInactiveTime
)
21 throw new TypeError('maxInactiveTime option is not an integer')
23 if (opts
?.maxInactiveTime
!= null && opts
.maxInactiveTime
< 5) {
25 'maxInactiveTime option is not a positive integer greater or equal than 5'
28 if (opts
?.killHandler
!= null && typeof opts
.killHandler
!== 'function') {
29 throw new TypeError('killHandler option is not a function')
31 if (opts
?.async != null) {
32 throw new Error('async option is deprecated')
36 export const checkValidTaskFunctionEntry
= <Data
= unknown
, Response
= unknown
>(
38 fn
: TaskFunction
<Data
, Response
>
40 if (typeof name
!== 'string') {
41 throw new TypeError('A taskFunctions parameter object key is not a string')
43 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
45 'A taskFunctions parameter object key is an empty string'
48 if (typeof fn
!== 'function') {
50 'A taskFunctions parameter object value is not a function'
55 export const checkTaskFunctionName
= (name
: string): void => {
56 if (typeof name
!== 'string') {
57 throw new TypeError('name parameter is not a string')
59 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
60 throw new TypeError('name parameter is an empty string')