@requence/service 1.0.0-alpha.2 → 1.0.0-alpha.21

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 (88) hide show
  1. package/CHANGELOG.md +109 -0
  2. package/build/{chunk-0x9092n4.js → chunk-6w7td62p.js} +5 -44
  3. package/build/chunk-6w7td62p.js.map +10 -0
  4. package/build/cli.js +22 -11
  5. package/build/cli.js.map +3 -3
  6. package/build/index.js +556 -775
  7. package/build/index.js.map +16 -16
  8. package/build/types/helpers/src/files/RequenceFile.d.ts +40 -0
  9. package/build/types/helpers/src/files/RequenceFile.d.ts.map +1 -0
  10. package/build/types/helpers/src/files/RequenceStream.d.ts +32 -0
  11. package/build/types/helpers/src/files/RequenceStream.d.ts.map +1 -0
  12. package/build/types/helpers/src/files/fileObject.d.ts +16 -0
  13. package/build/types/helpers/src/files/fileObject.d.ts.map +1 -0
  14. package/build/types/helpers/src/files/index.d.ts +7 -0
  15. package/build/types/helpers/src/files/index.d.ts.map +1 -0
  16. package/build/types/helpers/src/files/isValidMimeType.d.ts +5 -0
  17. package/build/types/helpers/src/files/isValidMimeType.d.ts.map +1 -0
  18. package/build/types/helpers/src/files/mapOutput.d.ts +10 -0
  19. package/build/types/helpers/src/files/mapOutput.d.ts.map +1 -0
  20. package/build/types/helpers/src/files/streamObject.d.ts +14 -0
  21. package/build/types/helpers/src/files/streamObject.d.ts.map +1 -0
  22. package/build/types/helpers/src/files/types.d.ts +4 -0
  23. package/build/types/helpers/src/files/types.d.ts.map +1 -0
  24. package/build/types/helpers/src/index.d.ts +9 -6
  25. package/build/types/helpers/src/index.d.ts.map +1 -1
  26. package/build/types/helpers/src/jsonschema/mapSchema.d.ts +20 -0
  27. package/build/types/helpers/src/jsonschema/mapSchema.d.ts.map +1 -0
  28. package/build/types/helpers/src/jsonschema/types.d.ts +11 -0
  29. package/build/types/helpers/src/jsonschema/types.d.ts.map +1 -0
  30. package/build/types/helpers/src/jsonschema/validate.d.ts +29 -0
  31. package/build/types/helpers/src/jsonschema/validate.d.ts.map +1 -0
  32. package/build/types/helpers/src/jsonschema/zod.d.ts +12 -0
  33. package/build/types/helpers/src/jsonschema/zod.d.ts.map +1 -0
  34. package/build/types/helpers/src/protocol/NodeTree.d.ts +504 -0
  35. package/build/types/helpers/src/protocol/NodeTree.d.ts.map +1 -0
  36. package/build/types/helpers/src/protocol/command.d.ts +420 -0
  37. package/build/types/helpers/src/protocol/command.d.ts.map +1 -0
  38. package/build/types/helpers/src/protocol/helpers.d.ts +5 -0
  39. package/build/types/helpers/src/protocol/helpers.d.ts.map +1 -0
  40. package/build/types/helpers/src/protocol/index.d.ts +8 -0
  41. package/build/types/helpers/src/protocol/index.d.ts.map +1 -0
  42. package/build/types/helpers/src/protocol/nodeType.d.ts +283 -0
  43. package/build/types/helpers/src/protocol/nodeType.d.ts.map +1 -0
  44. package/build/types/helpers/src/protocol/targetNodes.d.ts +253 -0
  45. package/build/types/helpers/src/protocol/targetNodes.d.ts.map +1 -0
  46. package/build/types/helpers/src/protocol/taskOptions.d.ts +17 -0
  47. package/build/types/helpers/src/protocol/taskOptions.d.ts.map +1 -0
  48. package/build/types/helpers/src/protocol/treeNodes.d.ts +606 -0
  49. package/build/types/helpers/src/protocol/treeNodes.d.ts.map +1 -0
  50. package/build/types/helpers/src/protocol/update.d.ts +264 -0
  51. package/build/types/helpers/src/protocol/update.d.ts.map +1 -0
  52. package/build/types/helpers/src/utils/callbackToAsyncIterator.d.ts +3 -0
  53. package/build/types/helpers/src/utils/callbackToAsyncIterator.d.ts.map +1 -0
  54. package/build/types/helpers/src/utils/clone.d.ts +3 -0
  55. package/build/types/helpers/src/utils/clone.d.ts.map +1 -0
  56. package/build/types/helpers/src/utils/createObjectProxy.d.ts +2 -0
  57. package/build/types/helpers/src/utils/createObjectProxy.d.ts.map +1 -0
  58. package/build/types/helpers/src/{createRemotePromise.d.ts → utils/createRemotePromise.d.ts} +1 -1
  59. package/build/types/helpers/src/utils/createRemotePromise.d.ts.map +1 -0
  60. package/build/types/helpers/src/utils/isRecord.d.ts +2 -0
  61. package/build/types/helpers/src/utils/isRecord.d.ts.map +1 -0
  62. package/build/types/helpers/src/utils/mapData.d.ts +17 -0
  63. package/build/types/helpers/src/utils/mapData.d.ts.map +1 -0
  64. package/build/types/helpers/src/{utils.d.ts → utils/matchSchema.d.ts} +2 -2
  65. package/build/types/helpers/src/utils/matchSchema.d.ts.map +1 -0
  66. package/build/types/helpers/src/utils/obfuscate.d.ts.map +1 -0
  67. package/build/types/helpers/src/utils/resolveRequenceTypes.d.ts +2 -0
  68. package/build/types/helpers/src/utils/resolveRequenceTypes.d.ts.map +1 -0
  69. package/build/types/helpers/src/utils/types.d.ts +4 -0
  70. package/build/types/helpers/src/utils/types.d.ts.map +1 -0
  71. package/build/types/service/src/createAmqpConnection.d.ts +17 -18
  72. package/build/types/service/src/createAmqpConnection.d.ts.map +1 -1
  73. package/build/types/service/src/helpers.d.ts +13 -88
  74. package/build/types/service/src/helpers.d.ts.map +1 -1
  75. package/build/types/service/src/index.d.ts +7 -5
  76. package/build/types/service/src/index.d.ts.map +1 -1
  77. package/build/types/service/src/mapInput.d.ts +2 -0
  78. package/build/types/service/src/mapInput.d.ts.map +1 -0
  79. package/package.json +7 -8
  80. package/build/chunk-0x9092n4.js.map +0 -12
  81. package/build/types/helpers/src/clone.d.ts +0 -3
  82. package/build/types/helpers/src/clone.d.ts.map +0 -1
  83. package/build/types/helpers/src/createObjectProxy.d.ts +0 -2
  84. package/build/types/helpers/src/createObjectProxy.d.ts.map +0 -1
  85. package/build/types/helpers/src/createRemotePromise.d.ts.map +0 -1
  86. package/build/types/helpers/src/obfuscate.d.ts.map +0 -1
  87. package/build/types/helpers/src/utils.d.ts.map +0 -1
  88. /package/build/types/helpers/src/{obfuscate.d.ts → utils/obfuscate.d.ts} +0 -0
package/build/index.js CHANGED
@@ -1,675 +1,407 @@
1
1
  import {
2
- clone,
3
- createObjectProxy,
4
2
  deobfuscate,
5
3
  obfuscate
6
- } from "./chunk-0x9092n4.js";
4
+ } from "./chunk-6w7td62p.js";
7
5
 
8
6
  // src/index.ts
9
- import { readFileSync } from "node:fs";
10
- import { z as z8 } from "zod";
7
+ import fs from "node:fs";
11
8
 
12
- // ../helpers/src/context/context.ts
13
- import { z } from "zod";
14
- var contextSchema = z.object({
15
- input: z.any(),
16
- meta: z.any(),
17
- tenant: z.object({ name: z.string() }),
18
- data: z.record(z.string(), z.any()),
19
- errors: z.record(z.string(), z.string()),
20
- dates: z.record(z.string(), z.string().datetime())
21
- });
22
- function isRecord(data) {
23
- return typeof data === "object" && data !== null && !Array.isArray(data);
24
- }
25
- var internalKey = Symbol.for("internal");
26
- function getInternalContext(contextHelper) {
27
- return contextHelper[internalKey];
28
- }
29
- function createContextHelper(context, nodes) {
30
- const results = new Map(nodes.map((node) => [
31
- node.id,
32
- {
33
- node,
34
- executionDate: node.id in context.dates ? new Date(context.dates[node.id]) : null,
35
- data: context.data[node.id] ?? null,
36
- error: context.errors[node.id] ?? null
37
- }
38
- ]));
39
- const lookUpMap = new Map;
40
- for (const node of nodes) {
41
- const alias = typeof node.meta?.mapAlias === "string" ? node.meta.mapAlias : ("alias" in node) && node.alias;
42
- if (alias) {
43
- if (lookUpMap.has(alias)) {
44
- lookUpMap.get(alias).push(node.id);
45
- } else {
46
- lookUpMap.set(alias, [node.id]);
47
- }
48
- }
49
- const name = typeof node.meta?.mapName === "string" ? node.meta.mapName : node.type === "service" && node.name;
50
- if (name) {
51
- if (lookUpMap.has(name)) {
52
- lookUpMap.get(name).push(node.id);
53
- } else {
54
- lookUpMap.set(name, [node.id]);
55
- }
56
- }
57
- }
58
- return {
59
- [internalKey]: {
60
- results
61
- },
62
- getInput() {
63
- return context.input;
64
- },
65
- getMeta() {
66
- return context.meta;
67
- },
68
- getData(alias) {
69
- if (alias) {
70
- const nodeIds = lookUpMap.get(alias);
71
- if (!nodeIds) {
72
- return null;
73
- }
74
- const combinedData = nodeIds.map((nodeId) => results.get(nodeId)?.data).filter(Boolean);
75
- if (combinedData.length === 0) {
76
- return null;
77
- }
78
- if (combinedData.length === 1) {
79
- return combinedData[0];
80
- }
81
- if (combinedData.every(isRecord)) {
82
- return combinedData.reduce((acc, data) => ({ ...acc, ...data }), {});
9
+ // ../helpers/src/utils/createRemotePromise.ts
10
+ function createRemotePromise(onAwait) {
11
+ let resolvePromise;
12
+ let rejectPromise;
13
+ const internalPromise = new Promise((resolve, reject) => {
14
+ resolvePromise = resolve;
15
+ rejectPromise = reject;
16
+ });
17
+ let isAwaited = false;
18
+ const wrappedPromise = new Proxy(internalPromise, {
19
+ get(target, property) {
20
+ if (property === "then" || property === "catch" || property === "finally") {
21
+ if (!isAwaited && onAwait) {
22
+ isAwaited = true;
23
+ onAwait(resolvePromise, rejectPromise);
83
24
  }
84
- return combinedData;
85
- }
86
- return Array.from(results.values()).filter((result) => result.executionDate && isRecord(result.data)).toSorted((a, b) => a.executionDate.getTime() - b.executionDate.getTime()).reduce((acc, result) => ({
87
- ...acc,
88
- ...result.data
89
- }), isRecord(context.input) ? { ...context.input } : {});
90
- },
91
- getError(alias) {
92
- const nodeIds = lookUpMap.get(alias);
93
- if (!nodeIds) {
94
- return null;
25
+ return target[property].bind(target);
95
26
  }
96
- return nodeIds.map((nodeId) => results.get(nodeId)?.error).filter((str) => typeof str === "string").join(`
97
- `);
27
+ return target[property];
98
28
  }
99
- };
29
+ });
30
+ return Object.assign(wrappedPromise, {
31
+ resolve: resolvePromise,
32
+ reject: rejectPromise,
33
+ promise: wrappedPromise
34
+ });
100
35
  }
101
- // ../helpers/src/protocol/command.ts
102
- import { z as z5 } from "zod";
103
36
 
104
- // ../helpers/src/protocol/nodes.ts
105
- import { ZodError, z as z4 } from "zod";
37
+ // ../helpers/src/utils/isRecord.ts
38
+ import { z } from "zod/v4";
39
+ var recordSchema = z.record(z.string(), z.unknown());
40
+ function isRecord(data) {
41
+ return recordSchema.safeParse(data).success;
42
+ }
106
43
 
107
- // ../helpers/src/protocol/getNodeOutputs.ts
108
- function getNodeOutputs(node, outputName) {
109
- const outputs = !outputName ? node.output : outputName === "__default__" ? node.output.filter((o) => o.default) : node.output.filter((o) => ("name" in o) && o.name === outputName);
110
- const outputTargets = new Set;
111
- for (const output of outputs) {
112
- if (output.exit) {
113
- outputTargets.add("__exit__");
114
- } else {
115
- outputTargets.add(output.target);
116
- }
44
+ // ../helpers/src/utils/mapData.ts
45
+ function resolve(value, onResolve) {
46
+ if (value instanceof Promise) {
47
+ return Promise.resolve().then(() => value).then(onResolve);
117
48
  }
118
- return Array.from(outputTargets);
49
+ return onResolve(value);
50
+ }
51
+ function notUndefined(value, fallback) {
52
+ return resolve(value, (resolvedValue) => typeof resolvedValue === "undefined" ? fallback : resolvedValue);
119
53
  }
120
54
 
121
- // ../helpers/src/protocol/createNodeMaps.ts
122
- function createNodeMaps(nodes) {
123
- const nodesByIdMap = new Map;
124
- const nodesByNameOrAlias = new Map;
125
- const inputsNodeMap = new Map([
126
- ["__exit__", []]
127
- ]);
128
- for (const node of nodes) {
129
- nodesByIdMap.set(node.id, node);
130
- if ("alias" in node && node.alias) {
131
- nodesByNameOrAlias.set(node.alias, node);
132
- }
133
- if ("name" in node && node.name) {
134
- nodesByNameOrAlias.set(node.name, node);
55
+ class AbortMapData extends Error {
56
+ }
57
+ function mapData(data, visitors) {
58
+ let aborted = false;
59
+ const traverseData = (data2, path) => {
60
+ if (aborted) {
61
+ return data2;
135
62
  }
136
- }
137
- for (const node of nodes) {
138
- for (const outputNodeId of getNodeOutputs(node)) {
139
- if (!inputsNodeMap.has(outputNodeId)) {
140
- inputsNodeMap.set(outputNodeId, []);
63
+ try {
64
+ if (Array.isArray(data2)) {
65
+ return resolve(visitors.Array?.(data2, path), (resolvedData) => {
66
+ if (resolvedData) {
67
+ return resolvedData;
68
+ }
69
+ const items = data2.map((item, index) => traverseData(item, [...path, index]));
70
+ if (items.some((item) => item instanceof Promise)) {
71
+ return Promise.all(items);
72
+ }
73
+ return items;
74
+ });
141
75
  }
142
- inputsNodeMap.get(outputNodeId).push(node);
143
- }
144
- }
145
- return {
146
- getInputs(node) {
147
- if (typeof node === "string") {
148
- return inputsNodeMap.get(node) ?? [];
76
+ if (data2 === null) {
77
+ return notUndefined(visitors.Null?.(data2, path), data2);
149
78
  }
150
- if (node.type === "catch" || node.type === "entry") {
151
- return [];
79
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(data2)) {
80
+ return notUndefined(visitors.Buffer?.(data2, path), data2);
152
81
  }
153
- return inputsNodeMap.get(node.id) ?? [];
154
- },
155
- getNode(nodeId) {
156
- return nodesByIdMap.get(nodeId);
157
- },
158
- lookupNode(nameOrAlias) {
159
- return nodesByNameOrAlias.get(nameOrAlias);
82
+ if (typeof data2 === "string") {
83
+ return notUndefined(visitors.String?.(data2, path), data2);
84
+ }
85
+ if (typeof data2 === "number") {
86
+ return notUndefined(visitors.Number?.(data2, path), data2);
87
+ }
88
+ if (typeof data2 === "symbol") {
89
+ return notUndefined(visitors.Symbol?.(data2, path), data2);
90
+ }
91
+ if (isRecord(data2)) {
92
+ return resolve(visitors.Object?.(structuredClone(data2), path), (resolvedData) => {
93
+ const nextData = resolvedData ?? data2;
94
+ const entries = Object.entries(nextData).map(([key, value]) => [
95
+ key,
96
+ traverseData(value, [...path, key])
97
+ ]);
98
+ if (entries.some(([, value]) => value instanceof Promise)) {
99
+ return Promise.all(entries.map(([key, value]) => Promise.resolve().then(() => value).then((resolvedValue) => [key, resolvedValue]))).then((entries2) => Object.fromEntries(entries2));
100
+ }
101
+ return Object.fromEntries(entries);
102
+ });
103
+ }
104
+ if (typeof data2 === "object" && data2) {
105
+ return notUndefined(visitors.Object?.(data2, path), data2);
106
+ }
107
+ return data2;
108
+ } catch (error) {
109
+ if (error instanceof AbortMapData) {
110
+ aborted = true;
111
+ return data2;
112
+ }
113
+ throw error;
160
114
  }
161
115
  };
116
+ return traverseData(data, []);
162
117
  }
118
+ mapData.abort = () => {
119
+ throw new AbortMapData;
120
+ };
163
121
 
164
- // ../helpers/src/protocol/getPossiblePaths.ts
165
- function getPossiblePaths({
166
- getInputs,
167
- getNode,
168
- onMissingInputs
169
- }) {
170
- const sets = new Set;
171
- const followPath = (nodeId, set2) => {
172
- if (nodeId === "__entry__") {
173
- return;
174
- }
175
- const node = nodeId !== "__exit__" ? getNode(nodeId) : null;
176
- const isOrNode = node?.type === "or";
177
- const inputNodes = getInputs(nodeId);
178
- if (node && node.type !== "catch" && inputNodes.length === 0) {
179
- onMissingInputs?.(node, Array.from(set2).toReversed());
180
- }
181
- for (const inputNode of inputNodes) {
182
- let usedSet = set2;
183
- if (isOrNode) {
184
- usedSet = new Set(set2);
185
- sets.add(usedSet);
122
+ // ../helpers/src/files/mapOutput.ts
123
+ async function upload(uploadUrl, body, signal, contentType) {
124
+ const uploadResponse = await fetch(uploadUrl, {
125
+ method: "POST",
126
+ signal,
127
+ headers: contentType ? {
128
+ "Content-Type": contentType
129
+ } : {},
130
+ body
131
+ });
132
+ const data = await uploadResponse.json();
133
+ if (!data || typeof data !== "object") {
134
+ throw new Error("invalid upload response");
135
+ }
136
+ return toFileObject(data);
137
+ }
138
+ async function stream(streamUrl, stream2, signal, contentType) {
139
+ const [testStream, dataStream] = stream2.tee();
140
+ const reader = testStream.getReader();
141
+ const dataPromise = reader.read().then(({ done: done2 }) => {
142
+ if (done2) {
143
+ throw new Error("No streaming date received");
144
+ }
145
+ reader.cancel();
146
+ });
147
+ const streamResponse = await fetch(streamUrl, {
148
+ method: "POST",
149
+ signal,
150
+ headers: contentType ? { "Content-Type": contentType } : {}
151
+ });
152
+ const data = await streamResponse.json();
153
+ if (!data || typeof data !== "object") {
154
+ throw new Error("invalid stream response");
155
+ }
156
+ const { mime, url } = data;
157
+ await dataPromise;
158
+ const done = fetch(url, {
159
+ method: "POST",
160
+ signal,
161
+ duplex: "half",
162
+ body: dataStream
163
+ });
164
+ return [toStreamObject({ mime, url }), done];
165
+ }
166
+ async function mapOutput(params) {
167
+ const streamPromises = [];
168
+ const mapped = await mapData(params.value, {
169
+ async Object(obj) {
170
+ if (obj instanceof Blob || obj instanceof RequenceFile) {
171
+ if (obj instanceof RequenceFile && obj.fileObject) {
172
+ return obj.fileObject;
173
+ }
174
+ if (params.options.uploadUrl) {
175
+ try {
176
+ if (obj instanceof Blob) {
177
+ return await upload(params.options.uploadUrl, obj, params.signal, obj.type);
178
+ }
179
+ if (obj instanceof RequenceFile) {
180
+ return await upload(params.options.uploadUrl, await obj.blob(), params.signal, obj.mimeType);
181
+ }
182
+ } catch (error) {
183
+ if (!(error instanceof Error)) {
184
+ throw error;
185
+ }
186
+ params.onFail?.(error);
187
+ return null;
188
+ }
189
+ } else {
190
+ params.onSkip?.();
191
+ return null;
192
+ }
186
193
  }
187
- const inputNodeId = inputNode.id;
188
- if (inputNode.type !== "or") {
189
- if (usedSet.has(inputNodeId)) {
190
- return;
194
+ if (obj instanceof Response || obj instanceof RequenceStream) {
195
+ if (obj instanceof RequenceStream && obj.streamObject) {
196
+ return obj.streamObject;
197
+ }
198
+ if (params.options.streamUrl) {
199
+ try {
200
+ if (obj instanceof Response && obj.body) {
201
+ const [streamObject, streamPromise] = await stream(params.options.streamUrl, obj.body, params.signal, obj.headers.get("Content-Type"));
202
+ streamPromises.push(streamPromise);
203
+ return streamObject;
204
+ }
205
+ if (obj instanceof RequenceStream) {
206
+ const [streamObject, streamPromise] = await stream(params.options.streamUrl, obj.stream(), params.signal, obj.mimeType);
207
+ streamPromises.push(streamPromise);
208
+ return streamObject;
209
+ }
210
+ } catch (error) {
211
+ if (!(error instanceof Error)) {
212
+ throw error;
213
+ }
214
+ params.onFail?.(error);
215
+ return null;
216
+ }
217
+ } else {
218
+ params.onSkip?.();
219
+ return null;
191
220
  }
192
- usedSet.add(inputNodeId);
193
221
  }
194
- followPath(inputNodeId, usedSet);
195
- }
196
- if (isOrNode) {
197
- sets.delete(set2);
198
222
  }
199
- };
200
- const set = new Set(["__exit__"]);
201
- sets.add(set);
202
- followPath("__exit__", set);
203
- return Array.from(sets, (set2) => Array.from(set2).toReversed());
223
+ });
224
+ Promise.allSettled(streamPromises).then(() => params.onDone?.());
225
+ return mapped;
204
226
  }
205
227
 
206
- // ../helpers/src/protocol/identifyNode.ts
207
- function identifyNode(nodeId, getNode) {
208
- if (nodeId === "__entry__") {
209
- return "ENTRY";
228
+ // ../helpers/src/files/RequenceFile.ts
229
+ class RequenceFile {
230
+ #data;
231
+ #file = null;
232
+ constructor(blobParts, options) {
233
+ this.#data = new Blob(blobParts, options);
210
234
  }
211
- if (nodeId === "__exit__") {
212
- return "EXIT";
235
+ static fromFileObject(file) {
236
+ const instance = new this([], { type: "x/x" });
237
+ instance.#file = file;
238
+ return instance;
213
239
  }
214
- if (typeof nodeId === "symbol") {
215
- return String(nodeId);
240
+ get fileObject() {
241
+ return this.#file;
216
242
  }
217
- const node = getNode(nodeId);
218
- if (!node) {
219
- return `Unknown[${nodeId}]`;
243
+ get size() {
244
+ return this.#file?.size ?? this.#data.size;
220
245
  }
221
- switch (node.type) {
222
- case "service": {
223
- return `Service[${node.alias || node.name}]`;
224
- }
225
- case "catch": {
226
- return `Catch[${identifyNode(node.subject, getNode)}]`;
246
+ get mimeType() {
247
+ return this.#file?.mime ?? this.#data.type;
248
+ }
249
+ get mainMimeType() {
250
+ return this.mimeType.split("/")[0];
251
+ }
252
+ get subMimeType() {
253
+ return this.mimeType.split("/")[1];
254
+ }
255
+ async text() {
256
+ if (this.#file) {
257
+ return fetch(this.#file.url).then((response) => response.text());
227
258
  }
228
- case "logic": {
229
- return `Logic[${node.alias || node.id}]`;
259
+ return this.#data.text();
260
+ }
261
+ async json() {
262
+ if (this.#file) {
263
+ return fetch(this.#file.url).then((response) => response.json());
230
264
  }
231
- case "entry": {
232
- return "ENTRY";
265
+ return JSON.parse(await this.text());
266
+ }
267
+ async blob() {
268
+ if (this.#file) {
269
+ return fetch(this.#file.url).then((response) => response.blob());
233
270
  }
234
- case "or": {
235
- return `OR[${node.alias || node.id}]`;
271
+ return this.#data;
272
+ }
273
+ async arrayBuffer() {
274
+ if (this.#file) {
275
+ return fetch(this.#file.url).then((response) => response.arrayBuffer());
236
276
  }
277
+ return this.#data.arrayBuffer();
237
278
  }
238
- }
239
-
240
- // ../helpers/src/protocol/node.ts
241
- import { z as z3 } from "zod";
242
-
243
- // ../helpers/src/protocol/jsonSchema.ts
244
- import { z as z2 } from "zod";
245
- var jsonSchema7Schema = z2.lazy(() => z2.object({
246
- $id: z2.string().optional(),
247
- $schema: z2.string().optional(),
248
- $ref: z2.string().optional(),
249
- title: z2.string().optional(),
250
- description: z2.string().optional(),
251
- type: z2.union([
252
- z2.literal("string"),
253
- z2.literal("number"),
254
- z2.literal("integer"),
255
- z2.literal("boolean"),
256
- z2.literal("object"),
257
- z2.literal("array"),
258
- z2.literal("null")
259
- ]).optional(),
260
- enum: z2.array(z2.union([z2.string(), z2.number(), z2.boolean(), z2.null()])).optional(),
261
- const: z2.union([z2.string(), z2.number(), z2.boolean(), z2.null()]).optional(),
262
- multipleOf: z2.number().positive().optional(),
263
- maximum: z2.number().optional(),
264
- exclusiveMaximum: z2.number().optional(),
265
- minimum: z2.number().optional(),
266
- exclusiveMinimum: z2.number().optional(),
267
- maxLength: z2.number().int().nonnegative().optional(),
268
- minLength: z2.number().int().nonnegative().optional(),
269
- pattern: z2.string().optional(),
270
- additionalItems: z2.boolean().optional(),
271
- items: z2.union([z2.lazy(() => jsonSchema7Schema), z2.array(jsonSchema7Schema)]).optional(),
272
- maxItems: z2.number().int().nonnegative().optional(),
273
- minItems: z2.number().int().nonnegative().optional(),
274
- uniqueItems: z2.boolean().optional(),
275
- contains: z2.lazy(() => jsonSchema7Schema).optional(),
276
- maxProperties: z2.number().int().nonnegative().optional(),
277
- minProperties: z2.number().int().nonnegative().optional(),
278
- required: z2.array(z2.string()).optional(),
279
- properties: z2.record(jsonSchema7Schema).optional(),
280
- patternProperties: z2.record(jsonSchema7Schema).optional(),
281
- additionalProperties: z2.union([z2.boolean(), z2.lazy(() => jsonSchema7Schema)]).optional(),
282
- dependencies: z2.record(z2.union([z2.array(z2.string()), jsonSchema7Schema])).optional(),
283
- propertyNames: z2.lazy(() => jsonSchema7Schema).optional(),
284
- if: z2.lazy(() => jsonSchema7Schema).optional(),
285
- then: z2.lazy(() => jsonSchema7Schema).optional(),
286
- else: z2.lazy(() => jsonSchema7Schema).optional(),
287
- allOf: z2.array(jsonSchema7Schema).optional(),
288
- anyOf: z2.array(jsonSchema7Schema).optional(),
289
- oneOf: z2.array(jsonSchema7Schema).optional(),
290
- not: z2.lazy(() => jsonSchema7Schema).optional(),
291
- definitions: z2.record(jsonSchema7Schema).optional(),
292
- format: z2.string().optional(),
293
- contentMediaType: z2.string().optional(),
294
- contentEncoding: z2.string().optional()
295
- }));
296
- var jsonSchema7ObjectSchema = z2.object({
297
- type: z2.literal("object"),
298
- properties: z2.record(jsonSchema7Schema),
299
- required: z2.array(z2.string()).optional(),
300
- description: z2.string().optional(),
301
- maxProperties: z2.number().int().nonnegative().optional(),
302
- minProperties: z2.number().int().nonnegative().optional(),
303
- additionalProperties: z2.boolean().optional()
304
- });
305
-
306
- // ../helpers/src/protocol/node.ts
307
- var outputDefinitionSchema = z3.object({
308
- schema: jsonSchema7Schema.nullable().optional()
309
- }).and(z3.union([
310
- z3.object({
311
- default: z3.literal(false).default(false),
312
- name: z3.string().min(1)
313
- }),
314
- z3.object({
315
- default: z3.literal(true).default(true)
316
- })
317
- ])).and(z3.union([
318
- z3.object({
319
- exit: z3.literal(false).default(false),
320
- target: z3.string().min(1)
321
- }),
322
- z3.object({ exit: z3.literal(true) })
323
- ]));
324
- var defaultOutputSchema = z3.union([
325
- z3.string().min(1),
326
- z3.object({ default: z3.literal(true).default(true), exit: z3.literal(true) }),
327
- z3.object({
328
- default: z3.literal(true).default(true),
329
- exit: z3.literal(false).default(false),
330
- target: z3.string().min(1)
331
- })
332
- ]).transform((output) => {
333
- if (typeof output === "string") {
334
- return {
335
- default: true,
336
- exit: false,
337
- target: output
338
- };
279
+ toString() {
280
+ return `RequenceFile<${this.mimeType}>`;
339
281
  }
340
- return output;
341
- });
342
- var outputSchema = z3.union([z3.string().min(1), outputDefinitionSchema]).transform((output) => {
343
- if (typeof output === "string") {
344
- return {
345
- default: true,
346
- exit: false,
347
- target: output
348
- };
282
+ toJSON() {
283
+ return `RequenceFile<${this.mimeType}>`;
349
284
  }
350
- return output;
351
- });
352
- function validateOutputs(outputs, ctx) {
353
- const exitOutputs = new Set(outputs.filter((output) => output.exit).map((output) => output.default ? "__default__" : output.name));
354
- outputs.filter((output) => !output.exit).forEach((output) => {
355
- const name = output.default ? "__default__" : output.name;
356
- if (exitOutputs.has(name)) {
357
- ctx.addIssue({
358
- code: "custom",
359
- message: output.default ? `default exit output cannot be mixed with target output "${output.target}"` : `named exit output "${name}" cannot be mixed with target output "${output.target}"`
360
- });
361
- }
362
- });
363
- const targets = new Map;
364
- outputs.forEach((output) => {
365
- const name = output.default ? "__default__" : output.name;
366
- const target = output.exit ? "__exit__" : output.target;
367
- if (!targets.has(name)) {
368
- targets.set(name, new Set);
369
- }
370
- const knownTargets = targets.get(name);
371
- if (knownTargets.has(target)) {
372
- ctx.addIssue({
373
- code: "custom",
374
- message: [
375
- output.default ? "duplicate default output" : `duplicate named output "${name}"`,
376
- output.exit ? "for exit" : `for target "${target}"`
377
- ].join(" ")
378
- });
379
- }
380
- knownTargets.add(target);
381
- });
382
285
  }
383
- var defaultOutputsSchema = z3.array(defaultOutputSchema).default([]).superRefine(validateOutputs);
384
- var outputsSchema = z3.array(outputSchema).default([]).superRefine(validateOutputs);
385
- var idSchema = z3.string().uuid().default(() => crypto.randomUUID());
386
- var metaSchema = z3.record(z3.any()).optional();
387
- var entryNodeSchema = z3.object({
388
- type: z3.literal("entry"),
389
- id: z3.literal("__entry__").default("__entry__"),
390
- output: defaultOutputsSchema,
391
- inputSchema: jsonSchema7Schema.nullable().optional(),
392
- metaSchema: jsonSchema7Schema.nullable().optional(),
393
- meta: metaSchema
394
- });
395
- var serviceNodeSchema = z3.object({
396
- type: z3.literal("service"),
397
- output: outputsSchema,
398
- inputSchema: jsonSchema7ObjectSchema.nullable().optional(),
399
- id: idSchema,
400
- name: z3.string(),
401
- version: z3.string(),
402
- configuration: z3.any().optional(),
403
- configurationSchema: jsonSchema7Schema.nullable().optional(),
404
- alias: z3.string().optional(),
405
- ttl: z3.number().int().min(1).optional(),
406
- retry: z3.number().int().min(1).optional(),
407
- retryDelay: z3.number().int().min(100).optional(),
408
- meta: metaSchema
409
- });
410
- var catchNodeSchema = z3.object({
411
- type: z3.literal("catch"),
412
- output: outputsSchema,
413
- id: idSchema,
414
- subject: z3.string(),
415
- meta: metaSchema
416
- });
417
- var logicNodeSchema = z3.object({
418
- type: z3.literal("logic"),
419
- alias: z3.string().optional(),
420
- inputSchema: jsonSchema7ObjectSchema.nullable().optional(),
421
- output: outputsSchema,
422
- id: idSchema,
423
- script: z3.string(),
424
- concurrency: z3.boolean().default(true),
425
- language: z3.enum(["javascript", "python", "typescript"]).default("javascript"),
426
- maxExecutionTime: z3.number().int().min(0).default(200),
427
- meta: metaSchema
428
- });
429
- var orNodeSchema = z3.object({
430
- id: idSchema,
431
- type: z3.literal("or"),
432
- alias: z3.string().optional(),
433
- output: outputsSchema,
434
- meta: metaSchema
435
- });
436
- var nodeSchema = z3.discriminatedUnion("type", [
437
- entryNodeSchema,
438
- serviceNodeSchema,
439
- catchNodeSchema,
440
- logicNodeSchema,
441
- orNodeSchema
442
- ]);
443
286
 
444
- // ../helpers/src/protocol/nodes.ts
445
- class NodesError extends ZodError {
446
- constructor(message, params, path = []) {
447
- super([
448
- {
449
- code: z4.ZodIssueCode.custom,
450
- message,
451
- path,
452
- params
453
- }
454
- ]);
455
- }
456
- }
457
- var nodesSchema = z4.array(nodeSchema).transform((nodes) => {
458
- const aliases = new Map;
459
- const names = new Map;
460
- let entryNode = null;
461
- let firstNodeId = null;
462
- for (const node of nodes) {
463
- if (node.type === "entry") {
464
- entryNode = node;
465
- }
466
- if ((node.type === "service" || node.type === "logic") && !firstNodeId) {
467
- firstNodeId = node.id;
468
- }
469
- if ("alias" in node && node.alias) {
470
- if (!aliases.has(node.alias)) {
471
- aliases.set(node.alias, []);
472
- }
473
- aliases.get(node.alias).push(node.id);
474
- }
475
- if ("name" in node && node.name) {
476
- if (!names.has(node.name)) {
477
- names.set(node.name, []);
478
- }
479
- names.get(node.name).push(node.id);
480
- }
287
+ // ../helpers/src/files/RequenceStream.ts
288
+ class RequenceStream {
289
+ #data;
290
+ #stream = null;
291
+ #options;
292
+ constructor(stream2, options) {
293
+ this.#data = stream2;
294
+ this.#options = options;
481
295
  }
482
- if (!entryNode && firstNodeId) {
483
- nodes.unshift({
484
- type: "entry",
485
- id: "__entry__",
486
- output: [{ default: true, exit: false, target: firstNodeId }]
296
+ static fromStreamObject(stream2) {
297
+ const instance = new this(null, {
298
+ type: "x/x"
487
299
  });
300
+ instance.#stream = stream2;
301
+ return instance;
488
302
  }
489
- const resolveOutput = (identifier) => {
490
- if (aliases.get(identifier)?.length === 1) {
491
- return aliases.get(identifier)[0];
492
- }
493
- if (names.get(identifier)?.length === 1) {
494
- return names.get(identifier)[0];
495
- }
496
- return identifier;
497
- };
498
- return nodes.map((node) => {
499
- node.output = node.output.map((output) => ("target" in output) ? {
500
- ...output,
501
- target: resolveOutput(output.target)
502
- } : output);
503
- if ("subject" in node) {
504
- node.subject = resolveOutput(node.subject);
505
- }
506
- return node;
507
- });
508
- }).transform((nodes) => {
509
- const { getInputs, getNode } = createNodeMaps(nodes);
510
- const nodeAliases = new Set;
511
- const nodeIds = new Set;
512
- const catchSubjects = new Map;
513
- const identify = (nodeId) => identifyNode(nodeId, getNode);
514
- for (const node of nodes) {
515
- if (nodeIds.has(node.id)) {
516
- throw new NodesError("id is not unique", {
517
- id: node.id
518
- });
519
- }
520
- nodeIds.add(node.id);
521
- if ("alias" in node && node.alias) {
522
- if (nodeAliases.has(node.alias)) {
523
- throw new NodesError("alias is not unique", {
524
- alias: node.alias
525
- });
526
- }
527
- nodeAliases.add(node.alias);
528
- }
529
- if (node.type === "catch") {
530
- if (catchSubjects.has(node.subject)) {
531
- throw new NodesError("only one catch node per service allowed", {
532
- id: node.subject
533
- });
534
- }
535
- catchSubjects.set(node.subject, node);
536
- }
303
+ get streamObject() {
304
+ return this.#stream;
537
305
  }
538
- if (!getNode("__entry__")) {
539
- throw new NodesError("missing entry node");
306
+ get mimeType() {
307
+ return this.#stream?.mime ?? this.#options.type;
540
308
  }
541
- if (getInputs("__exit__").length === 0) {
542
- throw new NodesError("no path leads to exit");
309
+ get mainMimeType() {
310
+ return this.mimeType.split("/")[0];
543
311
  }
544
- for (const catchNode of nodes.filter((node) => node.type === "catch")) {
545
- if (!getNode(catchNode.subject)) {
546
- throw new NodesError("catch node has invalid subject", {
547
- id: catchNode.subject
548
- });
549
- }
312
+ get subMimeType() {
313
+ return this.mimeType.split("/")[1];
550
314
  }
551
- for (const node of nodes) {
552
- if (node.type === "or") {
553
- const inputs = getInputs(node);
554
- if (!inputs || inputs.length < 2) {
555
- throw new NodesError("or nodes need at least 2 inputs");
556
- }
315
+ stream() {
316
+ if (this.#stream) {
317
+ const { readable, writable } = new TransformStream;
318
+ fetch(this.#stream.url).then((response) => response.body?.pipeTo(writable));
319
+ return readable;
557
320
  }
558
- for (const targetNodeId of getNodeOutputs(node)) {
559
- if (targetNodeId !== "__exit__" && !getNode(targetNodeId)) {
560
- throw new NodesError("missing node", {
561
- id: targetNodeId
562
- });
321
+ return this.#data;
322
+ }
323
+ asyncIterable() {
324
+ const reader = this.stream().getReader();
325
+ return {
326
+ [Symbol.asyncIterator]() {
327
+ return {
328
+ async next() {
329
+ return reader.read();
330
+ },
331
+ async return() {
332
+ reader.cancel();
333
+ reader.releaseLock();
334
+ return { done: true, value: null };
335
+ }
336
+ };
563
337
  }
564
- }
338
+ };
565
339
  }
566
- const possiblePaths = getPossiblePaths({
567
- getInputs,
568
- getNode,
569
- onMissingInputs: (_node, path) => {
570
- throw new NodesError("no path from entry to node", {
571
- path: path.map(identify)
572
- });
573
- }
574
- });
575
- if (possiblePaths.every((possiblePath) => !possiblePath.includes("__entry__"))) {
576
- throw new NodesError("no path from entry to exit");
340
+ toString() {
341
+ return `RequenceStream<${this.mimeType}>`;
577
342
  }
578
- for (const possiblePath of possiblePaths) {
579
- for (const pathSegment of possiblePath) {
580
- const node = pathSegment === "__exit__" ? null : getNode(pathSegment);
581
- if (node?.type === "catch" && possiblePath.includes(node.subject)) {
582
- throw new NodesError("Unresolvable, inputs needs both catch and subject", {
583
- path: possiblePath.map(identify)
584
- });
585
- }
586
- }
343
+ toJSON() {
344
+ return `RequenceStream<${this.mimeType}>`;
587
345
  }
588
- return nodes;
589
- });
346
+ }
590
347
 
591
- // ../helpers/src/protocol/command.ts
592
- var abortSchema = z5.object({
593
- type: z5.literal("ABORT"),
594
- reason: z5.string().optional()
595
- });
596
- var nodeOptionsSchema = z5.object({
597
- maxExecutions: z5.number().int().min(0).optional().default(10)
598
- });
599
- var createNodesSchema = z5.object({
600
- type: z5.literal("CREATE_NODES"),
601
- nodes: nodesSchema,
602
- input: z5.any(),
603
- meta: z5.any(),
604
- options: nodeOptionsSchema
605
- });
606
- var commandSchema = z5.union([abortSchema, createNodesSchema]);
607
- // ../helpers/src/protocol/update.ts
608
- import { z as z6 } from "zod";
609
- var baseUpdateSchema = z6.object({
610
- timestamp: z6.number().int().transform((int) => new Date(int))
611
- });
612
- var baseNodeUpdateSchema = baseUpdateSchema.extend({
613
- nodeId: z6.string().uuid()
614
- });
615
- var nodeStartUpdateSchema = baseNodeUpdateSchema.extend({
616
- type: z6.literal("NODE_START"),
617
- context: contextSchema
618
- });
619
- var nodeErrorUpdateSchema = baseNodeUpdateSchema.extend({
620
- type: z6.literal("NODE_ERROR"),
621
- error: z6.string(),
622
- context: contextSchema
623
- });
624
- var nodeUpdateSchema = baseNodeUpdateSchema.extend({
625
- type: z6.literal("NODE_UPDATE"),
626
- output: z6.string(),
627
- data: z6.unknown(),
628
- context: contextSchema
629
- });
630
- var nodeFinishedUpdateSchema = baseNodeUpdateSchema.extend({
631
- type: z6.literal("NODE_FINISHED"),
632
- context: contextSchema
633
- });
634
- var nodeDebugUpdateSchema = baseNodeUpdateSchema.extend({
635
- type: z6.literal("NODE_DEBUG"),
636
- severity: z6.enum(["log", "info", "warn", "error"]),
637
- data: z6.unknown(),
638
- context: contextSchema
639
- });
640
- var nodeDeferUpdateSchema = baseNodeUpdateSchema.extend({
641
- type: z6.literal("NODE_DEFER"),
642
- data: z6.unknown(),
643
- context: contextSchema
644
- });
645
- var taskStartUpdateSchema = baseUpdateSchema.extend({
646
- type: z6.literal("TASK_START"),
647
- nodes: nodesSchema,
648
- context: contextSchema
649
- });
650
- var taskErrorUpdateSchema = baseUpdateSchema.extend({
651
- type: z6.literal("TASK_ERROR"),
652
- reason: z6.string().optional(),
653
- context: contextSchema
348
+ // ../helpers/src/files/streamObject.ts
349
+ import { z as z2 } from "zod/v4";
350
+ var streamObjectSchema = z2.object({
351
+ mime: z2.string(),
352
+ url: z2.string().url(),
353
+ __REQUENCE_type: z2.literal("RequenceStream")
654
354
  });
655
- var taskFinishedUpdateSchema = baseUpdateSchema.extend({
656
- type: z6.literal("TASK_FINISHED"),
657
- context: contextSchema
355
+ function isStreamObject(data) {
356
+ return streamObjectSchema.safeParse(data).success;
357
+ }
358
+ function toStreamObject(streamInfo) {
359
+ return {
360
+ ...streamInfo,
361
+ __REQUENCE_type: "RequenceStream"
362
+ };
363
+ }
364
+
365
+ // ../helpers/src/files/fileObject.ts
366
+ import { z as z3 } from "zod/v4";
367
+ var fileObjectSchema = z3.object({
368
+ size: z3.number().int().positive(),
369
+ mime: z3.string(),
370
+ url: z3.string().url(),
371
+ __REQUENCE_type: z3.literal("RequenceFile")
658
372
  });
659
- var updateSchema = z6.discriminatedUnion("type", [
660
- nodeStartUpdateSchema,
661
- nodeErrorUpdateSchema,
662
- nodeUpdateSchema,
663
- nodeFinishedUpdateSchema,
664
- nodeDebugUpdateSchema,
665
- nodeDeferUpdateSchema,
666
- taskStartUpdateSchema,
667
- taskErrorUpdateSchema,
668
- taskFinishedUpdateSchema
669
- ]);
373
+ function toFileObject(fileInfo) {
374
+ return {
375
+ ...fileInfo,
376
+ __REQUENCE_type: "RequenceFile"
377
+ };
378
+ }
379
+ function isFileObject(data) {
380
+ return fileObjectSchema.safeParse(data).success;
381
+ }
382
+
383
+ // ../helpers/src/utils/resolveRequenceTypes.ts
384
+ function resolveRequenceTypes(data) {
385
+ return mapData(data, {
386
+ Object(obj) {
387
+ if (isFileObject(obj)) {
388
+ return RequenceFile.fromFileObject(obj);
389
+ }
390
+ if (isStreamObject(obj)) {
391
+ return RequenceStream.fromStreamObject(obj);
392
+ }
393
+ }
394
+ });
395
+ }
396
+
397
+ // src/index.ts
398
+ import { z as z5 } from "zod/v4";
399
+
670
400
  // src/createAmqpConnection.ts
671
- import { connect } from "amqp-connection-manager";
672
- import { z as z7 } from "zod";
401
+ import {
402
+ connect
403
+ } from "amqp-connection-manager";
404
+ import { z as z4 } from "zod/v4";
673
405
 
674
406
  // src/helpers.ts
675
407
  class RetryError extends Error {
@@ -680,23 +412,32 @@ class RetryError extends Error {
680
412
  }
681
413
  }
682
414
 
415
+ class SkipError extends Error {
416
+ }
417
+
683
418
  class AbortError extends Error {
684
419
  }
685
420
  var OUTPUT_VALUE = Symbol("output_value");
686
421
  function isOutputValue(value) {
687
422
  return value !== null && typeof value === "object" && OUTPUT_VALUE in value;
688
423
  }
689
- function unwrapResult(result) {
424
+ async function unwrapResult(result, options, onDone) {
690
425
  if (isOutputValue(result)) {
691
- return { value: result.value, outputName: result[OUTPUT_VALUE] };
426
+ return {
427
+ value: await mapOutput({ value: result.value, options, onDone }),
428
+ outputName: result[OUTPUT_VALUE]
429
+ };
692
430
  }
693
- return { value: result, outputName: null };
431
+ return {
432
+ value: await mapOutput({ value: result, options, onDone }),
433
+ outputName: null
434
+ };
694
435
  }
695
- function createExtendedContextHelper(options) {
696
- const contextHelper = createContextHelper(options.context, options.nodes);
697
- const internalContext = getInternalContext(contextHelper);
698
- return Object.assign(contextHelper, {
436
+ function createContextHelper(options) {
437
+ return {
699
438
  taskId: options.taskId,
439
+ input: resolveRequenceTypes(options.input),
440
+ configuration: resolveRequenceTypes(options.configuration),
700
441
  toOutput(name, value) {
701
442
  return {
702
443
  [OUTPUT_VALUE]: name,
@@ -704,17 +445,6 @@ function createExtendedContextHelper(options) {
704
445
  };
705
446
  },
706
447
  defer: options.onDefer,
707
- getConfiguration() {
708
- const result = internalContext.results.get(options.serviceId);
709
- if (result?.node.type === "service") {
710
- return result.node.configuration ?? null;
711
- }
712
- return null;
713
- },
714
- getNode() {
715
- return internalContext.results.get(options.serviceId).node;
716
- },
717
- state: createObjectProxy(options.state, options.onStateChange),
718
448
  debug: {
719
449
  log(...data) {
720
450
  options.onLog("log", data);
@@ -734,36 +464,53 @@ function createExtendedContextHelper(options) {
734
464
  },
735
465
  abort(reason) {
736
466
  throw new AbortError(reason);
467
+ },
468
+ skip() {
469
+ throw new SkipError;
470
+ }
471
+ };
472
+ }
473
+
474
+ // src/mapInput.ts
475
+ function mapInput(data) {
476
+ return mapData(data, {
477
+ Object(obj) {
478
+ if (isFileObject(obj)) {
479
+ return RequenceFile.fromFileObject(obj);
480
+ }
481
+ if (isStreamObject(obj)) {
482
+ return RequenceStream.fromStreamObject(obj);
483
+ }
737
484
  }
738
485
  });
739
486
  }
740
487
 
741
488
  // src/createAmqpConnection.ts
742
- var messageKeySchema = z7.string().transform((str) => deobfuscate(str)).pipe(z7.array(z7.string().uuid()).length(3)).transform((arr) => ({
489
+ var messageKeySchema = z4.string().transform((str) => deobfuscate(str)).pipe(z4.tuple([z4.uuid(), z4.uuid()]).rest(z4.string())).transform((arr) => ({
743
490
  taskId: arr[0],
744
- serviceId: arr[1],
745
- messageId: arr[2]
491
+ messageId: arr[1],
492
+ uploadUrl: arr[2] || undefined,
493
+ streamUrl: arr[3] || undefined
746
494
  }));
747
- var headerSchema = z7.object({
748
- properties: z7.object({
749
- correlationId: z7.string().uuid(),
750
- headers: z7.object({
751
- version: z7.string(),
752
- taskId: z7.string().uuid(),
753
- serviceId: z7.string().uuid()
495
+ var headerSchema = z4.object({
496
+ properties: z4.object({
497
+ correlationId: z4.uuid(),
498
+ headers: z4.object({
499
+ version: z4.string(),
500
+ taskId: z4.uuid()
754
501
  })
755
502
  })
756
503
  }).transform((msg) => ({
757
504
  version: msg.properties.headers.version,
758
505
  messageId: msg.properties.correlationId,
759
- taskId: msg.properties.headers.taskId,
760
- serviceId: msg.properties.headers.serviceId
506
+ taskId: msg.properties.headers.taskId
761
507
  }));
762
- var payloadSchema = z7.object({ content: z7.instanceof(Buffer) }).transform((msg) => JSON.parse(msg.content.toString())).pipe(z7.object({
763
- context: contextSchema,
764
- meta: z7.object({
765
- nodes: nodesSchema,
766
- state: z7.record(z7.any())
508
+ var payloadSchema = z4.object({ content: z4.instanceof(Buffer) }).transform((msg) => JSON.parse(msg.content.toString())).pipe(z4.object({
509
+ input: z4.unknown(),
510
+ configuration: z4.unknown(),
511
+ options: z4.object({
512
+ uploadUrl: z4.url().optional(),
513
+ streamUrl: z4.url().optional()
767
514
  })
768
515
  }));
769
516
  function isMessageHandlerGenerator(handler) {
@@ -775,12 +522,13 @@ function createConnection({
775
522
  version,
776
523
  prefetch = 1,
777
524
  silent = false,
778
- connectionTimeout
525
+ connectionTimeout,
526
+ connectionOptions
779
527
  }, messageHandler) {
780
528
  const queue = `${url.username}@${version}`;
781
529
  const exchange = url.username;
782
- const connection = connect(url.toString());
783
- const connectedPromise = new Promise((resolve, reject) => {
530
+ const connection = connect(url.toString(), { connectionOptions });
531
+ const connectedPromise = new Promise((resolve2, reject) => {
784
532
  const unsubscribe = () => {
785
533
  clearTimeout(timeout);
786
534
  connection.removeListener("connect", handleConnect);
@@ -791,7 +539,7 @@ function createConnection({
791
539
  }, connectionTimeout);
792
540
  const handleConnect = () => {
793
541
  unsubscribe();
794
- resolve();
542
+ resolve2();
795
543
  };
796
544
  const handleConnectFailed = ({ err }) => {
797
545
  unsubscribe();
@@ -812,11 +560,14 @@ function createConnection({
812
560
  data,
813
561
  outputName,
814
562
  messageId,
563
+ priority,
815
564
  ...headers
816
565
  }) => {
817
566
  channel.publish(exchange, "", Buffer.from(JSON.stringify(data ?? null)), {
818
567
  correlationId: messageId,
819
568
  contentType: "application/json",
569
+ persistent: true,
570
+ priority,
820
571
  headers: {
821
572
  outputName: outputName ?? null,
822
573
  ...headers
@@ -824,52 +575,35 @@ function createConnection({
824
575
  });
825
576
  };
826
577
  channel.consume(queue, async (message) => {
827
- if (message.properties.headers?.abort || message.properties.headers?.exception) {
828
- channel.nack(message, false, false);
829
- return;
830
- }
831
- let removeActiveMessage = () => {
578
+ let publishIndex = 0;
579
+ const donePromises = [];
580
+ let removeActiveMessage = () => {};
581
+ let acked = false;
582
+ const ack = () => {
583
+ if (!acked) {
584
+ acked = true;
585
+ channel.ack(message);
586
+ }
832
587
  };
833
588
  try {
834
589
  let deferred = false;
835
590
  let deferredReason = null;
836
- const { messageId, serviceId, taskId } = headerSchema.parse(message);
591
+ const { messageId, taskId } = headerSchema.parse(message);
837
592
  activeMessages.add(messageId);
838
593
  removeActiveMessage = () => activeMessages.delete(messageId);
839
594
  const payload = payloadSchema.parse(message);
840
- let acked = false;
841
- const ack = () => {
842
- if (!acked) {
843
- acked = true;
844
- channel.ack(message);
845
- }
846
- };
847
- let immediateUpdateState;
848
- const extendedContext = createExtendedContextHelper({
849
- serviceId,
850
- context: payload.context,
851
- nodes: payload.meta.nodes,
595
+ const extendedContext = createContextHelper({
852
596
  taskId,
853
- state: payload.meta.state,
854
- onStateChange(newState) {
855
- clearImmediate(immediateUpdateState);
856
- immediateUpdateState = setImmediate(() => {
857
- publish({
858
- messageId,
859
- taskId,
860
- serviceId,
861
- data: clone(newState),
862
- state: true
863
- });
864
- });
865
- },
597
+ input: mapInput(payload.input),
598
+ configuration: mapInput(payload.configuration),
866
599
  onLog(debugMethod, data) {
867
600
  publish({
601
+ index: publishIndex++,
868
602
  messageId,
869
603
  taskId,
870
- serviceId,
871
604
  data,
872
605
  debugMethod,
606
+ priority: message.properties.priority,
873
607
  debug: true
874
608
  });
875
609
  },
@@ -877,7 +611,7 @@ function createConnection({
877
611
  deferred = true;
878
612
  deferredReason = reason || null;
879
613
  ack();
880
- return obfuscate(taskId, serviceId, messageId);
614
+ return obfuscate(taskId, messageId, payload.options.uploadUrl ?? "", payload.options.streamUrl ?? "");
881
615
  }
882
616
  });
883
617
  if (isMessageHandlerGenerator(messageHandler)) {
@@ -889,13 +623,16 @@ function createConnection({
889
623
  deferred = false;
890
624
  console.warn("returning a value in a deferred message resets the deferred status");
891
625
  }
892
- const { value, outputName } = unwrapResult(result);
626
+ const donePromise = createRemotePromise();
627
+ donePromises.push(donePromise);
628
+ const { value, outputName } = await unwrapResult(result, payload.options, donePromise.resolve);
893
629
  if (!done || typeof value !== "undefined") {
894
630
  publish({
631
+ index: publishIndex++,
895
632
  messageId,
896
633
  taskId,
897
- serviceId,
898
634
  data: value,
635
+ priority: message.properties.priority,
899
636
  outputName
900
637
  });
901
638
  }
@@ -906,51 +643,74 @@ function createConnection({
906
643
  } else {
907
644
  const result = await messageHandler(extendedContext);
908
645
  ack();
909
- const { value, outputName } = unwrapResult(result);
646
+ const donePromise = createRemotePromise();
647
+ donePromises.push(donePromise);
648
+ const { value, outputName } = await unwrapResult(result, payload.options, donePromise.resolve);
910
649
  if (deferred && value) {
911
650
  console.warn("returning a value in a deferred message resets the deferred status");
912
651
  deferred = false;
913
652
  }
914
653
  if (!deferred || value) {
915
654
  publish({
655
+ index: publishIndex++,
916
656
  messageId,
917
657
  taskId,
918
- serviceId,
919
658
  data: value,
659
+ priority: message.properties.priority,
920
660
  outputName
921
661
  });
922
662
  }
923
663
  }
924
- setImmediate(() => publish({
925
- messageId,
926
- taskId,
927
- serviceId,
928
- data: deferredReason,
929
- deferred,
930
- done: !deferred
931
- }));
664
+ setImmediate(async () => {
665
+ if (!deferred) {
666
+ await Promise.allSettled(donePromises);
667
+ }
668
+ publish({
669
+ index: publishIndex++,
670
+ messageId,
671
+ taskId,
672
+ data: deferredReason,
673
+ priority: message.properties.priority,
674
+ deferred,
675
+ done: !deferred
676
+ });
677
+ });
932
678
  } catch (error) {
933
- if (error instanceof RetryError) {
934
- const properties = {
935
- ...message.properties,
936
- expiration: error.delay || 1000
937
- };
938
- channel.publish(`${exchange}-retry`, "retry", message.content, properties);
939
- channel.ack(message);
679
+ const assertNack = (message2) => {
680
+ if (acked) {
681
+ console.error(message2);
682
+ return false;
683
+ }
684
+ return true;
685
+ };
686
+ if (error instanceof SkipError) {
687
+ if (assertNack("data already send, cannot skip")) {
688
+ channel.nack(message);
689
+ }
940
690
  return;
941
691
  }
692
+ if (error instanceof RetryError) {
693
+ if (assertNack("data already send, cannot retry")) {
694
+ const properties = {
695
+ ...message.properties,
696
+ expiration: error.delay || 1000
697
+ };
698
+ channel.publish(`${exchange}-retry`, "retry", message.content, properties);
699
+ ack();
700
+ }
701
+ }
942
702
  if (error instanceof AbortError && error.message.length > 0) {
943
- channel.publish(`${exchange}-retry`, "requeue", error.message, {
944
- ...message.properties,
945
- expiration: 0,
946
- priority: 0,
947
- contentType: "text/plain",
948
- headers: {
949
- ...message.properties.headers,
950
- abort: true
951
- }
952
- });
953
- channel.ack(message);
703
+ if (assertNack("data already send, cannot abort")) {
704
+ channel.publish(`${exchange}-retry`, "operator", error.message, {
705
+ ...message.properties,
706
+ contentType: "text/plain",
707
+ headers: {
708
+ ...message.properties.headers,
709
+ abort: true
710
+ }
711
+ });
712
+ ack();
713
+ }
954
714
  return;
955
715
  }
956
716
  if (error instanceof Error) {
@@ -958,20 +718,22 @@ function createConnection({
958
718
  console.error(`Encountered an error inside service handler:`);
959
719
  console.error(error);
960
720
  }
961
- channel.publish(`${exchange}-retry`, "requeue", message.content, {
962
- ...message.properties,
963
- expiration: 0,
964
- priority: 0,
965
- headers: {
966
- ...message.properties.headers,
967
- exception: true,
968
- "error-message": error.message || "error in service handler"
969
- }
970
- });
971
- channel.ack(message);
721
+ if (!acked) {
722
+ channel.publish(`${exchange}-retry`, "operator", message.content, {
723
+ ...message.properties,
724
+ headers: {
725
+ ...message.properties.headers,
726
+ exception: true,
727
+ "error-message": error.message || "error in service handler"
728
+ }
729
+ });
730
+ channel.ack(message);
731
+ }
972
732
  return;
973
733
  }
974
- channel.nack(message, false, false);
734
+ if (!acked) {
735
+ channel.nack(message, false, false);
736
+ }
975
737
  } finally {
976
738
  removeActiveMessage();
977
739
  }
@@ -983,73 +745,86 @@ function createConnection({
983
745
  },
984
746
  open: () => connectedPromise.then(() => api),
985
747
  async act(messageKey, actor) {
748
+ let publishIndex = 0;
749
+ const { taskId, messageId, ...options } = messageKeySchema.parse(messageKey);
750
+ const checkActiveMessage = () => {
751
+ if (activeMessages.has(messageId)) {
752
+ throw new Error("Cannot send deferred message while callback handler is still running");
753
+ }
754
+ };
755
+ const donePromises = [];
986
756
  await actor({
987
- sendToOutput(outputName, data) {
988
- const { taskId: taskId2, serviceId: serviceId2, messageId: messageId2 } = messageKeySchema.parse(messageKey);
989
- if (activeMessages.has(messageId2)) {
990
- throw new Error("Cannot send deferred message while callback handler is still running");
991
- }
757
+ async sendToOutput(outputName, data) {
758
+ checkActiveMessage();
759
+ const donePromise = createRemotePromise();
760
+ donePromises.push(donePromise);
992
761
  publish({
993
- messageId: messageId2,
994
- taskId: taskId2,
995
- serviceId: serviceId2,
996
- data,
762
+ index: publishIndex++,
763
+ messageId,
764
+ taskId,
765
+ data: await mapOutput({
766
+ value: data,
767
+ options,
768
+ onDone: donePromise.resolve
769
+ }),
997
770
  outputName
998
771
  });
999
772
  },
1000
- send(data) {
1001
- const { taskId: taskId2, serviceId: serviceId2, messageId: messageId2 } = messageKeySchema.parse(messageKey);
1002
- if (activeMessages.has(messageId2)) {
1003
- throw new Error("Cannot send deferred message while callback handler is still running");
1004
- }
773
+ async send(data) {
774
+ checkActiveMessage();
775
+ const donePromise = createRemotePromise();
776
+ donePromises.push(donePromise);
1005
777
  publish({
1006
- messageId: messageId2,
1007
- taskId: taskId2,
1008
- serviceId: serviceId2,
1009
- data
778
+ index: publishIndex++,
779
+ messageId,
780
+ taskId,
781
+ data: await mapOutput({
782
+ value: data,
783
+ options,
784
+ onDone: donePromise.resolve
785
+ })
1010
786
  });
1011
787
  },
1012
788
  abort(error) {
1013
- const { taskId: taskId2, serviceId: serviceId2, messageId: messageId2 } = messageKeySchema.parse(messageKey);
1014
- if (activeMessages.has(messageId2)) {
1015
- throw new Error("Cannot send deferred message while callback handler is still running");
1016
- }
1017
- channel.publish(`${exchange}-retry`, "requeue", error instanceof Error ? error.message : error, {
1018
- correlationId: messageId2,
789
+ checkActiveMessage();
790
+ channel.publish(`${exchange}-retry`, "operator", error instanceof Error ? error.message : error, {
791
+ persistent: true,
792
+ correlationId: messageId,
1019
793
  expiration: 0,
1020
794
  priority: 0,
1021
795
  contentType: "text/plain",
1022
796
  headers: {
1023
- taskId: taskId2,
1024
- serviceId: serviceId2,
797
+ taskId,
1025
798
  abort: true
1026
799
  }
1027
800
  });
1028
801
  }
1029
802
  });
1030
- const { taskId, serviceId, messageId } = messageKeySchema.parse(messageKey);
1031
- setImmediate(() => publish({
1032
- messageId,
1033
- taskId,
1034
- serviceId,
1035
- done: true
1036
- }));
803
+ setImmediate(async () => {
804
+ await Promise.allSettled(donePromises);
805
+ publish({
806
+ index: publishIndex++,
807
+ messageId,
808
+ taskId,
809
+ done: true
810
+ });
811
+ });
1037
812
  }
1038
813
  };
1039
814
  return api;
1040
815
  }
1041
816
 
1042
817
  // src/index.ts
1043
- var versionSchema = z8.string().regex(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/, "not a valid version");
1044
- var connectionOptionsSchema = z8.object({
1045
- url: z8.union([
1046
- z8.string().regex(/^amqps?:\/\/(.+):(.+)@(.+)$/).transform((str) => new URL(str)),
1047
- z8.instanceof(URL).refine((url) => url.protocol.match(/^amqps?:/), "Wrong protocol")
818
+ var connectionOptionsSchema = z5.object({
819
+ url: z5.union([
820
+ z5.string().regex(/^amqps?:\/\/(.+):(.+)@(.+)$/).transform((str) => new URL(str)),
821
+ z5.instanceof(URL).refine((url) => url.protocol.match(/^amqps?:/), "Wrong protocol")
1048
822
  ]),
1049
- version: versionSchema,
1050
- silent: z8.boolean().optional(),
1051
- prefetch: z8.number().int().optional(),
1052
- connectionTimeout: z8.number().positive().default(3000)
823
+ version: z5.string(),
824
+ silent: z5.boolean().optional(),
825
+ prefetch: z5.number().int().optional(),
826
+ connectionTimeout: z5.number().positive().default(3000),
827
+ connectionOptions: z5.any()
1053
828
  });
1054
829
  function createService(optionsOrVersion, optionalMessageHandler) {
1055
830
  let options;
@@ -1063,11 +838,15 @@ function createService(optionsOrVersion, optionalMessageHandler) {
1063
838
  options = optionsOrVersion;
1064
839
  messageHandler = optionalMessageHandler;
1065
840
  }
1066
- const key = options.serviceKey ?? process.env.REQUENCE_SERVICE_KEY ?? JSON.parse(readFileSync("package.json", "utf-8")).requence?.serviceKey;
1067
- if (!key) {
841
+ let token = options.accessToken ?? process.env.REQUENCE_SERVICE_ACCESS_TOKEN ?? process.env.REQUENCE_ACCESS_TOKEN;
842
+ if (!token) {
843
+ const pkgJson = JSON.parse(fs.readFileSync("package.json", "utf-8"));
844
+ token = pkgJson.requence?.service?.accessToken ?? pkgJson.requence?.accessToken;
845
+ }
846
+ if (!token) {
1068
847
  throw new Error("No service key found");
1069
848
  }
1070
- const parts = deobfuscate(key);
849
+ const parts = deobfuscate(token);
1071
850
  if (parts.length < 2 || parts[0] !== "service") {
1072
851
  throw new Error("Invalid service key");
1073
852
  }
@@ -1078,8 +857,10 @@ function createService(optionsOrVersion, optionalMessageHandler) {
1078
857
  return createConnection(connectionOptionsSchema.parse(connectionOptions), messageHandler);
1079
858
  }
1080
859
  export {
1081
- createService as default
860
+ createService,
861
+ RequenceStream,
862
+ RequenceFile
1082
863
  };
1083
864
 
1084
- //# debugId=8A6F0759E8DF605064756E2164756E21
865
+ //# debugId=62B89EF7DDD1ECAA64756E2164756E21
1085
866
  //# sourceMappingURL=index.js.map