]> Piment Noir Git Repositories - poolifier.git/commitdiff
refactor(worker): simplify task function validation (#3427)
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 27 Nov 2026 00:43:22 +0000 (01:43 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Nov 2026 00:43:22 +0000 (01:43 +0100)
* refactor(worker): simplify task function validation

* test(worker): cover invalid add message sources

* test(worker): reject truthy non-string task sources

src/worker/abstract-worker.ts
tests/worker/abstract-worker.test.mjs

index 1ad9cceb0bcec3ced0bc8c98b48108f9c417fd52..8b16df3964de0645f19e8211c480d8a53fd9ae63 100644 (file)
@@ -386,7 +386,7 @@ export abstract class AbstractWorker<
     switch (taskFunctionOperation) {
       case 'add':
         if (typeof taskFunction !== 'string') {
-          throw new Error(
+          throw new TypeError(
             `Cannot handle task function operation ${taskFunctionOperation} message without task function`
           )
         }
@@ -676,31 +676,32 @@ export abstract class AbstractWorker<
           : 'fn1',
         fnObj
       )
-    } else if (isPlainObject(taskFunctions)) {
-      let firstEntry = true
-      for (let [name, fnObj] of Object.entries(taskFunctions)) {
-        if (typeof fnObj === 'function') {
-          fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject<
-            Data,
-            Response
-          >
-        }
-        checkValidTaskFunctionObjectEntry<Data, Response>(name, fnObj)
-        fnObj.taskFunction = fnObj.taskFunction.bind(this)
-        if (firstEntry) {
-          this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
-          firstEntry = false
-        }
-        this.taskFunctions.set(name, fnObj)
-      }
-      if (firstEntry) {
-        throw new Error('taskFunctions parameter object is empty')
-      }
-    } else {
+      return
+    }
+    if (!isPlainObject(taskFunctions)) {
       throw new TypeError(
         'taskFunctions parameter is not a function or a plain object'
       )
     }
+    let firstEntry = true
+    for (let [name, fnObj] of Object.entries(taskFunctions)) {
+      if (typeof fnObj === 'function') {
+        fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject<
+          Data,
+          Response
+        >
+      }
+      checkValidTaskFunctionObjectEntry<Data, Response>(name, fnObj)
+      fnObj.taskFunction = fnObj.taskFunction.bind(this)
+      if (firstEntry) {
+        this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
+        firstEntry = false
+      }
+      this.taskFunctions.set(name, fnObj)
+    }
+    if (firstEntry) {
+      throw new Error('taskFunctions parameter object is empty')
+    }
   }
 
   private checkWorkerOptions (opts: WorkerOptions): void {
index a63f44356075fb70421713d3261ad5dad43d55a5..beb08888959e7995e26a6a1aa56efe245e8b1153 100644 (file)
@@ -750,17 +750,23 @@ describe('Abstract worker test suite', () => {
       )
     })
 
-    it('Verify that handleTaskFunctionOperationMessage() throws add without function', () => {
-      const worker = new ThreadWorker(() => {})
-      expect(() =>
-        worker.handleTaskFunctionOperationMessage({
-          taskFunctionOperation: 'add',
-          taskFunctionProperties: { name: 'fn' },
-        })
-      ).toThrow(
-        /Cannot handle task function operation add message without task function/
-      )
-    })
+    it.each([{}, { taskFunction: 1 }])(
+      'rejects an add message without a string task function (%j)',
+      taskFunctionFields => {
+        const worker = new ThreadWorker(() => {})
+        const handleAdd = () =>
+          worker.handleTaskFunctionOperationMessage({
+            ...taskFunctionFields,
+            taskFunctionOperation: 'add',
+            taskFunctionProperties: { name: 'fn' },
+          })
+
+        expect(handleAdd).toThrow(TypeError)
+        expect(handleAdd).toThrow(
+          /^Cannot handle task function operation add message without task function$/
+        )
+      }
+    )
   })
 
   describe('Check active mechanism', () => {