From f9e6cc35ca852305c1a6e6a784dadfcb8f9712fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 24 Sep 2024 19:09:10 +0200 Subject: [PATCH] refactor: cleanup task function ops validation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/abstract-worker.ts | 36 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 53ec62f9..04515412 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -264,6 +264,11 @@ export abstract class AbstractWorker< ): void { const { taskFunction, taskFunctionOperation, taskFunctionProperties } = message + if (typeof taskFunction !== 'string') { + throw new Error( + 'Cannot handle task function operation message without task function' + ) + } if (taskFunctionProperties == null) { throw new Error( 'Cannot handle task function operation message without task function properties' @@ -272,25 +277,18 @@ export abstract class AbstractWorker< let response: TaskFunctionOperationResult switch (taskFunctionOperation) { case 'add': - if (typeof taskFunction === 'string') { - response = this.addTaskFunction(taskFunctionProperties.name, { - // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func, @typescript-eslint/no-unsafe-call - taskFunction: new Function( - `return ${taskFunction}` - )() as TaskFunction, - ...(taskFunctionProperties.priority != null && { - priority: taskFunctionProperties.priority, - }), - ...(taskFunctionProperties.strategy != null && { - strategy: taskFunctionProperties.strategy, - }), - }) - } else { - response = { - error: new Error("'taskFunction' property is not a string"), - status: false, - } - } + response = this.addTaskFunction(taskFunctionProperties.name, { + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func, @typescript-eslint/no-unsafe-call + taskFunction: new Function( + `return ${taskFunction}` + )() as TaskFunction, + ...(taskFunctionProperties.priority != null && { + priority: taskFunctionProperties.priority, + }), + ...(taskFunctionProperties.strategy != null && { + strategy: taskFunctionProperties.strategy, + }), + }) break case 'default': response = this.setDefaultTaskFunction(taskFunctionProperties.name) -- 2.34.1