Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-cluster...
[poolifier.git] / src / worker / abstract-worker.ts
index f42fae1751e0381cd4cf8505a6a689cf7adf3e19..a00e8f8faadd4e912de558c5d4f8ca980db76036 100644 (file)
@@ -165,12 +165,7 @@ export abstract class AbstractWorker<
    * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
    */
   public hasTaskFunction (name: string): boolean {
-    if (typeof name !== 'string') {
-      throw new TypeError('name parameter is not a string')
-    }
-    if (typeof name === 'string' && name.trim().length === 0) {
-      throw new TypeError('name parameter is an empty string')
-    }
+    this.checkTaskFunctionName(name)
     return this.taskFunctions.has(name)
   }
 
@@ -189,12 +184,7 @@ export abstract class AbstractWorker<
     name: string,
     fn: TaskFunction<Data, Response>
   ): boolean {
-    if (typeof name !== 'string') {
-      throw new TypeError('name parameter is not a string')
-    }
-    if (typeof name === 'string' && name.trim().length === 0) {
-      throw new TypeError('name parameter is an empty string')
-    }
+    this.checkTaskFunctionName(name)
     if (name === DEFAULT_TASK_NAME) {
       throw new Error(
         'Cannot add a task function with the default reserved name'
@@ -229,12 +219,7 @@ export abstract class AbstractWorker<
    * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function.
    */
   public removeTaskFunction (name: string): boolean {
-    if (typeof name !== 'string') {
-      throw new TypeError('name parameter is not a string')
-    }
-    if (typeof name === 'string' && name.trim().length === 0) {
-      throw new TypeError('name parameter is an empty string')
-    }
+    this.checkTaskFunctionName(name)
     if (name === DEFAULT_TASK_NAME) {
       throw new Error(
         'Cannot remove the task function with the default reserved name'
@@ -288,12 +273,7 @@ export abstract class AbstractWorker<
    * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
    */
   public setDefaultTaskFunction (name: string): boolean {
-    if (typeof name !== 'string') {
-      throw new TypeError('name parameter is not a string')
-    }
-    if (typeof name === 'string' && name.trim().length === 0) {
-      throw new TypeError('name parameter is an empty string')
-    }
+    this.checkTaskFunctionName(name)
     if (name === DEFAULT_TASK_NAME) {
       throw new Error(
         'Cannot set the default task function reserved name as the default task function'
@@ -315,6 +295,15 @@ export abstract class AbstractWorker<
     }
   }
 
+  private checkTaskFunctionName (name: string): void {
+    if (typeof name !== 'string') {
+      throw new TypeError('name parameter is not a string')
+    }
+    if (typeof name === 'string' && name.trim().length === 0) {
+      throw new TypeError('name parameter is an empty string')
+    }
+  }
+
   /**
    * Handles the ready message sent by the main worker.
    *
@@ -507,7 +496,7 @@ export abstract class AbstractWorker<
       const errorMessage = this.handleError(e as Error | string)
       this.sendToMainWorker({
         taskError: {
-          name: name ?? DEFAULT_TASK_NAME,
+          name: name as string,
           message: errorMessage,
           data
         },
@@ -546,7 +535,7 @@ export abstract class AbstractWorker<
         const errorMessage = this.handleError(e as Error | string)
         this.sendToMainWorker({
           taskError: {
-            name: name ?? DEFAULT_TASK_NAME,
+            name: name as string,
             message: errorMessage,
             data
           },