@requence/task 1.0.0-alpha.1 → 1.0.0-alpha.11
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 +55 -0
- package/build/chunk-ajdmm777.js +125 -0
- package/build/chunk-ajdmm777.js.map +12 -0
- package/build/cli.js +17 -502
- package/build/cli.js.map +4 -8
- package/build/index.js +350 -88
- package/build/index.js.map +12 -6
- package/build/types/helpers/src/context/context.d.ts +156 -0
- package/build/types/helpers/src/context/context.d.ts.map +1 -0
- package/build/types/helpers/src/context/index.d.ts +2 -0
- package/build/types/helpers/src/context/index.d.ts.map +1 -0
- 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/index.d.ts +48 -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 +7 -0
- package/build/types/helpers/src/files/mapOutput.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/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 +11 -0
- package/build/types/helpers/src/jsonschema/types.d.ts.map +1 -0
- package/build/types/helpers/src/jsonschema/validate.d.ts +27 -0
- package/build/types/helpers/src/jsonschema/validate.d.ts.map +1 -0
- package/build/types/helpers/src/jsonschema/zod.d.ts +20 -0
- package/build/types/helpers/src/jsonschema/zod.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/command.d.ts +1969 -0
- package/build/types/helpers/src/protocol/command.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/createNodeMaps.d.ts +10 -0
- package/build/types/helpers/src/protocol/createNodeMaps.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/getCycles.d.ts +3 -0
- package/build/types/helpers/src/protocol/getCycles.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/getNodeOutputs.d.ts +4 -0
- package/build/types/helpers/src/protocol/getNodeOutputs.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/getPossiblePaths.d.ts +9 -0
- package/build/types/helpers/src/protocol/getPossiblePaths.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/identifyNode.d.ts +4 -0
- package/build/types/helpers/src/protocol/identifyNode.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/index.d.ts +10 -0
- package/build/types/helpers/src/protocol/index.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/node.d.ts +1014 -0
- package/build/types/helpers/src/protocol/node.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/nodes.d.ts +801 -0
- package/build/types/helpers/src/protocol/nodes.d.ts.map +1 -0
- package/build/types/helpers/src/protocol/update.d.ts +3087 -0
- package/build/types/helpers/src/protocol/update.d.ts.map +1 -0
- package/build/types/helpers/src/utils.d.ts +14 -1
- package/build/types/helpers/src/utils.d.ts.map +1 -1
- package/build/types/task/src/createTask.d.ts +110 -0
- package/build/types/task/src/createTask.d.ts.map +1 -0
- package/build/types/task/src/index.d.ts +3 -81
- package/build/types/task/src/index.d.ts.map +1 -1
- package/build/types/task/src/types.d.ts +658 -41
- package/build/types/task/src/types.d.ts.map +1 -1
- package/build/types/task/src/utils/callbackToAsyncIterator.d.ts +1 -1
- package/build/types/task/src/utils/callbackToAsyncIterator.d.ts.map +1 -1
- package/build/types/task/src/utils/getAccessToken.d.ts +5 -0
- package/build/types/task/src/utils/getAccessToken.d.ts.map +1 -0
- package/build/types/task/src/utils/mapOutput.d.ts +2 -0
- package/build/types/task/src/utils/mapOutput.d.ts.map +1 -0
- package/build/types/task/src/watchTasks.d.ts +105 -0
- package/build/types/task/src/watchTasks.d.ts.map +1 -0
- package/package.json +7 -4
- package/build/chunk-s4kgf465.js +0 -67
- package/build/chunk-s4kgf465.js.map +0 -12
package/build/index.js
CHANGED
|
@@ -1,88 +1,231 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createRemotePromise,
|
|
3
|
-
deobfuscate
|
|
4
|
-
|
|
3
|
+
deobfuscate,
|
|
4
|
+
mapData
|
|
5
|
+
} from "./chunk-ajdmm777.js";
|
|
5
6
|
|
|
6
|
-
// src/index.ts
|
|
7
|
-
import {
|
|
7
|
+
// ../helpers/src/files/index.ts
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
|
|
10
|
+
// ../helpers/src/files/RequenceFile.ts
|
|
11
|
+
class RequenceFile {
|
|
12
|
+
#data;
|
|
13
|
+
#file = null;
|
|
14
|
+
constructor(blobParts, options) {
|
|
15
|
+
this.#data = new Blob(blobParts, options);
|
|
16
|
+
}
|
|
17
|
+
static fromFileObject(file) {
|
|
18
|
+
const instance = new this([], { type: "x/x" });
|
|
19
|
+
instance.#file = file;
|
|
20
|
+
return instance;
|
|
21
|
+
}
|
|
22
|
+
get fileObject() {
|
|
23
|
+
return this.#file;
|
|
24
|
+
}
|
|
25
|
+
get size() {
|
|
26
|
+
return this.#file?.size ?? this.#data.size;
|
|
27
|
+
}
|
|
28
|
+
get mimeType() {
|
|
29
|
+
return this.#file?.mime ?? this.#data.type;
|
|
30
|
+
}
|
|
31
|
+
get mainMimeType() {
|
|
32
|
+
return this.mimeType.split("/")[0];
|
|
33
|
+
}
|
|
34
|
+
get subMimeType() {
|
|
35
|
+
return this.mimeType.split("/")[1];
|
|
36
|
+
}
|
|
37
|
+
async text() {
|
|
38
|
+
if (this.#file) {
|
|
39
|
+
return fetch(this.#file.url).then((response) => response.text());
|
|
40
|
+
}
|
|
41
|
+
return this.#data.text();
|
|
42
|
+
}
|
|
43
|
+
async json() {
|
|
44
|
+
if (this.#file) {
|
|
45
|
+
return fetch(this.#file.url).then((response) => response.json());
|
|
46
|
+
}
|
|
47
|
+
return JSON.parse(await this.text());
|
|
48
|
+
}
|
|
49
|
+
async blob() {
|
|
50
|
+
if (this.#file) {
|
|
51
|
+
return fetch(this.#file.url).then((response) => response.blob());
|
|
52
|
+
}
|
|
53
|
+
return this.#data;
|
|
54
|
+
}
|
|
55
|
+
async arrayBuffer() {
|
|
56
|
+
if (this.#file) {
|
|
57
|
+
return fetch(this.#file.url).then((response) => response.arrayBuffer());
|
|
58
|
+
}
|
|
59
|
+
return this.#data.arrayBuffer();
|
|
60
|
+
}
|
|
61
|
+
toString() {
|
|
62
|
+
return `RequenceFile<${this.mimeType}>`;
|
|
63
|
+
}
|
|
64
|
+
toJSON() {
|
|
65
|
+
return `RequenceFile<${this.mimeType}>`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// ../helpers/src/files/RequenceStream.ts
|
|
69
|
+
class RequenceStream {
|
|
70
|
+
#data;
|
|
71
|
+
#stream = null;
|
|
72
|
+
#options;
|
|
73
|
+
constructor(stream, options) {
|
|
74
|
+
this.#data = stream;
|
|
75
|
+
this.#options = options;
|
|
76
|
+
}
|
|
77
|
+
static fromStreamObject(stream) {
|
|
78
|
+
const instance = new this(null, {
|
|
79
|
+
type: "x/x"
|
|
80
|
+
});
|
|
81
|
+
instance.#stream = stream;
|
|
82
|
+
return instance;
|
|
83
|
+
}
|
|
84
|
+
get streamObject() {
|
|
85
|
+
return this.#stream;
|
|
86
|
+
}
|
|
87
|
+
get mimeType() {
|
|
88
|
+
return this.#stream?.mime ?? this.#options.type;
|
|
89
|
+
}
|
|
90
|
+
get mainMimeType() {
|
|
91
|
+
return this.mimeType.split("/")[0];
|
|
92
|
+
}
|
|
93
|
+
get subMimeType() {
|
|
94
|
+
return this.mimeType.split("/")[1];
|
|
95
|
+
}
|
|
96
|
+
stream() {
|
|
97
|
+
if (this.#stream) {
|
|
98
|
+
const { readable, writable } = new TransformStream;
|
|
99
|
+
fetch(this.#stream.url).then((response) => response.body?.pipeTo(writable));
|
|
100
|
+
return readable;
|
|
101
|
+
}
|
|
102
|
+
return this.#data;
|
|
103
|
+
}
|
|
104
|
+
asyncIterable() {
|
|
105
|
+
const reader = this.stream().getReader();
|
|
106
|
+
return {
|
|
107
|
+
[Symbol.asyncIterator]() {
|
|
108
|
+
return {
|
|
109
|
+
async next() {
|
|
110
|
+
return reader.read();
|
|
111
|
+
},
|
|
112
|
+
async return() {
|
|
113
|
+
reader.cancel();
|
|
114
|
+
reader.releaseLock();
|
|
115
|
+
return { done: true, value: null };
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
toString() {
|
|
122
|
+
return `RequenceStream<${this.mimeType}>`;
|
|
123
|
+
}
|
|
124
|
+
toJSON() {
|
|
125
|
+
return `RequenceStream<${this.mimeType}>`;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ../helpers/src/files/index.ts
|
|
130
|
+
var fileObjectSchema = z.object({
|
|
131
|
+
size: z.number().int().positive(),
|
|
132
|
+
mime: z.string(),
|
|
133
|
+
url: z.string().url(),
|
|
134
|
+
__REQUENCE_type: z.literal("RequenceFile")
|
|
135
|
+
});
|
|
136
|
+
function isFileObject(data) {
|
|
137
|
+
return fileObjectSchema.safeParse(data).success;
|
|
138
|
+
}
|
|
139
|
+
var streamObjectSchema = z.object({
|
|
140
|
+
mime: z.string(),
|
|
141
|
+
url: z.string().url(),
|
|
142
|
+
__REQUENCE_type: z.literal("RequenceStream")
|
|
143
|
+
});
|
|
144
|
+
function isStreamObject(data) {
|
|
145
|
+
return streamObjectSchema.safeParse(data).success;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/createTask.ts
|
|
8
149
|
import { EventSource } from "eventsource";
|
|
9
|
-
import { z as z2 } from "zod";
|
|
10
150
|
|
|
11
151
|
// src/types.ts
|
|
12
|
-
import { z } from "zod";
|
|
13
|
-
var serviceNodeInfoSchema =
|
|
14
|
-
id:
|
|
15
|
-
type:
|
|
16
|
-
alias:
|
|
17
|
-
service:
|
|
18
|
-
id:
|
|
19
|
-
name:
|
|
20
|
-
version:
|
|
152
|
+
import { z as z2 } from "zod";
|
|
153
|
+
var serviceNodeInfoSchema = z2.object({
|
|
154
|
+
id: z2.string().uuid(),
|
|
155
|
+
type: z2.literal("service"),
|
|
156
|
+
alias: z2.string().optional(),
|
|
157
|
+
service: z2.object({
|
|
158
|
+
id: z2.string().uuid(),
|
|
159
|
+
name: z2.string(),
|
|
160
|
+
version: z2.string()
|
|
21
161
|
}).optional()
|
|
22
162
|
});
|
|
23
|
-
var logicNodeInfoSchema =
|
|
24
|
-
id:
|
|
25
|
-
type:
|
|
26
|
-
alias:
|
|
27
|
-
logic:
|
|
28
|
-
language:
|
|
163
|
+
var logicNodeInfoSchema = z2.object({
|
|
164
|
+
id: z2.string().uuid(),
|
|
165
|
+
type: z2.literal("logic"),
|
|
166
|
+
alias: z2.string().optional(),
|
|
167
|
+
logic: z2.object({
|
|
168
|
+
language: z2.enum(["typescript", "javascript", "python"])
|
|
29
169
|
}).optional()
|
|
30
170
|
});
|
|
31
|
-
var nodeInfoSchema =
|
|
171
|
+
var nodeInfoSchema = z2.discriminatedUnion("type", [
|
|
32
172
|
serviceNodeInfoSchema,
|
|
33
173
|
logicNodeInfoSchema
|
|
34
174
|
]);
|
|
35
|
-
var
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
175
|
+
var baseUpdate = z2.object({
|
|
176
|
+
timestamp: z2.coerce.date(),
|
|
177
|
+
taskId: z2.string().uuid()
|
|
178
|
+
});
|
|
179
|
+
var taskStartUpdateSchema = baseUpdate.extend({
|
|
180
|
+
type: z2.literal("taskStart"),
|
|
181
|
+
input: z2.unknown(),
|
|
182
|
+
meta: z2.unknown()
|
|
40
183
|
});
|
|
41
|
-
var taskErrorUpdateSchema =
|
|
42
|
-
type:
|
|
43
|
-
|
|
44
|
-
error: z.string()
|
|
184
|
+
var taskErrorUpdateSchema = baseUpdate.extend({
|
|
185
|
+
type: z2.literal("taskError"),
|
|
186
|
+
error: z2.string()
|
|
45
187
|
});
|
|
46
|
-
var taskEndUpdateSchema =
|
|
47
|
-
type:
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
partialResults: z.array(z.object({
|
|
188
|
+
var taskEndUpdateSchema = baseUpdate.extend({
|
|
189
|
+
type: z2.literal("taskEnd"),
|
|
190
|
+
result: z2.record(z2.string(), z2.unknown()),
|
|
191
|
+
partialResults: z2.array(z2.object({
|
|
51
192
|
node: nodeInfoSchema,
|
|
52
|
-
timestamp:
|
|
53
|
-
data:
|
|
54
|
-
error:
|
|
193
|
+
timestamp: z2.coerce.date().nullable(),
|
|
194
|
+
data: z2.unknown(),
|
|
195
|
+
error: z2.string().optional()
|
|
55
196
|
}))
|
|
56
197
|
});
|
|
57
|
-
var nodeStartUpdateSchema =
|
|
58
|
-
type:
|
|
59
|
-
timestamp: z.coerce.date(),
|
|
198
|
+
var nodeStartUpdateSchema = baseUpdate.extend({
|
|
199
|
+
type: z2.literal("nodeStart"),
|
|
60
200
|
node: nodeInfoSchema
|
|
61
201
|
});
|
|
62
|
-
var nodeUpdateUpdateSchema =
|
|
63
|
-
type:
|
|
64
|
-
|
|
65
|
-
data: z.unknown()
|
|
202
|
+
var nodeUpdateUpdateSchema = baseUpdate.extend({
|
|
203
|
+
type: z2.literal("nodeUpdate"),
|
|
204
|
+
data: z2.unknown()
|
|
66
205
|
});
|
|
67
|
-
var nodeErrorUpdateSchema =
|
|
68
|
-
type:
|
|
69
|
-
|
|
70
|
-
error: z.string()
|
|
206
|
+
var nodeErrorUpdateSchema = baseUpdate.extend({
|
|
207
|
+
type: z2.literal("nodeError"),
|
|
208
|
+
error: z2.string()
|
|
71
209
|
});
|
|
72
|
-
var nodeDeferUpdateSchema =
|
|
73
|
-
type:
|
|
74
|
-
|
|
75
|
-
reason: z.string().optional()
|
|
210
|
+
var nodeDeferUpdateSchema = baseUpdate.extend({
|
|
211
|
+
type: z2.literal("nodeDefer"),
|
|
212
|
+
reason: z2.string().optional()
|
|
76
213
|
});
|
|
77
|
-
var
|
|
214
|
+
var nodeEndUpdateSchema = baseUpdate.extend({
|
|
215
|
+
type: z2.literal("nodeEnd"),
|
|
216
|
+
node: nodeInfoSchema
|
|
217
|
+
});
|
|
218
|
+
var updateSchema = z2.discriminatedUnion("type", [
|
|
78
219
|
taskStartUpdateSchema,
|
|
79
220
|
taskErrorUpdateSchema,
|
|
80
221
|
taskEndUpdateSchema,
|
|
81
222
|
nodeStartUpdateSchema,
|
|
82
223
|
nodeUpdateUpdateSchema,
|
|
83
224
|
nodeErrorUpdateSchema,
|
|
84
|
-
nodeDeferUpdateSchema
|
|
225
|
+
nodeDeferUpdateSchema,
|
|
226
|
+
nodeEndUpdateSchema
|
|
85
227
|
]);
|
|
228
|
+
var eventSchema = z2.object({ type: z2.string(), data: z2.string() }).transform((event) => ({ type: event.type, ...JSON.parse(event.data) })).pipe(updateSchema);
|
|
86
229
|
|
|
87
230
|
// src/utils/callbackToAsyncIterator.ts
|
|
88
231
|
function callbackToAsyncIterator(options) {
|
|
@@ -113,7 +256,9 @@ function callbackToAsyncIterator(options) {
|
|
|
113
256
|
return { value: undefined, done: true };
|
|
114
257
|
}
|
|
115
258
|
const value = await pullValue();
|
|
116
|
-
|
|
259
|
+
if (options.done) {
|
|
260
|
+
done = options.done(value);
|
|
261
|
+
}
|
|
117
262
|
return { value, done: false };
|
|
118
263
|
},
|
|
119
264
|
async return() {
|
|
@@ -125,30 +270,52 @@ function callbackToAsyncIterator(options) {
|
|
|
125
270
|
};
|
|
126
271
|
}
|
|
127
272
|
|
|
128
|
-
// src/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.taskTemplate = taskTemplate;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
var eventSchema = z2.object({ type: z2.string(), data: z2.string() }).transform((event) => ({ type: event.type, ...JSON.parse(event.data) })).pipe(updateSchema);
|
|
137
|
-
function createTask(params, onUpdate) {
|
|
138
|
-
const taskUpdateHandlers = new Set;
|
|
139
|
-
const accessKey = params.accessToken ?? process.env.REQUENCE_ACCESS_TOKEN ?? JSON.parse(readFileSync("package.json", "utf-8")).requence?.accessToken;
|
|
140
|
-
if (!accessKey) {
|
|
273
|
+
// src/utils/getAccessToken.ts
|
|
274
|
+
import { readFileSync } from "node:fs";
|
|
275
|
+
function getAccessToken(accessToken) {
|
|
276
|
+
const token = accessToken ?? process.env.REQUENCE_ACCESS_TOKEN ?? JSON.parse(readFileSync("package.json", "utf-8")).requence?.accessToken;
|
|
277
|
+
if (!token) {
|
|
141
278
|
throw new Error(`No access token found
|
|
142
279
|
You can provide the access token as argument to createTask,
|
|
143
280
|
as environment variable REQUENCE_ACCESS_TOKEN or
|
|
144
281
|
in your package.json in requence.accessToken`);
|
|
145
282
|
}
|
|
146
|
-
const parts = deobfuscate(
|
|
283
|
+
const parts = deobfuscate(token);
|
|
147
284
|
if (parts.length < 2 || parts[0] !== "api") {
|
|
148
285
|
throw new Error("Invalid api key");
|
|
149
286
|
}
|
|
150
|
-
|
|
151
|
-
|
|
287
|
+
return {
|
|
288
|
+
token: parts[1],
|
|
289
|
+
apiBaseUrl: parts[2]
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/utils/mapOutput.ts
|
|
294
|
+
function mapOutput2(data) {
|
|
295
|
+
return mapData(data, {
|
|
296
|
+
Object(obj) {
|
|
297
|
+
if (isFileObject(obj)) {
|
|
298
|
+
return RequenceFile.fromFileObject(obj);
|
|
299
|
+
}
|
|
300
|
+
if (isStreamObject(obj)) {
|
|
301
|
+
return RequenceStream.fromStreamObject(obj);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// src/createTask.ts
|
|
308
|
+
class TaskError extends Error {
|
|
309
|
+
taskTemplate;
|
|
310
|
+
constructor(message, taskTemplate) {
|
|
311
|
+
super(message);
|
|
312
|
+
this.taskTemplate = taskTemplate;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function createTask(options, onUpdate) {
|
|
316
|
+
const taskUpdateHandlers = new Set;
|
|
317
|
+
const { apiBaseUrl, token } = getAccessToken(options.accessToken);
|
|
318
|
+
const startUrl = `${apiBaseUrl}/task/start/${options.taskTemplate}`;
|
|
152
319
|
const payloadPromise = createRemotePromise();
|
|
153
320
|
let isMonitoring = false;
|
|
154
321
|
const startMonitoring = async () => {
|
|
@@ -157,6 +324,23 @@ in your package.json in requence.accessToken`);
|
|
|
157
324
|
}
|
|
158
325
|
isMonitoring = true;
|
|
159
326
|
const payload = await payloadPromise;
|
|
327
|
+
if (!payload.urls) {
|
|
328
|
+
resultPromise.resolve({
|
|
329
|
+
getInput() {
|
|
330
|
+
return input;
|
|
331
|
+
},
|
|
332
|
+
getMeta() {
|
|
333
|
+
return meta;
|
|
334
|
+
},
|
|
335
|
+
getError(_alias) {
|
|
336
|
+
return null;
|
|
337
|
+
},
|
|
338
|
+
getData(_alias) {
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
160
344
|
const eventSource = new EventSource(payload.urls.watch, {
|
|
161
345
|
fetch: (input2, init) => fetch(input2, {
|
|
162
346
|
...init,
|
|
@@ -172,7 +356,8 @@ in your package.json in requence.accessToken`);
|
|
|
172
356
|
"nodeStart",
|
|
173
357
|
"nodeUpdate",
|
|
174
358
|
"nodeDefer",
|
|
175
|
-
"nodeError"
|
|
359
|
+
"nodeError",
|
|
360
|
+
"nodeEnd"
|
|
176
361
|
];
|
|
177
362
|
let input = null;
|
|
178
363
|
let meta = null;
|
|
@@ -181,11 +366,10 @@ in your package.json in requence.accessToken`);
|
|
|
181
366
|
const updateResult = eventSchema.safeParse(event);
|
|
182
367
|
if (!updateResult.success) {
|
|
183
368
|
eventSource.close();
|
|
184
|
-
resultPromise.reject(new TaskError(`Invalid ${taskEvent} update received`,
|
|
369
|
+
resultPromise.reject(new TaskError(`Invalid ${taskEvent} update received`, options.taskTemplate));
|
|
185
370
|
return;
|
|
186
371
|
}
|
|
187
372
|
const update = updateResult.data;
|
|
188
|
-
taskUpdateHandlers.forEach((handler) => handler(update));
|
|
189
373
|
if (update.type === "taskEnd" || update.type === "taskError") {
|
|
190
374
|
eventSource.close();
|
|
191
375
|
}
|
|
@@ -197,42 +381,64 @@ in your package.json in requence.accessToken`);
|
|
|
197
381
|
const getPartialResult = (alias) => update.partialResults.find((partialResult) => partialResult.node.alias === alias);
|
|
198
382
|
resultPromise.resolve({
|
|
199
383
|
getInput() {
|
|
200
|
-
return input;
|
|
384
|
+
return mapOutput2(input);
|
|
201
385
|
},
|
|
202
386
|
getMeta() {
|
|
203
|
-
return meta;
|
|
387
|
+
return mapOutput2(meta);
|
|
204
388
|
},
|
|
205
389
|
getError(alias) {
|
|
206
390
|
return getPartialResult(alias)?.error ?? null;
|
|
207
391
|
},
|
|
208
392
|
getData(alias) {
|
|
209
393
|
if (!alias) {
|
|
210
|
-
return update.result;
|
|
394
|
+
return mapOutput2(update.result);
|
|
211
395
|
}
|
|
212
|
-
return getPartialResult(alias)?.data ?? null;
|
|
396
|
+
return mapOutput2(getPartialResult(alias)?.data ?? null);
|
|
213
397
|
}
|
|
214
398
|
});
|
|
215
399
|
}
|
|
216
400
|
if (update.type === "taskError") {
|
|
217
|
-
resultPromise.reject(new TaskError(update.error,
|
|
401
|
+
resultPromise.reject(new TaskError(update.error, options.taskTemplate));
|
|
218
402
|
}
|
|
403
|
+
taskUpdateHandlers.forEach((handler) => handler(update));
|
|
219
404
|
});
|
|
220
405
|
});
|
|
221
406
|
};
|
|
222
407
|
const taskIdPromise = createRemotePromise();
|
|
223
408
|
const resultPromise = createRemotePromise(startMonitoring);
|
|
224
409
|
(async () => {
|
|
410
|
+
const formData = new FormData;
|
|
411
|
+
const walkData = async (data, path = []) => {
|
|
412
|
+
const name = path.map((segment, index) => index === 0 ? segment : typeof segment === "string" ? `.${segment}` : `[${segment}]`).join("");
|
|
413
|
+
if (data instanceof RequenceFile) {
|
|
414
|
+
formData.append(name, await data.blob(), name);
|
|
415
|
+
} else if (data instanceof RequenceStream) {
|
|
416
|
+
console.warn("stream not supported");
|
|
417
|
+
formData.append(name, `json://null`);
|
|
418
|
+
} else if (Array.isArray(data)) {
|
|
419
|
+
await Promise.all(data.map((item, index) => walkData(item, [...path, index])));
|
|
420
|
+
} else if (data && typeof data === "object") {
|
|
421
|
+
for (const [key, value] of Object.entries(data)) {
|
|
422
|
+
await walkData(value, [...path, key]);
|
|
423
|
+
}
|
|
424
|
+
} else if (typeof data !== "undefined") {
|
|
425
|
+
formData.append(name, `json://${JSON.stringify(data)}`);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
await walkData({
|
|
429
|
+
input: options.input ?? null,
|
|
430
|
+
meta: options.meta ?? null,
|
|
431
|
+
name: options.name ?? null
|
|
432
|
+
});
|
|
225
433
|
const response = await fetch(startUrl, {
|
|
226
|
-
method: "
|
|
434
|
+
method: "POST",
|
|
435
|
+
body: formData,
|
|
227
436
|
headers: {
|
|
228
|
-
Accept: "application/json",
|
|
229
|
-
"Content-Type": "application/json",
|
|
230
437
|
Authorization: `Bearer ${token}`
|
|
231
|
-
}
|
|
232
|
-
body: JSON.stringify({ input: params.input, meta: params.meta })
|
|
438
|
+
}
|
|
233
439
|
});
|
|
234
440
|
if (!response.ok) {
|
|
235
|
-
const error = new TaskError(await response.text(),
|
|
441
|
+
const error = new TaskError(await response.text(), options.taskTemplate);
|
|
236
442
|
taskIdPromise.reject(error);
|
|
237
443
|
resultPromise.reject(error);
|
|
238
444
|
return;
|
|
@@ -249,6 +455,9 @@ in your package.json in requence.accessToken`);
|
|
|
249
455
|
subscribe: (push) => {
|
|
250
456
|
taskUpdateHandlers.add(push);
|
|
251
457
|
startMonitoring();
|
|
458
|
+
return () => {
|
|
459
|
+
taskUpdateHandlers.delete(push);
|
|
460
|
+
};
|
|
252
461
|
},
|
|
253
462
|
done: (value) => value.type === "taskError" || value.type === "taskEnd"
|
|
254
463
|
});
|
|
@@ -258,10 +467,63 @@ in your package.json in requence.accessToken`);
|
|
|
258
467
|
}
|
|
259
468
|
});
|
|
260
469
|
}
|
|
470
|
+
// src/watchTasks.ts
|
|
471
|
+
import { EventSource as EventSource2 } from "eventsource";
|
|
472
|
+
function watchTasks(options) {
|
|
473
|
+
const { apiBaseUrl, token } = getAccessToken(options.accessToken);
|
|
474
|
+
const taskUpdateHandlers = new Set;
|
|
475
|
+
if (options.onUpdate) {
|
|
476
|
+
taskUpdateHandlers.add(options.onUpdate);
|
|
477
|
+
}
|
|
478
|
+
const watchUrl = new URL(`${apiBaseUrl}/task/watch-all`);
|
|
479
|
+
watchUrl.searchParams.set("since", options.since.toISOString());
|
|
480
|
+
const eventSource = new EventSource2(watchUrl, {
|
|
481
|
+
fetch: (input, init) => fetch(input, {
|
|
482
|
+
...init,
|
|
483
|
+
headers: {
|
|
484
|
+
Authorization: `Bearer ${token}`
|
|
485
|
+
}
|
|
486
|
+
})
|
|
487
|
+
});
|
|
488
|
+
const asyncIterable = callbackToAsyncIterator({
|
|
489
|
+
subscribe: (push) => {
|
|
490
|
+
taskUpdateHandlers.add(push);
|
|
491
|
+
return () => {
|
|
492
|
+
taskUpdateHandlers.delete(push);
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
const taskEvents = [
|
|
497
|
+
"taskStart",
|
|
498
|
+
"taskError",
|
|
499
|
+
"taskEnd",
|
|
500
|
+
"nodeStart",
|
|
501
|
+
"nodeUpdate",
|
|
502
|
+
"nodeDefer",
|
|
503
|
+
"nodeError",
|
|
504
|
+
"nodeEnd"
|
|
505
|
+
];
|
|
506
|
+
taskEvents.forEach((taskEvent) => {
|
|
507
|
+
eventSource.addEventListener(taskEvent, (event) => {
|
|
508
|
+
const updateResult = eventSchema.safeParse(event);
|
|
509
|
+
if (!updateResult.success) {
|
|
510
|
+
eventSource.close();
|
|
511
|
+
throw new Error("invalid update");
|
|
512
|
+
}
|
|
513
|
+
taskUpdateHandlers.forEach((handler) => handler(updateResult.data));
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
const stopWatching = () => {
|
|
517
|
+
eventSource.close();
|
|
518
|
+
};
|
|
519
|
+
return Object.assign(stopWatching, asyncIterable);
|
|
520
|
+
}
|
|
261
521
|
export {
|
|
522
|
+
watchTasks,
|
|
262
523
|
createTask,
|
|
263
|
-
|
|
524
|
+
RequenceStream,
|
|
525
|
+
RequenceFile
|
|
264
526
|
};
|
|
265
527
|
|
|
266
|
-
//# debugId=
|
|
528
|
+
//# debugId=0DC345883E2D093D64756E2164756E21
|
|
267
529
|
//# sourceMappingURL=index.js.map
|