From: Jérôme Benoit Date: Fri, 27 Nov 2026 00:43:22 +0000 (+0100) Subject: refactor(worker): simplify task function validation (#3427) X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=8d7669d9a68c0c9a8950ac08caf3b76f659f78e6;p=poolifier.git refactor(worker): simplify task function validation (#3427) * refactor(worker): simplify task function validation * test(worker): cover invalid add message sources * test(worker): reject truthy non-string task sources --- diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 1ad9cceb0..8b16df396 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -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(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(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 { diff --git a/tests/worker/abstract-worker.test.mjs b/tests/worker/abstract-worker.test.mjs index a63f44356..beb088889 100644 --- a/tests/worker/abstract-worker.test.mjs +++ b/tests/worker/abstract-worker.test.mjs @@ -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', () => {