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