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