codehooks-js 1.3.19 → 1.3.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -76,9 +76,14 @@ class Codehooks {
76
76
  this.queues[topic] = hook;
77
77
  };
78
78
 
79
- worker = (name, ...hook) => {
80
- this.workers[name] = hook;
81
- this.queues[name] = hook; // legacy support
79
+ worker = async (name, hook, options={}) => {
80
+ this.workers[name] = [hook];
81
+ this.queues[name] = [hook]; // legacy support
82
+
83
+ // add option to key-value store if options is not empty
84
+ if (Object.keys(options).length > 0) {
85
+ this.set(`queue_worker:${name}`, options);
86
+ }
82
87
  };
83
88
 
84
89
  job = (cronExpression, ...hook) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.3.19",
3
+ "version": "1.3.20",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",
package/types/index.d.ts CHANGED
@@ -891,31 +891,38 @@ export class Codehooks {
891
891
  ) => void;
892
892
  /**
893
893
  * Add application worker function
894
- * @param {object} name a unique worker name or JSON configuration
895
- * @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
894
+ * @param {string} name - Unique worker name
895
+ * @param {function} hook - Worker callback function with parameters (req, res, [next])
896
+ * @param {Record<string, any>} options - Optional configuration stored in key-value store under `worker:${name}` key in keyspace '_codehooks_worker_settings'
897
+ *
896
898
  * @example
897
- * app.worker('myworker', (data, job) => {
898
- * const {body:{payload}} = data
899
- * //console.debug('worker payload data', payload)
900
- * // do stuff with payload data
901
- * job.end()
899
+ * // Simple worker
900
+ * await app.worker('myworker', (req, res) => {
901
+ * const {body:{payload}} = req
902
+ * console.log('worker payload data', payload)
903
+ * res.end()
902
904
  *})
905
+ *
903
906
  * @example
904
- * app.worker({name: 'myworker', workers: 5}, (data, job) => {
905
- * const {body:{payload}} = data
906
- * //console.debug('worker payload data', payload)
907
- * // do stuff with payload data
908
- * job.end()
909
- *})
907
+ * // Worker with configuration stored in KV store
908
+ * await app.worker('myworker', (req, res) => {
909
+ * const {body:{payload}} = req
910
+ * console.log('Processing:', payload)
911
+ * res.end()
912
+ * }, {
913
+ * workers: 2,
914
+ * timeout: 30000
915
+ * })
910
916
  */
911
917
  worker: (
912
- name: string | object,
913
- ...hook: ((
918
+ name: string,
919
+ hook: (
914
920
  request: httpRequest,
915
921
  response: httpResponse,
916
- next: nextFunction
917
- ) => any)[]
918
- ) => void;
922
+ next?: nextFunction
923
+ ) => any,
924
+ options?: Record<string, any>
925
+ ) => Promise<void>;
919
926
  /**
920
927
  * Create cron background jobs
921
928
  * @example
@@ -345,6 +345,11 @@ class Workflow extends EventEmitter {
345
345
  return;
346
346
  }
347
347
 
348
+ // Additional safety check to prevent enqueueing null or invalid steps
349
+ if (nextStep === null || nextStep === undefined || nextStep === 'null') {
350
+ throw new Error(`Invalid nextStep value: ${nextStep}. This should have been caught earlier.`);
351
+ }
352
+
348
353
  // Enqueue the next step, single or parallel
349
354
  if (Array.isArray(nextStep)) {
350
355
  const now = new Date().toISOString();
@@ -429,7 +434,8 @@ class Workflow extends EventEmitter {
429
434
  // Validate each step in the definition
430
435
  for (const [stepName, step] of Object.entries(definition)) {
431
436
  try {
432
- if (stepName !== undefined) {
437
+ // Skip null, undefined, or invalid step names
438
+ if (stepName !== undefined && stepName !== null && stepName !== 'null' && stepName.trim() !== '') {
433
439
  console.debug('registering queue for step', `${this.#queuePrefix}_${name}_${stepName}`);
434
440
  app.worker(`${this.#queuePrefix}_${name}_${stepName}`, async (req, res) => {
435
441
  try {
@@ -555,6 +561,12 @@ class Workflow extends EventEmitter {
555
561
  if (!state) {
556
562
  throw new Error(`No steps found with instanceId: ${instanceId}`);
557
563
  }
564
+
565
+ // Check if the workflow has already completed
566
+ if (state.nextStep === null) {
567
+ throw new Error(`Workflow ${instanceId} has already completed and cannot be continued`);
568
+ }
569
+
558
570
  if (reset) {
559
571
  // reset the step count
560
572
  // update all step counts to 0