refactor: stricter worker constructor arguments check
[poolifier.git] / src / worker / abstract-worker.ts
index cd8f2346365f20ca5fb9e029dd436113594f19ac..8551ffc8ae2eef49bfb61567198ead269fef6bd7 100644 (file)
@@ -99,8 +99,35 @@ export abstract class AbstractWorker<
   }
 
   private checkWorkerOptions (opts: WorkerOptions): void {
+    if (opts != null && !isPlainObject(opts)) {
+      throw new TypeError('opts worker options parameter is not a plain object')
+    }
+    if (
+      opts?.killBehavior != null &&
+      !Object.values(KillBehaviors).includes(opts.killBehavior)
+    ) {
+      throw new TypeError(
+        `killBehavior option '${opts.killBehavior}' is not valid`
+      )
+    }
+    if (
+      opts?.maxInactiveTime != null &&
+      !Number.isSafeInteger(opts.maxInactiveTime)
+    ) {
+      throw new TypeError('maxInactiveTime option is not an integer')
+    }
+    if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) {
+      throw new TypeError(
+        'maxInactiveTime option is not a positive integer greater or equal than 5'
+      )
+    }
+    if (opts?.killHandler != null && typeof opts.killHandler !== 'function') {
+      throw new TypeError('killHandler option is not a function')
+    }
+    if (opts?.async != null) {
+      throw new Error('async option is deprecated')
+    }
     this.opts = { ...DEFAULT_WORKER_OPTIONS, ...opts }
-    delete this.opts.async
   }
 
   private checkValidTaskFunction (
@@ -125,7 +152,7 @@ export abstract class AbstractWorker<
   }
 
   /**
-   * Checks if the `taskFunctions` parameter is passed to the constructor.
+   * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
    *
    * @param taskFunctions - The task function(s) parameter that should be checked.
    */