@requence/task 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 (98) hide show
  1. package/CHANGELOG.md +115 -0
  2. package/build/{chunk-s4kgf465.js → chunk-xdx5gf9h.js} +4 -33
  3. package/build/chunk-xdx5gf9h.js.map +10 -0
  4. package/build/cli.js +18 -22
  5. package/build/cli.js.map +3 -3
  6. package/build/index.js +787 -158
  7. package/build/index.js.map +19 -6
  8. package/build/types/helpers/src/context/context.d.ts +156 -0
  9. package/build/types/helpers/src/context/context.d.ts.map +1 -0
  10. package/build/types/helpers/src/context/index.d.ts +2 -0
  11. package/build/types/helpers/src/context/index.d.ts.map +1 -0
  12. package/build/types/helpers/src/files/RequenceFile.d.ts +40 -0
  13. package/build/types/helpers/src/files/RequenceFile.d.ts.map +1 -0
  14. package/build/types/helpers/src/files/RequenceStream.d.ts +32 -0
  15. package/build/types/helpers/src/files/RequenceStream.d.ts.map +1 -0
  16. package/build/types/helpers/src/files/fileObject.d.ts +26 -0
  17. package/build/types/helpers/src/files/fileObject.d.ts.map +1 -0
  18. package/build/types/helpers/src/files/index.d.ts +7 -0
  19. package/build/types/helpers/src/files/index.d.ts.map +1 -0
  20. package/build/types/helpers/src/files/isValidMimeType.d.ts +5 -0
  21. package/build/types/helpers/src/files/isValidMimeType.d.ts.map +1 -0
  22. package/build/types/helpers/src/files/mapOutput.d.ts +10 -0
  23. package/build/types/helpers/src/files/mapOutput.d.ts.map +1 -0
  24. package/build/types/helpers/src/files/streamObject.d.ts +22 -0
  25. package/build/types/helpers/src/files/streamObject.d.ts.map +1 -0
  26. package/build/types/helpers/src/files/types.d.ts +4 -0
  27. package/build/types/helpers/src/files/types.d.ts.map +1 -0
  28. package/build/types/helpers/src/index.d.ts +8 -6
  29. package/build/types/helpers/src/index.d.ts.map +1 -1
  30. package/build/types/helpers/src/jsonschema/mapSchema.d.ts +20 -0
  31. package/build/types/helpers/src/jsonschema/mapSchema.d.ts.map +1 -0
  32. package/build/types/helpers/src/jsonschema/types.d.ts +11 -0
  33. package/build/types/helpers/src/jsonschema/types.d.ts.map +1 -0
  34. package/build/types/helpers/src/jsonschema/validate.d.ts +28 -0
  35. package/build/types/helpers/src/jsonschema/validate.d.ts.map +1 -0
  36. package/build/types/helpers/src/jsonschema/zod.d.ts +20 -0
  37. package/build/types/helpers/src/jsonschema/zod.d.ts.map +1 -0
  38. package/build/types/helpers/src/protocol/command.d.ts +1995 -0
  39. package/build/types/helpers/src/protocol/command.d.ts.map +1 -0
  40. package/build/types/helpers/src/protocol/createNodeMaps.d.ts +10 -0
  41. package/build/types/helpers/src/protocol/createNodeMaps.d.ts.map +1 -0
  42. package/build/types/helpers/src/protocol/getCycles.d.ts +3 -0
  43. package/build/types/helpers/src/protocol/getCycles.d.ts.map +1 -0
  44. package/build/types/helpers/src/protocol/getNodeOutputs.d.ts +4 -0
  45. package/build/types/helpers/src/protocol/getNodeOutputs.d.ts.map +1 -0
  46. package/build/types/helpers/src/protocol/getPossiblePaths.d.ts +9 -0
  47. package/build/types/helpers/src/protocol/getPossiblePaths.d.ts.map +1 -0
  48. package/build/types/helpers/src/protocol/identifyNode.d.ts +4 -0
  49. package/build/types/helpers/src/protocol/identifyNode.d.ts.map +1 -0
  50. package/build/types/helpers/src/protocol/index.d.ts +10 -0
  51. package/build/types/helpers/src/protocol/index.d.ts.map +1 -0
  52. package/build/types/helpers/src/protocol/node.d.ts +1014 -0
  53. package/build/types/helpers/src/protocol/node.d.ts.map +1 -0
  54. package/build/types/helpers/src/protocol/nodes.d.ts +801 -0
  55. package/build/types/helpers/src/protocol/nodes.d.ts.map +1 -0
  56. package/build/types/helpers/src/protocol/update.d.ts +17951 -0
  57. package/build/types/helpers/src/protocol/update.d.ts.map +1 -0
  58. package/build/types/helpers/src/utils/callbackToAsyncIterator.d.ts +3 -0
  59. package/build/types/helpers/src/utils/callbackToAsyncIterator.d.ts.map +1 -0
  60. package/build/types/helpers/src/utils/clone.d.ts +3 -0
  61. package/build/types/helpers/src/utils/clone.d.ts.map +1 -0
  62. package/build/types/helpers/src/utils/createObjectProxy.d.ts +2 -0
  63. package/build/types/helpers/src/utils/createObjectProxy.d.ts.map +1 -0
  64. package/build/types/helpers/src/{createRemotePromise.d.ts → utils/createRemotePromise.d.ts} +1 -1
  65. package/build/types/helpers/src/utils/createRemotePromise.d.ts.map +1 -0
  66. package/build/types/helpers/src/utils/isRecord.d.ts +2 -0
  67. package/build/types/helpers/src/utils/isRecord.d.ts.map +1 -0
  68. package/build/types/helpers/src/utils/mapData.d.ts +13 -0
  69. package/build/types/helpers/src/utils/mapData.d.ts.map +1 -0
  70. package/build/types/helpers/src/{utils.d.ts → utils/matchSchema.d.ts} +1 -1
  71. package/build/types/helpers/src/utils/matchSchema.d.ts.map +1 -0
  72. package/build/types/helpers/src/utils/obfuscate.d.ts.map +1 -0
  73. package/build/types/task/src/abortTask.d.ts +8 -0
  74. package/build/types/task/src/abortTask.d.ts.map +1 -0
  75. package/build/types/task/src/createTask.d.ts +13 -0
  76. package/build/types/task/src/createTask.d.ts.map +1 -0
  77. package/build/types/task/src/index.d.ts +4 -101
  78. package/build/types/task/src/index.d.ts.map +1 -1
  79. package/build/types/task/src/types.d.ts +4954 -231
  80. package/build/types/task/src/types.d.ts.map +1 -1
  81. package/build/types/task/src/utils/getAccessToken.d.ts +5 -0
  82. package/build/types/task/src/utils/getAccessToken.d.ts.map +1 -0
  83. package/build/types/task/src/utils/mapOutput.d.ts +2 -0
  84. package/build/types/task/src/utils/mapOutput.d.ts.map +1 -0
  85. package/build/types/task/src/watchTasks.d.ts +10 -0
  86. package/build/types/task/src/watchTasks.d.ts.map +1 -0
  87. package/package.json +7 -5
  88. package/build/chunk-s4kgf465.js.map +0 -12
  89. package/build/types/helpers/src/clone.d.ts +0 -3
  90. package/build/types/helpers/src/clone.d.ts.map +0 -1
  91. package/build/types/helpers/src/createObjectProxy.d.ts +0 -2
  92. package/build/types/helpers/src/createObjectProxy.d.ts.map +0 -1
  93. package/build/types/helpers/src/createRemotePromise.d.ts.map +0 -1
  94. package/build/types/helpers/src/obfuscate.d.ts.map +0 -1
  95. package/build/types/helpers/src/utils.d.ts.map +0 -1
  96. package/build/types/task/src/utils/callbackToAsyncIterator.d.ts +0 -5
  97. package/build/types/task/src/utils/callbackToAsyncIterator.d.ts.map +0 -1
  98. /package/build/types/helpers/src/{obfuscate.d.ts → utils/obfuscate.d.ts} +0 -0
package/build/index.js CHANGED
@@ -1,85 +1,623 @@
1
1
  import {
2
- createRemotePromise,
3
2
  deobfuscate
4
- } from "./chunk-s4kgf465.js";
3
+ } from "./chunk-xdx5gf9h.js";
5
4
 
6
- // src/index.ts
7
- import { readFileSync } from "node:fs";
8
- import { EventSource } from "eventsource";
5
+ // ../helpers/src/utils/createRemotePromise.ts
6
+ function createRemotePromise(onAwait) {
7
+ let resolvePromise;
8
+ let rejectPromise;
9
+ const internalPromise = new Promise((resolve, reject) => {
10
+ resolvePromise = resolve;
11
+ rejectPromise = reject;
12
+ });
13
+ let isAwaited = false;
14
+ const wrappedPromise = new Proxy(internalPromise, {
15
+ get(target, property) {
16
+ if (property === "then" || property === "catch" || property === "finally") {
17
+ if (!isAwaited && onAwait) {
18
+ isAwaited = true;
19
+ onAwait(resolvePromise, rejectPromise);
20
+ }
21
+ return target[property].bind(target);
22
+ }
23
+ return target[property];
24
+ }
25
+ });
26
+ return Object.assign(wrappedPromise, {
27
+ resolve: resolvePromise,
28
+ reject: rejectPromise,
29
+ promise: wrappedPromise
30
+ });
31
+ }
32
+
33
+ // ../helpers/src/utils/callbackToAsyncIterator.ts
34
+ function callbackToAsyncIterator(subscribe) {
35
+ const pullQueue = [];
36
+ const pushQueue = [];
37
+ const pushValue = (value) => {
38
+ if (pullQueue.length !== 0) {
39
+ const resolver = pullQueue.shift();
40
+ resolver(value);
41
+ } else {
42
+ pushQueue.push(value);
43
+ }
44
+ };
45
+ const pullValue = () => new Promise((resolve) => {
46
+ if (pushQueue.length !== 0) {
47
+ resolve(pushQueue.shift());
48
+ } else {
49
+ pullQueue.push(resolve);
50
+ }
51
+ });
52
+ subscribe((value) => {
53
+ pushValue({ value });
54
+ }, (error) => {
55
+ pushValue({ error });
56
+ }, () => {
57
+ pushValue({ done: true });
58
+ });
59
+ return {
60
+ async next() {
61
+ const value = await pullValue();
62
+ if (value.done) {
63
+ return { value: undefined, done: true };
64
+ }
65
+ if (value.error) {
66
+ throw value.error;
67
+ }
68
+ return { value: value.value, done: false };
69
+ }
70
+ };
71
+ }
72
+ function callbackToAsyncIterable(...args) {
73
+ return {
74
+ [Symbol.asyncIterator]() {
75
+ return callbackToAsyncIterator(...args);
76
+ }
77
+ };
78
+ }
79
+
80
+ // ../helpers/src/utils/isRecord.ts
81
+ import { z } from "zod";
82
+ var recordSchema = z.record(z.unknown());
83
+ function isRecord(data) {
84
+ return recordSchema.safeParse(data).success;
85
+ }
86
+
87
+ // ../helpers/src/utils/mapData.ts
88
+ function resolve(value, onResolve) {
89
+ if (value instanceof Promise) {
90
+ return Promise.resolve().then(() => value).then(onResolve);
91
+ }
92
+ return onResolve(value);
93
+ }
94
+ function notUndefined(value, fallback) {
95
+ return resolve(value, (resolvedValue) => typeof resolvedValue === "undefined" ? fallback : resolvedValue);
96
+ }
97
+ function mapData(data, visitors) {
98
+ const traverseData = (data2, path) => {
99
+ if (Array.isArray(data2)) {
100
+ return resolve(visitors.Array?.(data2, path), (resolvedData) => {
101
+ if (resolvedData) {
102
+ return resolvedData;
103
+ }
104
+ const items = data2.map((item, index) => traverseData(item, [...path, index]));
105
+ if (items.some((item) => item instanceof Promise)) {
106
+ return Promise.all(items);
107
+ }
108
+ return items;
109
+ });
110
+ }
111
+ if (data2 === null) {
112
+ return notUndefined(visitors.Null?.(data2, path), data2);
113
+ }
114
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(data2)) {
115
+ return notUndefined(visitors.Buffer?.(data2, path), data2);
116
+ }
117
+ if (typeof data2 === "string") {
118
+ return notUndefined(visitors.String?.(data2, path), data2);
119
+ }
120
+ if (typeof data2 === "number") {
121
+ return notUndefined(visitors.Number?.(data2, path), data2);
122
+ }
123
+ if (isRecord(data2)) {
124
+ return resolve(visitors.Object?.(data2, path), (resolvedData) => {
125
+ if (resolvedData) {
126
+ return resolvedData;
127
+ }
128
+ const entries = Object.entries(data2).map(([key, value]) => [
129
+ key,
130
+ traverseData(value, [...path, key])
131
+ ]);
132
+ if (entries.some(([, value]) => value instanceof Promise)) {
133
+ return Promise.all(entries.map(([key, value]) => Promise.resolve().then(() => value).then((resolvedValue) => [key, resolvedValue]))).then((entries2) => Object.fromEntries(entries2));
134
+ }
135
+ return Object.fromEntries(entries);
136
+ });
137
+ }
138
+ return data2;
139
+ };
140
+ return traverseData(data, []);
141
+ }
142
+
143
+ // ../helpers/src/context/context.ts
144
+ import { z as z4 } from "zod";
145
+
146
+ // ../helpers/src/files/mapOutput.ts
147
+ async function upload(uploadUrl, body, signal, contentType) {
148
+ const uploadResponse = await fetch(uploadUrl, {
149
+ method: "POST",
150
+ signal,
151
+ headers: contentType ? {
152
+ "Content-Type": contentType
153
+ } : {},
154
+ body
155
+ });
156
+ const data = await uploadResponse.json();
157
+ if (!data || typeof data !== "object") {
158
+ throw new Error("invalid upload response");
159
+ }
160
+ return toFileObject(data);
161
+ }
162
+ async function stream(streamUrl, stream2, signal, contentType) {
163
+ const [testStream, dataStream] = stream2.tee();
164
+ const reader = testStream.getReader();
165
+ const dataPromise = reader.read().then(({ done: done2 }) => {
166
+ if (done2) {
167
+ throw new Error("No streaming date received");
168
+ }
169
+ reader.cancel();
170
+ });
171
+ const streamResponse = await fetch(streamUrl, {
172
+ method: "POST",
173
+ signal,
174
+ headers: contentType ? { "Content-Type": contentType } : {}
175
+ });
176
+ const data = await streamResponse.json();
177
+ if (!data || typeof data !== "object") {
178
+ throw new Error("invalid stream response");
179
+ }
180
+ const { mime, url } = data;
181
+ await dataPromise;
182
+ const done = fetch(url, {
183
+ method: "POST",
184
+ signal,
185
+ duplex: "half",
186
+ body: dataStream
187
+ });
188
+ return [toStreamObject({ mime, url }), done];
189
+ }
190
+ async function mapOutput(params) {
191
+ const streamPromises = [];
192
+ const mapped = await mapData(params.value, {
193
+ async Object(obj) {
194
+ if (obj instanceof Blob || obj instanceof RequenceFile) {
195
+ if (obj instanceof RequenceFile && obj.fileObject) {
196
+ return obj.fileObject;
197
+ }
198
+ if (params.options.uploadUrl) {
199
+ try {
200
+ if (obj instanceof Blob) {
201
+ return await upload(params.options.uploadUrl, obj, params.signal, obj.type);
202
+ }
203
+ if (obj instanceof RequenceFile) {
204
+ return await upload(params.options.uploadUrl, await obj.blob(), params.signal, obj.mimeType);
205
+ }
206
+ } catch (error) {
207
+ if (!(error instanceof Error)) {
208
+ throw error;
209
+ }
210
+ params.onFail?.(error);
211
+ return null;
212
+ }
213
+ } else {
214
+ params.onSkip?.();
215
+ return null;
216
+ }
217
+ }
218
+ if (obj instanceof Response || obj instanceof RequenceStream) {
219
+ if (obj instanceof RequenceStream && obj.streamObject) {
220
+ return obj.streamObject;
221
+ }
222
+ if (params.options.streamUrl) {
223
+ try {
224
+ if (obj instanceof Response && obj.body) {
225
+ const [streamObject, streamPromise] = await stream(params.options.streamUrl, obj.body, params.signal, obj.headers.get("Content-Type"));
226
+ streamPromises.push(streamPromise);
227
+ return streamObject;
228
+ }
229
+ if (obj instanceof RequenceStream) {
230
+ const [streamObject, streamPromise] = await stream(params.options.streamUrl, obj.stream(), params.signal, obj.mimeType);
231
+ streamPromises.push(streamPromise);
232
+ return streamObject;
233
+ }
234
+ } catch (error) {
235
+ if (!(error instanceof Error)) {
236
+ throw error;
237
+ }
238
+ params.onFail?.(error);
239
+ return null;
240
+ }
241
+ } else {
242
+ params.onSkip?.();
243
+ return null;
244
+ }
245
+ }
246
+ }
247
+ });
248
+ Promise.allSettled(streamPromises).then(() => params.onDone?.());
249
+ return mapped;
250
+ }
251
+
252
+ // ../helpers/src/files/RequenceFile.ts
253
+ class RequenceFile {
254
+ #data;
255
+ #file = null;
256
+ constructor(blobParts, options) {
257
+ this.#data = new Blob(blobParts, options);
258
+ }
259
+ static fromFileObject(file) {
260
+ const instance = new this([], { type: "x/x" });
261
+ instance.#file = file;
262
+ return instance;
263
+ }
264
+ get fileObject() {
265
+ return this.#file;
266
+ }
267
+ get size() {
268
+ return this.#file?.size ?? this.#data.size;
269
+ }
270
+ get mimeType() {
271
+ return this.#file?.mime ?? this.#data.type;
272
+ }
273
+ get mainMimeType() {
274
+ return this.mimeType.split("/")[0];
275
+ }
276
+ get subMimeType() {
277
+ return this.mimeType.split("/")[1];
278
+ }
279
+ async text() {
280
+ if (this.#file) {
281
+ return fetch(this.#file.url).then((response) => response.text());
282
+ }
283
+ return this.#data.text();
284
+ }
285
+ async json() {
286
+ if (this.#file) {
287
+ return fetch(this.#file.url).then((response) => response.json());
288
+ }
289
+ return JSON.parse(await this.text());
290
+ }
291
+ async blob() {
292
+ if (this.#file) {
293
+ return fetch(this.#file.url).then((response) => response.blob());
294
+ }
295
+ return this.#data;
296
+ }
297
+ async arrayBuffer() {
298
+ if (this.#file) {
299
+ return fetch(this.#file.url).then((response) => response.arrayBuffer());
300
+ }
301
+ return this.#data.arrayBuffer();
302
+ }
303
+ toString() {
304
+ return `RequenceFile<${this.mimeType}>`;
305
+ }
306
+ toJSON() {
307
+ return `RequenceFile<${this.mimeType}>`;
308
+ }
309
+ }
310
+
311
+ // ../helpers/src/files/RequenceStream.ts
312
+ class RequenceStream {
313
+ #data;
314
+ #stream = null;
315
+ #options;
316
+ constructor(stream2, options) {
317
+ this.#data = stream2;
318
+ this.#options = options;
319
+ }
320
+ static fromStreamObject(stream2) {
321
+ const instance = new this(null, {
322
+ type: "x/x"
323
+ });
324
+ instance.#stream = stream2;
325
+ return instance;
326
+ }
327
+ get streamObject() {
328
+ return this.#stream;
329
+ }
330
+ get mimeType() {
331
+ return this.#stream?.mime ?? this.#options.type;
332
+ }
333
+ get mainMimeType() {
334
+ return this.mimeType.split("/")[0];
335
+ }
336
+ get subMimeType() {
337
+ return this.mimeType.split("/")[1];
338
+ }
339
+ stream() {
340
+ if (this.#stream) {
341
+ const { readable, writable } = new TransformStream;
342
+ fetch(this.#stream.url).then((response) => response.body?.pipeTo(writable));
343
+ return readable;
344
+ }
345
+ return this.#data;
346
+ }
347
+ asyncIterable() {
348
+ const reader = this.stream().getReader();
349
+ return {
350
+ [Symbol.asyncIterator]() {
351
+ return {
352
+ async next() {
353
+ return reader.read();
354
+ },
355
+ async return() {
356
+ reader.cancel();
357
+ reader.releaseLock();
358
+ return { done: true, value: null };
359
+ }
360
+ };
361
+ }
362
+ };
363
+ }
364
+ toString() {
365
+ return `RequenceStream<${this.mimeType}>`;
366
+ }
367
+ toJSON() {
368
+ return `RequenceStream<${this.mimeType}>`;
369
+ }
370
+ }
371
+
372
+ // ../helpers/src/files/streamObject.ts
9
373
  import { z as z2 } from "zod";
374
+ var streamObjectSchema = z2.object({
375
+ mime: z2.string(),
376
+ url: z2.string().url(),
377
+ __REQUENCE_type: z2.literal("RequenceStream")
378
+ });
379
+ function isStreamObject(data) {
380
+ return streamObjectSchema.safeParse(data).success;
381
+ }
382
+ function toStreamObject(streamInfo) {
383
+ return {
384
+ ...streamInfo,
385
+ __REQUENCE_type: "RequenceStream"
386
+ };
387
+ }
388
+
389
+ // ../helpers/src/files/fileObject.ts
390
+ import { z as z3 } from "zod";
391
+ var fileObjectSchema = z3.object({
392
+ size: z3.number().int().positive(),
393
+ mime: z3.string(),
394
+ url: z3.string().url(),
395
+ __REQUENCE_type: z3.literal("RequenceFile")
396
+ });
397
+ function toFileObject(fileInfo) {
398
+ return {
399
+ ...fileInfo,
400
+ __REQUENCE_type: "RequenceFile"
401
+ };
402
+ }
403
+ function isFileObject(data) {
404
+ return fileObjectSchema.safeParse(data).success;
405
+ }
406
+
407
+ // ../helpers/src/context/context.ts
408
+ var contextSchema = z4.object({
409
+ input: z4.any(),
410
+ meta: z4.any(),
411
+ tenant: z4.object({ name: z4.string() }),
412
+ data: z4.record(z4.string(), z4.any()),
413
+ errors: z4.record(z4.string(), z4.string()),
414
+ dates: z4.record(z4.string(), z4.string().datetime())
415
+ });
416
+ var internalKey = Symbol.for("internal");
417
+ function resolveRequenceTypes(data) {
418
+ return mapData(data, {
419
+ Object(obj) {
420
+ if (isFileObject(obj)) {
421
+ return RequenceFile.fromFileObject(obj);
422
+ }
423
+ if (isStreamObject(obj)) {
424
+ return RequenceStream.fromStreamObject(obj);
425
+ }
426
+ }
427
+ });
428
+ }
429
+ function createContextHelper(context, nodes) {
430
+ const input = resolveRequenceTypes(context.input);
431
+ const meta = resolveRequenceTypes(context.meta);
432
+ const results = new Map(nodes.map((node) => [
433
+ node.id,
434
+ {
435
+ node,
436
+ executionDate: node.id in context.dates ? new Date(context.dates[node.id]) : null,
437
+ data: resolveRequenceTypes(context.data[node.id] ?? null),
438
+ error: context.errors[node.id] ?? null
439
+ }
440
+ ]));
441
+ const lookUpMap = new Map;
442
+ for (const node of nodes) {
443
+ const alias = typeof node.meta?.mapAlias === "string" ? node.meta.mapAlias : ("alias" in node) && node.alias;
444
+ if (alias) {
445
+ if (lookUpMap.has(alias)) {
446
+ lookUpMap.get(alias).push(node.id);
447
+ } else {
448
+ lookUpMap.set(alias, [node.id]);
449
+ }
450
+ }
451
+ const name = typeof node.meta?.mapName === "string" ? node.meta.mapName : node.type === "service" && node.name;
452
+ if (name) {
453
+ if (lookUpMap.has(name)) {
454
+ lookUpMap.get(name).push(node.id);
455
+ } else {
456
+ lookUpMap.set(name, [node.id]);
457
+ }
458
+ }
459
+ }
460
+ return {
461
+ [internalKey]: {
462
+ results
463
+ },
464
+ getInput() {
465
+ return input;
466
+ },
467
+ getMeta() {
468
+ return meta;
469
+ },
470
+ getData(alias) {
471
+ if (alias) {
472
+ const nodeIds = lookUpMap.get(alias);
473
+ if (!nodeIds) {
474
+ return null;
475
+ }
476
+ const uniqueNodeIds = Array.from(new Set(nodeIds));
477
+ const combinedData = uniqueNodeIds.map((nodeId) => results.get(nodeId)?.data).filter(Boolean);
478
+ if (combinedData.length === 0) {
479
+ return null;
480
+ }
481
+ if (combinedData.length === 1) {
482
+ return combinedData[0];
483
+ }
484
+ if (combinedData.every(isRecord)) {
485
+ return combinedData.reduce((acc, data) => ({ ...acc, ...data }), {});
486
+ }
487
+ return combinedData;
488
+ }
489
+ return Array.from(results.values()).filter((result) => result.executionDate && isRecord(result.data)).toSorted((a, b) => a.executionDate.getTime() - b.executionDate.getTime()).reduce((acc, result) => ({
490
+ ...acc,
491
+ ...result.data
492
+ }), isRecord(input) ? input : {});
493
+ },
494
+ getError(alias) {
495
+ const nodeIds = lookUpMap.get(alias);
496
+ if (!nodeIds) {
497
+ return null;
498
+ }
499
+ return nodeIds.map((nodeId) => results.get(nodeId)?.error).filter((str) => typeof str === "string").join(`
500
+ `);
501
+ }
502
+ };
503
+ }
504
+
505
+ // src/createTask.ts
506
+ import { EventSource } from "eventsource";
507
+
508
+ // src/utils/getAccessToken.ts
509
+ import { readFileSync } from "node:fs";
510
+ function getAccessToken(accessToken) {
511
+ let token = accessToken ?? process.env.REQUENCE_TASK_ACCESS_TOKEN ?? process.env.REQUENCE_ACCESS_TOKEN;
512
+ if (!token) {
513
+ const pkgJson = JSON.parse(readFileSync("package.json", "utf-8"));
514
+ token = pkgJson.requence?.task?.accessToken ?? pkgJson.requence?.accessToken;
515
+ }
516
+ if (!token) {
517
+ throw new Error(`No access token found
518
+ You can provide the access token as argument to createTask,
519
+ as environment variable REQUENCE_ACCESS_TOKEN or
520
+ in your package.json in requence.accessToken`);
521
+ }
522
+ const parts = deobfuscate(token);
523
+ if (parts.length < 2 || parts[0] !== "api") {
524
+ throw new Error("Invalid access token");
525
+ }
526
+ return {
527
+ token: parts[1],
528
+ apiBaseUrl: parts[2]
529
+ };
530
+ }
531
+
532
+ // src/abortTask.ts
533
+ class TaskAbortError extends Error {
534
+ taskId;
535
+ constructor(message, taskId) {
536
+ super(message);
537
+ this.taskId = taskId;
538
+ }
539
+ }
540
+ async function abortTask(options) {
541
+ const { apiBaseUrl, token } = getAccessToken(options.accessToken);
542
+ const abortUrl = `${apiBaseUrl}/task/abort/${options.taskId}`;
543
+ const response = await fetch(abortUrl, {
544
+ method: "POST",
545
+ body: options.reason ?? "",
546
+ headers: {
547
+ Authorization: `Bearer ${token}`
548
+ }
549
+ });
550
+ if (!response.ok) {
551
+ throw new TaskAbortError(await response.text(), options.taskId);
552
+ }
553
+ }
10
554
 
11
555
  // src/types.ts
12
- import { z } from "zod";
13
- var serviceNodeInfoSchema = z.object({
14
- id: z.string().uuid(),
15
- type: z.literal("service"),
16
- alias: z.string().optional(),
17
- service: z.object({
18
- id: z.string().uuid(),
19
- name: z.string(),
20
- version: z.string()
556
+ import { z as z5 } from "zod";
557
+ var serviceNodeInfoSchema = z5.object({
558
+ id: z5.string().uuid(),
559
+ type: z5.literal("service"),
560
+ alias: z5.string().optional(),
561
+ service: z5.object({
562
+ id: z5.string().uuid(),
563
+ name: z5.string(),
564
+ version: z5.string()
21
565
  }).optional()
22
566
  });
23
- var logicNodeInfoSchema = z.object({
24
- id: z.string().uuid(),
25
- type: z.literal("logic"),
26
- alias: z.string().optional(),
27
- logic: z.object({
28
- language: z.enum(["typescript", "javascript", "python"])
567
+ var logicNodeInfoSchema = z5.object({
568
+ id: z5.string().uuid(),
569
+ type: z5.literal("logic"),
570
+ alias: z5.string().optional(),
571
+ logic: z5.object({
572
+ language: z5.enum(["typescript", "javascript", "python"])
29
573
  }).optional()
30
574
  });
31
- var nodeInfoSchema = z.discriminatedUnion("type", [
575
+ var nodeInfoSchema = z5.discriminatedUnion("type", [
32
576
  serviceNodeInfoSchema,
33
577
  logicNodeInfoSchema
34
578
  ]);
35
- var taskStartUpdateSchema = z.object({
36
- type: z.literal("taskStart"),
37
- timestamp: z.coerce.date(),
38
- input: z.unknown(),
39
- meta: z.unknown()
579
+ var baseUpdate = z5.object({
580
+ timestamp: z5.coerce.date(),
581
+ taskId: z5.string().uuid(),
582
+ updateId: z5.string().uuid(),
583
+ context: contextSchema,
584
+ nodes: z5.custom((data) => Array.isArray(data))
585
+ });
586
+ var taskStartUpdateSchema = baseUpdate.extend({
587
+ type: z5.literal("taskStart")
40
588
  });
41
- var taskErrorUpdateSchema = z.object({
42
- type: z.literal("taskError"),
43
- timestamp: z.coerce.date(),
44
- error: z.string()
589
+ var taskErrorUpdateSchema = baseUpdate.extend({
590
+ type: z5.literal("taskError"),
591
+ error: z5.string()
45
592
  });
46
- var taskEndUpdateSchema = z.object({
47
- type: z.literal("taskEnd"),
48
- timestamp: z.coerce.date(),
49
- result: z.record(z.string(), z.unknown()),
50
- partialResults: z.array(z.object({
51
- node: nodeInfoSchema,
52
- timestamp: z.coerce.date().nullable(),
53
- data: z.unknown(),
54
- error: z.string().optional()
55
- }))
593
+ var taskEndUpdateSchema = baseUpdate.extend({
594
+ type: z5.literal("taskEnd")
56
595
  });
57
- var nodeStartUpdateSchema = z.object({
58
- type: z.literal("nodeStart"),
59
- timestamp: z.coerce.date(),
596
+ var nodeStartUpdateSchema = baseUpdate.extend({
597
+ type: z5.literal("nodeStart"),
60
598
  node: nodeInfoSchema
61
599
  });
62
- var nodeUpdateUpdateSchema = z.object({
63
- type: z.literal("nodeUpdate"),
64
- timestamp: z.coerce.date(),
65
- data: z.unknown()
600
+ var nodeUpdateUpdateSchema = baseUpdate.extend({
601
+ type: z5.literal("nodeUpdate"),
602
+ node: nodeInfoSchema,
603
+ data: z5.unknown(),
604
+ output: z5.union([z5.null(), z5.string()])
66
605
  });
67
- var nodeErrorUpdateSchema = z.object({
68
- type: z.literal("nodeError"),
69
- timestamp: z.coerce.date(),
70
- error: z.string()
606
+ var nodeErrorUpdateSchema = baseUpdate.extend({
607
+ type: z5.literal("nodeError"),
608
+ node: nodeInfoSchema,
609
+ error: z5.string()
71
610
  });
72
- var nodeDeferUpdateSchema = z.object({
73
- type: z.literal("nodeDefer"),
74
- timestamp: z.coerce.date(),
75
- reason: z.string().optional()
611
+ var nodeDeferUpdateSchema = baseUpdate.extend({
612
+ type: z5.literal("nodeDefer"),
613
+ node: nodeInfoSchema,
614
+ reason: z5.string().optional()
76
615
  });
77
- var nodeEndUpdateSchema = z.object({
78
- type: z.literal("nodeEnd"),
79
- timestamp: z.coerce.date(),
616
+ var nodeEndUpdateSchema = baseUpdate.extend({
617
+ type: z5.literal("nodeEnd"),
80
618
  node: nodeInfoSchema
81
619
  });
82
- var updateSchema = z.discriminatedUnion("type", [
620
+ var updateSchema = z5.discriminatedUnion("type", [
83
621
  taskStartUpdateSchema,
84
622
  taskErrorUpdateSchema,
85
623
  taskEndUpdateSchema,
@@ -89,49 +627,23 @@ var updateSchema = z.discriminatedUnion("type", [
89
627
  nodeDeferUpdateSchema,
90
628
  nodeEndUpdateSchema
91
629
  ]);
630
+ var eventSchema = z5.object({ type: z5.string(), data: z5.string() }).transform((event) => ({ type: event.type, ...JSON.parse(event.data) })).pipe(updateSchema);
92
631
 
93
- // src/utils/callbackToAsyncIterator.ts
94
- function callbackToAsyncIterator(options) {
95
- const pullQueue = [];
96
- const pushQueue = [];
97
- const pushValue = (value) => {
98
- if (pullQueue.length !== 0) {
99
- const resolver = pullQueue.shift();
100
- resolver(value);
101
- } else {
102
- pushQueue.push(value);
103
- }
104
- };
105
- const pullValue = () => new Promise((resolve) => {
106
- if (pushQueue.length !== 0) {
107
- resolve(pushQueue.shift());
108
- } else {
109
- pullQueue.push(resolve);
632
+ // src/utils/mapOutput.ts
633
+ function mapOutput2(data) {
634
+ return mapData(data, {
635
+ Object(obj) {
636
+ if (isFileObject(obj)) {
637
+ return RequenceFile.fromFileObject(obj);
638
+ }
639
+ if (isStreamObject(obj)) {
640
+ return RequenceStream.fromStreamObject(obj);
641
+ }
110
642
  }
111
643
  });
112
- return {
113
- [Symbol.asyncIterator]() {
114
- let done = false;
115
- const unsubscribe = options.subscribe(pushValue);
116
- return {
117
- async next() {
118
- if (done) {
119
- return { value: undefined, done: true };
120
- }
121
- const value = await pullValue();
122
- done = options.done(value);
123
- return { value, done: false };
124
- },
125
- async return() {
126
- unsubscribe?.();
127
- return { value: undefined, done: true };
128
- }
129
- };
130
- }
131
- };
132
644
  }
133
645
 
134
- // src/index.ts
646
+ // src/createTask.ts
135
647
  class TaskError extends Error {
136
648
  taskTemplate;
137
649
  constructor(message, taskTemplate) {
@@ -139,22 +651,10 @@ class TaskError extends Error {
139
651
  this.taskTemplate = taskTemplate;
140
652
  }
141
653
  }
142
- var eventSchema = z2.object({ type: z2.string(), data: z2.string() }).transform((event) => ({ type: event.type, ...JSON.parse(event.data) })).pipe(updateSchema);
143
- function createTask(params, onUpdate) {
654
+ function createTask(options, onUpdate) {
144
655
  const taskUpdateHandlers = new Set;
145
- const accessKey = params.accessToken ?? process.env.REQUENCE_ACCESS_TOKEN ?? JSON.parse(readFileSync("package.json", "utf-8")).requence?.accessToken;
146
- if (!accessKey) {
147
- throw new Error(`No access token found
148
- You can provide the access token as argument to createTask,
149
- as environment variable REQUENCE_ACCESS_TOKEN or
150
- in your package.json in requence.accessToken`);
151
- }
152
- const parts = deobfuscate(accessKey);
153
- if (parts.length < 2 || parts[0] !== "api") {
154
- throw new Error("Invalid api key");
155
- }
156
- const [, token, apiBaseUrl] = parts;
157
- const startUrl = `${apiBaseUrl}/task/start/${params.taskTemplate}`;
656
+ const { apiBaseUrl, token } = getAccessToken(options.accessToken);
657
+ const prepareUrl = `${apiBaseUrl}/task/prepare/${options.taskTemplate}`;
158
658
  const payloadPromise = createRemotePromise();
159
659
  let isMonitoring = false;
160
660
  const startMonitoring = async () => {
@@ -163,8 +663,31 @@ in your package.json in requence.accessToken`);
163
663
  }
164
664
  isMonitoring = true;
165
665
  const payload = await payloadPromise;
666
+ if (!payload.urls) {
667
+ resultPromise.resolve({
668
+ getInput() {
669
+ return options.input;
670
+ },
671
+ getMeta() {
672
+ return options.meta;
673
+ },
674
+ getError(_alias) {
675
+ return null;
676
+ },
677
+ getData(_alias) {
678
+ return null;
679
+ },
680
+ getTaskId() {
681
+ return payload.taskId;
682
+ },
683
+ getTaskUrl() {
684
+ return "";
685
+ }
686
+ });
687
+ return;
688
+ }
166
689
  const eventSource = new EventSource(payload.urls.watch, {
167
- fetch: (input2, init) => fetch(input2, {
690
+ fetch: (input, init) => fetch(input, {
168
691
  ...init,
169
692
  headers: {
170
693
  Authorization: `Bearer ${token}`
@@ -181,94 +704,200 @@ in your package.json in requence.accessToken`);
181
704
  "nodeError",
182
705
  "nodeEnd"
183
706
  ];
184
- let input = null;
185
- let meta = null;
186
707
  taskEvents.forEach((taskEvent) => {
187
708
  eventSource.addEventListener(taskEvent, (event) => {
188
709
  const updateResult = eventSchema.safeParse(event);
189
710
  if (!updateResult.success) {
190
711
  eventSource.close();
191
- resultPromise.reject(new TaskError(`Invalid ${taskEvent} update received`, params.taskTemplate));
712
+ resultPromise.reject(new TaskError(`Invalid ${taskEvent} update received`, options.taskTemplate));
192
713
  return;
193
714
  }
194
715
  const update = updateResult.data;
195
716
  if (update.type === "taskEnd" || update.type === "taskError") {
196
717
  eventSource.close();
197
718
  }
198
- if (update.type === "taskStart") {
199
- input = update.input;
200
- meta = update.meta;
719
+ const context = update.context;
720
+ if (update.type === "nodeUpdate") {
721
+ context.dates[update.node.id] = update.timestamp.toISOString();
722
+ context.data[update.node.id] = update.data;
201
723
  }
202
- if (update.type === "taskEnd") {
203
- const getPartialResult = (alias) => update.partialResults.find((partialResult) => partialResult.node.alias === alias);
724
+ const contextUpdate = {
725
+ ...update,
726
+ context: createContextHelper(context, update.nodes)
727
+ };
728
+ if (contextUpdate.type === "taskEnd") {
204
729
  resultPromise.resolve({
205
- getInput() {
206
- return input;
730
+ ...contextUpdate.context,
731
+ getTaskId() {
732
+ return payload.taskId;
207
733
  },
208
- getMeta() {
209
- return meta;
210
- },
211
- getError(alias) {
212
- return getPartialResult(alias)?.error ?? null;
213
- },
214
- getData(alias) {
215
- if (!alias) {
216
- return update.result;
217
- }
218
- return getPartialResult(alias)?.data ?? null;
734
+ getTaskUrl() {
735
+ return payload.urls?.show ?? "";
219
736
  }
220
737
  });
221
738
  }
222
- if (update.type === "taskError") {
223
- resultPromise.reject(new TaskError(update.error, params.taskTemplate));
739
+ if (contextUpdate.type === "taskError") {
740
+ resultPromise.reject(new TaskError(contextUpdate.error, options.taskTemplate));
224
741
  }
225
- taskUpdateHandlers.forEach((handler) => handler(update));
742
+ const extendedUpdate = contextUpdate.type === "nodeUpdate" ? {
743
+ ...contextUpdate,
744
+ output: contextUpdate.output === "__default__" ? null : contextUpdate.output,
745
+ data: mapOutput2(contextUpdate.data)
746
+ } : contextUpdate;
747
+ taskUpdateHandlers.forEach((handler) => handler(extendedUpdate));
226
748
  });
227
749
  });
228
750
  };
229
- const taskIdPromise = createRemotePromise();
751
+ const taskMetaPromise = createRemotePromise();
230
752
  const resultPromise = createRemotePromise(startMonitoring);
753
+ taskMetaPromise.catch(() => {});
231
754
  (async () => {
232
- const response = await fetch(startUrl, {
233
- method: "post",
755
+ const prepareResponse = await fetch(prepareUrl, {
756
+ method: "POST",
234
757
  headers: {
235
- Accept: "application/json",
236
- "Content-Type": "application/json",
237
758
  Authorization: `Bearer ${token}`
759
+ }
760
+ });
761
+ if (!prepareResponse.ok) {
762
+ const error = new TaskError(await prepareResponse.text(), options.taskTemplate);
763
+ taskMetaPromise.reject(error);
764
+ resultPromise.reject(error);
765
+ return;
766
+ }
767
+ const prepareData = await prepareResponse.json();
768
+ const mappedParams = await mapOutput({
769
+ value: {
770
+ name: options.name,
771
+ input: options.input,
772
+ meta: options.meta
238
773
  },
239
- body: JSON.stringify({ input: params.input, meta: params.meta })
774
+ options: {
775
+ uploadUrl: prepareData.fileUrls?.uploadUrl,
776
+ streamUrl: prepareData.fileUrls?.streamUrl
777
+ }
778
+ });
779
+ const finalizeResponse = await fetch(prepareData.finalizeUrl, {
780
+ method: "POST",
781
+ body: JSON.stringify(mappedParams),
782
+ headers: {
783
+ Authorization: `Bearer ${token}`,
784
+ "Content-Type": "application/json"
785
+ }
240
786
  });
241
- if (!response.ok) {
242
- const error = new TaskError(await response.text(), params.taskTemplate);
243
- taskIdPromise.reject(error);
787
+ if (!finalizeResponse.ok) {
788
+ const error = new TaskError(await prepareResponse.text(), options.taskTemplate);
789
+ taskMetaPromise.reject(error);
244
790
  resultPromise.reject(error);
245
791
  return;
246
792
  }
247
- const payload = await response.json();
248
- taskIdPromise.resolve(payload.id);
793
+ const payload = await finalizeResponse.json();
794
+ taskMetaPromise.resolve({
795
+ id: payload.taskId,
796
+ showUrl: payload.urls?.show ?? null
797
+ });
249
798
  payloadPromise.resolve(payload);
250
799
  if (onUpdate) {
251
800
  taskUpdateHandlers.add(onUpdate);
252
801
  startMonitoring();
253
802
  }
254
803
  })();
255
- const asyncIterable = callbackToAsyncIterator({
256
- subscribe: (push) => {
257
- taskUpdateHandlers.add(push);
258
- startMonitoring();
259
- },
260
- done: (value) => value.type === "taskError" || value.type === "taskEnd"
804
+ const asyncIterable = callbackToAsyncIterable((push, _fail, done) => {
805
+ taskUpdateHandlers.add(function handleUpdate(update) {
806
+ push(update);
807
+ if (update.type === "taskEnd" || update.type === "taskError") {
808
+ done();
809
+ taskUpdateHandlers.delete(handleUpdate);
810
+ }
811
+ });
812
+ startMonitoring();
261
813
  });
262
814
  return Object.assign(resultPromise.promise, asyncIterable, {
263
- getTaskId() {
264
- return taskIdPromise;
815
+ async getTaskId() {
816
+ const meta = await taskMetaPromise;
817
+ return meta.id;
818
+ },
819
+ async getTaskUrl() {
820
+ const meta = await taskMetaPromise;
821
+ return meta.showUrl;
822
+ },
823
+ async abort(reason) {
824
+ const meta = await taskMetaPromise;
825
+ await abortTask({
826
+ taskId: meta.id,
827
+ accessToken: options.accessToken,
828
+ reason
829
+ });
265
830
  }
266
831
  });
267
832
  }
833
+ // src/watchTasks.ts
834
+ import { EventSource as EventSource2 } from "eventsource";
835
+ function watchTasks(options) {
836
+ const { apiBaseUrl, token } = getAccessToken(options.accessToken);
837
+ const taskUpdateHandlers = new Set;
838
+ if (options.onUpdate) {
839
+ taskUpdateHandlers.add(options.onUpdate);
840
+ }
841
+ const watchUrl = new URL(`${apiBaseUrl}/task/watch-all`);
842
+ watchUrl.searchParams.set("since", options.since.toISOString());
843
+ const eventSource = new EventSource2(watchUrl, {
844
+ fetch: (input, init) => fetch(input, {
845
+ ...init,
846
+ headers: {
847
+ Authorization: `Bearer ${token}`
848
+ }
849
+ })
850
+ });
851
+ const asyncIterable = callbackToAsyncIterable((push) => {
852
+ taskUpdateHandlers.add(push);
853
+ });
854
+ const taskEvents = [
855
+ "taskStart",
856
+ "taskError",
857
+ "taskEnd",
858
+ "nodeStart",
859
+ "nodeUpdate",
860
+ "nodeDefer",
861
+ "nodeError",
862
+ "nodeEnd"
863
+ ];
864
+ taskEvents.forEach((taskEvent) => {
865
+ eventSource.addEventListener(taskEvent, (event) => {
866
+ const updateResult = eventSchema.safeParse(event);
867
+ if (!updateResult.success) {
868
+ eventSource.close();
869
+ throw new Error("invalid update");
870
+ }
871
+ const update = updateResult.data;
872
+ const context = update.context;
873
+ if (update.type === "nodeUpdate") {
874
+ context.dates[update.node.id] = update.timestamp.toISOString();
875
+ context.data[update.node.id] = update.data;
876
+ }
877
+ const contextUpdate = {
878
+ ...update,
879
+ context: createContextHelper(context, update.nodes)
880
+ };
881
+ const extendedUpdate = contextUpdate.type === "nodeUpdate" ? {
882
+ ...contextUpdate,
883
+ output: contextUpdate.output === "__default__" ? null : contextUpdate.output,
884
+ data: mapOutput2(contextUpdate.data)
885
+ } : contextUpdate;
886
+ taskUpdateHandlers.forEach((handler) => handler(extendedUpdate));
887
+ });
888
+ });
889
+ const stopWatching = () => {
890
+ eventSource.close();
891
+ };
892
+ return Object.assign(stopWatching, asyncIterable);
893
+ }
268
894
  export {
895
+ watchTasks,
269
896
  createTask,
270
- TaskError
897
+ abortTask,
898
+ RequenceStream,
899
+ RequenceFile
271
900
  };
272
901
 
273
- //# debugId=1F3F1932800384D964756E2164756E21
902
+ //# debugId=5101AFB42955246864756E2164756E21
274
903
  //# sourceMappingURL=index.js.map