@hotmeshio/hotmesh 0.0.52 → 0.0.54

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.
Files changed (134) hide show
  1. package/README.md +22 -18
  2. package/build/index.d.ts +1 -2
  3. package/build/index.js +1 -3
  4. package/build/modules/enums.d.ts +8 -3
  5. package/build/modules/enums.js +16 -8
  6. package/build/modules/errors.d.ts +58 -20
  7. package/build/modules/errors.js +90 -33
  8. package/build/package.json +7 -2
  9. package/build/services/activities/activity.d.ts +8 -0
  10. package/build/services/activities/activity.js +63 -14
  11. package/build/services/activities/await.js +6 -6
  12. package/build/services/activities/cycle.d.ts +2 -2
  13. package/build/services/activities/cycle.js +5 -5
  14. package/build/services/activities/hook.js +9 -5
  15. package/build/services/activities/interrupt.d.ts +3 -3
  16. package/build/services/activities/interrupt.js +15 -6
  17. package/build/services/activities/signal.d.ts +2 -2
  18. package/build/services/activities/signal.js +4 -4
  19. package/build/services/activities/trigger.d.ts +5 -2
  20. package/build/services/activities/trigger.js +34 -4
  21. package/build/services/activities/worker.js +6 -6
  22. package/build/services/compiler/deployer.js +33 -5
  23. package/build/services/compiler/validator.d.ts +2 -0
  24. package/build/services/compiler/validator.js +5 -1
  25. package/build/services/durable/client.d.ts +7 -1
  26. package/build/services/durable/client.js +57 -38
  27. package/build/services/durable/exporter.d.ts +27 -81
  28. package/build/services/durable/exporter.js +153 -325
  29. package/build/services/durable/handle.d.ts +13 -8
  30. package/build/services/durable/handle.js +61 -48
  31. package/build/services/durable/index.d.ts +0 -2
  32. package/build/services/durable/index.js +0 -2
  33. package/build/services/durable/schemas/factory.d.ts +33 -0
  34. package/build/services/durable/schemas/factory.js +2356 -0
  35. package/build/services/durable/search.js +8 -8
  36. package/build/services/durable/worker.js +117 -25
  37. package/build/services/durable/workflow.d.ts +67 -52
  38. package/build/services/durable/workflow.js +322 -306
  39. package/build/services/engine/index.d.ts +2 -2
  40. package/build/services/engine/index.js +5 -2
  41. package/build/services/exporter/index.d.ts +2 -4
  42. package/build/services/exporter/index.js +4 -5
  43. package/build/services/hotmesh/index.d.ts +2 -2
  44. package/build/services/hotmesh/index.js +2 -2
  45. package/build/services/mapper/index.d.ts +6 -2
  46. package/build/services/mapper/index.js +6 -2
  47. package/build/services/pipe/functions/array.d.ts +2 -10
  48. package/build/services/pipe/functions/array.js +30 -28
  49. package/build/services/pipe/functions/conditional.d.ts +1 -0
  50. package/build/services/pipe/functions/conditional.js +3 -0
  51. package/build/services/pipe/functions/date.d.ts +1 -0
  52. package/build/services/pipe/functions/date.js +4 -0
  53. package/build/services/pipe/functions/index.d.ts +2 -0
  54. package/build/services/pipe/functions/index.js +2 -0
  55. package/build/services/pipe/functions/logical.d.ts +5 -0
  56. package/build/services/pipe/functions/logical.js +12 -0
  57. package/build/services/pipe/functions/object.d.ts +3 -0
  58. package/build/services/pipe/functions/object.js +25 -7
  59. package/build/services/pipe/index.d.ts +20 -3
  60. package/build/services/pipe/index.js +82 -16
  61. package/build/services/router/index.js +14 -3
  62. package/build/services/serializer/index.d.ts +3 -2
  63. package/build/services/serializer/index.js +11 -4
  64. package/build/services/store/clients/ioredis.js +6 -6
  65. package/build/services/store/clients/redis.js +7 -7
  66. package/build/services/store/index.d.ts +2 -0
  67. package/build/services/store/index.js +4 -1
  68. package/build/services/stream/clients/ioredis.js +8 -8
  69. package/build/services/stream/clients/redis.js +1 -1
  70. package/build/types/activity.d.ts +60 -5
  71. package/build/types/durable.d.ts +183 -36
  72. package/build/types/error.d.ts +48 -0
  73. package/build/types/error.js +2 -0
  74. package/build/types/exporter.d.ts +35 -7
  75. package/build/types/index.d.ts +4 -3
  76. package/build/types/job.d.ts +93 -6
  77. package/build/types/pipe.d.ts +81 -3
  78. package/build/types/stream.d.ts +61 -1
  79. package/build/types/stream.js +4 -0
  80. package/index.ts +1 -2
  81. package/modules/enums.ts +16 -8
  82. package/modules/errors.ts +139 -34
  83. package/package.json +7 -2
  84. package/services/activities/activity.ts +63 -14
  85. package/services/activities/await.ts +6 -6
  86. package/services/activities/cycle.ts +7 -6
  87. package/services/activities/hook.ts +12 -5
  88. package/services/activities/interrupt.ts +19 -9
  89. package/services/activities/signal.ts +6 -5
  90. package/services/activities/trigger.ts +43 -6
  91. package/services/activities/worker.ts +7 -7
  92. package/services/compiler/deployer.ts +33 -6
  93. package/services/compiler/validator.ts +7 -3
  94. package/services/durable/client.ts +49 -22
  95. package/services/durable/exporter.ts +162 -349
  96. package/services/durable/handle.ts +66 -53
  97. package/services/durable/index.ts +0 -2
  98. package/services/durable/schemas/factory.ts +2358 -0
  99. package/services/durable/search.ts +8 -8
  100. package/services/durable/worker.ts +128 -29
  101. package/services/durable/workflow.ts +371 -322
  102. package/services/engine/index.ts +8 -3
  103. package/services/exporter/index.ts +10 -12
  104. package/services/hotmesh/index.ts +4 -3
  105. package/services/mapper/index.ts +6 -2
  106. package/services/pipe/functions/array.ts +24 -37
  107. package/services/pipe/functions/conditional.ts +4 -0
  108. package/services/pipe/functions/date.ts +6 -0
  109. package/services/pipe/functions/index.ts +7 -5
  110. package/services/pipe/functions/logical.ts +11 -0
  111. package/services/pipe/functions/object.ts +26 -7
  112. package/services/pipe/index.ts +99 -21
  113. package/services/quorum/index.ts +1 -3
  114. package/services/router/index.ts +14 -3
  115. package/services/serializer/index.ts +12 -5
  116. package/services/store/clients/ioredis.ts +6 -6
  117. package/services/store/clients/redis.ts +7 -7
  118. package/services/store/index.ts +4 -1
  119. package/services/stream/clients/ioredis.ts +8 -8
  120. package/services/stream/clients/redis.ts +1 -1
  121. package/types/activity.ts +87 -15
  122. package/types/durable.ts +263 -75
  123. package/types/error.ts +52 -0
  124. package/types/exporter.ts +43 -9
  125. package/types/index.ts +14 -8
  126. package/types/job.ts +157 -36
  127. package/types/pipe.ts +84 -3
  128. package/types/stream.ts +82 -23
  129. package/build/services/durable/factory.d.ts +0 -17
  130. package/build/services/durable/factory.js +0 -817
  131. package/build/services/durable/meshos.d.ts +0 -127
  132. package/build/services/durable/meshos.js +0 -380
  133. package/services/durable/factory.ts +0 -818
  134. package/services/durable/meshos.ts +0 -441
@@ -1,7 +1,6 @@
1
- import { HMSH_CODE_INTERRUPT } from '../../modules/enums';
2
1
  import { ExporterService } from './exporter';
3
2
  import { HotMeshService as HotMesh } from '../hotmesh';
4
- import { DurableJobExport } from '../../types/exporter';
3
+ import { DurableJobExport, ExportOptions } from '../../types/exporter';
5
4
  import { JobInterruptOptions, JobOutput } from '../../types/job';
6
5
  import { StreamError } from '../../types/stream';
7
6
 
@@ -22,15 +21,15 @@ export class WorkflowHandleService {
22
21
  );
23
22
  }
24
23
 
25
- async export(): Promise<DurableJobExport> {
26
- return this.exporter.export(this.workflowId);
24
+ async export(options?: ExportOptions): Promise<DurableJobExport> {
25
+ return this.exporter.export(this.workflowId, options);
27
26
  }
28
27
 
29
28
  /**
30
29
  * Sends a signal to the workflow. This is a way to send
31
30
  * a message to a workflow that is paused due to having
32
- * executed a `waitForSignal` workflow extension. Awakens
33
- * the workflow if no other signals are pending.
31
+ * executed `Durable.workflow.waitFor`. The workflow
32
+ * will awaken if no other signals are pending.
34
33
  */
35
34
  async signal(signalId: string, data: Record<any, any>): Promise<void> {
36
35
  await this.hotMesh.hook(`${this.hotMesh.appId}.wfs.signal`, { id: signalId, data });
@@ -81,67 +80,81 @@ export class WorkflowHandleService {
81
80
  }
82
81
 
83
82
  /**
84
- * Awaits for the workflow to complete and returns the result. If
85
- * the workflow thows and error, this method will likewise throw
86
- * an error.
83
+ * Waits for the workflow to complete and returns the result. If
84
+ * the workflow response includes an error, this method will rethrow
85
+ * the error, including the stack trace if available.
86
+ * Wrap calls in a try/catch as necessary to avoid unhandled exceptions.
87
87
  */
88
- async result(loadState?: boolean): Promise<any> {
89
- if (loadState) {
90
- const state = await this.hotMesh.getState(`${this.hotMesh.appId}.execute`, this.workflowId);
91
- if (!state.data && state.metadata.err) {
92
- throw new Error(JSON.parse(state.metadata.err));
93
- }
94
- if (state?.data?.done) {
95
- //child flows are never 'done'; they use a hook
96
- //that only closes upon parent flow completion.
97
- return state.data.response;
98
- }
99
- }
100
- let status = await this.hotMesh.getStatus(this.workflowId);
88
+ async result<T>(config?: {state?: boolean, throwOnError?: boolean}): Promise<T | StreamError> {
101
89
  const topic = `${this.hotMesh.appId}.executed.${this.workflowId}`;
102
-
103
- return new Promise((resolve, reject) => {
104
- let isResolved = false;
105
- //common fulfill/unsubscribe
106
- const complete = async (response?: any, err?: string) => {
90
+ let isResolved = false;
91
+
92
+ return new Promise(async (resolve, reject) => {
93
+
94
+ /**
95
+ * rejects/resolves the promise based on the `throwOnError`
96
+ * default behavior is to throw if error
97
+ */
98
+ const safeReject = (err: StreamError) => {
99
+ if (config?.throwOnError === false) {
100
+ return resolve(err);
101
+ }
102
+ reject(err);
103
+ };
104
+
105
+ /**
106
+ * Common completion function that unsubscribes from the topic/returns
107
+ */
108
+ const complete = async (response?: T, err?: StreamError) => {
107
109
  if (isResolved) return;
108
110
  isResolved = true;
109
- this.hotMesh.unsub(topic);
111
+
110
112
  if (err) {
111
- return reject(JSON.parse(err));
113
+ return safeReject(err as StreamError);
112
114
  } else if (!response) {
113
115
  const state = await this.hotMesh.getState(`${this.hotMesh.appId}.execute`, this.workflowId);
114
- if (state.metadata.err) {
115
- const error = JSON.parse(state.metadata.err) as StreamError;
116
- if (error.code === HMSH_CODE_INTERRUPT || !state.data) {
117
- return reject({ ...error, job_id: this.workflowId });
118
- }
116
+ if (state.data?.done && !state.data?.$error) {
117
+ return resolve(state.data.response as T);
118
+ } else if (state.data?.$error) {
119
+ return safeReject(state.data.$error as StreamError)
120
+ } else if (state.metadata.err) {
121
+ return safeReject(JSON.parse(state.metadata.err) as StreamError);
119
122
  }
120
- response = state.data?.response;
123
+ response = state.data?.response as T;
121
124
  }
122
- resolve(response);
125
+ resolve(response as T);
123
126
  };
124
- //check for done
125
- if (status <= 0) {
126
- return complete();
127
+
128
+ //more expensive; fetches the entire job, not just the `status`
129
+ if (config?.state) {
130
+ const state = await this.hotMesh.getState(`${this.hotMesh.appId}.execute`, this.workflowId);
131
+ if (state?.data?.done && !state.data?.$error) {
132
+ return complete(state.data.response as T);
133
+ } else if (state.data?.$error) {
134
+ return complete(null, state.data.$error as StreamError);
135
+ } else if (state.metadata.err) {
136
+ return complete(null, JSON.parse(state.metadata.err) as StreamError);
137
+ }
127
138
  }
128
- //subscribe to topic
129
- this.hotMesh.sub(topic, async (topic: string, state: JobOutput) => {
130
- if (state.metadata.err) {
139
+
140
+ //subscribe to 'done' topic
141
+ this.hotMesh.sub(topic, async (_topic: string, state: JobOutput) => {
142
+ this.hotMesh.unsub(topic);
143
+ if (state.data.done && !state.data?.$error) {
144
+ await complete(state.data?.response as T);
145
+ } else if (state.data?.$error) {
146
+ return complete(null, state.data.$error as StreamError)
147
+ } else if (state.metadata.err) {
131
148
  const error = JSON.parse(state.metadata.err) as StreamError;
132
- if (error.code === HMSH_CODE_INTERRUPT || !state.data) {
133
- return await complete(null, state.metadata.err);
134
- }
149
+ return await complete(null, error);
135
150
  }
136
- await complete(state.data?.response);
137
151
  });
138
- //resolve for race condition
139
- setTimeout(async () => {
140
- status = await this.hotMesh.getStatus(this.workflowId);
141
- if (status <= 0) {
142
- await complete();
143
- }
144
- }, 0);
152
+
153
+ //check state in case completed during wiring
154
+ const status = await this.hotMesh.getStatus(this.workflowId);
155
+ if (status <= 0) {
156
+ await complete();
157
+ }
145
158
  });
146
159
  }
147
160
  }
@@ -1,6 +1,5 @@
1
1
  import { ClientService } from './client';
2
2
  import { ConnectionService } from './connection';
3
- import { MeshOSService } from './meshos';
4
3
  import { Search } from './search';
5
4
  import { WorkerService } from './worker';
6
5
  import { WorkflowService } from './workflow';
@@ -11,7 +10,6 @@ export const Durable = {
11
10
  Client: ClientService,
12
11
  Connection: ConnectionService,
13
12
  Search,
14
- MeshOS: MeshOSService,
15
13
  Worker: WorkerService,
16
14
  workflow: WorkflowService,
17
15