@workglow/util 0.0.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +203 -0
  2. package/README.md +318 -0
  3. package/dist/browser.js +1554 -0
  4. package/dist/browser.js.map +27 -0
  5. package/dist/bun.js +1558 -0
  6. package/dist/bun.js.map +27 -0
  7. package/dist/common.d.ts +20 -0
  8. package/dist/common.d.ts.map +1 -0
  9. package/dist/compress/compress.browser.d.ts +8 -0
  10. package/dist/compress/compress.browser.d.ts.map +1 -0
  11. package/dist/compress/compress.node.d.ts +8 -0
  12. package/dist/compress/compress.node.d.ts.map +1 -0
  13. package/dist/crypto/Crypto.browser.d.ts +9 -0
  14. package/dist/crypto/Crypto.browser.d.ts.map +1 -0
  15. package/dist/crypto/Crypto.bun.d.ts +9 -0
  16. package/dist/crypto/Crypto.bun.d.ts.map +1 -0
  17. package/dist/crypto/Crypto.node.d.ts +9 -0
  18. package/dist/crypto/Crypto.node.d.ts.map +1 -0
  19. package/dist/di/Container.d.ts +53 -0
  20. package/dist/di/Container.d.ts.map +1 -0
  21. package/dist/di/ServiceRegistry.d.ts +60 -0
  22. package/dist/di/ServiceRegistry.d.ts.map +1 -0
  23. package/dist/di/index.d.ts +8 -0
  24. package/dist/di/index.d.ts.map +1 -0
  25. package/dist/events/EventEmitter.d.ts +80 -0
  26. package/dist/events/EventEmitter.d.ts.map +1 -0
  27. package/dist/graph/directedAcyclicGraph.d.ts +70 -0
  28. package/dist/graph/directedAcyclicGraph.d.ts.map +1 -0
  29. package/dist/graph/directedGraph.d.ts +90 -0
  30. package/dist/graph/directedGraph.d.ts.map +1 -0
  31. package/dist/graph/errors.d.ts +38 -0
  32. package/dist/graph/errors.d.ts.map +1 -0
  33. package/dist/graph/graph.d.ts +217 -0
  34. package/dist/graph/graph.d.ts.map +1 -0
  35. package/dist/graph/index.d.ts +5 -0
  36. package/dist/graph/index.d.ts.map +1 -0
  37. package/dist/json-schema/DataPortSchema.d.ts +17 -0
  38. package/dist/json-schema/DataPortSchema.d.ts.map +1 -0
  39. package/dist/json-schema/FromSchema.d.ts +96 -0
  40. package/dist/json-schema/FromSchema.d.ts.map +1 -0
  41. package/dist/json-schema/JsonSchema.d.ts +22 -0
  42. package/dist/json-schema/JsonSchema.d.ts.map +1 -0
  43. package/dist/json-schema/SchemaUtils.d.ts +58 -0
  44. package/dist/json-schema/SchemaUtils.d.ts.map +1 -0
  45. package/dist/json-schema/SchemaValidation.d.ts +8 -0
  46. package/dist/json-schema/SchemaValidation.d.ts.map +1 -0
  47. package/dist/node.js +1579 -0
  48. package/dist/node.js.map +27 -0
  49. package/dist/types.d.ts +10 -0
  50. package/dist/types.d.ts.map +1 -0
  51. package/dist/utilities/BaseError.d.ts +14 -0
  52. package/dist/utilities/BaseError.d.ts.map +1 -0
  53. package/dist/utilities/Misc.d.ts +20 -0
  54. package/dist/utilities/Misc.d.ts.map +1 -0
  55. package/dist/utilities/TypeUtilities.d.ts +27 -0
  56. package/dist/utilities/TypeUtilities.d.ts.map +1 -0
  57. package/dist/utilities/objectOfArraysAsArrayOfObjects.d.ts +26 -0
  58. package/dist/utilities/objectOfArraysAsArrayOfObjects.d.ts.map +1 -0
  59. package/dist/worker/Worker.browser.d.ts +7 -0
  60. package/dist/worker/Worker.browser.d.ts.map +1 -0
  61. package/dist/worker/Worker.bun.d.ts +7 -0
  62. package/dist/worker/Worker.bun.d.ts.map +1 -0
  63. package/dist/worker/Worker.node.d.ts +11 -0
  64. package/dist/worker/Worker.node.d.ts.map +1 -0
  65. package/dist/worker/WorkerManager.d.ts +17 -0
  66. package/dist/worker/WorkerManager.d.ts.map +1 -0
  67. package/dist/worker/WorkerServer.d.ts +26 -0
  68. package/dist/worker/WorkerServer.d.ts.map +1 -0
  69. package/package.json +42 -0
package/dist/bun.js ADDED
@@ -0,0 +1,1558 @@
1
+ // @bun
2
+ // src/di/Container.ts
3
+ class Container {
4
+ services = new Map;
5
+ factories = new Map;
6
+ singletons = new Set;
7
+ register(token, factory, singleton = true) {
8
+ this.factories.set(token, factory);
9
+ if (singleton) {
10
+ this.singletons.add(token);
11
+ }
12
+ }
13
+ registerInstance(token, instance) {
14
+ this.services.set(token, instance);
15
+ this.singletons.add(token);
16
+ }
17
+ get(token) {
18
+ if (this.services.has(token)) {
19
+ return this.services.get(token);
20
+ }
21
+ const factory = this.factories.get(token);
22
+ if (!factory) {
23
+ throw new Error(`Service not registered: ${String(token)}`);
24
+ }
25
+ const instance = factory();
26
+ if (this.singletons.has(token)) {
27
+ this.services.set(token, instance);
28
+ }
29
+ return instance;
30
+ }
31
+ has(token) {
32
+ return this.services.has(token) || this.factories.has(token);
33
+ }
34
+ remove(token) {
35
+ this.services.delete(token);
36
+ this.factories.delete(token);
37
+ this.singletons.delete(token);
38
+ }
39
+ createChildContainer() {
40
+ const child = new Container;
41
+ this.factories.forEach((factory, token) => {
42
+ child.factories.set(token, factory);
43
+ if (this.singletons.has(token)) {
44
+ child.singletons.add(token);
45
+ }
46
+ });
47
+ this.services.forEach((service, token) => {
48
+ if (this.singletons.has(token)) {
49
+ child.services.set(token, service);
50
+ child.singletons.add(token);
51
+ }
52
+ });
53
+ return child;
54
+ }
55
+ }
56
+ var globalContainer = new Container;
57
+ // src/di/ServiceRegistry.ts
58
+ function createServiceToken(id) {
59
+ return { id, _type: null };
60
+ }
61
+
62
+ class ServiceRegistry {
63
+ container;
64
+ constructor(container = globalContainer) {
65
+ this.container = container;
66
+ }
67
+ register(token, factory, singleton = true) {
68
+ this.container.register(token.id, factory, singleton);
69
+ }
70
+ registerInstance(token, instance) {
71
+ this.container.registerInstance(token.id, instance);
72
+ }
73
+ get(token) {
74
+ return this.container.get(token.id);
75
+ }
76
+ has(token) {
77
+ return this.container.has(token.id);
78
+ }
79
+ }
80
+ var globalServiceRegistry = new ServiceRegistry(globalContainer);
81
+ // src/events/EventEmitter.ts
82
+ class EventEmitter {
83
+ listeners = {};
84
+ removeAllListeners(event) {
85
+ if (event) {
86
+ delete this.listeners[event];
87
+ } else {
88
+ this.listeners = {};
89
+ }
90
+ return this;
91
+ }
92
+ on(event, listener) {
93
+ const listeners = this.listeners[event] || (this.listeners[event] = []);
94
+ listeners.push({ listener });
95
+ return this;
96
+ }
97
+ off(event, listener) {
98
+ const listeners = this.listeners[event];
99
+ if (!listeners)
100
+ return this;
101
+ const index = listeners.findIndex((l) => l.listener === listener);
102
+ if (index >= 0) {
103
+ listeners.splice(index, 1);
104
+ }
105
+ return this;
106
+ }
107
+ once(event, listener) {
108
+ const listeners = this.listeners[event] || (this.listeners[event] = []);
109
+ listeners.push({ listener, once: true });
110
+ return this;
111
+ }
112
+ waitOn(event) {
113
+ return new Promise((resolve, reject) => {
114
+ const listener = (...args) => {
115
+ resolve(args);
116
+ };
117
+ this.once(event, listener);
118
+ });
119
+ }
120
+ emit(event, ...args) {
121
+ const listeners = this.listeners[event];
122
+ if (listeners) {
123
+ listeners.forEach(({ listener, once }) => {
124
+ listener(...args);
125
+ });
126
+ this.listeners[event] = listeners.filter((l) => !l.once);
127
+ }
128
+ }
129
+ subscribe(event, listener) {
130
+ this.on(event, listener);
131
+ return () => this.off(event, listener);
132
+ }
133
+ }
134
+ // src/utilities/BaseError.ts
135
+ class BaseError {
136
+ static type = "BaseError";
137
+ message;
138
+ name;
139
+ stack;
140
+ constructor(message = "") {
141
+ this.message = message;
142
+ const constructor = this.constructor;
143
+ this.name = constructor.type ?? this.constructor.name;
144
+ if (typeof Error !== "undefined" && Error.captureStackTrace) {
145
+ const temp = { stack: "" };
146
+ Error.captureStackTrace(temp, this.constructor);
147
+ this.stack = temp.stack;
148
+ } else {
149
+ try {
150
+ throw new Error(message);
151
+ } catch (err) {
152
+ if (err instanceof Error) {
153
+ this.stack = err.stack;
154
+ }
155
+ }
156
+ }
157
+ }
158
+ toString() {
159
+ return `${this.name}: ${this.message}`;
160
+ }
161
+ }
162
+
163
+ // src/graph/errors.ts
164
+ class NodeAlreadyExistsError extends BaseError {
165
+ static type = "NodeAlreadyExistsError";
166
+ newNode;
167
+ oldNode;
168
+ identity;
169
+ constructor(newNode, oldNode, identity) {
170
+ super(`${JSON.stringify(newNode)} shares an identity (${String(identity)}) with ${JSON.stringify(oldNode)}`);
171
+ this.newNode = newNode;
172
+ this.oldNode = oldNode;
173
+ this.identity = identity;
174
+ this.name = "NodeAlreadyExistsError";
175
+ Object.setPrototypeOf(this, NodeAlreadyExistsError.prototype);
176
+ }
177
+ }
178
+
179
+ class NodeDoesntExistError extends BaseError {
180
+ static type = "NodeDoesntExistError";
181
+ identity;
182
+ constructor(identity) {
183
+ super(`A node with identity ${String(identity)} doesn't exist in the graph`);
184
+ this.identity = identity;
185
+ this.name = "NodeDoesntExistError";
186
+ Object.setPrototypeOf(this, NodeDoesntExistError.prototype);
187
+ }
188
+ }
189
+
190
+ class CycleError extends BaseError {
191
+ static type = "CycleError";
192
+ constructor(message) {
193
+ super(message);
194
+ this.name = "CycleError";
195
+ Object.setPrototypeOf(this, CycleError.prototype);
196
+ }
197
+ }
198
+
199
+ // src/graph/graph.ts
200
+ class Graph {
201
+ nodes;
202
+ adjacency;
203
+ nodeIdentity;
204
+ edgeIdentity;
205
+ constructor(nodeIdentity, edgeIdentity) {
206
+ this.nodes = new Map;
207
+ this.adjacency = [];
208
+ this.nodeIdentity = nodeIdentity;
209
+ this.edgeIdentity = edgeIdentity;
210
+ }
211
+ events = new EventEmitter;
212
+ on(name, fn) {
213
+ this.events.on.call(this.events, name, fn);
214
+ }
215
+ off(name, fn) {
216
+ this.events.off.call(this.events, name, fn);
217
+ }
218
+ emit(name, ...args) {
219
+ this.events.emit.call(this.events, name, ...args);
220
+ }
221
+ insert(node) {
222
+ const id = this.nodeIdentity(node);
223
+ const isOverwrite = this.nodes.has(id);
224
+ if (isOverwrite) {
225
+ throw new NodeAlreadyExistsError(node, this.nodes.get(id), id);
226
+ }
227
+ this.nodes.set(id, node);
228
+ this.adjacency.map((adj) => adj.push(null));
229
+ this.adjacency.push(new Array(this.adjacency.length + 1).fill(null));
230
+ this.emit("node-added", id);
231
+ return id;
232
+ }
233
+ replace(node) {
234
+ const id = this.nodeIdentity(node);
235
+ const isOverwrite = this.nodes.has(id);
236
+ if (!isOverwrite) {
237
+ throw new NodeDoesntExistError(id);
238
+ }
239
+ this.nodes.set(id, node);
240
+ this.emit("node-replaced", id);
241
+ }
242
+ upsert(node) {
243
+ const id = this.nodeIdentity(node);
244
+ const isOverwrite = this.nodes.has(id);
245
+ this.nodes.set(id, node);
246
+ if (!isOverwrite) {
247
+ this.adjacency.map((adj) => adj.push(null));
248
+ this.adjacency.push(new Array(this.adjacency.length + 1).fill(null));
249
+ this.emit("node-added", id);
250
+ } else {
251
+ this.emit("node-replaced", id);
252
+ }
253
+ return id;
254
+ }
255
+ addEdge(node1Identity, node2Identity, edge) {
256
+ if (edge === undefined) {
257
+ edge = true;
258
+ }
259
+ const node1Exists = this.nodes.has(node1Identity);
260
+ const node2Exists = this.nodes.has(node2Identity);
261
+ if (!node1Exists) {
262
+ throw new NodeDoesntExistError(node1Identity);
263
+ }
264
+ if (!node2Exists) {
265
+ throw new NodeDoesntExistError(node2Identity);
266
+ }
267
+ const node1Index = Array.from(this.nodes.keys()).indexOf(node1Identity);
268
+ const node2Index = Array.from(this.nodes.keys()).indexOf(node2Identity);
269
+ if (this.adjacency[node1Index][node2Index] === null) {
270
+ this.adjacency[node1Index][node2Index] = [edge];
271
+ } else {
272
+ if (!this.adjacency[node1Index][node2Index].includes(edge)) {
273
+ this.adjacency[node1Index][node2Index].push(edge);
274
+ }
275
+ }
276
+ const id = this.edgeIdentity(edge, node1Identity, node2Identity);
277
+ this.emit("edge-added", id);
278
+ return id;
279
+ }
280
+ getNodes(compareFunc) {
281
+ const temp = Array.from(this.nodes.values());
282
+ if (compareFunc !== undefined) {
283
+ return temp.sort(compareFunc);
284
+ }
285
+ return temp;
286
+ }
287
+ getNode(nodeIdentity) {
288
+ return this.nodes.get(nodeIdentity);
289
+ }
290
+ hasNode(nodeIdentity) {
291
+ return this.nodes.has(nodeIdentity);
292
+ }
293
+ getEdges() {
294
+ const toReturn = [];
295
+ const nodeKeys = Array.from(this.nodes.keys());
296
+ this.adjacency.forEach((row, rowIndex) => {
297
+ const node1Identity = nodeKeys[rowIndex];
298
+ if (node1Identity != null) {
299
+ row.forEach((edges, colIndex) => {
300
+ if (edges !== null) {
301
+ const node2Identity = nodeKeys[colIndex];
302
+ if (node2Identity != null) {
303
+ for (const edge of edges) {
304
+ toReturn.push([node1Identity, node2Identity, edge]);
305
+ }
306
+ }
307
+ }
308
+ });
309
+ }
310
+ });
311
+ return toReturn;
312
+ }
313
+ outEdges(node1Identity) {
314
+ const nodeKeys = Array.from(this.nodes.keys());
315
+ const nodeIndex = nodeKeys.indexOf(node1Identity);
316
+ const toReturn = [];
317
+ this.adjacency[nodeIndex].forEach((edges, colIndex) => {
318
+ if (edges !== null) {
319
+ const node2Identity = nodeKeys[colIndex];
320
+ if (node2Identity != null) {
321
+ for (const edge of edges) {
322
+ toReturn.push([node1Identity, node2Identity, edge]);
323
+ }
324
+ }
325
+ }
326
+ });
327
+ return toReturn;
328
+ }
329
+ inEdges(node2Identity) {
330
+ const nodeKeys = Array.from(this.nodes.keys());
331
+ const node2Index = nodeKeys.indexOf(node2Identity);
332
+ const toReturn = [];
333
+ this.adjacency.forEach((row, rowIndex) => {
334
+ const node1Identity = nodeKeys[rowIndex];
335
+ const edges = row[node2Index];
336
+ if (edges !== null) {
337
+ for (const edge of edges) {
338
+ toReturn.push([node1Identity, node2Identity, edge]);
339
+ }
340
+ }
341
+ });
342
+ return toReturn;
343
+ }
344
+ nodeEdges(nodeIdentity) {
345
+ return [...this.outEdges(nodeIdentity), ...this.inEdges(nodeIdentity)];
346
+ }
347
+ removeEdge(node1Identity, node2Identity, edgeIdentity) {
348
+ const node1Exists = this.nodes.has(node1Identity);
349
+ const node2Exists = this.nodes.has(node2Identity);
350
+ if (!node1Exists) {
351
+ throw new NodeDoesntExistError(node1Identity);
352
+ }
353
+ if (!node2Exists) {
354
+ throw new NodeDoesntExistError(node2Identity);
355
+ }
356
+ const node1Index = Array.from(this.nodes.keys()).indexOf(node1Identity);
357
+ const node2Index = Array.from(this.nodes.keys()).indexOf(node2Identity);
358
+ if (edgeIdentity === undefined) {
359
+ this.adjacency[node1Index][node2Index] = null;
360
+ } else {
361
+ for (const row of this.adjacency) {
362
+ for (const edgelist of row) {
363
+ if (edgelist !== null) {
364
+ for (let edgeIndex = 0;edgeIndex < edgelist.length; edgeIndex++) {
365
+ if (this.edgeIdentity(edgelist[edgeIndex], node1Identity, node2Identity) === edgeIdentity) {
366
+ edgelist.splice(edgeIndex, 1);
367
+ }
368
+ }
369
+ }
370
+ }
371
+ }
372
+ }
373
+ this.emit("edge-removed", edgeIdentity);
374
+ }
375
+ remove(nodeIdentity) {
376
+ if (!this.nodes.has(nodeIdentity)) {
377
+ throw new NodeDoesntExistError(nodeIdentity);
378
+ }
379
+ this.nodes.delete(nodeIdentity);
380
+ const nodeIndex = Array.from(this.nodes.keys()).indexOf(nodeIdentity);
381
+ this.adjacency.splice(nodeIndex, 1);
382
+ this.adjacency.forEach((row) => row.splice(nodeIndex, 1));
383
+ this.emit("node-removed", nodeIdentity);
384
+ }
385
+ removeNode(nodeIdentity) {
386
+ return this.remove(nodeIdentity);
387
+ }
388
+ addNode(node) {
389
+ return this.insert(node);
390
+ }
391
+ addNodes(nodes) {
392
+ return nodes.map((node) => this.insert(node));
393
+ }
394
+ addEdges(edges) {
395
+ return edges.map(([node1Identity, node2Identity, edge]) => this.addEdge(node1Identity, node2Identity, edge));
396
+ }
397
+ }
398
+
399
+ // src/graph/directedGraph.ts
400
+ class DirectedGraph extends Graph {
401
+ hasCycle = false;
402
+ isAcyclic() {
403
+ if (this.hasCycle !== undefined) {
404
+ return !this.hasCycle;
405
+ }
406
+ const nodeIndices = Array.from(this.nodes.keys());
407
+ const nodeInDegrees = new Map(Array.from(this.nodes.keys()).map((n) => [n, this.indegreeOfNode(n)]));
408
+ const toSearch = Array.from(nodeInDegrees).filter((pair) => pair[1] === 0);
409
+ let visitedNodes = 0;
410
+ while (toSearch.length > 0) {
411
+ const cur = toSearch.pop();
412
+ if (cur === undefined) {
413
+ continue;
414
+ }
415
+ const nodeIndex = nodeIndices.indexOf(cur[0]);
416
+ this.adjacency[nodeIndex].forEach((hasAdj, index) => {
417
+ if (hasAdj !== null) {
418
+ const currentInDegree = nodeInDegrees.get(nodeIndices[index]);
419
+ if (currentInDegree !== undefined) {
420
+ nodeInDegrees.set(nodeIndices[index], currentInDegree - 1);
421
+ if (currentInDegree - 1 === 0) {
422
+ toSearch.push([nodeIndices[index], currentInDegree - 1]);
423
+ }
424
+ }
425
+ }
426
+ });
427
+ visitedNodes++;
428
+ }
429
+ this.hasCycle = !(visitedNodes === this.nodes.size);
430
+ return visitedNodes === this.nodes.size;
431
+ }
432
+ indegreeOfNode(nodeID) {
433
+ const nodeIdentities = Array.from(this.nodes.keys());
434
+ const indexOfNode = nodeIdentities.indexOf(nodeID);
435
+ if (indexOfNode === -1) {
436
+ throw new NodeDoesntExistError(nodeID);
437
+ }
438
+ return this.adjacency.reduce((carry, row) => {
439
+ return carry + (row[indexOfNode] == null ? 0 : 1);
440
+ }, 0);
441
+ }
442
+ addEdge(sourceNodeIdentity, targetNodeIdentity, edge, skipUpdatingCyclicality = false) {
443
+ if (edge === undefined) {
444
+ edge = true;
445
+ }
446
+ if (this.hasCycle === false && !skipUpdatingCyclicality) {
447
+ this.hasCycle = this.wouldAddingEdgeCreateCycle(sourceNodeIdentity, targetNodeIdentity);
448
+ } else if (skipUpdatingCyclicality) {
449
+ this.hasCycle = undefined;
450
+ }
451
+ return super.addEdge(sourceNodeIdentity, targetNodeIdentity, edge);
452
+ }
453
+ canReachFrom(startNode, endNode) {
454
+ const nodeIdentities = Array.from(this.nodes.keys());
455
+ const startNodeIndex = nodeIdentities.indexOf(startNode);
456
+ const endNodeIndex = nodeIdentities.indexOf(endNode);
457
+ if (this.adjacency[startNodeIndex][endNodeIndex] != null) {
458
+ return true;
459
+ }
460
+ return this.adjacency[startNodeIndex].reduce((carry, edge, index) => {
461
+ if (carry || edge === null) {
462
+ return carry;
463
+ }
464
+ return this.canReachFrom(nodeIdentities[index], endNode);
465
+ }, false);
466
+ }
467
+ wouldAddingEdgeCreateCycle(sourceNodeIdentity, targetNodeIdentity) {
468
+ return this.hasCycle || sourceNodeIdentity === targetNodeIdentity || this.canReachFrom(targetNodeIdentity, sourceNodeIdentity);
469
+ }
470
+ getSubGraphStartingFrom(startNodeIdentity) {
471
+ const nodeIndices = Array.from(this.nodes.keys());
472
+ const initalNode = this.nodes.get(startNodeIdentity);
473
+ if (initalNode == null) {
474
+ throw new NodeDoesntExistError(startNodeIdentity);
475
+ }
476
+ const recur = (startNodeIdentity2, nodesToInclude) => {
477
+ let toReturn = [...nodesToInclude];
478
+ const nodeIndex = nodeIndices.indexOf(startNodeIdentity2);
479
+ this.adjacency[nodeIndex].forEach((hasAdj, index) => {
480
+ if (hasAdj !== null && nodesToInclude.find((n) => this.nodeIdentity(n) === nodeIndices[index]) == null) {
481
+ const newNode = this.nodes.get(nodeIndices[index]);
482
+ if (newNode != null) {
483
+ toReturn = [...recur(nodeIndices[index], toReturn), newNode];
484
+ }
485
+ }
486
+ });
487
+ return toReturn;
488
+ };
489
+ const newGraph = new DirectedGraph(this.nodeIdentity, this.edgeIdentity);
490
+ const nodeList = recur(startNodeIdentity, [initalNode]);
491
+ const includeIdents = nodeList.map((t) => this.nodeIdentity(t));
492
+ Array.from(this.nodes.values()).forEach((n) => {
493
+ if (includeIdents.includes(this.nodeIdentity(n))) {
494
+ newGraph.insert(n);
495
+ }
496
+ });
497
+ newGraph.adjacency = this.subAdj(nodeList);
498
+ return newGraph;
499
+ }
500
+ subAdj(include) {
501
+ const includeIdents = include.map((t) => this.nodeIdentity(t));
502
+ const nodeIndices = Array.from(this.nodes.keys());
503
+ return this.adjacency.reduce((carry, cur, index) => {
504
+ if (includeIdents.includes(nodeIndices[index])) {
505
+ return [...carry, cur.filter((_, index2) => includeIdents.includes(nodeIndices[index2]))];
506
+ } else {
507
+ return carry;
508
+ }
509
+ }, []);
510
+ }
511
+ getEdges() {
512
+ return super.getEdges();
513
+ }
514
+ removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity) {
515
+ super.removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity);
516
+ this.hasCycle = undefined;
517
+ }
518
+ remove(nodeIdentity) {
519
+ super.remove(nodeIdentity);
520
+ this.hasCycle = undefined;
521
+ }
522
+ addEdges(edges) {
523
+ return super.addEdges(edges);
524
+ }
525
+ }
526
+
527
+ // src/graph/directedAcyclicGraph.ts
528
+ class DirectedAcyclicGraph extends DirectedGraph {
529
+ _topologicallySortedNodes;
530
+ static fromDirectedGraph(graph) {
531
+ if (!graph.isAcyclic()) {
532
+ throw new CycleError("Can't convert that graph to a DAG because it contains a cycle");
533
+ }
534
+ const toRet = new DirectedAcyclicGraph(graph.nodeIdentity, graph.edgeIdentity);
535
+ toRet.nodes = graph.nodes;
536
+ toRet.adjacency = graph.adjacency;
537
+ return toRet;
538
+ }
539
+ addEdge(sourceNodeIdentity, targetNodeIdentity, edge) {
540
+ if (edge === undefined) {
541
+ edge = true;
542
+ }
543
+ if (this.wouldAddingEdgeCreateCycle(sourceNodeIdentity, targetNodeIdentity)) {
544
+ throw new CycleError(`Can't add edge from ${String(sourceNodeIdentity)} to ${String(targetNodeIdentity)} it would create a cycle`);
545
+ }
546
+ this._topologicallySortedNodes = undefined;
547
+ return super.addEdge(sourceNodeIdentity, targetNodeIdentity, edge, true);
548
+ }
549
+ insert(node) {
550
+ if (this._topologicallySortedNodes !== undefined) {
551
+ this._topologicallySortedNodes = [node, ...this._topologicallySortedNodes];
552
+ }
553
+ return super.insert(node);
554
+ }
555
+ topologicallySortedNodes() {
556
+ if (this._topologicallySortedNodes !== undefined) {
557
+ return this._topologicallySortedNodes;
558
+ }
559
+ const nodeIndices = Array.from(this.nodes.keys());
560
+ const nodeInDegrees = new Map(Array.from(this.nodes.keys()).map((n) => [n, this.indegreeOfNode(n)]));
561
+ const adjCopy = this.adjacency.map((a) => [...a]);
562
+ const toSearch = Array.from(nodeInDegrees).filter((pair) => pair[1] === 0);
563
+ if (toSearch.length === this.nodes.size) {
564
+ const arrayOfNodes = Array.from(this.nodes.values());
565
+ this._topologicallySortedNodes = arrayOfNodes;
566
+ return arrayOfNodes;
567
+ }
568
+ const toReturn = [];
569
+ while (toSearch.length > 0) {
570
+ const n = toSearch.pop();
571
+ if (n === undefined) {
572
+ throw new Error("Unexpected empty array");
573
+ }
574
+ const curNode = this.nodes.get(n[0]);
575
+ if (curNode == null) {
576
+ throw new Error("This should never happen");
577
+ }
578
+ toReturn.push(curNode);
579
+ adjCopy[nodeIndices.indexOf(n[0])]?.forEach((edge, index) => {
580
+ if (edge !== null) {
581
+ adjCopy[nodeIndices.indexOf(n[0])][index] = null;
582
+ const target = nodeInDegrees.get(nodeIndices[index]);
583
+ if (target !== undefined) {
584
+ nodeInDegrees.set(nodeIndices[index], target - 1);
585
+ if (target - 1 === 0) {
586
+ toSearch.push([nodeIndices[index], 0]);
587
+ }
588
+ } else {
589
+ throw new Error("This should never happen");
590
+ }
591
+ }
592
+ });
593
+ }
594
+ this._topologicallySortedNodes = toReturn;
595
+ return toReturn;
596
+ }
597
+ getSubGraphStartingFrom(startNodeIdentity) {
598
+ return DirectedAcyclicGraph.fromDirectedGraph(super.getSubGraphStartingFrom(startNodeIdentity));
599
+ }
600
+ removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity) {
601
+ super.removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity);
602
+ this._topologicallySortedNodes = undefined;
603
+ }
604
+ remove(nodeIdentity) {
605
+ super.remove(nodeIdentity);
606
+ this._topologicallySortedNodes = undefined;
607
+ }
608
+ }
609
+ // src/json-schema/FromSchema.ts
610
+ var FromSchemaDefaultOptions = {
611
+ parseNotKeyword: true,
612
+ parseIfThenElseKeywords: false,
613
+ keepDefaultedPropertiesOptional: true,
614
+ references: false,
615
+ deserialize: false
616
+ };
617
+ // src/json-schema/SchemaUtils.ts
618
+ function areFormatStringsCompatible(sourceFormat, targetFormat) {
619
+ const formatPattern = /^\w+(:\w+)?$/;
620
+ if (!formatPattern.test(sourceFormat) || !formatPattern.test(targetFormat)) {
621
+ return "incompatible";
622
+ }
623
+ const [sourceName, sourceNarrow] = sourceFormat.split(":");
624
+ const [targetName, targetNarrow] = targetFormat.split(":");
625
+ if (sourceName !== targetName) {
626
+ return "incompatible";
627
+ }
628
+ if (!sourceNarrow && !targetNarrow) {
629
+ return "static";
630
+ }
631
+ if (sourceNarrow && !targetNarrow) {
632
+ return "static";
633
+ }
634
+ if (!sourceNarrow && targetNarrow) {
635
+ return "runtime";
636
+ }
637
+ if (sourceNarrow === targetNarrow) {
638
+ return "static";
639
+ }
640
+ return "incompatible";
641
+ }
642
+ function isTypeStaticallyCompatible(sourceType, targetType) {
643
+ if (!targetType) {
644
+ return true;
645
+ }
646
+ if (!sourceType) {
647
+ return false;
648
+ }
649
+ const sourceTypes = Array.isArray(sourceType) ? sourceType : [sourceType];
650
+ const targetTypes = Array.isArray(targetType) ? targetType : [targetType];
651
+ return sourceTypes.some((st) => targetTypes.includes(st));
652
+ }
653
+ function mergeAllOfSchemas(schemas) {
654
+ if (schemas.length === 0)
655
+ return null;
656
+ if (schemas.length === 1)
657
+ return schemas[0];
658
+ let merged = {};
659
+ for (const schema of schemas) {
660
+ if (typeof schema === "boolean") {
661
+ if (schema === false)
662
+ return false;
663
+ continue;
664
+ }
665
+ const schemaObj = schema;
666
+ if (schemaObj.type !== undefined) {
667
+ if (merged.type === undefined) {
668
+ merged.type = schemaObj.type;
669
+ } else if (merged.type !== schemaObj.type) {
670
+ const mergedTypes = Array.isArray(merged.type) ? merged.type : [merged.type];
671
+ const schemaTypes = Array.isArray(schemaObj.type) ? schemaObj.type : [schemaObj.type];
672
+ const commonTypes = mergedTypes.filter((t) => schemaTypes.includes(t));
673
+ if (commonTypes.length === 0) {
674
+ return false;
675
+ }
676
+ merged.type = commonTypes.length === 1 ? commonTypes[0] : commonTypes;
677
+ }
678
+ }
679
+ const schemaFormat = schemaObj.format;
680
+ const mergedFormat = merged.format;
681
+ if (schemaFormat) {
682
+ if (!mergedFormat) {
683
+ merged.format = schemaFormat;
684
+ } else {
685
+ const formatCompat = areFormatStringsCompatible(mergedFormat, schemaFormat);
686
+ if (formatCompat === "incompatible") {
687
+ return false;
688
+ }
689
+ const mergedHasNarrow = mergedFormat.includes(":");
690
+ const schemaHasNarrow = schemaFormat.includes(":");
691
+ if (schemaHasNarrow && !mergedHasNarrow) {
692
+ merged.format = schemaFormat;
693
+ } else if (!schemaHasNarrow && mergedHasNarrow) {} else if (mergedFormat !== schemaFormat) {
694
+ return false;
695
+ }
696
+ }
697
+ }
698
+ if (schemaObj.properties && typeof schemaObj.properties === "object") {
699
+ if (!merged.properties) {
700
+ merged.properties = {};
701
+ }
702
+ const mergedProps = merged.properties;
703
+ const schemaProps = schemaObj.properties;
704
+ for (const [key, value] of Object.entries(schemaProps)) {
705
+ if (mergedProps[key]) {
706
+ const nestedMerged = mergeAllOfSchemas([mergedProps[key], value]);
707
+ if (nestedMerged === null || nestedMerged === false) {
708
+ return false;
709
+ }
710
+ mergedProps[key] = nestedMerged;
711
+ } else {
712
+ mergedProps[key] = value;
713
+ }
714
+ }
715
+ }
716
+ if (schemaObj.required && Array.isArray(schemaObj.required)) {
717
+ if (!merged.required) {
718
+ merged.required = [];
719
+ }
720
+ const mergedRequired = merged.required;
721
+ const schemaRequired = schemaObj.required;
722
+ merged.required = mergedRequired.filter((r) => schemaRequired.includes(r));
723
+ }
724
+ if (schemaObj.additionalProperties !== undefined) {
725
+ if (merged.additionalProperties === undefined) {
726
+ merged.additionalProperties = schemaObj.additionalProperties;
727
+ } else if (merged.additionalProperties === true && schemaObj.additionalProperties === false) {
728
+ merged.additionalProperties = false;
729
+ }
730
+ }
731
+ if (schemaObj.items !== undefined) {
732
+ if (merged.items === undefined) {
733
+ merged.items = schemaObj.items;
734
+ } else {
735
+ const mergedItems = mergeAllOfSchemas([
736
+ merged.items,
737
+ schemaObj.items
738
+ ]);
739
+ if (mergedItems === null || mergedItems === false) {
740
+ return false;
741
+ }
742
+ merged.items = mergedItems;
743
+ }
744
+ }
745
+ }
746
+ return merged;
747
+ }
748
+ function isCompatibleWithUnion(sourceSchema, unionSchemas) {
749
+ let hasStatic = false;
750
+ let hasRuntime = false;
751
+ for (const unionSchema of unionSchemas) {
752
+ const compatibility = areSemanticallyCompatible(sourceSchema, unionSchema);
753
+ if (compatibility === "static") {
754
+ hasStatic = true;
755
+ } else if (compatibility === "runtime") {
756
+ hasRuntime = true;
757
+ }
758
+ }
759
+ if (hasStatic)
760
+ return "static";
761
+ if (hasRuntime)
762
+ return "runtime";
763
+ return "incompatible";
764
+ }
765
+ function areSemanticallyCompatible(sourceSchema, targetSchema) {
766
+ if (sourceSchema === undefined || targetSchema === undefined) {
767
+ return "incompatible";
768
+ }
769
+ if (typeof targetSchema === "boolean") {
770
+ if (targetSchema === false)
771
+ return "incompatible";
772
+ if (targetSchema === true)
773
+ return "static";
774
+ return "incompatible";
775
+ }
776
+ if (typeof sourceSchema === "boolean") {
777
+ if (sourceSchema === false)
778
+ return "incompatible";
779
+ if (sourceSchema === true)
780
+ return "runtime";
781
+ }
782
+ if (sourceSchema.allOf && Array.isArray(sourceSchema.allOf)) {
783
+ const mergedSchema = mergeAllOfSchemas(sourceSchema.allOf);
784
+ if (mergedSchema === null || mergedSchema === false) {
785
+ return "incompatible";
786
+ }
787
+ return areSemanticallyCompatible(mergedSchema, targetSchema);
788
+ }
789
+ const sourceType = sourceSchema.type;
790
+ const targetType = targetSchema.type;
791
+ if (sourceSchema.oneOf && Array.isArray(sourceSchema.oneOf)) {
792
+ let hasStatic = false;
793
+ let hasRuntime = false;
794
+ for (const sourceOption of sourceSchema.oneOf) {
795
+ const compatibility = areSemanticallyCompatible(sourceOption, targetSchema);
796
+ if (compatibility === "static") {
797
+ hasStatic = true;
798
+ } else if (compatibility === "runtime") {
799
+ hasRuntime = true;
800
+ }
801
+ }
802
+ if (hasRuntime)
803
+ return "runtime";
804
+ if (hasStatic)
805
+ return "static";
806
+ return "incompatible";
807
+ }
808
+ if (sourceSchema.anyOf && Array.isArray(sourceSchema.anyOf)) {
809
+ let hasStatic = false;
810
+ let hasRuntime = false;
811
+ for (const sourceOption of sourceSchema.anyOf) {
812
+ const compatibility = areSemanticallyCompatible(sourceOption, targetSchema);
813
+ if (compatibility === "static") {
814
+ hasStatic = true;
815
+ } else if (compatibility === "runtime") {
816
+ hasRuntime = true;
817
+ }
818
+ }
819
+ if (hasRuntime)
820
+ return "runtime";
821
+ if (hasStatic)
822
+ return "static";
823
+ return "incompatible";
824
+ }
825
+ if (targetSchema.oneOf && Array.isArray(targetSchema.oneOf)) {
826
+ return isCompatibleWithUnion(sourceSchema, targetSchema.oneOf);
827
+ }
828
+ if (targetSchema.anyOf && Array.isArray(targetSchema.anyOf)) {
829
+ return isCompatibleWithUnion(sourceSchema, targetSchema.anyOf);
830
+ }
831
+ if (targetSchema.allOf && Array.isArray(targetSchema.allOf)) {
832
+ let hasStatic = false;
833
+ let hasRuntime = false;
834
+ for (const allOfSchema of targetSchema.allOf) {
835
+ const compatibility = areSemanticallyCompatible(sourceSchema, allOfSchema);
836
+ if (compatibility === "incompatible") {
837
+ return "incompatible";
838
+ } else if (compatibility === "static") {
839
+ hasStatic = true;
840
+ } else if (compatibility === "runtime") {
841
+ hasRuntime = true;
842
+ }
843
+ }
844
+ if (hasRuntime)
845
+ return "runtime";
846
+ if (hasStatic)
847
+ return "static";
848
+ return "incompatible";
849
+ }
850
+ if (sourceType === "object" && targetType === "object") {
851
+ const sourceProperties = sourceSchema.properties;
852
+ const targetProperties = targetSchema.properties;
853
+ if (!targetProperties) {
854
+ return "static";
855
+ }
856
+ if (!sourceProperties) {
857
+ if (targetSchema.additionalProperties === false) {
858
+ return "incompatible";
859
+ }
860
+ return "static";
861
+ }
862
+ const targetRequired = targetSchema.required || [];
863
+ let hasStatic = true;
864
+ let hasRuntime = false;
865
+ for (const propName of targetRequired) {
866
+ const targetProp = targetProperties?.[propName];
867
+ const sourceProp = sourceProperties?.[propName];
868
+ if (!sourceProp) {
869
+ return "incompatible";
870
+ }
871
+ if (targetProp) {
872
+ const propCompatibility = areSemanticallyCompatible(sourceProp, targetProp);
873
+ if (propCompatibility === "incompatible") {
874
+ return "incompatible";
875
+ } else if (propCompatibility === "runtime") {
876
+ hasRuntime = true;
877
+ hasStatic = false;
878
+ }
879
+ }
880
+ }
881
+ if (targetSchema.additionalProperties === false) {
882
+ const sourcePropNames = Object.keys(sourceProperties);
883
+ const targetPropNames = Object.keys(targetProperties);
884
+ const extraProps = sourcePropNames.filter((name) => !targetPropNames.includes(name));
885
+ if (extraProps.length > 0) {
886
+ return "incompatible";
887
+ }
888
+ }
889
+ if (hasRuntime)
890
+ return "runtime";
891
+ return "static";
892
+ }
893
+ if (sourceType === "array" && targetType === "array") {
894
+ const sourceFormat2 = sourceSchema?.format;
895
+ const targetFormat2 = targetSchema?.format;
896
+ let formatCompatibility = null;
897
+ if (sourceFormat2 && targetFormat2) {
898
+ formatCompatibility = areFormatStringsCompatible(sourceFormat2, targetFormat2);
899
+ if (formatCompatibility === "incompatible") {
900
+ return "incompatible";
901
+ }
902
+ }
903
+ if (sourceFormat2 && !targetFormat2) {
904
+ return "static";
905
+ }
906
+ if (!sourceFormat2 && targetFormat2) {
907
+ return "incompatible";
908
+ }
909
+ const sourceItems = sourceSchema.items;
910
+ const targetItems = targetSchema.items;
911
+ if (sourceItems && typeof sourceItems === "object" && !Array.isArray(sourceItems) && targetItems && typeof targetItems === "object" && !Array.isArray(targetItems)) {
912
+ const itemsCompatibility = areSemanticallyCompatible(sourceItems, targetItems);
913
+ if (formatCompatibility === "runtime") {
914
+ return "runtime";
915
+ }
916
+ return itemsCompatibility;
917
+ }
918
+ if (!targetItems) {
919
+ return "static";
920
+ }
921
+ if (!sourceItems) {
922
+ return "incompatible";
923
+ }
924
+ if (Array.isArray(targetItems)) {
925
+ return isCompatibleWithUnion(sourceItems, targetItems);
926
+ }
927
+ return "static";
928
+ }
929
+ if (!sourceType) {
930
+ const targetFormat2 = targetSchema?.format;
931
+ if (targetFormat2) {
932
+ return "runtime";
933
+ }
934
+ return "static";
935
+ }
936
+ if (!targetType) {
937
+ const targetFormat2 = targetSchema?.format;
938
+ if (targetFormat2) {
939
+ const sourceFormat2 = sourceSchema?.format;
940
+ if (!sourceFormat2) {
941
+ return "incompatible";
942
+ }
943
+ return areFormatStringsCompatible(sourceFormat2, targetFormat2);
944
+ }
945
+ return "static";
946
+ }
947
+ if (!isTypeStaticallyCompatible(sourceType, targetType)) {
948
+ return "incompatible";
949
+ }
950
+ const sourceFormat = sourceSchema?.format;
951
+ const targetFormat = targetSchema?.format;
952
+ if (sourceFormat && targetFormat) {
953
+ return areFormatStringsCompatible(sourceFormat, targetFormat);
954
+ }
955
+ if (sourceFormat && !targetFormat) {
956
+ return "static";
957
+ }
958
+ if (!sourceFormat && targetFormat) {
959
+ return "incompatible";
960
+ }
961
+ return "static";
962
+ }
963
+ function areObjectSchemasSemanticallyCompatible(sourceSchema, targetSchema) {
964
+ return areSemanticallyCompatible(sourceSchema, targetSchema);
965
+ }
966
+ // src/json-schema/SchemaValidation.ts
967
+ import { compileSchema } from "json-schema-library";
968
+ // src/utilities/Misc.ts
969
+ function forceArray(input) {
970
+ if (Array.isArray(input))
971
+ return input;
972
+ return [input];
973
+ }
974
+ async function sleep(ms) {
975
+ return new Promise((resolve) => setTimeout(resolve, ms));
976
+ }
977
+ function collectPropertyValues(input) {
978
+ const output = {};
979
+ (input || []).forEach((item) => {
980
+ Object.keys(item).forEach((key) => {
981
+ const value = item[key];
982
+ if (output[key]) {
983
+ output[key].push(value);
984
+ } else {
985
+ output[key] = [value];
986
+ }
987
+ });
988
+ });
989
+ return output;
990
+ }
991
+ function toSQLiteTimestamp(date) {
992
+ if (!date)
993
+ return null;
994
+ const pad = (number) => number < 10 ? "0" + number : number;
995
+ const year = date.getUTCFullYear();
996
+ const month = pad(date.getUTCMonth() + 1);
997
+ const day = pad(date.getUTCDate());
998
+ const hours = pad(date.getUTCHours());
999
+ const minutes = pad(date.getUTCMinutes());
1000
+ const seconds = pad(date.getUTCSeconds());
1001
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
1002
+ }
1003
+ function deepEqual(a, b) {
1004
+ if (a === b) {
1005
+ return true;
1006
+ }
1007
+ if (typeof a !== "object" || typeof b !== "object" || a == null || b == null) {
1008
+ return false;
1009
+ }
1010
+ const keysA = Object.keys(a);
1011
+ const keysB = Object.keys(b);
1012
+ if (keysA.length !== keysB.length) {
1013
+ return false;
1014
+ }
1015
+ for (const key of keysA) {
1016
+ if (!keysB.includes(key)) {
1017
+ return false;
1018
+ }
1019
+ if (!deepEqual(a[key], b[key])) {
1020
+ return false;
1021
+ }
1022
+ }
1023
+ return true;
1024
+ }
1025
+ function sortObject(obj) {
1026
+ return Object.keys(obj).sort().reduce((result, key) => {
1027
+ result[key] = obj[key];
1028
+ return result;
1029
+ }, {});
1030
+ }
1031
+ function serialize(obj) {
1032
+ const sortedObj = sortObject(obj);
1033
+ return JSON.stringify(sortedObj);
1034
+ }
1035
+ // src/utilities/objectOfArraysAsArrayOfObjects.ts
1036
+ function objectOfArraysAsArrayOfObjects(data) {
1037
+ const keys = Object.keys(data);
1038
+ const length = data[keys[0]].length;
1039
+ for (const key of keys) {
1040
+ if (data[key].length !== length) {
1041
+ console.error("All arrays must have the same length", key, data[key].length, length, data);
1042
+ throw new Error("All arrays must have the same length");
1043
+ }
1044
+ }
1045
+ const indexSymbol = Symbol("index");
1046
+ function createRowProxy(index) {
1047
+ let currentIndex = index;
1048
+ return new Proxy({}, {
1049
+ get(_target, prop, receiver) {
1050
+ if (currentIndex < 0 || currentIndex >= data[keys[0]].length) {
1051
+ return;
1052
+ }
1053
+ if (typeof prop === "string" && keys.includes(prop)) {
1054
+ return data[prop][currentIndex];
1055
+ }
1056
+ if (prop === indexSymbol) {
1057
+ return currentIndex;
1058
+ }
1059
+ return Reflect.get(_target, prop, receiver);
1060
+ },
1061
+ set(_target, prop, value, receiver) {
1062
+ if (currentIndex < 0 || currentIndex >= data[keys[0]].length) {
1063
+ return false;
1064
+ }
1065
+ if (typeof prop === "string" && keys.includes(prop)) {
1066
+ data[prop][currentIndex] = value;
1067
+ return true;
1068
+ }
1069
+ if (prop === indexSymbol) {
1070
+ currentIndex = value;
1071
+ return true;
1072
+ }
1073
+ return Reflect.set(_target, prop, value, receiver);
1074
+ },
1075
+ ownKeys(_target) {
1076
+ return keys;
1077
+ },
1078
+ getOwnPropertyDescriptor(_target, prop) {
1079
+ if (typeof prop === "string" && keys.includes(prop)) {
1080
+ return { enumerable: true, configurable: true };
1081
+ }
1082
+ return;
1083
+ }
1084
+ });
1085
+ }
1086
+ function createCursor() {
1087
+ let currentIndex = 0;
1088
+ const cursor = createRowProxy(0);
1089
+ const obj = {
1090
+ get length() {
1091
+ return data[keys[0]].length;
1092
+ },
1093
+ next() {
1094
+ if (currentIndex < length) {
1095
+ cursor[indexSymbol] = currentIndex;
1096
+ currentIndex++;
1097
+ return { done: false, value: cursor };
1098
+ } else {
1099
+ return { done: true, value: undefined };
1100
+ }
1101
+ },
1102
+ [Symbol.iterator]() {
1103
+ currentIndex = 0;
1104
+ cursor[indexSymbol] = currentIndex;
1105
+ return obj;
1106
+ }
1107
+ };
1108
+ return obj;
1109
+ }
1110
+ function shallowEqual(index, row) {
1111
+ for (const key of keys) {
1112
+ if (data[key][index] !== row[key])
1113
+ return false;
1114
+ }
1115
+ return true;
1116
+ }
1117
+ return new Proxy([], {
1118
+ get(target, prop, receiver) {
1119
+ if (prop === "length") {
1120
+ return data[keys[0]].length;
1121
+ }
1122
+ if (prop === "cursor") {
1123
+ return function() {
1124
+ return createCursor();
1125
+ };
1126
+ }
1127
+ if (prop === "reverse") {
1128
+ return function() {
1129
+ for (const key of keys) {
1130
+ data[key].reverse();
1131
+ }
1132
+ return receiver;
1133
+ };
1134
+ }
1135
+ if (prop === "push") {
1136
+ return function(...args) {
1137
+ for (const item of args) {
1138
+ for (const key of keys) {
1139
+ data[key].push(item[key]);
1140
+ }
1141
+ }
1142
+ return data[keys[0]].length;
1143
+ };
1144
+ }
1145
+ if (prop === "pop") {
1146
+ return function() {
1147
+ const len = data[keys[0]].length;
1148
+ if (len === 0)
1149
+ return;
1150
+ const poppedRow = {};
1151
+ for (const key of keys) {
1152
+ poppedRow[key] = data[key].pop();
1153
+ }
1154
+ return poppedRow;
1155
+ };
1156
+ }
1157
+ if (prop === "unshift") {
1158
+ return function(...args) {
1159
+ for (let i = args.length - 1;i >= 0; i--) {
1160
+ const item = args[i];
1161
+ for (const key of keys) {
1162
+ data[key].unshift(item[key]);
1163
+ }
1164
+ }
1165
+ return data[keys[0]].length;
1166
+ };
1167
+ }
1168
+ if (prop === "shift") {
1169
+ return function() {
1170
+ if (data[keys[0]].length === 0)
1171
+ return;
1172
+ const shiftedRow = {};
1173
+ for (const key of keys) {
1174
+ shiftedRow[key] = data[key].shift();
1175
+ }
1176
+ return shiftedRow;
1177
+ };
1178
+ }
1179
+ if (prop === "splice") {
1180
+ return function(start, deleteCount, ...items) {
1181
+ const len = data[keys[0]].length;
1182
+ if (start < 0) {
1183
+ start = len + start;
1184
+ if (start < 0)
1185
+ start = 0;
1186
+ }
1187
+ if (deleteCount === undefined) {
1188
+ deleteCount = len - start;
1189
+ }
1190
+ const removedByKey = {};
1191
+ for (const key of keys) {
1192
+ removedByKey[key] = data[key].splice(start, deleteCount, ...items.map((item) => item[key]));
1193
+ }
1194
+ const removed = [];
1195
+ for (let i = 0;i < deleteCount; i++) {
1196
+ const row = {};
1197
+ for (const key of keys) {
1198
+ row[key] = removedByKey[key][i];
1199
+ }
1200
+ removed.push(row);
1201
+ }
1202
+ return removed;
1203
+ };
1204
+ }
1205
+ if (prop === "sort") {
1206
+ return function(compareFn) {
1207
+ const rows = [...receiver];
1208
+ rows.sort(compareFn);
1209
+ for (const key of keys) {
1210
+ data[key] = rows.map((row) => row[key]);
1211
+ }
1212
+ return receiver;
1213
+ };
1214
+ }
1215
+ if (prop === "includes") {
1216
+ return function(searchElement, fromIndex) {
1217
+ const len = data[keys[0]].length;
1218
+ let start = fromIndex ?? 0;
1219
+ if (start < 0) {
1220
+ start = Math.max(0, len + start);
1221
+ }
1222
+ for (let i = start;i < len; i++) {
1223
+ if (shallowEqual(i, searchElement))
1224
+ return true;
1225
+ }
1226
+ return false;
1227
+ };
1228
+ }
1229
+ if (prop === "indexOf") {
1230
+ return function(searchElement, fromIndex) {
1231
+ const len = data[keys[0]].length;
1232
+ let start = fromIndex ?? 0;
1233
+ if (start < 0) {
1234
+ start = Math.max(0, len + start);
1235
+ }
1236
+ for (let i = start;i < len; i++) {
1237
+ if (shallowEqual(i, searchElement))
1238
+ return i;
1239
+ }
1240
+ return -1;
1241
+ };
1242
+ }
1243
+ if (prop === "lastIndexOf") {
1244
+ return function(searchElement, fromIndex) {
1245
+ const len = data[keys[0]].length;
1246
+ let start = fromIndex ?? len - 1;
1247
+ if (start < 0) {
1248
+ start = len + start;
1249
+ }
1250
+ for (let i = start;i >= 0; i--) {
1251
+ if (shallowEqual(i, searchElement))
1252
+ return i;
1253
+ }
1254
+ return -1;
1255
+ };
1256
+ }
1257
+ if (prop === "forEach") {
1258
+ return function(callback, thisArg) {
1259
+ return [...receiver].forEach(callback, thisArg);
1260
+ };
1261
+ }
1262
+ if (prop === "map") {
1263
+ return function(callback, thisArg) {
1264
+ return [...receiver].map(callback, thisArg);
1265
+ };
1266
+ }
1267
+ if (prop === "filter") {
1268
+ return function(callback, thisArg) {
1269
+ return [...receiver].filter(callback, thisArg);
1270
+ };
1271
+ }
1272
+ if (prop === "reduce") {
1273
+ return function(callback, initialValue) {
1274
+ return [...receiver].reduce(callback, initialValue);
1275
+ };
1276
+ }
1277
+ if (prop === "find") {
1278
+ return function(callback, thisArg) {
1279
+ return [...receiver].find(callback, thisArg);
1280
+ };
1281
+ }
1282
+ if (prop === "every") {
1283
+ return function(callback, thisArg) {
1284
+ return [...receiver].every(callback, thisArg);
1285
+ };
1286
+ }
1287
+ if (prop === "some") {
1288
+ return function(callback, thisArg) {
1289
+ return [...receiver].some(callback, thisArg);
1290
+ };
1291
+ }
1292
+ if (typeof prop === "string" && !isNaN(Number(prop))) {
1293
+ const index = Number(prop);
1294
+ if (index < 0 || index >= data[keys[0]].length) {
1295
+ return;
1296
+ }
1297
+ return createRowProxy(index);
1298
+ }
1299
+ if (prop === Symbol.iterator) {
1300
+ return function* () {
1301
+ for (let i = 0;i < data[keys[0]].length; i++) {
1302
+ yield createRowProxy(i);
1303
+ }
1304
+ };
1305
+ }
1306
+ return Reflect.get(target, prop, receiver);
1307
+ },
1308
+ set(target, prop, value, receiver) {
1309
+ if (typeof prop === "string" && !isNaN(Number(prop))) {
1310
+ const index = Number(prop);
1311
+ if (index === data[keys[0]].length) {
1312
+ for (const key of keys) {
1313
+ data[key].push(value[key]);
1314
+ }
1315
+ return true;
1316
+ } else if (index < data[keys[0]].length) {
1317
+ for (const key of keys) {
1318
+ if (value.hasOwnProperty(key)) {
1319
+ data[key][index] = value[key];
1320
+ }
1321
+ }
1322
+ return true;
1323
+ }
1324
+ }
1325
+ return Reflect.set(target, prop, value, receiver);
1326
+ },
1327
+ deleteProperty(target, prop) {
1328
+ if (typeof prop === "string" && !isNaN(Number(prop))) {
1329
+ const index = Number(prop);
1330
+ if (index >= 0 && index < data[keys[0]].length) {
1331
+ for (const key of keys) {
1332
+ data[key].splice(index, 1);
1333
+ }
1334
+ return true;
1335
+ }
1336
+ }
1337
+ return Reflect.deleteProperty(target, prop);
1338
+ }
1339
+ });
1340
+ }
1341
+ // src/worker/WorkerManager.ts
1342
+ class WorkerManager {
1343
+ workers = new Map;
1344
+ readyWorkers = new Map;
1345
+ registerWorker(name, worker) {
1346
+ if (this.workers.has(name))
1347
+ throw new Error(`Worker ${name} is already registered.`);
1348
+ this.workers.set(name, worker);
1349
+ this.workers.set(name, worker);
1350
+ worker.addEventListener("error", (event) => {
1351
+ console.error("Worker Error:", event.message, "at", event.filename, "line:", event.lineno);
1352
+ });
1353
+ worker.addEventListener("messageerror", (event) => {
1354
+ console.error("Worker message error:", event);
1355
+ });
1356
+ const readyPromise = new Promise((resolve) => {
1357
+ const handleReady = (event) => {
1358
+ if (event.data?.type === "ready") {
1359
+ worker.removeEventListener("message", handleReady);
1360
+ resolve();
1361
+ }
1362
+ };
1363
+ worker.addEventListener("message", handleReady);
1364
+ });
1365
+ this.readyWorkers.set(name, readyPromise);
1366
+ }
1367
+ getWorker(name) {
1368
+ const worker = this.workers.get(name);
1369
+ if (!worker)
1370
+ throw new Error(`Worker ${name} not found.`);
1371
+ return worker;
1372
+ }
1373
+ async callWorkerFunction(workerName, functionName, args, options) {
1374
+ const worker = this.workers.get(workerName);
1375
+ if (!worker)
1376
+ throw new Error(`Worker ${workerName} not found.`);
1377
+ await this.readyWorkers.get(workerName);
1378
+ return new Promise((resolve, reject) => {
1379
+ const requestId = crypto.randomUUID();
1380
+ const handleMessage = (event) => {
1381
+ const { id, type, data } = event.data;
1382
+ if (id !== requestId)
1383
+ return;
1384
+ if (type === "progress" && options?.onProgress) {
1385
+ options.onProgress(data.progress, data.message, data.details);
1386
+ } else if (type === "complete") {
1387
+ cleanup();
1388
+ resolve(data);
1389
+ } else if (type === "error") {
1390
+ cleanup();
1391
+ reject(new Error(data));
1392
+ }
1393
+ };
1394
+ const handleAbort = () => {
1395
+ worker.postMessage({ id: requestId, type: "abort" });
1396
+ };
1397
+ const cleanup = () => {
1398
+ worker.removeEventListener("message", handleMessage);
1399
+ options?.signal?.removeEventListener("abort", handleAbort);
1400
+ };
1401
+ worker.addEventListener("message", handleMessage);
1402
+ if (options?.signal) {
1403
+ options.signal.addEventListener("abort", handleAbort, { once: true });
1404
+ }
1405
+ worker.postMessage({ id: requestId, type: "call", functionName, args });
1406
+ });
1407
+ }
1408
+ }
1409
+ var WORKER_MANAGER = createServiceToken("worker.manager");
1410
+ globalServiceRegistry.register(WORKER_MANAGER, () => new WorkerManager, true);
1411
+ // src/worker/WorkerServer.ts
1412
+ import { parentPort } from "@workglow/util";
1413
+ function extractTransferables(obj) {
1414
+ const transferables = [];
1415
+ function findTransferables(value) {
1416
+ if (value instanceof Float32Array || value instanceof Int16Array) {
1417
+ transferables.push(value.buffer);
1418
+ } else if (value && typeof value === "object") {
1419
+ Object.values(value).forEach(findTransferables);
1420
+ }
1421
+ }
1422
+ findTransferables(obj);
1423
+ return transferables;
1424
+ }
1425
+
1426
+ class WorkerServer {
1427
+ constructor() {
1428
+ parentPort?.addEventListener("message", async (event) => {
1429
+ const msg = {
1430
+ type: event.type,
1431
+ data: event.data
1432
+ };
1433
+ await this.handleMessage(msg);
1434
+ });
1435
+ }
1436
+ functions = {};
1437
+ postResult = (id, result) => {
1438
+ const transferables = extractTransferables(result);
1439
+ postMessage({ id, type: "complete", data: result }, transferables);
1440
+ };
1441
+ postError = (id, errorMessage) => {
1442
+ postMessage({ id, type: "error", data: errorMessage });
1443
+ };
1444
+ requestControllers = new Map;
1445
+ registerFunction(name, fn) {
1446
+ this.functions[name] = fn;
1447
+ }
1448
+ async handleMessage(event) {
1449
+ const { id, type, functionName, args } = event.data;
1450
+ if (type === "abort") {
1451
+ return await this.handleAbort(id);
1452
+ }
1453
+ if (type === "call") {
1454
+ return await this.handleCall(id, functionName, args);
1455
+ }
1456
+ }
1457
+ async handleAbort(id) {
1458
+ if (this.requestControllers.has(id)) {
1459
+ this.requestControllers.get(id)?.abort();
1460
+ }
1461
+ }
1462
+ async handleCall(id, functionName, [input, model]) {
1463
+ if (!(functionName in this.functions)) {
1464
+ this.postError(id, `Function ${functionName} not found`);
1465
+ return;
1466
+ }
1467
+ try {
1468
+ const abortController = new AbortController;
1469
+ this.requestControllers.set(id, abortController);
1470
+ const fn = this.functions[functionName];
1471
+ const postProgress = (progress, message, details) => {
1472
+ postMessage({ id, type: "progress", data: { progress, message, details } });
1473
+ };
1474
+ const result = await fn(input, model, postProgress, abortController.signal);
1475
+ this.postResult(id, result);
1476
+ } catch (error) {
1477
+ this.postError(id, error.message);
1478
+ } finally {
1479
+ this.requestControllers.delete(id);
1480
+ }
1481
+ }
1482
+ }
1483
+ var WORKER_SERVER = createServiceToken("worker.server");
1484
+ globalServiceRegistry.register(WORKER_SERVER, () => new WorkerServer, true);
1485
+ // src/compress/compress.node.ts
1486
+ import { promisify } from "util";
1487
+ import zlib from "zlib";
1488
+ async function compress(input, algorithm = "gzip") {
1489
+ const compressFn = algorithm === "br" ? zlib.brotliCompress : zlib.gzip;
1490
+ const compressAsync = promisify(compressFn);
1491
+ const compressAsyncTyped = compressAsync;
1492
+ const sourceBuffer = Buffer.isBuffer(input) ? input : Buffer.from(input);
1493
+ const result = await compressAsyncTyped(sourceBuffer);
1494
+ return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
1495
+ }
1496
+ async function decompress(input, algorithm = "gzip") {
1497
+ const decompressFn = algorithm === "br" ? zlib.brotliDecompress : zlib.gunzip;
1498
+ const decompressAsync = promisify(decompressFn);
1499
+ const decompressAsyncTyped = decompressAsync;
1500
+ const sourceBuffer = Buffer.isBuffer(input) ? input : Buffer.from(input);
1501
+ const resultBuffer = await decompressAsyncTyped(sourceBuffer);
1502
+ return resultBuffer.toString();
1503
+ }
1504
+ // src/crypto/Crypto.bun.ts
1505
+ async function sha256(data) {
1506
+ return new Bun.CryptoHasher("sha256").update(data).digest("hex");
1507
+ }
1508
+ async function makeFingerprint(input) {
1509
+ const serializedObj = serialize(input);
1510
+ const hash = await sha256(serializedObj);
1511
+ return hash;
1512
+ }
1513
+ function uuid4() {
1514
+ return crypto.randomUUID();
1515
+ }
1516
+ // src/worker/Worker.bun.ts
1517
+ var Worker = globalThis.Worker;
1518
+ var parentPort2 = self;
1519
+ export {
1520
+ uuid4,
1521
+ toSQLiteTimestamp,
1522
+ sortObject,
1523
+ sleep,
1524
+ sha256,
1525
+ serialize,
1526
+ parentPort2 as parentPort,
1527
+ objectOfArraysAsArrayOfObjects,
1528
+ makeFingerprint,
1529
+ globalServiceRegistry,
1530
+ globalContainer,
1531
+ forceArray,
1532
+ deepEqual,
1533
+ decompress,
1534
+ createServiceToken,
1535
+ compress,
1536
+ compileSchema,
1537
+ collectPropertyValues,
1538
+ areSemanticallyCompatible,
1539
+ areObjectSchemasSemanticallyCompatible,
1540
+ WorkerServer,
1541
+ WorkerManager,
1542
+ Worker,
1543
+ WORKER_SERVER,
1544
+ WORKER_MANAGER,
1545
+ ServiceRegistry,
1546
+ NodeDoesntExistError,
1547
+ NodeAlreadyExistsError,
1548
+ Graph,
1549
+ FromSchemaDefaultOptions,
1550
+ EventEmitter,
1551
+ DirectedGraph,
1552
+ DirectedAcyclicGraph,
1553
+ CycleError,
1554
+ Container,
1555
+ BaseError
1556
+ };
1557
+
1558
+ //# debugId=367E927F3666D6AD64756E2164756E21