@requence/task 1.0.0-alpha.2 → 1.0.0-alpha.20

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