@cadenza.io/service 1.15.4 → 1.16.0
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/dist/index.js +576 -619
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +576 -622
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,28 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defProps = Object.defineProperties;
|
|
5
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
10
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
-
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
13
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
14
|
-
var __spreadValues = (a, b) => {
|
|
15
|
-
for (var prop in b || (b = {}))
|
|
16
|
-
if (__hasOwnProp.call(b, prop))
|
|
17
|
-
__defNormalProp(a, prop, b[prop]);
|
|
18
|
-
if (__getOwnPropSymbols)
|
|
19
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
20
|
-
if (__propIsEnum.call(b, prop))
|
|
21
|
-
__defNormalProp(a, prop, b[prop]);
|
|
22
|
-
}
|
|
23
|
-
return a;
|
|
24
|
-
};
|
|
25
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
26
8
|
var __export = (target, all) => {
|
|
27
9
|
for (var name in all)
|
|
28
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -44,41 +26,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
44
26
|
mod
|
|
45
27
|
));
|
|
46
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
47
|
-
var __async = (__this, __arguments, generator) => {
|
|
48
|
-
return new Promise((resolve, reject) => {
|
|
49
|
-
var fulfilled = (value) => {
|
|
50
|
-
try {
|
|
51
|
-
step(generator.next(value));
|
|
52
|
-
} catch (e) {
|
|
53
|
-
reject(e);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
var rejected = (value) => {
|
|
57
|
-
try {
|
|
58
|
-
step(generator.throw(value));
|
|
59
|
-
} catch (e) {
|
|
60
|
-
reject(e);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
64
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
var __await = function(promise, isYieldStar) {
|
|
68
|
-
this[0] = promise;
|
|
69
|
-
this[1] = isYieldStar;
|
|
70
|
-
};
|
|
71
|
-
var __asyncGenerator = (__this, __arguments, generator) => {
|
|
72
|
-
var resume = (k, v, yes, no) => {
|
|
73
|
-
try {
|
|
74
|
-
var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
|
|
75
|
-
Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
|
|
76
|
-
} catch (e) {
|
|
77
|
-
no(e);
|
|
78
|
-
}
|
|
79
|
-
}, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
|
|
80
|
-
return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
|
|
81
|
-
};
|
|
82
29
|
|
|
83
30
|
// src/index.ts
|
|
84
31
|
var index_exports = {};
|
|
@@ -150,7 +97,9 @@ var DeputyTask = class extends import_core.Task {
|
|
|
150
97
|
}
|
|
151
98
|
const processId = (0, import_uuid.v4)();
|
|
152
99
|
context.__metadata.__deputyExecId = processId;
|
|
153
|
-
emit("meta.deputy.delegation_requested",
|
|
100
|
+
emit("meta.deputy.delegation_requested", {
|
|
101
|
+
...context
|
|
102
|
+
});
|
|
154
103
|
CadenzaService.createEphemeralMetaTask(
|
|
155
104
|
`On progress deputy ${this.remoteRoutineName}`,
|
|
156
105
|
(ctx) => {
|
|
@@ -172,7 +121,7 @@ var DeputyTask = class extends import_core.Task {
|
|
|
172
121
|
`Resolve deputy ${this.remoteRoutineName}`,
|
|
173
122
|
(responseCtx) => {
|
|
174
123
|
console.log("resolve deputy", responseCtx);
|
|
175
|
-
if (responseCtx
|
|
124
|
+
if (responseCtx?.errored) {
|
|
176
125
|
reject(new Error(responseCtx.__error));
|
|
177
126
|
} else {
|
|
178
127
|
delete responseCtx.__isDeputy;
|
|
@@ -231,19 +180,20 @@ var DeputyTask = class extends import_core.Task {
|
|
|
231
180
|
* @note The resolution and progress are managed by ephemeral meta-tasks.
|
|
232
181
|
*/
|
|
233
182
|
execute(context, emit, progressCallback) {
|
|
234
|
-
var _a2;
|
|
235
183
|
const ctx = context.getContext();
|
|
236
184
|
const metadata = context.getMetadata();
|
|
237
|
-
const deputyContext =
|
|
185
|
+
const deputyContext = {
|
|
238
186
|
__localTaskName: this.name,
|
|
239
187
|
__remoteRoutineName: this.remoteRoutineName,
|
|
240
188
|
__serviceName: this.serviceName,
|
|
241
189
|
__localRoutineExecId: metadata.__routineExecId,
|
|
242
|
-
__executionTraceId:
|
|
243
|
-
__metadata:
|
|
190
|
+
__executionTraceId: metadata.__executionTraceId ?? null,
|
|
191
|
+
__metadata: {
|
|
192
|
+
...metadata,
|
|
244
193
|
__deputyTaskName: this.name
|
|
245
|
-
}
|
|
246
|
-
|
|
194
|
+
},
|
|
195
|
+
...ctx
|
|
196
|
+
};
|
|
247
197
|
console.log("DEPUTY CONTEXT", deputyContext);
|
|
248
198
|
return this.taskFunction(deputyContext, emit, progressCallback);
|
|
249
199
|
}
|
|
@@ -313,22 +263,26 @@ var DatabaseTask = class extends DeputyTask {
|
|
|
313
263
|
* @note The resolution and progress are managed by ephemeral meta-tasks.
|
|
314
264
|
*/
|
|
315
265
|
execute(context, emit, progressCallback) {
|
|
316
|
-
var _a2, _b2;
|
|
317
266
|
const ctx = context.getContext();
|
|
318
267
|
const metadata = context.getMetadata();
|
|
319
|
-
const dynamicQueryData =
|
|
268
|
+
const dynamicQueryData = ctx.queryData ?? {};
|
|
320
269
|
delete ctx.queryData;
|
|
321
270
|
const deputyContext = {
|
|
322
271
|
__localTaskName: this.name,
|
|
323
272
|
__remoteRoutineName: this.remoteRoutineName,
|
|
324
273
|
__serviceName: this.serviceName,
|
|
325
|
-
__executionTraceId:
|
|
326
|
-
__metadata:
|
|
274
|
+
__executionTraceId: metadata.__executionTraceId ?? null,
|
|
275
|
+
__metadata: {
|
|
276
|
+
...metadata,
|
|
327
277
|
__deputyTaskName: this.name
|
|
328
|
-
}
|
|
329
|
-
queryData:
|
|
330
|
-
|
|
331
|
-
|
|
278
|
+
},
|
|
279
|
+
queryData: {
|
|
280
|
+
...this.queryData,
|
|
281
|
+
data: {
|
|
282
|
+
...ctx.data
|
|
283
|
+
},
|
|
284
|
+
...dynamicQueryData
|
|
285
|
+
}
|
|
332
286
|
};
|
|
333
287
|
return this.taskFunction(deputyContext, emit, progressCallback);
|
|
334
288
|
}
|
|
@@ -348,7 +302,6 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
348
302
|
this.handleInstanceUpdateTask = CadenzaService.createMetaTask(
|
|
349
303
|
"Handle Instance Update",
|
|
350
304
|
(ctx, emit) => {
|
|
351
|
-
var _a2, _b2;
|
|
352
305
|
const { serviceInstance } = ctx;
|
|
353
306
|
const { uuid: uuid4, serviceName, address, port, exposed } = serviceInstance;
|
|
354
307
|
if (uuid4 === this.serviceInstanceId) return;
|
|
@@ -360,14 +313,14 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
360
313
|
Object.assign(existing, serviceInstance);
|
|
361
314
|
} else {
|
|
362
315
|
if (this.deputies.has(serviceName) || this.remoteSignals.has(serviceName) || this.remoteSignals.has("*") && this.serviceName !== serviceName) {
|
|
363
|
-
const clientCreated = instances
|
|
316
|
+
const clientCreated = instances?.some(
|
|
364
317
|
(i) => i.address === address && i.port === port && i.clientCreated && i.isActive
|
|
365
318
|
);
|
|
366
319
|
if (!clientCreated) {
|
|
367
320
|
try {
|
|
368
321
|
const communicationTypes = Array.from(
|
|
369
322
|
new Set(
|
|
370
|
-
|
|
323
|
+
this.deputies.get(serviceName)?.map((d) => d.communicationType) ?? []
|
|
371
324
|
)
|
|
372
325
|
);
|
|
373
326
|
if (!communicationTypes.includes("signal") && (this.remoteSignals.has(serviceName) || this.remoteSignals.has("*"))) {
|
|
@@ -448,13 +401,13 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
448
401
|
this.getRemoteSignalsTask = CadenzaService.createMetaTask(
|
|
449
402
|
"Get remote signals",
|
|
450
403
|
(ctx) => {
|
|
451
|
-
var _a2, _b2;
|
|
452
404
|
const { serviceName } = ctx;
|
|
453
|
-
let remoteSignals =
|
|
454
|
-
remoteSignals = remoteSignals.concat(
|
|
455
|
-
return
|
|
456
|
-
remoteSignals
|
|
457
|
-
|
|
405
|
+
let remoteSignals = this.remoteSignals.get(serviceName) ?? [];
|
|
406
|
+
remoteSignals = remoteSignals.concat(this.remoteSignals.get("*") ?? []);
|
|
407
|
+
return {
|
|
408
|
+
remoteSignals,
|
|
409
|
+
...ctx
|
|
410
|
+
};
|
|
458
411
|
},
|
|
459
412
|
"Gets remote signals"
|
|
460
413
|
).doOn(
|
|
@@ -466,7 +419,7 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
466
419
|
(ctx, emit) => {
|
|
467
420
|
const { serviceName, serviceAddress, servicePort } = ctx;
|
|
468
421
|
const serviceInstances = this.instances.get(serviceName);
|
|
469
|
-
const instances = serviceInstances
|
|
422
|
+
const instances = serviceInstances?.filter(
|
|
470
423
|
(i) => i.address === serviceAddress && i.port === servicePort
|
|
471
424
|
);
|
|
472
425
|
console.log(
|
|
@@ -475,7 +428,7 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
475
428
|
servicePort,
|
|
476
429
|
instances
|
|
477
430
|
);
|
|
478
|
-
for (const instance of instances
|
|
431
|
+
for (const instance of instances ?? []) {
|
|
479
432
|
instance.isActive = false;
|
|
480
433
|
instance.isNonResponsive = true;
|
|
481
434
|
instance.clientCreated = false;
|
|
@@ -506,10 +459,10 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
506
459
|
this.instances.get(serviceName)
|
|
507
460
|
);
|
|
508
461
|
const serviceInstances = this.instances.get(serviceName);
|
|
509
|
-
const instances = serviceInstances
|
|
462
|
+
const instances = serviceInstances?.filter(
|
|
510
463
|
(i) => i.address === serviceAddress && i.port === servicePort
|
|
511
464
|
);
|
|
512
|
-
for (const instance of instances
|
|
465
|
+
for (const instance of instances ?? []) {
|
|
513
466
|
emit("meta.service_registry.service_handshake", {
|
|
514
467
|
data: {
|
|
515
468
|
isActive: instance.isActive,
|
|
@@ -530,7 +483,7 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
530
483
|
const instanceId = ctx.__serviceInstanceId;
|
|
531
484
|
const serviceName = ctx.__serviceName;
|
|
532
485
|
const instances = this.instances.get(serviceName);
|
|
533
|
-
const instance = instances
|
|
486
|
+
const instance = instances?.find((i) => i.uuid === instanceId);
|
|
534
487
|
if (instance) {
|
|
535
488
|
instance.health = ctx.health;
|
|
536
489
|
instance.numberOfRunningGraphs = ctx.numberOfRunningGraphs;
|
|
@@ -579,7 +532,7 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
579
532
|
instance = instances.find((i) => i.uuid === __id);
|
|
580
533
|
if (instance) break;
|
|
581
534
|
}
|
|
582
|
-
return
|
|
535
|
+
return { ...context, __instance: instance };
|
|
583
536
|
},
|
|
584
537
|
"Gets instance by id."
|
|
585
538
|
);
|
|
@@ -591,7 +544,7 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
591
544
|
if (!instances) {
|
|
592
545
|
return false;
|
|
593
546
|
}
|
|
594
|
-
return
|
|
547
|
+
return { ...context, __instances: instances };
|
|
595
548
|
},
|
|
596
549
|
"Gets instances by name."
|
|
597
550
|
);
|
|
@@ -611,7 +564,8 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
611
564
|
).doOn("meta.deputy.created");
|
|
612
565
|
this.getAllInstances = CadenzaService.createMetaTask(
|
|
613
566
|
"Get all instances",
|
|
614
|
-
(context) =>
|
|
567
|
+
(context) => ({
|
|
568
|
+
...context,
|
|
615
569
|
__instances: Array.from(this.instances.values()).flat()
|
|
616
570
|
}),
|
|
617
571
|
"Gets all instances."
|
|
@@ -621,7 +575,7 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
621
575
|
function* (context) {
|
|
622
576
|
for (const instances of this.instances.values()) {
|
|
623
577
|
for (const instance of instances) {
|
|
624
|
-
yield
|
|
578
|
+
yield { ...context, __instance: instance };
|
|
625
579
|
}
|
|
626
580
|
}
|
|
627
581
|
}.bind(this),
|
|
@@ -639,11 +593,10 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
639
593
|
this.getBalancedInstance = CadenzaService.createMetaTask(
|
|
640
594
|
"Get balanced instance",
|
|
641
595
|
(context, emit) => {
|
|
642
|
-
var _a2;
|
|
643
596
|
const { __serviceName, __triedInstances, __retries } = context;
|
|
644
|
-
let retries = __retries
|
|
645
|
-
let triedInstances = __triedInstances
|
|
646
|
-
const instances =
|
|
597
|
+
let retries = __retries ?? 0;
|
|
598
|
+
let triedInstances = __triedInstances ?? [];
|
|
599
|
+
const instances = this.instances.get(__serviceName)?.filter((i) => i.isActive && !i.isNonResponsive && !i.isBlocked).sort((a, b) => a.numberOfRunningGraphs - b.numberOfRunningGraphs);
|
|
647
600
|
if (!instances || instances.length === 0 || retries > this.retryCount) {
|
|
648
601
|
context.errored = true;
|
|
649
602
|
context.__error = "No active instances";
|
|
@@ -654,7 +607,7 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
654
607
|
return context;
|
|
655
608
|
}
|
|
656
609
|
let instancesToTry = instances.filter(
|
|
657
|
-
(i) => !
|
|
610
|
+
(i) => !__triedInstances?.includes(i.uuid)
|
|
658
611
|
);
|
|
659
612
|
if (instancesToTry.length === 0) {
|
|
660
613
|
if (this.useSocket) {
|
|
@@ -698,7 +651,6 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
698
651
|
"meta.socket_client.signal_transmission_failed"
|
|
699
652
|
);
|
|
700
653
|
this.getStatusTask = CadenzaService.createMetaTask("Get status", (ctx) => {
|
|
701
|
-
var _a2, _b2, _c2, _d2;
|
|
702
654
|
if (!this.serviceName) {
|
|
703
655
|
return {
|
|
704
656
|
__status: "error",
|
|
@@ -713,13 +665,14 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
713
665
|
errored: true
|
|
714
666
|
};
|
|
715
667
|
}
|
|
716
|
-
const self =
|
|
717
|
-
return
|
|
668
|
+
const self = this.instances.get(this.serviceName)?.find((i) => i.uuid === this.serviceInstanceId);
|
|
669
|
+
return {
|
|
670
|
+
...ctx,
|
|
718
671
|
__status: "ok",
|
|
719
|
-
__numberOfRunningGraphs:
|
|
720
|
-
__health:
|
|
721
|
-
__active:
|
|
722
|
-
}
|
|
672
|
+
__numberOfRunningGraphs: self?.numberOfRunningGraphs ?? 0,
|
|
673
|
+
__health: self?.health ?? {},
|
|
674
|
+
__active: self?.isActive ?? false
|
|
675
|
+
};
|
|
723
676
|
}).doOn("meta.socket.status_check_requested");
|
|
724
677
|
this.insertServiceTask = CadenzaService.createCadenzaDBInsertTask(
|
|
725
678
|
"service",
|
|
@@ -846,12 +799,11 @@ var ServiceRegistry = class _ServiceRegistry {
|
|
|
846
799
|
CadenzaService.createMetaTask(
|
|
847
800
|
"Setup service",
|
|
848
801
|
(ctx) => {
|
|
849
|
-
var _a2, _b2;
|
|
850
802
|
const { serviceInstance, data, __useSocket, __retryCount } = ctx;
|
|
851
|
-
this.serviceInstanceId =
|
|
803
|
+
this.serviceInstanceId = serviceInstance?.uuid ?? data?.uuid;
|
|
852
804
|
this.instances.set(
|
|
853
|
-
|
|
854
|
-
[
|
|
805
|
+
data?.service_name ?? serviceInstance?.service_name,
|
|
806
|
+
[{ ...serviceInstance ?? data }]
|
|
855
807
|
);
|
|
856
808
|
this.useSocket = __useSocket;
|
|
857
809
|
this.retryCount = __retryCount;
|
|
@@ -920,7 +872,7 @@ var SignalTransmissionTask = class extends import_core2.Task {
|
|
|
920
872
|
CadenzaService.createEphemeralMetaTask(
|
|
921
873
|
`Resolve signal transmission for ${this.signalName}`,
|
|
922
874
|
(responseCtx) => {
|
|
923
|
-
if (responseCtx
|
|
875
|
+
if (responseCtx?.errored) {
|
|
924
876
|
reject(new Error(responseCtx.__error));
|
|
925
877
|
} else {
|
|
926
878
|
resolve(responseCtx);
|
|
@@ -980,18 +932,19 @@ var SignalTransmissionTask = class extends import_core2.Task {
|
|
|
980
932
|
* @note The resolution and progress are managed by ephemeral meta-tasks.
|
|
981
933
|
*/
|
|
982
934
|
execute(context, emit, progressCallback) {
|
|
983
|
-
var _a2;
|
|
984
935
|
const ctx = context.getContext();
|
|
985
936
|
const metadata = context.getMetadata();
|
|
986
|
-
const deputyContext =
|
|
937
|
+
const deputyContext = {
|
|
987
938
|
__localTaskName: this.name,
|
|
988
939
|
__serviceName: this.serviceName,
|
|
989
|
-
__executionTraceId:
|
|
990
|
-
__metadata:
|
|
940
|
+
__executionTraceId: metadata.__executionTraceId ?? null,
|
|
941
|
+
__metadata: {
|
|
942
|
+
...metadata,
|
|
991
943
|
__deputyTaskName: this.name
|
|
992
|
-
}
|
|
993
|
-
__signalName: this.signalName
|
|
994
|
-
|
|
944
|
+
},
|
|
945
|
+
__signalName: this.signalName,
|
|
946
|
+
...ctx
|
|
947
|
+
};
|
|
995
948
|
return this.taskFunction(deputyContext, emit, progressCallback);
|
|
996
949
|
}
|
|
997
950
|
};
|
|
@@ -1026,7 +979,6 @@ var RestController = class _RestController {
|
|
|
1026
979
|
CadenzaService.createMetaTask(
|
|
1027
980
|
"Setup Express app security",
|
|
1028
981
|
(ctx) => {
|
|
1029
|
-
var _a2, _b2;
|
|
1030
982
|
const app = (0, import_express.default)();
|
|
1031
983
|
app.use(import_body_parser.default.json());
|
|
1032
984
|
switch (ctx.__securityProfile) {
|
|
@@ -1038,7 +990,7 @@ var RestController = class _RestController {
|
|
|
1038
990
|
app.use((0, import_helmet.default)());
|
|
1039
991
|
app.use(
|
|
1040
992
|
(0, import_cors.default)({
|
|
1041
|
-
origin:
|
|
993
|
+
origin: process.env.CORS_ORIGIN ?? "*",
|
|
1042
994
|
methods: ["GET", "POST"]
|
|
1043
995
|
})
|
|
1044
996
|
);
|
|
@@ -1068,7 +1020,7 @@ var RestController = class _RestController {
|
|
|
1068
1020
|
}
|
|
1069
1021
|
app.use(
|
|
1070
1022
|
(0, import_cors.default)({
|
|
1071
|
-
origin:
|
|
1023
|
+
origin: process.env.CORS_ORIGIN ?? "*",
|
|
1072
1024
|
methods: ["GET", "POST"],
|
|
1073
1025
|
credentials: true
|
|
1074
1026
|
})
|
|
@@ -1091,7 +1043,7 @@ var RestController = class _RestController {
|
|
|
1091
1043
|
});
|
|
1092
1044
|
break;
|
|
1093
1045
|
}
|
|
1094
|
-
return
|
|
1046
|
+
return { ...ctx, __app: app };
|
|
1095
1047
|
},
|
|
1096
1048
|
"Sets up the Express server according to the security profile"
|
|
1097
1049
|
).then(
|
|
@@ -1121,11 +1073,12 @@ var RestController = class _RestController {
|
|
|
1121
1073
|
console.log("delegation", deputyExecId, ctx2);
|
|
1122
1074
|
} catch (e) {
|
|
1123
1075
|
console.error("Error in delegation", e);
|
|
1124
|
-
res.send(
|
|
1076
|
+
res.send({
|
|
1125
1077
|
__status: "error",
|
|
1126
1078
|
__error: e,
|
|
1127
|
-
errored: true
|
|
1128
|
-
|
|
1079
|
+
errored: true,
|
|
1080
|
+
...ctx2
|
|
1081
|
+
});
|
|
1129
1082
|
return;
|
|
1130
1083
|
}
|
|
1131
1084
|
CadenzaService.createEphemeralMetaTask(
|
|
@@ -1134,16 +1087,19 @@ var RestController = class _RestController {
|
|
|
1134
1087
|
console.log("Resolve delegation", endCtx);
|
|
1135
1088
|
const metadata = endCtx.__metadata;
|
|
1136
1089
|
delete endCtx.__metadata;
|
|
1137
|
-
res.json(
|
|
1090
|
+
res.json({
|
|
1091
|
+
...endCtx,
|
|
1092
|
+
...metadata,
|
|
1138
1093
|
__status: "success"
|
|
1139
|
-
})
|
|
1094
|
+
});
|
|
1140
1095
|
},
|
|
1141
1096
|
"Resolves a delegation request",
|
|
1142
1097
|
{ register: false }
|
|
1143
1098
|
).doOn(`meta.node.graph_completed:${deputyExecId}`).emits(`meta.rest.delegation_resolved:${deputyExecId}`);
|
|
1144
|
-
CadenzaService.broker.emit("meta.rest.delegation_requested",
|
|
1099
|
+
CadenzaService.broker.emit("meta.rest.delegation_requested", {
|
|
1100
|
+
...ctx2,
|
|
1145
1101
|
__name: ctx2.__remoteRoutineName
|
|
1146
|
-
})
|
|
1102
|
+
});
|
|
1147
1103
|
});
|
|
1148
1104
|
app.post("/signal", (req, res) => {
|
|
1149
1105
|
let ctx2;
|
|
@@ -1151,11 +1107,12 @@ var RestController = class _RestController {
|
|
|
1151
1107
|
ctx2 = req.body;
|
|
1152
1108
|
console.log("SIGNAL RECEIVED", ctx2);
|
|
1153
1109
|
if (!CadenzaService.broker.listObservedSignals().includes(ctx2.__signalName)) {
|
|
1154
|
-
res.send(
|
|
1110
|
+
res.send({
|
|
1111
|
+
...ctx2,
|
|
1155
1112
|
__status: "error",
|
|
1156
1113
|
__error: `No such signal: ${ctx2.__signalName}`,
|
|
1157
1114
|
errored: true
|
|
1158
|
-
})
|
|
1115
|
+
});
|
|
1159
1116
|
return;
|
|
1160
1117
|
}
|
|
1161
1118
|
res.send({
|
|
@@ -1198,17 +1155,16 @@ var RestController = class _RestController {
|
|
|
1198
1155
|
const server = import_node_http.default.createServer(ctx2.__app);
|
|
1199
1156
|
ctx2.__httpServer = server;
|
|
1200
1157
|
server.listen(ctx2.__port, () => {
|
|
1201
|
-
|
|
1202
|
-
if (typeof (server == null ? void 0 : server.address()) === "string") {
|
|
1158
|
+
if (typeof server?.address() === "string") {
|
|
1203
1159
|
address = server.address();
|
|
1204
|
-
} else if (
|
|
1160
|
+
} else if (server?.address()?.address === "::") {
|
|
1205
1161
|
if (process.env.NODE_ENV === "development") {
|
|
1206
1162
|
address = "localhost";
|
|
1207
1163
|
} else if (process.env.IS_DOCKER === "true") {
|
|
1208
1164
|
address = process.env.CADENZA_SERVER_URL || "localhost";
|
|
1209
1165
|
}
|
|
1210
1166
|
} else {
|
|
1211
|
-
address =
|
|
1167
|
+
address = server?.address()?.address || "";
|
|
1212
1168
|
}
|
|
1213
1169
|
console.log(`Server is running on ${address}:${port}`);
|
|
1214
1170
|
});
|
|
@@ -1233,15 +1189,14 @@ var RestController = class _RestController {
|
|
|
1233
1189
|
ctx2.__port = 443;
|
|
1234
1190
|
port = 443;
|
|
1235
1191
|
httpsServer.listen(443, () => {
|
|
1236
|
-
|
|
1237
|
-
if (typeof (httpsServer == null ? void 0 : httpsServer.address()) === "string") {
|
|
1192
|
+
if (typeof httpsServer?.address() === "string") {
|
|
1238
1193
|
address = httpsServer.address();
|
|
1239
|
-
} else if (
|
|
1194
|
+
} else if (httpsServer?.address()?.address === "::") {
|
|
1240
1195
|
if (process.env.IS_DOCKER === "true") {
|
|
1241
1196
|
address = process.env.CADENZA_SERVER_URL || "localhost";
|
|
1242
1197
|
}
|
|
1243
1198
|
} else {
|
|
1244
|
-
address =
|
|
1199
|
+
address = httpsServer?.address()?.address || "";
|
|
1245
1200
|
}
|
|
1246
1201
|
exposed = true;
|
|
1247
1202
|
console.log(`HTTPS Server is running on ${address}:443`);
|
|
@@ -1293,23 +1248,22 @@ var RestController = class _RestController {
|
|
|
1293
1248
|
console.log("Fetch connecting to", URL);
|
|
1294
1249
|
const handshakeTask = CadenzaService.createMetaTask(
|
|
1295
1250
|
`Send Handshake to ${URL}`,
|
|
1296
|
-
(ctx2, emit2) =>
|
|
1297
|
-
var _a2;
|
|
1251
|
+
async (ctx2, emit2) => {
|
|
1298
1252
|
console.log("Sending handshake", ctx2);
|
|
1299
|
-
const response =
|
|
1253
|
+
const response = await (0, import_node_fetch.default)(`${URL}/handshake`, {
|
|
1300
1254
|
headers: {
|
|
1301
1255
|
"Content-Type": "application/json"
|
|
1302
1256
|
},
|
|
1303
1257
|
method: "POST",
|
|
1304
1258
|
body: JSON.stringify(ctx2.handshakeData)
|
|
1305
1259
|
});
|
|
1306
|
-
const result =
|
|
1260
|
+
const result = await response.json();
|
|
1307
1261
|
console.log("Handshake result", result);
|
|
1308
1262
|
if (result.__status !== "success") {
|
|
1309
|
-
const error =
|
|
1263
|
+
const error = result.__error ?? `Failed to connect to service ${serviceName} ${ctx2.serviceInstanceId}`;
|
|
1310
1264
|
console.error(error);
|
|
1311
1265
|
emit2(`meta.fetch.handshake_failed:${fetchId}`, result);
|
|
1312
|
-
return
|
|
1266
|
+
return { ...ctx2, __error: error, errored: true };
|
|
1313
1267
|
}
|
|
1314
1268
|
ctx2.serviceInstanceId = result.__serviceInstanceId;
|
|
1315
1269
|
console.log(`Connected to service ${serviceName} ${URL}`, result);
|
|
@@ -1323,31 +1277,33 @@ var RestController = class _RestController {
|
|
|
1323
1277
|
});
|
|
1324
1278
|
}
|
|
1325
1279
|
return ctx2;
|
|
1326
|
-
}
|
|
1280
|
+
},
|
|
1327
1281
|
"Sends handshake request"
|
|
1328
1282
|
).doOn(`meta.fetch.handshake_requested:${fetchId}`).emits("meta.fetch.handshake_complete");
|
|
1329
1283
|
const delegateTask = CadenzaService.createMetaTask(
|
|
1330
1284
|
`Delegate flow to REST server ${URL}`,
|
|
1331
|
-
(ctx2, emit2) =>
|
|
1285
|
+
async (ctx2, emit2) => {
|
|
1332
1286
|
if (ctx2.__remoteRoutineName === void 0) {
|
|
1333
1287
|
return;
|
|
1334
1288
|
}
|
|
1335
1289
|
console.log("Delegating", ctx2);
|
|
1336
1290
|
let resultContext;
|
|
1337
1291
|
try {
|
|
1338
|
-
const response =
|
|
1292
|
+
const response = await (0, import_node_fetch.default)(`${URL}/delegation`, {
|
|
1339
1293
|
headers: {
|
|
1340
1294
|
"Content-Type": "application/json"
|
|
1341
1295
|
},
|
|
1342
1296
|
method: "POST",
|
|
1343
1297
|
body: JSON.stringify(ctx2)
|
|
1344
1298
|
});
|
|
1345
|
-
resultContext =
|
|
1299
|
+
resultContext = await response.json();
|
|
1346
1300
|
} catch (e) {
|
|
1347
|
-
resultContext =
|
|
1301
|
+
resultContext = {
|
|
1348
1302
|
__error: `Error: ${e}`,
|
|
1349
|
-
errored: true
|
|
1350
|
-
|
|
1303
|
+
errored: true,
|
|
1304
|
+
...ctx2,
|
|
1305
|
+
...ctx2.__metadata
|
|
1306
|
+
};
|
|
1351
1307
|
} finally {
|
|
1352
1308
|
emit2(
|
|
1353
1309
|
`meta.fetch.delegated:${ctx2.__metadata.__deputyExecId}`,
|
|
@@ -1355,7 +1311,7 @@ var RestController = class _RestController {
|
|
|
1355
1311
|
);
|
|
1356
1312
|
}
|
|
1357
1313
|
return resultContext;
|
|
1358
|
-
}
|
|
1314
|
+
},
|
|
1359
1315
|
"Sends delegation request"
|
|
1360
1316
|
).doOn(
|
|
1361
1317
|
`meta.service_registry.selected_instance_for_fetch:${fetchId}`,
|
|
@@ -1363,32 +1319,33 @@ var RestController = class _RestController {
|
|
|
1363
1319
|
).emitsOnFail("meta.fetch.delegate_failed");
|
|
1364
1320
|
const transmitTask = CadenzaService.createMetaTask(
|
|
1365
1321
|
`Transmit signal to server ${URL}`,
|
|
1366
|
-
(ctx2, emit2) =>
|
|
1322
|
+
async (ctx2, emit2) => {
|
|
1367
1323
|
if (ctx2.__signalName === void 0) {
|
|
1368
1324
|
return;
|
|
1369
1325
|
}
|
|
1370
1326
|
let response;
|
|
1371
1327
|
try {
|
|
1372
|
-
response =
|
|
1328
|
+
response = await (0, import_node_fetch.default)(`${URL}/signal`, {
|
|
1373
1329
|
headers: {
|
|
1374
1330
|
"Content-Type": "application/json"
|
|
1375
1331
|
},
|
|
1376
1332
|
method: "POST",
|
|
1377
1333
|
body: JSON.stringify(ctx2)
|
|
1378
1334
|
});
|
|
1379
|
-
response =
|
|
1335
|
+
response = await response.json();
|
|
1380
1336
|
console.log("SIGNAL TRANSMITTED", response);
|
|
1381
1337
|
if (ctx2.__routineExecId) {
|
|
1382
1338
|
emit2(`meta.fetch.transmitted:${ctx2.__routineExecId}`, response);
|
|
1383
1339
|
}
|
|
1384
1340
|
} catch (e) {
|
|
1385
|
-
response =
|
|
1341
|
+
response = {
|
|
1386
1342
|
__error: `Error: ${e}`,
|
|
1387
|
-
errored: true
|
|
1388
|
-
|
|
1343
|
+
errored: true,
|
|
1344
|
+
...ctx2
|
|
1345
|
+
};
|
|
1389
1346
|
}
|
|
1390
1347
|
return response;
|
|
1391
|
-
}
|
|
1348
|
+
},
|
|
1392
1349
|
"Sends signal request"
|
|
1393
1350
|
).doOn(
|
|
1394
1351
|
`meta.service_registry.selected_instance_for_fetch:${fetchId}`,
|
|
@@ -1397,21 +1354,22 @@ var RestController = class _RestController {
|
|
|
1397
1354
|
).emitsOnFail("meta.fetch.signal_transmission_failed");
|
|
1398
1355
|
const statusTask = CadenzaService.createMetaTask(
|
|
1399
1356
|
`Request status from ${URL}`,
|
|
1400
|
-
(ctx2) =>
|
|
1357
|
+
async (ctx2) => {
|
|
1401
1358
|
let status;
|
|
1402
1359
|
try {
|
|
1403
|
-
const response =
|
|
1360
|
+
const response = await (0, import_node_fetch.default)(`${URL}/status`, {
|
|
1404
1361
|
method: "GET"
|
|
1405
1362
|
});
|
|
1406
|
-
status =
|
|
1363
|
+
status = await response.json();
|
|
1407
1364
|
} catch (e) {
|
|
1408
|
-
status =
|
|
1365
|
+
status = {
|
|
1409
1366
|
__error: `Error: ${e}`,
|
|
1410
|
-
errored: true
|
|
1411
|
-
|
|
1367
|
+
errored: true,
|
|
1368
|
+
...ctx2
|
|
1369
|
+
};
|
|
1412
1370
|
}
|
|
1413
1371
|
return status;
|
|
1414
|
-
}
|
|
1372
|
+
},
|
|
1415
1373
|
"Requests status"
|
|
1416
1374
|
).doOn("meta.fetch.status_check_requested").emits("meta.fetch.status_checked").emitsOnFail("meta.fetch.status_check_failed");
|
|
1417
1375
|
CadenzaService.createEphemeralMetaTask("Destroy fetch client", (ctx2, emit2) => {
|
|
@@ -1474,20 +1432,18 @@ var SocketController = class _SocketController {
|
|
|
1474
1432
|
"SocketServer",
|
|
1475
1433
|
[
|
|
1476
1434
|
CadenzaService.createMetaTask("Setup SocketServer", (ctx) => {
|
|
1477
|
-
var _a2, _b2;
|
|
1478
1435
|
if (!ctx.__useSocket) {
|
|
1479
1436
|
return;
|
|
1480
1437
|
}
|
|
1481
1438
|
console.log("SocketServer: Setting up", ctx);
|
|
1482
1439
|
let server;
|
|
1483
|
-
server = new import_socket.Server(
|
|
1440
|
+
server = new import_socket.Server(ctx.__httpsServer ?? ctx.__httpServer);
|
|
1484
1441
|
console.log("SocketServer:", server);
|
|
1485
|
-
const profile =
|
|
1442
|
+
const profile = ctx.__securityProfile ?? "medium";
|
|
1486
1443
|
server.use((socket, next) => {
|
|
1487
|
-
|
|
1488
|
-
const origin = (_b3 = (_a3 = socket == null ? void 0 : socket.handshake) == null ? void 0 : _a3.headers) == null ? void 0 : _b3.origin;
|
|
1444
|
+
const origin = socket?.handshake?.headers?.origin;
|
|
1489
1445
|
const allowedOrigins = ["*"];
|
|
1490
|
-
const networkType =
|
|
1446
|
+
const networkType = ctx.__networkType ?? "internal";
|
|
1491
1447
|
let effectiveOrigin = origin || "unknown";
|
|
1492
1448
|
if (networkType === "internal") effectiveOrigin = "internal";
|
|
1493
1449
|
if (profile !== "low" && !allowedOrigins.includes(effectiveOrigin) && !allowedOrigins.includes("*")) {
|
|
@@ -1500,8 +1456,7 @@ var SocketController = class _SocketController {
|
|
|
1500
1456
|
};
|
|
1501
1457
|
const limiter = new import_rate_limiter_flexible2.RateLimiterMemory(limiterOptions[profile]);
|
|
1502
1458
|
socket.use((packet, next2) => {
|
|
1503
|
-
|
|
1504
|
-
limiter.consume(((_a4 = socket == null ? void 0 : socket.handshake) == null ? void 0 : _a4.address) || "unknown").then(() => next2()).catch((rej) => {
|
|
1459
|
+
limiter.consume(socket?.handshake?.address || "unknown").then(() => next2()).catch((rej) => {
|
|
1505
1460
|
if (rej.msBeforeNext > 0) {
|
|
1506
1461
|
console.log(
|
|
1507
1462
|
"SocketServer: Rate limit exceeded",
|
|
@@ -1522,7 +1477,7 @@ var SocketController = class _SocketController {
|
|
|
1522
1477
|
console.log("SocketServer: Setup complete");
|
|
1523
1478
|
if (!server) {
|
|
1524
1479
|
console.error("Socket setup error: No server");
|
|
1525
|
-
return
|
|
1480
|
+
return { ...ctx, __error: "No server", errored: true };
|
|
1526
1481
|
}
|
|
1527
1482
|
server.on("connection", (ws) => {
|
|
1528
1483
|
console.log("SocketServer: New connection");
|
|
@@ -1555,19 +1510,17 @@ var SocketController = class _SocketController {
|
|
|
1555
1510
|
"Updates delegation progress",
|
|
1556
1511
|
{
|
|
1557
1512
|
once: false,
|
|
1558
|
-
destroyCondition: (ctx3) =>
|
|
1559
|
-
var _a3;
|
|
1560
|
-
return ctx3.data.progress === 1 || ((_a3 = ctx3.data) == null ? void 0 : _a3.progress) === void 0;
|
|
1561
|
-
},
|
|
1513
|
+
destroyCondition: (ctx3) => ctx3.data.progress === 1 || ctx3.data?.progress === void 0,
|
|
1562
1514
|
register: false
|
|
1563
1515
|
}
|
|
1564
1516
|
).doOn(
|
|
1565
1517
|
`meta.node.routine_execution_progress:${deputyExecId}`,
|
|
1566
1518
|
`meta.node.graph_completed:${deputyExecId}`
|
|
1567
1519
|
).emitsOnFail(`meta.socket.progress_failed:${deputyExecId}`);
|
|
1568
|
-
CadenzaService.broker.emit("meta.socket.delegation_requested",
|
|
1520
|
+
CadenzaService.broker.emit("meta.socket.delegation_requested", {
|
|
1521
|
+
...ctx2,
|
|
1569
1522
|
__name: ctx2.__remoteRoutineName
|
|
1570
|
-
})
|
|
1523
|
+
});
|
|
1571
1524
|
}
|
|
1572
1525
|
);
|
|
1573
1526
|
ws.on(
|
|
@@ -1580,11 +1533,12 @@ var SocketController = class _SocketController {
|
|
|
1580
1533
|
});
|
|
1581
1534
|
CadenzaService.broker.emit(ctx2.__signalName, ctx2);
|
|
1582
1535
|
} else {
|
|
1583
|
-
callback(
|
|
1536
|
+
callback({
|
|
1537
|
+
...ctx2,
|
|
1584
1538
|
__status: "error",
|
|
1585
1539
|
__error: `No such signal: ${ctx2.__signalName}`,
|
|
1586
1540
|
errored: true
|
|
1587
|
-
})
|
|
1541
|
+
});
|
|
1588
1542
|
}
|
|
1589
1543
|
}
|
|
1590
1544
|
);
|
|
@@ -1682,7 +1636,7 @@ var SocketController = class _SocketController {
|
|
|
1682
1636
|
});
|
|
1683
1637
|
const delegateTask = CadenzaService.createMetaTask(
|
|
1684
1638
|
`Delegate flow to ${URL}`,
|
|
1685
|
-
(ctx2, emit) =>
|
|
1639
|
+
async (ctx2, emit) => {
|
|
1686
1640
|
if (ctx2.__remoteRoutineName === void 0) {
|
|
1687
1641
|
return;
|
|
1688
1642
|
}
|
|
@@ -1695,10 +1649,12 @@ var SocketController = class _SocketController {
|
|
|
1695
1649
|
(err, resultContext) => {
|
|
1696
1650
|
if (err) {
|
|
1697
1651
|
console.log("socket error:", err);
|
|
1698
|
-
resultContext =
|
|
1652
|
+
resultContext = {
|
|
1699
1653
|
__error: `Timeout error: ${err}`,
|
|
1700
|
-
errored: true
|
|
1701
|
-
|
|
1654
|
+
errored: true,
|
|
1655
|
+
...ctx2,
|
|
1656
|
+
...ctx2.__metadata
|
|
1657
|
+
};
|
|
1702
1658
|
emit(`meta.socket_client.delegate_failed`, resultContext);
|
|
1703
1659
|
resolve(resultContext);
|
|
1704
1660
|
return;
|
|
@@ -1707,30 +1663,34 @@ var SocketController = class _SocketController {
|
|
|
1707
1663
|
delete resultContext.__metadata;
|
|
1708
1664
|
emit(
|
|
1709
1665
|
`meta.socket_client.delegated:${ctx2.__metadata.__deputyExecId}`,
|
|
1710
|
-
|
|
1666
|
+
{
|
|
1667
|
+
...resultContext,
|
|
1668
|
+
...metadata
|
|
1669
|
+
}
|
|
1711
1670
|
);
|
|
1712
1671
|
resolve(resultContext);
|
|
1713
1672
|
}
|
|
1714
1673
|
);
|
|
1715
1674
|
});
|
|
1716
|
-
}
|
|
1675
|
+
},
|
|
1717
1676
|
`Delegate flow to service ${serviceName} with address ${URL}`
|
|
1718
1677
|
).doOn(`meta.service_registry.selected_instance_for_socket:${fetchId}`);
|
|
1719
1678
|
const transmitTask = CadenzaService.createMetaTask(
|
|
1720
1679
|
`Transmit signal to ${URL}`,
|
|
1721
|
-
(ctx2, emit) =>
|
|
1680
|
+
async (ctx2, emit) => {
|
|
1722
1681
|
if (ctx2.__signalName === void 0) {
|
|
1723
1682
|
return;
|
|
1724
1683
|
}
|
|
1725
1684
|
return new Promise((resolve, reject) => {
|
|
1726
|
-
|
|
1727
|
-
socket.timeout((_a2 = ctx2.__timeout) != null ? _a2 : 1e4).emit("signal", ctx2, (err, response) => {
|
|
1685
|
+
socket.timeout(ctx2.__timeout ?? 1e4).emit("signal", ctx2, (err, response) => {
|
|
1728
1686
|
if (err) {
|
|
1729
1687
|
console.log("socket error:", err);
|
|
1730
|
-
response =
|
|
1688
|
+
response = {
|
|
1731
1689
|
__error: `Timeout error: ${err}`,
|
|
1732
|
-
errored: true
|
|
1733
|
-
|
|
1690
|
+
errored: true,
|
|
1691
|
+
...ctx2,
|
|
1692
|
+
...ctx2.__metadata
|
|
1693
|
+
};
|
|
1734
1694
|
emit(
|
|
1735
1695
|
`meta.socket_client.signal_transmission_failed`,
|
|
1736
1696
|
response
|
|
@@ -1747,13 +1707,13 @@ var SocketController = class _SocketController {
|
|
|
1747
1707
|
resolve(response);
|
|
1748
1708
|
});
|
|
1749
1709
|
});
|
|
1750
|
-
}
|
|
1710
|
+
},
|
|
1751
1711
|
`Transmits signal to service ${serviceName} with address ${URL}`
|
|
1752
1712
|
).doOn(`meta.service_registry.selected_instance_for_socket:${fetchId}`);
|
|
1753
1713
|
CadenzaService.createEphemeralMetaTask(
|
|
1754
1714
|
`Shutdown SocketClient ${URL}`,
|
|
1755
1715
|
() => {
|
|
1756
|
-
socket
|
|
1716
|
+
socket?.close();
|
|
1757
1717
|
delegateTask.destroy();
|
|
1758
1718
|
transmitTask.destroy();
|
|
1759
1719
|
},
|
|
@@ -1856,7 +1816,6 @@ var SignalController = class _SignalController {
|
|
|
1856
1816
|
CadenzaService.createMetaTask(
|
|
1857
1817
|
"Add data to signal emission",
|
|
1858
1818
|
(ctx) => {
|
|
1859
|
-
var _a2;
|
|
1860
1819
|
const signalEmission = ctx.__signalEmission;
|
|
1861
1820
|
if (!signalEmission) {
|
|
1862
1821
|
return false;
|
|
@@ -1868,7 +1827,7 @@ var SignalController = class _SignalController {
|
|
|
1868
1827
|
task_version: signalEmission.taskVersion,
|
|
1869
1828
|
task_execution_id: signalEmission.taskExecutionId,
|
|
1870
1829
|
is_meta: signalEmission.isMeta,
|
|
1871
|
-
is_metric:
|
|
1830
|
+
is_metric: signalEmission.isMetric ?? false,
|
|
1872
1831
|
data: ctx,
|
|
1873
1832
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
1874
1833
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId
|
|
@@ -1882,10 +1841,11 @@ var SignalController = class _SignalController {
|
|
|
1882
1841
|
"Add metadata to signal consumption",
|
|
1883
1842
|
(ctx) => {
|
|
1884
1843
|
return {
|
|
1885
|
-
data:
|
|
1844
|
+
data: {
|
|
1845
|
+
...ctx.data,
|
|
1886
1846
|
serviceName: CadenzaService.serviceRegistry.serviceName,
|
|
1887
1847
|
serviceInstanceId: CadenzaService.serviceRegistry.serviceInstanceId
|
|
1888
|
-
}
|
|
1848
|
+
}
|
|
1889
1849
|
};
|
|
1890
1850
|
},
|
|
1891
1851
|
"",
|
|
@@ -1903,7 +1863,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1903
1863
|
constructor() {
|
|
1904
1864
|
CadenzaService.createMetaTask("Handle task creation", (ctx) => {
|
|
1905
1865
|
return {
|
|
1906
|
-
data:
|
|
1866
|
+
data: {
|
|
1867
|
+
...ctx.data,
|
|
1907
1868
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1908
1869
|
// input_context_schema_id: ctx.data.inputContextSchema ? { // TODO
|
|
1909
1870
|
//
|
|
@@ -1911,21 +1872,24 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1911
1872
|
// output_context_schema_id: ctx.data.outputContextSchema ? {
|
|
1912
1873
|
//
|
|
1913
1874
|
// } : null,
|
|
1914
|
-
}
|
|
1875
|
+
}
|
|
1915
1876
|
};
|
|
1916
1877
|
}).doOn("meta.task.created").emits("meta.graph_metadata.task_created");
|
|
1917
1878
|
CadenzaService.createMetaTask("Handle task update", (ctx) => {
|
|
1918
|
-
return
|
|
1919
|
-
|
|
1879
|
+
return {
|
|
1880
|
+
...ctx,
|
|
1881
|
+
filter: {
|
|
1882
|
+
...ctx.filter,
|
|
1920
1883
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1884
|
+
}
|
|
1885
|
+
};
|
|
1923
1886
|
}).doOn("meta.task.layer_index_changed", "meta.task.destroyed").emits("meta.graph_metadata.task_updated");
|
|
1924
1887
|
CadenzaService.createMetaTask("Handle task relationship creation", (ctx) => {
|
|
1925
1888
|
return {
|
|
1926
|
-
data:
|
|
1889
|
+
data: {
|
|
1890
|
+
...ctx.data,
|
|
1927
1891
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1928
|
-
}
|
|
1892
|
+
}
|
|
1929
1893
|
};
|
|
1930
1894
|
}).doOn("meta.task.relationship_added").emits("meta.graph_metadata.task_relationship_created");
|
|
1931
1895
|
CadenzaService.createMetaTask("Handle task signal observation", (ctx) => {
|
|
@@ -1937,18 +1901,20 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1937
1901
|
_signal = ctx.data.signalName.split(".").slice(1).join(".");
|
|
1938
1902
|
}
|
|
1939
1903
|
return {
|
|
1940
|
-
data:
|
|
1904
|
+
data: {
|
|
1905
|
+
...ctx.data,
|
|
1941
1906
|
signalName: _signal,
|
|
1942
1907
|
taskServiceName: CadenzaService.serviceRegistry.serviceName,
|
|
1943
|
-
signalServiceName: signalServiceName
|
|
1944
|
-
}
|
|
1908
|
+
signalServiceName: signalServiceName ?? CadenzaService.serviceRegistry.serviceName
|
|
1909
|
+
}
|
|
1945
1910
|
};
|
|
1946
1911
|
}).doOn("meta.task.observed_signal").emits("meta.graph_metadata.task_signal_observed");
|
|
1947
1912
|
CadenzaService.createMetaTask("Handle task signal attachment", (ctx) => {
|
|
1948
1913
|
return {
|
|
1949
|
-
data:
|
|
1914
|
+
data: {
|
|
1915
|
+
...ctx.data,
|
|
1950
1916
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1951
|
-
}
|
|
1917
|
+
}
|
|
1952
1918
|
};
|
|
1953
1919
|
}).doOn("meta.task.attached_signal").emits("meta.graph_metadata.task_attached_signal");
|
|
1954
1920
|
CadenzaService.createMetaTask("Handle task unsubscribing signal", (ctx) => {
|
|
@@ -1956,9 +1922,10 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1956
1922
|
data: {
|
|
1957
1923
|
deleted: true
|
|
1958
1924
|
},
|
|
1959
|
-
filter:
|
|
1925
|
+
filter: {
|
|
1926
|
+
...ctx.filter,
|
|
1960
1927
|
task_service_name: CadenzaService.serviceRegistry.serviceName
|
|
1961
|
-
}
|
|
1928
|
+
}
|
|
1962
1929
|
};
|
|
1963
1930
|
}).doOn("meta.task.unsubscribed_signal").emits("meta.graph_metadata.task_unsubscribed_signal");
|
|
1964
1931
|
CadenzaService.createMetaTask("Handle task detaching signal", (ctx) => {
|
|
@@ -1966,37 +1933,43 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1966
1933
|
data: {
|
|
1967
1934
|
deleted: true
|
|
1968
1935
|
},
|
|
1969
|
-
filter:
|
|
1936
|
+
filter: {
|
|
1937
|
+
...ctx.filter,
|
|
1970
1938
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1971
|
-
}
|
|
1939
|
+
}
|
|
1972
1940
|
};
|
|
1973
1941
|
}).doOn("meta.task.detached_signal").emits("meta.graph_metadata.task_detached_signal");
|
|
1974
1942
|
CadenzaService.createMetaTask("Handle routine creation", (ctx) => {
|
|
1975
1943
|
return {
|
|
1976
|
-
data:
|
|
1944
|
+
data: {
|
|
1945
|
+
...ctx.data,
|
|
1977
1946
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1978
|
-
}
|
|
1947
|
+
}
|
|
1979
1948
|
};
|
|
1980
1949
|
}).doAfter(CadenzaService.registry.registerRoutine).emits("meta.graph_metadata.routine_created");
|
|
1981
1950
|
CadenzaService.createMetaTask("Handle routine update", (ctx) => {
|
|
1982
|
-
return
|
|
1983
|
-
|
|
1951
|
+
return {
|
|
1952
|
+
...ctx,
|
|
1953
|
+
filter: {
|
|
1954
|
+
...ctx.filter,
|
|
1984
1955
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1985
|
-
}
|
|
1986
|
-
}
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1987
1958
|
}).doOn("meta.routine.destroyed").emits("meta.graph_metadata.routine_updated");
|
|
1988
1959
|
CadenzaService.createMetaTask("Handle adding task to routine", (ctx) => {
|
|
1989
1960
|
return {
|
|
1990
|
-
data:
|
|
1961
|
+
data: {
|
|
1962
|
+
...ctx.data,
|
|
1991
1963
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1992
|
-
}
|
|
1964
|
+
}
|
|
1993
1965
|
};
|
|
1994
1966
|
}).doOn("meta.routine.task_added").emits("meta.graph_metadata.task_added_to_routine");
|
|
1995
1967
|
CadenzaService.createMetaTask("Handle new trace", (ctx) => {
|
|
1996
1968
|
const context = ctx.data.context;
|
|
1997
1969
|
delete ctx.data.context;
|
|
1998
1970
|
return {
|
|
1999
|
-
data:
|
|
1971
|
+
data: {
|
|
1972
|
+
...ctx.data,
|
|
2000
1973
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2001
1974
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2002
1975
|
context_id: {
|
|
@@ -2009,7 +1982,7 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2009
1982
|
},
|
|
2010
1983
|
return: "uuid"
|
|
2011
1984
|
}
|
|
2012
|
-
}
|
|
1985
|
+
}
|
|
2013
1986
|
};
|
|
2014
1987
|
}).doOn("meta.runner.new_trace").emits("meta.graph_metadata.execution_trace_created");
|
|
2015
1988
|
CadenzaService.createMetaTask(
|
|
@@ -2021,7 +1994,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2021
1994
|
}
|
|
2022
1995
|
delete ctx.data.context;
|
|
2023
1996
|
return {
|
|
2024
|
-
data:
|
|
1997
|
+
data: {
|
|
1998
|
+
...ctx.data,
|
|
2025
1999
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2026
2000
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2027
2001
|
context_id: typeof context === "string" ? context : {
|
|
@@ -2034,7 +2008,7 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2034
2008
|
},
|
|
2035
2009
|
return: "uuid"
|
|
2036
2010
|
}
|
|
2037
|
-
}
|
|
2011
|
+
}
|
|
2038
2012
|
};
|
|
2039
2013
|
},
|
|
2040
2014
|
"Handles routine execution creation",
|
|
@@ -2054,7 +2028,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2054
2028
|
const context = ctx.data.resultContext;
|
|
2055
2029
|
delete ctx.data.resultContext;
|
|
2056
2030
|
return {
|
|
2057
|
-
data:
|
|
2031
|
+
data: {
|
|
2032
|
+
...ctx.data,
|
|
2058
2033
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2059
2034
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2060
2035
|
result_context_id: typeof context === "string" ? context : {
|
|
@@ -2067,8 +2042,10 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2067
2042
|
},
|
|
2068
2043
|
return: "uuid"
|
|
2069
2044
|
}
|
|
2070
|
-
}
|
|
2071
|
-
filter:
|
|
2045
|
+
},
|
|
2046
|
+
filter: {
|
|
2047
|
+
...ctx.filter
|
|
2048
|
+
}
|
|
2072
2049
|
};
|
|
2073
2050
|
},
|
|
2074
2051
|
"Handles routine execution ended",
|
|
@@ -2083,7 +2060,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2083
2060
|
console.log("Handle task execution creation", context);
|
|
2084
2061
|
}
|
|
2085
2062
|
return {
|
|
2086
|
-
data:
|
|
2063
|
+
data: {
|
|
2064
|
+
...ctx.data,
|
|
2087
2065
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2088
2066
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2089
2067
|
context_id: typeof context === "string" ? context : {
|
|
@@ -2096,7 +2074,7 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2096
2074
|
},
|
|
2097
2075
|
return: "uuid"
|
|
2098
2076
|
}
|
|
2099
|
-
}
|
|
2077
|
+
}
|
|
2100
2078
|
};
|
|
2101
2079
|
},
|
|
2102
2080
|
"Handles task execution creation",
|
|
@@ -2121,12 +2099,12 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2121
2099
|
CadenzaService.createMetaTask(
|
|
2122
2100
|
"Handle task execution ended",
|
|
2123
2101
|
(ctx) => {
|
|
2124
|
-
var _a2;
|
|
2125
2102
|
const context = ctx.data.resultContext;
|
|
2126
2103
|
console.log("Handle task execution ended", context);
|
|
2127
2104
|
delete ctx.data.resultContext;
|
|
2128
2105
|
return {
|
|
2129
|
-
data:
|
|
2106
|
+
data: {
|
|
2107
|
+
...ctx.data,
|
|
2130
2108
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2131
2109
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2132
2110
|
result_context_id: typeof context === "string" ? context : {
|
|
@@ -2135,12 +2113,14 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2135
2113
|
data: {
|
|
2136
2114
|
uuid: context.id,
|
|
2137
2115
|
context: context.context,
|
|
2138
|
-
is_meta:
|
|
2116
|
+
is_meta: ctx.data.isMeta ?? false
|
|
2139
2117
|
},
|
|
2140
2118
|
return: "uuid"
|
|
2141
2119
|
}
|
|
2142
|
-
}
|
|
2143
|
-
filter:
|
|
2120
|
+
},
|
|
2121
|
+
filter: {
|
|
2122
|
+
...ctx.filter
|
|
2123
|
+
}
|
|
2144
2124
|
};
|
|
2145
2125
|
},
|
|
2146
2126
|
"Handles task execution ended",
|
|
@@ -2153,9 +2133,10 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2153
2133
|
data: {
|
|
2154
2134
|
executionCount: "increment"
|
|
2155
2135
|
},
|
|
2156
|
-
filter:
|
|
2136
|
+
filter: {
|
|
2137
|
+
...ctx.filter,
|
|
2157
2138
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
2158
|
-
}
|
|
2139
|
+
}
|
|
2159
2140
|
};
|
|
2160
2141
|
},
|
|
2161
2142
|
"Handles task execution relationship creation",
|
|
@@ -2189,15 +2170,14 @@ var SCHEMA_TYPES = [
|
|
|
2189
2170
|
// src/database/DatabaseController.ts
|
|
2190
2171
|
var import_pg = require("pg");
|
|
2191
2172
|
var import_lodash_es = require("lodash-es");
|
|
2192
|
-
var _a, _b, _c, _d;
|
|
2193
2173
|
var DatabaseController = class _DatabaseController {
|
|
2194
2174
|
constructor() {
|
|
2195
2175
|
this.dbClient = new import_pg.Pool({
|
|
2196
|
-
user:
|
|
2197
|
-
host:
|
|
2198
|
-
port: parseInt(
|
|
2176
|
+
user: process.env.DATABASE_USER ?? "postgres",
|
|
2177
|
+
host: process.env.DATABASE_ADDRESS ?? "localhost",
|
|
2178
|
+
port: parseInt(process.env.DATABASE_PORT ?? "5432"),
|
|
2199
2179
|
database: "postgres",
|
|
2200
|
-
password:
|
|
2180
|
+
password: process.env.DATABASE_PASSWORD ?? "03gibnEF"
|
|
2201
2181
|
});
|
|
2202
2182
|
CadenzaService.createMetaRoutine(
|
|
2203
2183
|
"DatabaseServiceInit",
|
|
@@ -2207,8 +2187,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2207
2187
|
// Create schema version table
|
|
2208
2188
|
CadenzaService.createMetaTask(
|
|
2209
2189
|
"Create database",
|
|
2210
|
-
(ctx) =>
|
|
2211
|
-
var _a2, _b2, _c2, _d2;
|
|
2190
|
+
async (ctx) => {
|
|
2212
2191
|
const { databaseName } = ctx;
|
|
2213
2192
|
try {
|
|
2214
2193
|
if (!databaseName.split("").every((c) => /[a-z_]/.test(c))) {
|
|
@@ -2216,14 +2195,14 @@ var DatabaseController = class _DatabaseController {
|
|
|
2216
2195
|
`Invalid database name ${databaseName}. Names must only contain lowercase alphanumeric characters and underscores`
|
|
2217
2196
|
);
|
|
2218
2197
|
}
|
|
2219
|
-
|
|
2198
|
+
await this.dbClient.query(`CREATE DATABASE ${databaseName}`);
|
|
2220
2199
|
console.log(`Database ${databaseName} created`);
|
|
2221
2200
|
this.dbClient = new import_pg.Pool({
|
|
2222
|
-
user:
|
|
2223
|
-
host:
|
|
2224
|
-
port: parseInt(
|
|
2201
|
+
user: process.env.DATABASE_USER ?? "postgres",
|
|
2202
|
+
host: process.env.DATABASE_ADDRESS ?? "localhost",
|
|
2203
|
+
port: parseInt(process.env.DATABASE_PORT ?? "5432"),
|
|
2225
2204
|
database: databaseName,
|
|
2226
|
-
password:
|
|
2205
|
+
password: process.env.DATABASE_PASSWORD ?? "03gibnEF"
|
|
2227
2206
|
});
|
|
2228
2207
|
return true;
|
|
2229
2208
|
} catch (error) {
|
|
@@ -2233,15 +2212,14 @@ var DatabaseController = class _DatabaseController {
|
|
|
2233
2212
|
}
|
|
2234
2213
|
throw new Error(`Failed to create database: ${error.message}`);
|
|
2235
2214
|
}
|
|
2236
|
-
}
|
|
2215
|
+
},
|
|
2237
2216
|
"Creates the target database if it doesn't exist"
|
|
2238
2217
|
).then(
|
|
2239
2218
|
CadenzaService.createMetaTask(
|
|
2240
2219
|
"Validate schema",
|
|
2241
2220
|
(ctx) => {
|
|
2242
|
-
var _a2, _b2;
|
|
2243
2221
|
const { schema } = ctx;
|
|
2244
|
-
if (!
|
|
2222
|
+
if (!schema?.tables || typeof schema.tables !== "object") {
|
|
2245
2223
|
throw new Error("Invalid schema: missing or invalid tables");
|
|
2246
2224
|
}
|
|
2247
2225
|
for (const [tableName, table] of Object.entries(schema.tables)) {
|
|
@@ -2280,8 +2258,8 @@ var DatabaseController = class _DatabaseController {
|
|
|
2280
2258
|
}
|
|
2281
2259
|
if (table.customSignals) {
|
|
2282
2260
|
for (const op of ["query", "insert", "update", "delete"]) {
|
|
2283
|
-
const triggers =
|
|
2284
|
-
const emissions =
|
|
2261
|
+
const triggers = table.customSignals.triggers?.[op];
|
|
2262
|
+
const emissions = table.customSignals.emissions?.[op];
|
|
2285
2263
|
if (triggers && !Array.isArray(triggers) && typeof triggers !== "object") {
|
|
2286
2264
|
console.log(
|
|
2287
2265
|
tableName,
|
|
@@ -2324,7 +2302,6 @@ var DatabaseController = class _DatabaseController {
|
|
|
2324
2302
|
"Generates DDL for database schema"
|
|
2325
2303
|
).then(
|
|
2326
2304
|
CadenzaService.createMetaTask("Generate DDL from table", (ctx) => {
|
|
2327
|
-
var _a2;
|
|
2328
2305
|
const {
|
|
2329
2306
|
ddl,
|
|
2330
2307
|
table,
|
|
@@ -2334,13 +2311,12 @@ var DatabaseController = class _DatabaseController {
|
|
|
2334
2311
|
sortedTables
|
|
2335
2312
|
} = ctx;
|
|
2336
2313
|
const fieldDefs = Object.entries(table.fields).map((value) => {
|
|
2337
|
-
var _a3, _b2, _c2, _d2, _e, _f, _g;
|
|
2338
2314
|
const [fieldName, field] = value;
|
|
2339
2315
|
let def = `${fieldName} ${field.type.toUpperCase()}`;
|
|
2340
2316
|
if (field.type === "varchar")
|
|
2341
|
-
def += `(${
|
|
2317
|
+
def += `(${field.constraints?.maxLength ?? 255})`;
|
|
2342
2318
|
if (field.type === "decimal")
|
|
2343
|
-
def += `(${
|
|
2319
|
+
def += `(${field.constraints?.precision ?? 10},${field.constraints?.scale ?? 2})`;
|
|
2344
2320
|
if (field.primary) def += " PRIMARY KEY";
|
|
2345
2321
|
if (field.unique) def += " UNIQUE";
|
|
2346
2322
|
if (field.default !== void 0)
|
|
@@ -2352,12 +2328,12 @@ var DatabaseController = class _DatabaseController {
|
|
|
2352
2328
|
if (field.references)
|
|
2353
2329
|
def += ` REFERENCES ${field.references} ON DELETE ${field.onDelete || "DO NOTHING"}`;
|
|
2354
2330
|
if (field.encrypted) def += " ENCRYPTED";
|
|
2355
|
-
if (
|
|
2331
|
+
if (field.constraints?.check) {
|
|
2356
2332
|
def += ` CHECK (${field.constraints.check})`;
|
|
2357
2333
|
}
|
|
2358
2334
|
return def;
|
|
2359
2335
|
}).join(", ");
|
|
2360
|
-
if (
|
|
2336
|
+
if (schema.meta?.dropExisting) {
|
|
2361
2337
|
this.dbClient.query(
|
|
2362
2338
|
`DROP TABLE IF EXISTS ${tableName} CASCADE;`
|
|
2363
2339
|
);
|
|
@@ -2576,13 +2552,13 @@ var DatabaseController = class _DatabaseController {
|
|
|
2576
2552
|
).then(
|
|
2577
2553
|
CadenzaService.createMetaTask(
|
|
2578
2554
|
"Apply Database Changes",
|
|
2579
|
-
(ctx) =>
|
|
2555
|
+
async (ctx) => {
|
|
2580
2556
|
const { ddl } = ctx;
|
|
2581
2557
|
if (ddl && ddl.length > 0) {
|
|
2582
2558
|
for (const sql of ddl) {
|
|
2583
2559
|
try {
|
|
2584
2560
|
console.log("Applying SQL", sql);
|
|
2585
|
-
|
|
2561
|
+
await this.dbClient.query(sql);
|
|
2586
2562
|
} catch (error) {
|
|
2587
2563
|
console.error(
|
|
2588
2564
|
"Error applying DDL",
|
|
@@ -2593,7 +2569,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2593
2569
|
}
|
|
2594
2570
|
console.log("DDL applied");
|
|
2595
2571
|
return true;
|
|
2596
|
-
}
|
|
2572
|
+
},
|
|
2597
2573
|
"Applies generated DDL to the database"
|
|
2598
2574
|
).then(
|
|
2599
2575
|
CadenzaService.createMetaTask(
|
|
@@ -2671,50 +2647,45 @@ var DatabaseController = class _DatabaseController {
|
|
|
2671
2647
|
reset() {
|
|
2672
2648
|
this.dbClient.end();
|
|
2673
2649
|
}
|
|
2674
|
-
getClient() {
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
client.
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
return client;
|
|
2696
|
-
});
|
|
2650
|
+
async getClient() {
|
|
2651
|
+
const client = await this.dbClient.connect();
|
|
2652
|
+
const query = client.query;
|
|
2653
|
+
const release = client.release;
|
|
2654
|
+
const timeout = setTimeout(() => {
|
|
2655
|
+
console.error("A client has been checked out for more than 5 seconds!");
|
|
2656
|
+
console.error(
|
|
2657
|
+
`The last executed query on this client was: ${client.lastQuery}`
|
|
2658
|
+
);
|
|
2659
|
+
}, 5e3);
|
|
2660
|
+
client.query = (...args) => {
|
|
2661
|
+
client.lastQuery = args;
|
|
2662
|
+
return query.apply(client, args);
|
|
2663
|
+
};
|
|
2664
|
+
client.release = () => {
|
|
2665
|
+
clearTimeout(timeout);
|
|
2666
|
+
client.query = query;
|
|
2667
|
+
client.release = release;
|
|
2668
|
+
return release.apply(client);
|
|
2669
|
+
};
|
|
2670
|
+
return client;
|
|
2697
2671
|
}
|
|
2698
|
-
waitForDatabase(transaction, client, context) {
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
return { rows: [] };
|
|
2710
|
-
}
|
|
2672
|
+
async waitForDatabase(transaction, client, context) {
|
|
2673
|
+
for (let i = 0; i < 10; i++) {
|
|
2674
|
+
try {
|
|
2675
|
+
return await transaction(client, context);
|
|
2676
|
+
} catch (err) {
|
|
2677
|
+
if (err && err.message.includes("does not exist")) {
|
|
2678
|
+
console.log(`Waiting for database to be ready...`);
|
|
2679
|
+
await new Promise((res) => setTimeout(res, 1e3));
|
|
2680
|
+
} else {
|
|
2681
|
+
console.error("Database query errored: ", err, context);
|
|
2682
|
+
return { rows: [] };
|
|
2711
2683
|
}
|
|
2712
2684
|
}
|
|
2713
|
-
|
|
2714
|
-
|
|
2685
|
+
}
|
|
2686
|
+
throw new Error(`Timeout waiting for database to be ready`);
|
|
2715
2687
|
}
|
|
2716
2688
|
sortTablesByReferences(ctx) {
|
|
2717
|
-
var _a2, _b2;
|
|
2718
2689
|
const schema = ctx.schema;
|
|
2719
2690
|
const graph = /* @__PURE__ */ new Map();
|
|
2720
2691
|
const allTables = Object.keys(schema.tables);
|
|
@@ -2724,7 +2695,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2724
2695
|
if (field.references) {
|
|
2725
2696
|
const [refTable] = field.references.split("(");
|
|
2726
2697
|
if (refTable !== tableName && allTables.includes(refTable)) {
|
|
2727
|
-
|
|
2698
|
+
graph.get(refTable)?.add(tableName);
|
|
2728
2699
|
}
|
|
2729
2700
|
}
|
|
2730
2701
|
}
|
|
@@ -2732,7 +2703,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2732
2703
|
for (const foreignKey of table.foreignKeys) {
|
|
2733
2704
|
const refTable = foreignKey.tableName;
|
|
2734
2705
|
if (refTable !== tableName && allTables.includes(refTable)) {
|
|
2735
|
-
|
|
2706
|
+
graph.get(refTable)?.add(tableName);
|
|
2736
2707
|
}
|
|
2737
2708
|
}
|
|
2738
2709
|
}
|
|
@@ -2767,16 +2738,14 @@ var DatabaseController = class _DatabaseController {
|
|
|
2767
2738
|
}
|
|
2768
2739
|
sorted.reverse();
|
|
2769
2740
|
console.log("sorted tables", sorted, "has cycles", hasCycles);
|
|
2770
|
-
return
|
|
2771
|
-
}
|
|
2772
|
-
splitTables(ctx) {
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
}
|
|
2779
|
-
});
|
|
2741
|
+
return { ...ctx, sortedTables: sorted, hasCycles };
|
|
2742
|
+
}
|
|
2743
|
+
async *splitTables(ctx) {
|
|
2744
|
+
const { sortedTables, schema, options = {} } = ctx;
|
|
2745
|
+
for (const tableName of sortedTables) {
|
|
2746
|
+
const table = schema.tables[tableName];
|
|
2747
|
+
yield { ddl: [], table, tableName, schema, options, sortedTables };
|
|
2748
|
+
}
|
|
2780
2749
|
}
|
|
2781
2750
|
toCamelCase(rows) {
|
|
2782
2751
|
return rows.map((row) => {
|
|
@@ -2787,218 +2756,215 @@ var DatabaseController = class _DatabaseController {
|
|
|
2787
2756
|
return camelCasedRow;
|
|
2788
2757
|
});
|
|
2789
2758
|
}
|
|
2790
|
-
queryFunction(tableName, context) {
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
}
|
|
2832
|
-
}
|
|
2833
|
-
}
|
|
2759
|
+
async queryFunction(tableName, context) {
|
|
2760
|
+
const {
|
|
2761
|
+
filter = {},
|
|
2762
|
+
fields = [],
|
|
2763
|
+
joins = {},
|
|
2764
|
+
sort = {},
|
|
2765
|
+
limit,
|
|
2766
|
+
offset
|
|
2767
|
+
} = context;
|
|
2768
|
+
let sql = `SELECT ${fields.length ? fields.join(", ") : "*"} FROM ${tableName}`;
|
|
2769
|
+
const params = [];
|
|
2770
|
+
if (Object.keys(filter).length > 0) {
|
|
2771
|
+
sql += " " + this.buildWhereClause(filter, params);
|
|
2772
|
+
}
|
|
2773
|
+
if (Object.keys(joins).length > 0) {
|
|
2774
|
+
sql += " " + this.buildJoinClause(joins);
|
|
2775
|
+
}
|
|
2776
|
+
if (Object.keys(sort).length > 0) {
|
|
2777
|
+
sql += " ORDER BY " + Object.entries(sort).map(([field, direction]) => `${field} ${direction}`).join(", ");
|
|
2778
|
+
}
|
|
2779
|
+
if (limit !== void 0) {
|
|
2780
|
+
sql += ` LIMIT $${params.length + 1}`;
|
|
2781
|
+
params.push(limit);
|
|
2782
|
+
}
|
|
2783
|
+
if (offset !== void 0) {
|
|
2784
|
+
sql += ` OFFSET $${params.length + 1}`;
|
|
2785
|
+
params.push(offset);
|
|
2786
|
+
}
|
|
2787
|
+
try {
|
|
2788
|
+
const result = await this.dbClient.query(sql, params);
|
|
2789
|
+
const rows = this.toCamelCase(result.rows);
|
|
2790
|
+
return {
|
|
2791
|
+
[`${(0, import_lodash_es.camelCase)(tableName)}s`]: rows,
|
|
2792
|
+
rowCount: result.rowCount,
|
|
2793
|
+
__success: true,
|
|
2794
|
+
...context
|
|
2795
|
+
};
|
|
2796
|
+
} catch (error) {
|
|
2797
|
+
return {
|
|
2798
|
+
...context,
|
|
2799
|
+
errored: true,
|
|
2800
|
+
__error: `Query failed: ${error.message}`
|
|
2801
|
+
};
|
|
2802
|
+
}
|
|
2834
2803
|
}
|
|
2835
|
-
insertFunction(tableName, context) {
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
(
|
|
2850
|
-
if (
|
|
2851
|
-
|
|
2852
|
-
return `${Object.keys(row)[i]} + 1`;
|
|
2853
|
-
}
|
|
2854
|
-
if (value.__effect === "decrement") {
|
|
2855
|
-
return `${Object.keys(row)[i]} - 1`;
|
|
2856
|
-
}
|
|
2857
|
-
if (value.__effect === "set") {
|
|
2858
|
-
return `${Object.keys(row)[i]} = ${value.__value}`;
|
|
2859
|
-
}
|
|
2804
|
+
async insertFunction(tableName, context) {
|
|
2805
|
+
const { data, transaction = true, fields = [], onConflict } = context;
|
|
2806
|
+
if (!data || Array.isArray(data) && data.length === 0) {
|
|
2807
|
+
return { errored: true, __error: "No data provided for insert" };
|
|
2808
|
+
}
|
|
2809
|
+
const client = transaction ? await this.getClient() : this.dbClient;
|
|
2810
|
+
try {
|
|
2811
|
+
if (transaction) await client.query("BEGIN");
|
|
2812
|
+
const resolvedData = await this.resolveNestedData(data, tableName);
|
|
2813
|
+
const isBatch = Array.isArray(resolvedData);
|
|
2814
|
+
const rows = isBatch ? resolvedData : [resolvedData];
|
|
2815
|
+
const sql = `INSERT INTO ${tableName} (${Object.keys(rows[0]).map(import_lodash_es.snakeCase).join(", ")}) VALUES `;
|
|
2816
|
+
const values = rows.map(
|
|
2817
|
+
(row) => `(${Object.values(row).map((value, i) => {
|
|
2818
|
+
if (typeof value === "object" && value?.__effect) {
|
|
2819
|
+
if (value.__effect === "increment") {
|
|
2820
|
+
return `${Object.keys(row)[i]} + 1`;
|
|
2860
2821
|
}
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
if (onConflict) {
|
|
2867
|
-
const { target, action } = onConflict;
|
|
2868
|
-
onConflictSql += ` ON CONFLICT (${target.join(", ")})`;
|
|
2869
|
-
if (action.do === "update") {
|
|
2870
|
-
if (!action.set || Object.keys(action.set).length === 0) {
|
|
2871
|
-
throw new Error("Update action requires 'set' fields");
|
|
2822
|
+
if (value.__effect === "decrement") {
|
|
2823
|
+
return `${Object.keys(row)[i]} - 1`;
|
|
2824
|
+
}
|
|
2825
|
+
if (value.__effect === "set") {
|
|
2826
|
+
return `${Object.keys(row)[i]} = ${value.__value}`;
|
|
2872
2827
|
}
|
|
2873
|
-
const setClauses = Object.entries(action.set).map(
|
|
2874
|
-
([field, value]) => `${field} = ${value === "excluded" ? "excluded." + field : `$${params.length + 1}`}`
|
|
2875
|
-
).join(", ");
|
|
2876
|
-
params.push(
|
|
2877
|
-
...Object.values(action.set).filter(
|
|
2878
|
-
(v) => typeof v !== "string" || !v.startsWith("excluded.")
|
|
2879
|
-
)
|
|
2880
|
-
);
|
|
2881
|
-
onConflictSql += ` DO UPDATE SET ${setClauses}`;
|
|
2882
|
-
if (action.where) onConflictSql += ` WHERE ${action.where}`;
|
|
2883
|
-
} else {
|
|
2884
|
-
onConflictSql += ` DO NOTHING`;
|
|
2885
2828
|
}
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
const
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2829
|
+
return `$${i + 1}`;
|
|
2830
|
+
}).join(", ")})`
|
|
2831
|
+
).join(", ");
|
|
2832
|
+
const params = rows.flatMap((row) => Object.values(row));
|
|
2833
|
+
let onConflictSql = "";
|
|
2834
|
+
if (onConflict) {
|
|
2835
|
+
const { target, action } = onConflict;
|
|
2836
|
+
onConflictSql += ` ON CONFLICT (${target.join(", ")})`;
|
|
2837
|
+
if (action.do === "update") {
|
|
2838
|
+
if (!action.set || Object.keys(action.set).length === 0) {
|
|
2839
|
+
throw new Error("Update action requires 'set' fields");
|
|
2840
|
+
}
|
|
2841
|
+
const setClauses = Object.entries(action.set).map(
|
|
2842
|
+
([field, value]) => `${field} = ${value === "excluded" ? "excluded." + field : `$${params.length + 1}`}`
|
|
2843
|
+
).join(", ");
|
|
2844
|
+
params.push(
|
|
2845
|
+
...Object.values(action.set).filter(
|
|
2846
|
+
(v) => typeof v !== "string" || !v.startsWith("excluded.")
|
|
2847
|
+
)
|
|
2848
|
+
);
|
|
2849
|
+
onConflictSql += ` DO UPDATE SET ${setClauses}`;
|
|
2850
|
+
if (action.where) onConflictSql += ` WHERE ${action.where}`;
|
|
2851
|
+
} else {
|
|
2852
|
+
onConflictSql += ` DO NOTHING`;
|
|
2907
2853
|
}
|
|
2908
2854
|
}
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2855
|
+
const result = await client.query(
|
|
2856
|
+
`${sql} ${values}${onConflictSql} RETURNING ${fields.length ? fields.join(", ") : "*"}`,
|
|
2857
|
+
params
|
|
2858
|
+
);
|
|
2859
|
+
if (transaction) await client.query("COMMIT");
|
|
2860
|
+
const resultRows = this.toCamelCase(result.rows);
|
|
2861
|
+
return {
|
|
2862
|
+
[`${(0, import_lodash_es.camelCase)(tableName)}${isBatch ? "s" : ""}`]: isBatch ? resultRows : resultRows[0],
|
|
2863
|
+
rowCount: result.rowCount,
|
|
2864
|
+
__inserted: true
|
|
2865
|
+
};
|
|
2866
|
+
} catch (error) {
|
|
2867
|
+
if (transaction) await client.query("ROLLBACK");
|
|
2868
|
+
return {
|
|
2869
|
+
...context,
|
|
2870
|
+
errored: true,
|
|
2871
|
+
__error: `Insert failed: ${error.message}`
|
|
2872
|
+
};
|
|
2873
|
+
} finally {
|
|
2874
|
+
if (transaction && client) {
|
|
2875
|
+
client.release();
|
|
2916
2876
|
}
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
return `${(0, import_lodash_es.snakeCase)(key)} =
|
|
2942
|
-
}).join(", ");
|
|
2943
|
-
const whereClause = this.buildWhereClause(filter, params);
|
|
2944
|
-
const sql = `UPDATE ${tableName} SET ${setClause} ${whereClause} RETURNING *;`;
|
|
2945
|
-
const result = yield client.query(sql, params);
|
|
2946
|
-
if (transaction) yield client.query("COMMIT");
|
|
2947
|
-
const rows = this.toCamelCase(result.rows);
|
|
2948
|
-
if (rows.length === 0) {
|
|
2949
|
-
return __spreadProps(__spreadValues({}, context), {
|
|
2950
|
-
__updated: false
|
|
2951
|
-
});
|
|
2877
|
+
}
|
|
2878
|
+
}
|
|
2879
|
+
async updateFunction(tableName, context) {
|
|
2880
|
+
const { data, filter = {}, transaction = true } = context;
|
|
2881
|
+
if (!data || Object.keys(data).length === 0) {
|
|
2882
|
+
return { errored: true, __error: "No data provided for update" };
|
|
2883
|
+
}
|
|
2884
|
+
const client = transaction ? await this.getClient() : this.dbClient;
|
|
2885
|
+
try {
|
|
2886
|
+
if (transaction) await client.query("BEGIN");
|
|
2887
|
+
const resolvedData = await this.resolveNestedData(data, tableName);
|
|
2888
|
+
const params = Object.values(resolvedData);
|
|
2889
|
+
let offset = 0;
|
|
2890
|
+
const setClause = Object.entries(Object.keys(resolvedData)).map(([i, key]) => {
|
|
2891
|
+
const value = resolvedData[key];
|
|
2892
|
+
const offsetIndex = parseInt(i) - offset;
|
|
2893
|
+
if (value.__effect === "increment") {
|
|
2894
|
+
params.splice(offsetIndex, 1);
|
|
2895
|
+
offset++;
|
|
2896
|
+
return `${(0, import_lodash_es.snakeCase)(key)} = ${(0, import_lodash_es.snakeCase)(key)} + 1`;
|
|
2897
|
+
}
|
|
2898
|
+
if (value.__effect === "decrement") {
|
|
2899
|
+
params.splice(offsetIndex, 1);
|
|
2900
|
+
offset++;
|
|
2901
|
+
return `${(0, import_lodash_es.snakeCase)(key)} = ${(0, import_lodash_es.snakeCase)(key)} - 1`;
|
|
2952
2902
|
}
|
|
2903
|
+
if (value.__effect === "set") {
|
|
2904
|
+
params.splice(offsetIndex, 1);
|
|
2905
|
+
offset++;
|
|
2906
|
+
return `${(0, import_lodash_es.snakeCase)(key)} = ${value.__value}`;
|
|
2907
|
+
}
|
|
2908
|
+
return `${(0, import_lodash_es.snakeCase)(key)} = $${offsetIndex + 1}`;
|
|
2909
|
+
}).join(", ");
|
|
2910
|
+
const whereClause = this.buildWhereClause(filter, params);
|
|
2911
|
+
const sql = `UPDATE ${tableName} SET ${setClause} ${whereClause} RETURNING *;`;
|
|
2912
|
+
const result = await client.query(sql, params);
|
|
2913
|
+
if (transaction) await client.query("COMMIT");
|
|
2914
|
+
const rows = this.toCamelCase(result.rows);
|
|
2915
|
+
if (rows.length === 0) {
|
|
2953
2916
|
return {
|
|
2954
|
-
|
|
2955
|
-
__updated:
|
|
2917
|
+
...context,
|
|
2918
|
+
__updated: false
|
|
2956
2919
|
};
|
|
2957
|
-
} catch (error) {
|
|
2958
|
-
if (transaction) yield client.query("ROLLBACK");
|
|
2959
|
-
return __spreadProps(__spreadValues({}, context), {
|
|
2960
|
-
errored: true,
|
|
2961
|
-
__error: `Update failed: ${error.message}`
|
|
2962
|
-
});
|
|
2963
|
-
} finally {
|
|
2964
|
-
if (transaction && client) {
|
|
2965
|
-
client.release();
|
|
2966
|
-
}
|
|
2967
2920
|
}
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
if (
|
|
2974
|
-
|
|
2921
|
+
return {
|
|
2922
|
+
[`${(0, import_lodash_es.camelCase)(tableName)}`]: rows[0],
|
|
2923
|
+
__updated: true
|
|
2924
|
+
};
|
|
2925
|
+
} catch (error) {
|
|
2926
|
+
if (transaction) await client.query("ROLLBACK");
|
|
2927
|
+
return {
|
|
2928
|
+
...context,
|
|
2929
|
+
errored: true,
|
|
2930
|
+
__error: `Update failed: ${error.message}`
|
|
2931
|
+
};
|
|
2932
|
+
} finally {
|
|
2933
|
+
if (transaction && client) {
|
|
2934
|
+
client.release();
|
|
2975
2935
|
}
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
2936
|
+
}
|
|
2937
|
+
}
|
|
2938
|
+
async deleteFunction(tableName, context) {
|
|
2939
|
+
const { filter = {}, transaction = true } = context;
|
|
2940
|
+
if (Object.keys(filter).length === 0) {
|
|
2941
|
+
return { errored: true, __error: "No filter provided for delete" };
|
|
2942
|
+
}
|
|
2943
|
+
const client = transaction ? await this.getClient() : this.dbClient;
|
|
2944
|
+
try {
|
|
2945
|
+
if (transaction) await client.query("BEGIN");
|
|
2946
|
+
const params = [];
|
|
2947
|
+
const whereClause = this.buildWhereClause(filter, params);
|
|
2948
|
+
const sql = `DELETE FROM ${tableName} ${whereClause} RETURNING *`;
|
|
2949
|
+
const result = await client.query(sql, params);
|
|
2950
|
+
if (transaction) await client.query("COMMIT");
|
|
2951
|
+
const rows = this.toCamelCase(result.rows);
|
|
2952
|
+
return {
|
|
2953
|
+
[`${(0, import_lodash_es.camelCase)(tableName)}`]: rows[0],
|
|
2954
|
+
__deleted: true
|
|
2955
|
+
};
|
|
2956
|
+
} catch (error) {
|
|
2957
|
+
if (transaction) await client.query("ROLLBACK");
|
|
2958
|
+
return {
|
|
2959
|
+
errored: true,
|
|
2960
|
+
__error: `Delete failed: ${error.message}`,
|
|
2961
|
+
__errors: { delete: error.message }
|
|
2962
|
+
};
|
|
2963
|
+
} finally {
|
|
2964
|
+
if (transaction && client) {
|
|
2965
|
+
client.release();
|
|
3000
2966
|
}
|
|
3001
|
-
}
|
|
2967
|
+
}
|
|
3002
2968
|
}
|
|
3003
2969
|
buildWhereClause(filter, params) {
|
|
3004
2970
|
const conditions = [];
|
|
@@ -3018,75 +2984,67 @@ var DatabaseController = class _DatabaseController {
|
|
|
3018
2984
|
}
|
|
3019
2985
|
return joinSql;
|
|
3020
2986
|
}
|
|
3021
|
-
resolveNestedData(data, tableName) {
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
} else if (typeof value === "object") {
|
|
3036
|
-
resolved[key] = yield this.resolveNestedData(value, tableName);
|
|
3037
|
-
}
|
|
2987
|
+
async resolveNestedData(data, tableName) {
|
|
2988
|
+
if (Array.isArray(data))
|
|
2989
|
+
return Promise.all(data.map((d) => this.resolveNestedData(d, tableName)));
|
|
2990
|
+
if (typeof data !== "object" || data === null) return data;
|
|
2991
|
+
const resolved = { ...data };
|
|
2992
|
+
for (const [key, value] of Object.entries(data)) {
|
|
2993
|
+
if (typeof value === "object" && value !== null && "subOperation" in value) {
|
|
2994
|
+
const subOp = value;
|
|
2995
|
+
const subResult = await this.executeSubOperation(subOp);
|
|
2996
|
+
resolved[key] = subResult[subOp.return || "full"] ?? subResult;
|
|
2997
|
+
} else if (typeof value === "string" && ["increment", "decrement", "set"].includes(value)) {
|
|
2998
|
+
resolved[key] = { __effect: value };
|
|
2999
|
+
} else if (typeof value === "object") {
|
|
3000
|
+
resolved[key] = await this.resolveNestedData(value, tableName);
|
|
3038
3001
|
}
|
|
3039
|
-
|
|
3040
|
-
|
|
3002
|
+
}
|
|
3003
|
+
return resolved;
|
|
3041
3004
|
}
|
|
3042
|
-
executeSubOperation(op) {
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
result =
|
|
3058
|
-
if (!result && op.return === "uuid") {
|
|
3059
|
-
result = resolvedData.uuid ? { uuid: resolvedData.uuid } : void 0;
|
|
3060
|
-
}
|
|
3061
|
-
} else if (op.subOperation === "query") {
|
|
3062
|
-
const params = [];
|
|
3063
|
-
const whereClause = this.buildWhereClause(op.filter || {}, params);
|
|
3064
|
-
const sql = `SELECT ${((_a2 = op.fields) == null ? void 0 : _a2.join(", ")) || "*"} FROM ${op.table} ${whereClause} LIMIT 1`;
|
|
3065
|
-
result = (yield client.query(sql, params)).rows[0];
|
|
3005
|
+
async executeSubOperation(op) {
|
|
3006
|
+
const client = await this.getClient();
|
|
3007
|
+
try {
|
|
3008
|
+
await client.query("BEGIN");
|
|
3009
|
+
let result;
|
|
3010
|
+
if (op.subOperation === "insert") {
|
|
3011
|
+
const resolvedData = await this.resolveNestedData(op.data, op.table);
|
|
3012
|
+
const sql = `INSERT INTO ${op.table} (${Object.keys(resolvedData).join(", ")}) VALUES (${Object.values(
|
|
3013
|
+
resolvedData
|
|
3014
|
+
).map((_, i) => `$${i + 1}`).join(
|
|
3015
|
+
", "
|
|
3016
|
+
)}) ON CONFLICT DO NOTHING RETURNING ${op.return === "uuid" ? "uuid" : "*"}`;
|
|
3017
|
+
result = await client.query(sql, Object.values(resolvedData));
|
|
3018
|
+
result = result.rows[0];
|
|
3019
|
+
if (!result && op.return === "uuid") {
|
|
3020
|
+
result = resolvedData.uuid ? { uuid: resolvedData.uuid } : void 0;
|
|
3066
3021
|
}
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
} finally {
|
|
3073
|
-
client.release();
|
|
3022
|
+
} else if (op.subOperation === "query") {
|
|
3023
|
+
const params = [];
|
|
3024
|
+
const whereClause = this.buildWhereClause(op.filter || {}, params);
|
|
3025
|
+
const sql = `SELECT ${op.fields?.join(", ") || "*"} FROM ${op.table} ${whereClause} LIMIT 1`;
|
|
3026
|
+
result = (await client.query(sql, params)).rows[0];
|
|
3074
3027
|
}
|
|
3075
|
-
|
|
3028
|
+
await client.query("COMMIT");
|
|
3029
|
+
return result || {};
|
|
3030
|
+
} catch (error) {
|
|
3031
|
+
await client.query("ROLLBACK");
|
|
3032
|
+
throw error;
|
|
3033
|
+
} finally {
|
|
3034
|
+
client.release();
|
|
3035
|
+
}
|
|
3076
3036
|
}
|
|
3077
3037
|
createDatabaseTask(op, tableName, table, queryFunction, options) {
|
|
3078
|
-
var _a2, _b2, _c2, _d2;
|
|
3079
3038
|
const opAction = op === "query" ? "queried" : op === "insert" ? "inserted" : op === "update" ? "updated" : op === "delete" ? "deleted" : "";
|
|
3080
3039
|
const defaultSignal = `${options.isMeta ? "meta." : ""}${tableName}.${opAction}`;
|
|
3081
3040
|
const tableNameFormatted = tableName.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
3082
3041
|
CadenzaService.createThrottledTask(
|
|
3083
3042
|
`db${op.charAt(0).toUpperCase() + op.slice(1)}${tableNameFormatted}`,
|
|
3084
|
-
(context, emit) =>
|
|
3085
|
-
|
|
3086
|
-
const triggerConditions = (_c3 = (_b3 = (_a3 = table.customSignals) == null ? void 0 : _a3.triggers) == null ? void 0 : _b3.query) == null ? void 0 : _c3.filter(
|
|
3043
|
+
async (context, emit) => {
|
|
3044
|
+
const triggerConditions = table.customSignals?.triggers?.query?.filter(
|
|
3087
3045
|
(trigger) => trigger.condition
|
|
3088
3046
|
);
|
|
3089
|
-
for (const triggerCondition of triggerConditions
|
|
3047
|
+
for (const triggerCondition of triggerConditions ?? []) {
|
|
3090
3048
|
if (triggerCondition.condition && !triggerCondition.condition(context)) {
|
|
3091
3049
|
return {
|
|
3092
3050
|
failed: true,
|
|
@@ -3094,13 +3052,13 @@ var DatabaseController = class _DatabaseController {
|
|
|
3094
3052
|
};
|
|
3095
3053
|
}
|
|
3096
3054
|
}
|
|
3097
|
-
context =
|
|
3055
|
+
context = await queryFunction(tableName, context.queryData ?? context);
|
|
3098
3056
|
if (!context.errored) {
|
|
3099
|
-
for (const signal of
|
|
3057
|
+
for (const signal of table.customSignals?.emissions?.[op] ?? []) {
|
|
3100
3058
|
if (signal.condition && !signal.condition(context)) {
|
|
3101
3059
|
continue;
|
|
3102
3060
|
}
|
|
3103
|
-
emit(
|
|
3061
|
+
emit(signal.signal ?? signal, context);
|
|
3104
3062
|
}
|
|
3105
3063
|
}
|
|
3106
3064
|
console.log(
|
|
@@ -3118,11 +3076,8 @@ var DatabaseController = class _DatabaseController {
|
|
|
3118
3076
|
delete context.limit;
|
|
3119
3077
|
delete context.offset;
|
|
3120
3078
|
return context;
|
|
3121
|
-
}),
|
|
3122
|
-
(context) => {
|
|
3123
|
-
var _a3, _b3, _c3;
|
|
3124
|
-
return (_c3 = (_b3 = (_a3 = context == null ? void 0 : context.__metadata) == null ? void 0 : _a3.__executionTraceId) != null ? _b3 : context == null ? void 0 : context.__executionTraceId) != null ? _c3 : "default";
|
|
3125
3079
|
},
|
|
3080
|
+
(context) => context?.__metadata?.__executionTraceId ?? context?.__executionTraceId ?? "default",
|
|
3126
3081
|
`Auto-generated ${op} task for ${tableName}`,
|
|
3127
3082
|
{
|
|
3128
3083
|
isMeta: options.isMeta,
|
|
@@ -3140,9 +3095,9 @@ var DatabaseController = class _DatabaseController {
|
|
|
3140
3095
|
}
|
|
3141
3096
|
}
|
|
3142
3097
|
).doOn(
|
|
3143
|
-
...
|
|
3098
|
+
...table.customSignals?.triggers?.[op]?.map((signal) => {
|
|
3144
3099
|
return typeof signal === "string" ? signal : signal.signal;
|
|
3145
|
-
})
|
|
3100
|
+
}) ?? []
|
|
3146
3101
|
).emits(defaultSignal);
|
|
3147
3102
|
}
|
|
3148
3103
|
};
|
|
@@ -3268,7 +3223,7 @@ var GraphSyncController = class _GraphSyncController {
|
|
|
3268
3223
|
taskName: task.name,
|
|
3269
3224
|
taskVersion: task.version,
|
|
3270
3225
|
taskServiceName: CadenzaService.serviceRegistry.serviceName,
|
|
3271
|
-
signalServiceName: signalServiceName
|
|
3226
|
+
signalServiceName: signalServiceName ?? CadenzaService.serviceRegistry.serviceName
|
|
3272
3227
|
}
|
|
3273
3228
|
});
|
|
3274
3229
|
});
|
|
@@ -3360,7 +3315,7 @@ var CadenzaService = class {
|
|
|
3360
3315
|
this.bootstrap();
|
|
3361
3316
|
this.validateName(routineName);
|
|
3362
3317
|
const name = `Deputy task for: ${routineName}`;
|
|
3363
|
-
options =
|
|
3318
|
+
options = {
|
|
3364
3319
|
concurrency: 0,
|
|
3365
3320
|
timeout: 0,
|
|
3366
3321
|
register: true,
|
|
@@ -3376,8 +3331,9 @@ var CadenzaService = class {
|
|
|
3376
3331
|
retryCount: 0,
|
|
3377
3332
|
retryDelay: 0,
|
|
3378
3333
|
retryDelayMax: 0,
|
|
3379
|
-
retryDelayFactor: 1
|
|
3380
|
-
|
|
3334
|
+
retryDelayFactor: 1,
|
|
3335
|
+
...options
|
|
3336
|
+
};
|
|
3381
3337
|
return new DeputyTask(
|
|
3382
3338
|
name,
|
|
3383
3339
|
routineName,
|
|
@@ -3431,7 +3387,7 @@ var CadenzaService = class {
|
|
|
3431
3387
|
this.bootstrap();
|
|
3432
3388
|
import_core3.default.validateName(signalName);
|
|
3433
3389
|
import_core3.default.validateName(serviceName);
|
|
3434
|
-
options =
|
|
3390
|
+
options = {
|
|
3435
3391
|
concurrency: 0,
|
|
3436
3392
|
timeout: 0,
|
|
3437
3393
|
register: true,
|
|
@@ -3447,8 +3403,9 @@ var CadenzaService = class {
|
|
|
3447
3403
|
retryCount: 1,
|
|
3448
3404
|
retryDelay: 0,
|
|
3449
3405
|
retryDelayMax: 0,
|
|
3450
|
-
retryDelayFactor: 1
|
|
3451
|
-
|
|
3406
|
+
retryDelayFactor: 1,
|
|
3407
|
+
...options
|
|
3408
|
+
};
|
|
3452
3409
|
options.isMeta = true;
|
|
3453
3410
|
const name = `Transmission of signal: ${signalName}`;
|
|
3454
3411
|
return new SignalTransmissionTask(
|
|
@@ -3479,10 +3436,10 @@ var CadenzaService = class {
|
|
|
3479
3436
|
import_core3.default.validateName(tableName);
|
|
3480
3437
|
import_core3.default.validateName(operation);
|
|
3481
3438
|
const tableNameFormatted = tableName.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
3482
|
-
const name = `${operation} ${tableName} in ${databaseServiceName
|
|
3483
|
-
const description = `Executes a ${operation} on table ${tableName} in ${databaseServiceName
|
|
3439
|
+
const name = `${operation} ${tableName} in ${databaseServiceName ?? "default database service"}`;
|
|
3440
|
+
const description = `Executes a ${operation} on table ${tableName} in ${databaseServiceName ?? "default database service"}`;
|
|
3484
3441
|
const taskName = `db${operation.charAt(0).toUpperCase() + operation.slice(1)}${tableNameFormatted}`;
|
|
3485
|
-
options =
|
|
3442
|
+
options = {
|
|
3486
3443
|
concurrency: 0,
|
|
3487
3444
|
timeout: 0,
|
|
3488
3445
|
register: true,
|
|
@@ -3498,8 +3455,9 @@ var CadenzaService = class {
|
|
|
3498
3455
|
retryCount: 3,
|
|
3499
3456
|
retryDelay: 100,
|
|
3500
3457
|
retryDelayMax: 0,
|
|
3501
|
-
retryDelayFactor: 1
|
|
3502
|
-
|
|
3458
|
+
retryDelayFactor: 1,
|
|
3459
|
+
...options
|
|
3460
|
+
};
|
|
3503
3461
|
return new DatabaseTask(
|
|
3504
3462
|
name,
|
|
3505
3463
|
taskName,
|
|
@@ -3581,40 +3539,40 @@ var CadenzaService = class {
|
|
|
3581
3539
|
* @throws Error if name invalid or duplicate.
|
|
3582
3540
|
*/
|
|
3583
3541
|
static createCadenzaService(serviceName, description = "", options = {}) {
|
|
3584
|
-
var _a2, _b2, _c2, _d2, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
3585
3542
|
if (this.serviceCreated) return;
|
|
3586
3543
|
this.bootstrap();
|
|
3587
3544
|
import_core3.default.validateName(serviceName);
|
|
3588
3545
|
this.validateServiceName(serviceName);
|
|
3589
|
-
const serviceId =
|
|
3546
|
+
const serviceId = options.customServiceId ?? (0, import_uuid3.v4)();
|
|
3590
3547
|
this.serviceRegistry.serviceName = serviceName;
|
|
3591
3548
|
this.serviceRegistry.serviceInstanceId = serviceId;
|
|
3592
|
-
options =
|
|
3549
|
+
options = {
|
|
3593
3550
|
loadBalance: true,
|
|
3594
3551
|
useSocket: true,
|
|
3595
3552
|
displayName: void 0,
|
|
3596
3553
|
isMeta: false,
|
|
3597
|
-
port: parseInt(
|
|
3598
|
-
securityProfile:
|
|
3599
|
-
networkMode:
|
|
3554
|
+
port: parseInt(process.env.HTTP_PORT ?? "3000"),
|
|
3555
|
+
securityProfile: process.env.SECURITY_PROFILE ?? "medium",
|
|
3556
|
+
networkMode: process.env.NETWORK_MODE ?? "dev",
|
|
3600
3557
|
retryCount: 3,
|
|
3601
3558
|
cadenzaDB: {
|
|
3602
3559
|
connect: true,
|
|
3603
|
-
address:
|
|
3604
|
-
port: parseInt(
|
|
3560
|
+
address: process.env.CADENZA_DB_ADDRESS ?? "localhost",
|
|
3561
|
+
port: parseInt(process.env.CADENZA_DB_PORT ?? "5000")
|
|
3605
3562
|
},
|
|
3606
3563
|
relatedServices: process.env.RELATED_SERVICES ? process.env.RELATED_SERVICES.split("|").map(
|
|
3607
3564
|
(s) => s.trim().split(",")
|
|
3608
|
-
) : []
|
|
3609
|
-
|
|
3610
|
-
|
|
3565
|
+
) : [],
|
|
3566
|
+
...options
|
|
3567
|
+
};
|
|
3568
|
+
if (options.cadenzaDB?.connect) {
|
|
3611
3569
|
import_core3.default.broker.emit("meta.initializing_service", {
|
|
3612
3570
|
// Seed the CadenzaDB
|
|
3613
3571
|
serviceInstance: {
|
|
3614
3572
|
uuid: "cadenza-db",
|
|
3615
3573
|
serviceName: "CadenzaDB",
|
|
3616
|
-
address:
|
|
3617
|
-
port:
|
|
3574
|
+
address: options.cadenzaDB?.address,
|
|
3575
|
+
port: options.cadenzaDB?.port,
|
|
3618
3576
|
exposed: options.networkMode !== "dev",
|
|
3619
3577
|
numberOfRunningGraphs: 0,
|
|
3620
3578
|
isActive: true,
|
|
@@ -3625,14 +3583,13 @@ var CadenzaService = class {
|
|
|
3625
3583
|
}
|
|
3626
3584
|
});
|
|
3627
3585
|
}
|
|
3628
|
-
|
|
3629
|
-
var _a3;
|
|
3586
|
+
options.relatedServices?.forEach((service) => {
|
|
3630
3587
|
import_core3.default.broker.emit("meta.initializing_service", {
|
|
3631
3588
|
serviceInstance: {
|
|
3632
3589
|
uuid: service[0],
|
|
3633
3590
|
serviceName: service[1],
|
|
3634
3591
|
address: service[2].split(":")[0],
|
|
3635
|
-
port:
|
|
3592
|
+
port: service[2].split(":")[1] ?? 3e3,
|
|
3636
3593
|
exposed: options.networkMode !== "dev",
|
|
3637
3594
|
numberOfRunningGraphs: 0,
|
|
3638
3595
|
isActive: true,
|
|
@@ -3648,7 +3605,7 @@ var CadenzaService = class {
|
|
|
3648
3605
|
data: {
|
|
3649
3606
|
name: serviceName,
|
|
3650
3607
|
description,
|
|
3651
|
-
displayName:
|
|
3608
|
+
displayName: options.displayName ?? "",
|
|
3652
3609
|
isMeta: options.isMeta
|
|
3653
3610
|
},
|
|
3654
3611
|
__serviceName: serviceName,
|
|
@@ -3659,12 +3616,12 @@ var CadenzaService = class {
|
|
|
3659
3616
|
__securityProfile: options.securityProfile,
|
|
3660
3617
|
__networkMode: options.networkMode,
|
|
3661
3618
|
__retryCount: options.retryCount,
|
|
3662
|
-
__cadenzaDBConnect:
|
|
3619
|
+
__cadenzaDBConnect: options.cadenzaDB?.connect
|
|
3663
3620
|
};
|
|
3664
|
-
if (
|
|
3665
|
-
import_core3.default.createEphemeralMetaTask("Create service", (_, emit) =>
|
|
3621
|
+
if (options.cadenzaDB?.connect) {
|
|
3622
|
+
import_core3.default.createEphemeralMetaTask("Create service", async (_, emit) => {
|
|
3666
3623
|
emit("meta.create_service_requested", initContext);
|
|
3667
|
-
})
|
|
3624
|
+
}).doOn("meta.fetch.handshake_complete");
|
|
3668
3625
|
} else {
|
|
3669
3626
|
import_core3.default.broker.emit("meta.create_service_requested", initContext);
|
|
3670
3627
|
}
|
|
@@ -3684,29 +3641,29 @@ var CadenzaService = class {
|
|
|
3684
3641
|
this.createCadenzaService(serviceName, description, options);
|
|
3685
3642
|
}
|
|
3686
3643
|
static createDatabaseService(name, schema, description = "", options = {}) {
|
|
3687
|
-
var _a2, _b2, _c2, _d2, _e, _f;
|
|
3688
3644
|
if (this.serviceCreated) return;
|
|
3689
3645
|
this.bootstrap();
|
|
3690
3646
|
this.serviceRegistry.serviceName = name;
|
|
3691
3647
|
DatabaseController.instance;
|
|
3692
|
-
options =
|
|
3648
|
+
options = {
|
|
3693
3649
|
loadBalance: true,
|
|
3694
3650
|
useSocket: true,
|
|
3695
3651
|
displayName: void 0,
|
|
3696
3652
|
isMeta: false,
|
|
3697
|
-
port: parseInt(
|
|
3698
|
-
securityProfile:
|
|
3699
|
-
networkMode:
|
|
3653
|
+
port: parseInt(process.env.HTTP_PORT ?? "3000"),
|
|
3654
|
+
securityProfile: process.env.SECURITY_PROFILE ?? "medium",
|
|
3655
|
+
networkMode: process.env.NETWORK_MODE ?? "dev",
|
|
3700
3656
|
retryCount: 3,
|
|
3701
3657
|
cadenzaDB: {
|
|
3702
3658
|
connect: true,
|
|
3703
|
-
address:
|
|
3704
|
-
port: parseInt(
|
|
3659
|
+
address: process.env.CADENZA_DB_ADDRESS ?? "localhost",
|
|
3660
|
+
port: parseInt(process.env.CADENZA_DB_PORT ?? "5000")
|
|
3705
3661
|
},
|
|
3706
3662
|
databaseType: "postgres",
|
|
3707
3663
|
databaseName: (0, import_lodash_es2.snakeCase)(name),
|
|
3708
|
-
poolSize: parseInt(
|
|
3709
|
-
|
|
3664
|
+
poolSize: parseInt(process.env.DATABASE_POOL_SIZE ?? "10"),
|
|
3665
|
+
...options
|
|
3666
|
+
};
|
|
3710
3667
|
import_core3.default.broker.emit("meta.database_init_requested", {
|
|
3711
3668
|
schema,
|
|
3712
3669
|
databaseName: options.databaseName,
|