@cadenza.io/service 1.15.3 → 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 +593 -627
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +593 -630
- 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,14 +990,14 @@ 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
|
);
|
|
1045
997
|
app.use((req, res, next) => {
|
|
1046
998
|
new import_rate_limiter_flexible.RateLimiterMemory({
|
|
1047
|
-
points:
|
|
1048
|
-
duration:
|
|
999
|
+
points: 1e4,
|
|
1000
|
+
duration: 10
|
|
1049
1001
|
}).consume(req.ip).then(() => next()).catch(
|
|
1050
1002
|
() => res.status(429).json({ error: "Too many requests" })
|
|
1051
1003
|
);
|
|
@@ -1068,14 +1020,14 @@ 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
|
})
|
|
1075
1027
|
);
|
|
1076
1028
|
app.use((req, res, next) => {
|
|
1077
1029
|
new import_rate_limiter_flexible.RateLimiterMemory({
|
|
1078
|
-
points:
|
|
1030
|
+
points: 1e3,
|
|
1079
1031
|
duration: 60,
|
|
1080
1032
|
blockDuration: 300
|
|
1081
1033
|
}).consume(req.ip).then(() => next()).catch((rej) => {
|
|
@@ -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,34 +1432,31 @@ 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("*")) {
|
|
1494
1450
|
return next(new Error("Unauthorized origin"));
|
|
1495
1451
|
}
|
|
1496
1452
|
const limiterOptions = {
|
|
1497
|
-
low: { points: Infinity, duration:
|
|
1498
|
-
medium: { points:
|
|
1499
|
-
high: { points:
|
|
1453
|
+
low: { points: Infinity, duration: 1 },
|
|
1454
|
+
medium: { points: 1e4, duration: 10 },
|
|
1455
|
+
high: { points: 1e3, duration: 60, blockDuration: 300 }
|
|
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,11 +1636,12 @@ 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
|
}
|
|
1689
1643
|
return new Promise((resolve, reject) => {
|
|
1644
|
+
delete ctx2.__isSubMeta;
|
|
1690
1645
|
console.log("Socket Delegate:", ctx2);
|
|
1691
1646
|
socket.timeout(1e4).emit(
|
|
1692
1647
|
"delegation",
|
|
@@ -1694,10 +1649,12 @@ var SocketController = class _SocketController {
|
|
|
1694
1649
|
(err, resultContext) => {
|
|
1695
1650
|
if (err) {
|
|
1696
1651
|
console.log("socket error:", err);
|
|
1697
|
-
resultContext =
|
|
1652
|
+
resultContext = {
|
|
1698
1653
|
__error: `Timeout error: ${err}`,
|
|
1699
|
-
errored: true
|
|
1700
|
-
|
|
1654
|
+
errored: true,
|
|
1655
|
+
...ctx2,
|
|
1656
|
+
...ctx2.__metadata
|
|
1657
|
+
};
|
|
1701
1658
|
emit(`meta.socket_client.delegate_failed`, resultContext);
|
|
1702
1659
|
resolve(resultContext);
|
|
1703
1660
|
return;
|
|
@@ -1706,30 +1663,34 @@ var SocketController = class _SocketController {
|
|
|
1706
1663
|
delete resultContext.__metadata;
|
|
1707
1664
|
emit(
|
|
1708
1665
|
`meta.socket_client.delegated:${ctx2.__metadata.__deputyExecId}`,
|
|
1709
|
-
|
|
1666
|
+
{
|
|
1667
|
+
...resultContext,
|
|
1668
|
+
...metadata
|
|
1669
|
+
}
|
|
1710
1670
|
);
|
|
1711
1671
|
resolve(resultContext);
|
|
1712
1672
|
}
|
|
1713
1673
|
);
|
|
1714
1674
|
});
|
|
1715
|
-
}
|
|
1675
|
+
},
|
|
1716
1676
|
`Delegate flow to service ${serviceName} with address ${URL}`
|
|
1717
1677
|
).doOn(`meta.service_registry.selected_instance_for_socket:${fetchId}`);
|
|
1718
1678
|
const transmitTask = CadenzaService.createMetaTask(
|
|
1719
1679
|
`Transmit signal to ${URL}`,
|
|
1720
|
-
(ctx2, emit) =>
|
|
1680
|
+
async (ctx2, emit) => {
|
|
1721
1681
|
if (ctx2.__signalName === void 0) {
|
|
1722
1682
|
return;
|
|
1723
1683
|
}
|
|
1724
1684
|
return new Promise((resolve, reject) => {
|
|
1725
|
-
|
|
1726
|
-
socket.timeout((_a2 = ctx2.__timeout) != null ? _a2 : 1e4).emit("signal", ctx2, (err, response) => {
|
|
1685
|
+
socket.timeout(ctx2.__timeout ?? 1e4).emit("signal", ctx2, (err, response) => {
|
|
1727
1686
|
if (err) {
|
|
1728
1687
|
console.log("socket error:", err);
|
|
1729
|
-
response =
|
|
1688
|
+
response = {
|
|
1730
1689
|
__error: `Timeout error: ${err}`,
|
|
1731
|
-
errored: true
|
|
1732
|
-
|
|
1690
|
+
errored: true,
|
|
1691
|
+
...ctx2,
|
|
1692
|
+
...ctx2.__metadata
|
|
1693
|
+
};
|
|
1733
1694
|
emit(
|
|
1734
1695
|
`meta.socket_client.signal_transmission_failed`,
|
|
1735
1696
|
response
|
|
@@ -1746,13 +1707,13 @@ var SocketController = class _SocketController {
|
|
|
1746
1707
|
resolve(response);
|
|
1747
1708
|
});
|
|
1748
1709
|
});
|
|
1749
|
-
}
|
|
1710
|
+
},
|
|
1750
1711
|
`Transmits signal to service ${serviceName} with address ${URL}`
|
|
1751
1712
|
).doOn(`meta.service_registry.selected_instance_for_socket:${fetchId}`);
|
|
1752
1713
|
CadenzaService.createEphemeralMetaTask(
|
|
1753
1714
|
`Shutdown SocketClient ${URL}`,
|
|
1754
1715
|
() => {
|
|
1755
|
-
socket
|
|
1716
|
+
socket?.close();
|
|
1756
1717
|
delegateTask.destroy();
|
|
1757
1718
|
transmitTask.destroy();
|
|
1758
1719
|
},
|
|
@@ -1855,7 +1816,6 @@ var SignalController = class _SignalController {
|
|
|
1855
1816
|
CadenzaService.createMetaTask(
|
|
1856
1817
|
"Add data to signal emission",
|
|
1857
1818
|
(ctx) => {
|
|
1858
|
-
var _a2;
|
|
1859
1819
|
const signalEmission = ctx.__signalEmission;
|
|
1860
1820
|
if (!signalEmission) {
|
|
1861
1821
|
return false;
|
|
@@ -1867,7 +1827,7 @@ var SignalController = class _SignalController {
|
|
|
1867
1827
|
task_version: signalEmission.taskVersion,
|
|
1868
1828
|
task_execution_id: signalEmission.taskExecutionId,
|
|
1869
1829
|
is_meta: signalEmission.isMeta,
|
|
1870
|
-
is_metric:
|
|
1830
|
+
is_metric: signalEmission.isMetric ?? false,
|
|
1871
1831
|
data: ctx,
|
|
1872
1832
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
1873
1833
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId
|
|
@@ -1881,10 +1841,11 @@ var SignalController = class _SignalController {
|
|
|
1881
1841
|
"Add metadata to signal consumption",
|
|
1882
1842
|
(ctx) => {
|
|
1883
1843
|
return {
|
|
1884
|
-
data:
|
|
1844
|
+
data: {
|
|
1845
|
+
...ctx.data,
|
|
1885
1846
|
serviceName: CadenzaService.serviceRegistry.serviceName,
|
|
1886
1847
|
serviceInstanceId: CadenzaService.serviceRegistry.serviceInstanceId
|
|
1887
|
-
}
|
|
1848
|
+
}
|
|
1888
1849
|
};
|
|
1889
1850
|
},
|
|
1890
1851
|
"",
|
|
@@ -1902,7 +1863,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1902
1863
|
constructor() {
|
|
1903
1864
|
CadenzaService.createMetaTask("Handle task creation", (ctx) => {
|
|
1904
1865
|
return {
|
|
1905
|
-
data:
|
|
1866
|
+
data: {
|
|
1867
|
+
...ctx.data,
|
|
1906
1868
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1907
1869
|
// input_context_schema_id: ctx.data.inputContextSchema ? { // TODO
|
|
1908
1870
|
//
|
|
@@ -1910,21 +1872,24 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1910
1872
|
// output_context_schema_id: ctx.data.outputContextSchema ? {
|
|
1911
1873
|
//
|
|
1912
1874
|
// } : null,
|
|
1913
|
-
}
|
|
1875
|
+
}
|
|
1914
1876
|
};
|
|
1915
1877
|
}).doOn("meta.task.created").emits("meta.graph_metadata.task_created");
|
|
1916
1878
|
CadenzaService.createMetaTask("Handle task update", (ctx) => {
|
|
1917
|
-
return
|
|
1918
|
-
|
|
1879
|
+
return {
|
|
1880
|
+
...ctx,
|
|
1881
|
+
filter: {
|
|
1882
|
+
...ctx.filter,
|
|
1919
1883
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1884
|
+
}
|
|
1885
|
+
};
|
|
1922
1886
|
}).doOn("meta.task.layer_index_changed", "meta.task.destroyed").emits("meta.graph_metadata.task_updated");
|
|
1923
1887
|
CadenzaService.createMetaTask("Handle task relationship creation", (ctx) => {
|
|
1924
1888
|
return {
|
|
1925
|
-
data:
|
|
1889
|
+
data: {
|
|
1890
|
+
...ctx.data,
|
|
1926
1891
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1927
|
-
}
|
|
1892
|
+
}
|
|
1928
1893
|
};
|
|
1929
1894
|
}).doOn("meta.task.relationship_added").emits("meta.graph_metadata.task_relationship_created");
|
|
1930
1895
|
CadenzaService.createMetaTask("Handle task signal observation", (ctx) => {
|
|
@@ -1936,18 +1901,20 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1936
1901
|
_signal = ctx.data.signalName.split(".").slice(1).join(".");
|
|
1937
1902
|
}
|
|
1938
1903
|
return {
|
|
1939
|
-
data:
|
|
1904
|
+
data: {
|
|
1905
|
+
...ctx.data,
|
|
1940
1906
|
signalName: _signal,
|
|
1941
1907
|
taskServiceName: CadenzaService.serviceRegistry.serviceName,
|
|
1942
|
-
signalServiceName: signalServiceName
|
|
1943
|
-
}
|
|
1908
|
+
signalServiceName: signalServiceName ?? CadenzaService.serviceRegistry.serviceName
|
|
1909
|
+
}
|
|
1944
1910
|
};
|
|
1945
1911
|
}).doOn("meta.task.observed_signal").emits("meta.graph_metadata.task_signal_observed");
|
|
1946
1912
|
CadenzaService.createMetaTask("Handle task signal attachment", (ctx) => {
|
|
1947
1913
|
return {
|
|
1948
|
-
data:
|
|
1914
|
+
data: {
|
|
1915
|
+
...ctx.data,
|
|
1949
1916
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1950
|
-
}
|
|
1917
|
+
}
|
|
1951
1918
|
};
|
|
1952
1919
|
}).doOn("meta.task.attached_signal").emits("meta.graph_metadata.task_attached_signal");
|
|
1953
1920
|
CadenzaService.createMetaTask("Handle task unsubscribing signal", (ctx) => {
|
|
@@ -1955,9 +1922,10 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1955
1922
|
data: {
|
|
1956
1923
|
deleted: true
|
|
1957
1924
|
},
|
|
1958
|
-
filter:
|
|
1925
|
+
filter: {
|
|
1926
|
+
...ctx.filter,
|
|
1959
1927
|
task_service_name: CadenzaService.serviceRegistry.serviceName
|
|
1960
|
-
}
|
|
1928
|
+
}
|
|
1961
1929
|
};
|
|
1962
1930
|
}).doOn("meta.task.unsubscribed_signal").emits("meta.graph_metadata.task_unsubscribed_signal");
|
|
1963
1931
|
CadenzaService.createMetaTask("Handle task detaching signal", (ctx) => {
|
|
@@ -1965,37 +1933,43 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
1965
1933
|
data: {
|
|
1966
1934
|
deleted: true
|
|
1967
1935
|
},
|
|
1968
|
-
filter:
|
|
1936
|
+
filter: {
|
|
1937
|
+
...ctx.filter,
|
|
1969
1938
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1970
|
-
}
|
|
1939
|
+
}
|
|
1971
1940
|
};
|
|
1972
1941
|
}).doOn("meta.task.detached_signal").emits("meta.graph_metadata.task_detached_signal");
|
|
1973
1942
|
CadenzaService.createMetaTask("Handle routine creation", (ctx) => {
|
|
1974
1943
|
return {
|
|
1975
|
-
data:
|
|
1944
|
+
data: {
|
|
1945
|
+
...ctx.data,
|
|
1976
1946
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1977
|
-
}
|
|
1947
|
+
}
|
|
1978
1948
|
};
|
|
1979
1949
|
}).doAfter(CadenzaService.registry.registerRoutine).emits("meta.graph_metadata.routine_created");
|
|
1980
1950
|
CadenzaService.createMetaTask("Handle routine update", (ctx) => {
|
|
1981
|
-
return
|
|
1982
|
-
|
|
1951
|
+
return {
|
|
1952
|
+
...ctx,
|
|
1953
|
+
filter: {
|
|
1954
|
+
...ctx.filter,
|
|
1983
1955
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1986
1958
|
}).doOn("meta.routine.destroyed").emits("meta.graph_metadata.routine_updated");
|
|
1987
1959
|
CadenzaService.createMetaTask("Handle adding task to routine", (ctx) => {
|
|
1988
1960
|
return {
|
|
1989
|
-
data:
|
|
1961
|
+
data: {
|
|
1962
|
+
...ctx.data,
|
|
1990
1963
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
1991
|
-
}
|
|
1964
|
+
}
|
|
1992
1965
|
};
|
|
1993
1966
|
}).doOn("meta.routine.task_added").emits("meta.graph_metadata.task_added_to_routine");
|
|
1994
1967
|
CadenzaService.createMetaTask("Handle new trace", (ctx) => {
|
|
1995
1968
|
const context = ctx.data.context;
|
|
1996
1969
|
delete ctx.data.context;
|
|
1997
1970
|
return {
|
|
1998
|
-
data:
|
|
1971
|
+
data: {
|
|
1972
|
+
...ctx.data,
|
|
1999
1973
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2000
1974
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2001
1975
|
context_id: {
|
|
@@ -2008,7 +1982,7 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2008
1982
|
},
|
|
2009
1983
|
return: "uuid"
|
|
2010
1984
|
}
|
|
2011
|
-
}
|
|
1985
|
+
}
|
|
2012
1986
|
};
|
|
2013
1987
|
}).doOn("meta.runner.new_trace").emits("meta.graph_metadata.execution_trace_created");
|
|
2014
1988
|
CadenzaService.createMetaTask(
|
|
@@ -2020,7 +1994,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2020
1994
|
}
|
|
2021
1995
|
delete ctx.data.context;
|
|
2022
1996
|
return {
|
|
2023
|
-
data:
|
|
1997
|
+
data: {
|
|
1998
|
+
...ctx.data,
|
|
2024
1999
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2025
2000
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2026
2001
|
context_id: typeof context === "string" ? context : {
|
|
@@ -2033,7 +2008,7 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2033
2008
|
},
|
|
2034
2009
|
return: "uuid"
|
|
2035
2010
|
}
|
|
2036
|
-
}
|
|
2011
|
+
}
|
|
2037
2012
|
};
|
|
2038
2013
|
},
|
|
2039
2014
|
"Handles routine execution creation",
|
|
@@ -2053,7 +2028,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2053
2028
|
const context = ctx.data.resultContext;
|
|
2054
2029
|
delete ctx.data.resultContext;
|
|
2055
2030
|
return {
|
|
2056
|
-
data:
|
|
2031
|
+
data: {
|
|
2032
|
+
...ctx.data,
|
|
2057
2033
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2058
2034
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2059
2035
|
result_context_id: typeof context === "string" ? context : {
|
|
@@ -2066,8 +2042,10 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2066
2042
|
},
|
|
2067
2043
|
return: "uuid"
|
|
2068
2044
|
}
|
|
2069
|
-
}
|
|
2070
|
-
filter:
|
|
2045
|
+
},
|
|
2046
|
+
filter: {
|
|
2047
|
+
...ctx.filter
|
|
2048
|
+
}
|
|
2071
2049
|
};
|
|
2072
2050
|
},
|
|
2073
2051
|
"Handles routine execution ended",
|
|
@@ -2082,7 +2060,8 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2082
2060
|
console.log("Handle task execution creation", context);
|
|
2083
2061
|
}
|
|
2084
2062
|
return {
|
|
2085
|
-
data:
|
|
2063
|
+
data: {
|
|
2064
|
+
...ctx.data,
|
|
2086
2065
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2087
2066
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2088
2067
|
context_id: typeof context === "string" ? context : {
|
|
@@ -2095,7 +2074,7 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2095
2074
|
},
|
|
2096
2075
|
return: "uuid"
|
|
2097
2076
|
}
|
|
2098
|
-
}
|
|
2077
|
+
}
|
|
2099
2078
|
};
|
|
2100
2079
|
},
|
|
2101
2080
|
"Handles task execution creation",
|
|
@@ -2120,12 +2099,12 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2120
2099
|
CadenzaService.createMetaTask(
|
|
2121
2100
|
"Handle task execution ended",
|
|
2122
2101
|
(ctx) => {
|
|
2123
|
-
var _a2;
|
|
2124
2102
|
const context = ctx.data.resultContext;
|
|
2125
2103
|
console.log("Handle task execution ended", context);
|
|
2126
2104
|
delete ctx.data.resultContext;
|
|
2127
2105
|
return {
|
|
2128
|
-
data:
|
|
2106
|
+
data: {
|
|
2107
|
+
...ctx.data,
|
|
2129
2108
|
service_name: CadenzaService.serviceRegistry.serviceName,
|
|
2130
2109
|
service_instance_id: CadenzaService.serviceRegistry.serviceInstanceId,
|
|
2131
2110
|
result_context_id: typeof context === "string" ? context : {
|
|
@@ -2134,12 +2113,14 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2134
2113
|
data: {
|
|
2135
2114
|
uuid: context.id,
|
|
2136
2115
|
context: context.context,
|
|
2137
|
-
is_meta:
|
|
2116
|
+
is_meta: ctx.data.isMeta ?? false
|
|
2138
2117
|
},
|
|
2139
2118
|
return: "uuid"
|
|
2140
2119
|
}
|
|
2141
|
-
}
|
|
2142
|
-
filter:
|
|
2120
|
+
},
|
|
2121
|
+
filter: {
|
|
2122
|
+
...ctx.filter
|
|
2123
|
+
}
|
|
2143
2124
|
};
|
|
2144
2125
|
},
|
|
2145
2126
|
"Handles task execution ended",
|
|
@@ -2152,9 +2133,10 @@ var GraphMetadataController = class _GraphMetadataController {
|
|
|
2152
2133
|
data: {
|
|
2153
2134
|
executionCount: "increment"
|
|
2154
2135
|
},
|
|
2155
|
-
filter:
|
|
2136
|
+
filter: {
|
|
2137
|
+
...ctx.filter,
|
|
2156
2138
|
service_name: CadenzaService.serviceRegistry.serviceName
|
|
2157
|
-
}
|
|
2139
|
+
}
|
|
2158
2140
|
};
|
|
2159
2141
|
},
|
|
2160
2142
|
"Handles task execution relationship creation",
|
|
@@ -2188,15 +2170,14 @@ var SCHEMA_TYPES = [
|
|
|
2188
2170
|
// src/database/DatabaseController.ts
|
|
2189
2171
|
var import_pg = require("pg");
|
|
2190
2172
|
var import_lodash_es = require("lodash-es");
|
|
2191
|
-
var _a, _b, _c, _d;
|
|
2192
2173
|
var DatabaseController = class _DatabaseController {
|
|
2193
2174
|
constructor() {
|
|
2194
2175
|
this.dbClient = new import_pg.Pool({
|
|
2195
|
-
user:
|
|
2196
|
-
host:
|
|
2197
|
-
port: parseInt(
|
|
2176
|
+
user: process.env.DATABASE_USER ?? "postgres",
|
|
2177
|
+
host: process.env.DATABASE_ADDRESS ?? "localhost",
|
|
2178
|
+
port: parseInt(process.env.DATABASE_PORT ?? "5432"),
|
|
2198
2179
|
database: "postgres",
|
|
2199
|
-
password:
|
|
2180
|
+
password: process.env.DATABASE_PASSWORD ?? "03gibnEF"
|
|
2200
2181
|
});
|
|
2201
2182
|
CadenzaService.createMetaRoutine(
|
|
2202
2183
|
"DatabaseServiceInit",
|
|
@@ -2206,8 +2187,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2206
2187
|
// Create schema version table
|
|
2207
2188
|
CadenzaService.createMetaTask(
|
|
2208
2189
|
"Create database",
|
|
2209
|
-
(ctx) =>
|
|
2210
|
-
var _a2, _b2, _c2, _d2;
|
|
2190
|
+
async (ctx) => {
|
|
2211
2191
|
const { databaseName } = ctx;
|
|
2212
2192
|
try {
|
|
2213
2193
|
if (!databaseName.split("").every((c) => /[a-z_]/.test(c))) {
|
|
@@ -2215,14 +2195,14 @@ var DatabaseController = class _DatabaseController {
|
|
|
2215
2195
|
`Invalid database name ${databaseName}. Names must only contain lowercase alphanumeric characters and underscores`
|
|
2216
2196
|
);
|
|
2217
2197
|
}
|
|
2218
|
-
|
|
2198
|
+
await this.dbClient.query(`CREATE DATABASE ${databaseName}`);
|
|
2219
2199
|
console.log(`Database ${databaseName} created`);
|
|
2220
2200
|
this.dbClient = new import_pg.Pool({
|
|
2221
|
-
user:
|
|
2222
|
-
host:
|
|
2223
|
-
port: parseInt(
|
|
2201
|
+
user: process.env.DATABASE_USER ?? "postgres",
|
|
2202
|
+
host: process.env.DATABASE_ADDRESS ?? "localhost",
|
|
2203
|
+
port: parseInt(process.env.DATABASE_PORT ?? "5432"),
|
|
2224
2204
|
database: databaseName,
|
|
2225
|
-
password:
|
|
2205
|
+
password: process.env.DATABASE_PASSWORD ?? "03gibnEF"
|
|
2226
2206
|
});
|
|
2227
2207
|
return true;
|
|
2228
2208
|
} catch (error) {
|
|
@@ -2232,15 +2212,14 @@ var DatabaseController = class _DatabaseController {
|
|
|
2232
2212
|
}
|
|
2233
2213
|
throw new Error(`Failed to create database: ${error.message}`);
|
|
2234
2214
|
}
|
|
2235
|
-
}
|
|
2215
|
+
},
|
|
2236
2216
|
"Creates the target database if it doesn't exist"
|
|
2237
2217
|
).then(
|
|
2238
2218
|
CadenzaService.createMetaTask(
|
|
2239
2219
|
"Validate schema",
|
|
2240
2220
|
(ctx) => {
|
|
2241
|
-
var _a2, _b2;
|
|
2242
2221
|
const { schema } = ctx;
|
|
2243
|
-
if (!
|
|
2222
|
+
if (!schema?.tables || typeof schema.tables !== "object") {
|
|
2244
2223
|
throw new Error("Invalid schema: missing or invalid tables");
|
|
2245
2224
|
}
|
|
2246
2225
|
for (const [tableName, table] of Object.entries(schema.tables)) {
|
|
@@ -2279,8 +2258,8 @@ var DatabaseController = class _DatabaseController {
|
|
|
2279
2258
|
}
|
|
2280
2259
|
if (table.customSignals) {
|
|
2281
2260
|
for (const op of ["query", "insert", "update", "delete"]) {
|
|
2282
|
-
const triggers =
|
|
2283
|
-
const emissions =
|
|
2261
|
+
const triggers = table.customSignals.triggers?.[op];
|
|
2262
|
+
const emissions = table.customSignals.emissions?.[op];
|
|
2284
2263
|
if (triggers && !Array.isArray(triggers) && typeof triggers !== "object") {
|
|
2285
2264
|
console.log(
|
|
2286
2265
|
tableName,
|
|
@@ -2323,7 +2302,6 @@ var DatabaseController = class _DatabaseController {
|
|
|
2323
2302
|
"Generates DDL for database schema"
|
|
2324
2303
|
).then(
|
|
2325
2304
|
CadenzaService.createMetaTask("Generate DDL from table", (ctx) => {
|
|
2326
|
-
var _a2;
|
|
2327
2305
|
const {
|
|
2328
2306
|
ddl,
|
|
2329
2307
|
table,
|
|
@@ -2333,13 +2311,12 @@ var DatabaseController = class _DatabaseController {
|
|
|
2333
2311
|
sortedTables
|
|
2334
2312
|
} = ctx;
|
|
2335
2313
|
const fieldDefs = Object.entries(table.fields).map((value) => {
|
|
2336
|
-
var _a3, _b2, _c2, _d2, _e, _f, _g;
|
|
2337
2314
|
const [fieldName, field] = value;
|
|
2338
2315
|
let def = `${fieldName} ${field.type.toUpperCase()}`;
|
|
2339
2316
|
if (field.type === "varchar")
|
|
2340
|
-
def += `(${
|
|
2317
|
+
def += `(${field.constraints?.maxLength ?? 255})`;
|
|
2341
2318
|
if (field.type === "decimal")
|
|
2342
|
-
def += `(${
|
|
2319
|
+
def += `(${field.constraints?.precision ?? 10},${field.constraints?.scale ?? 2})`;
|
|
2343
2320
|
if (field.primary) def += " PRIMARY KEY";
|
|
2344
2321
|
if (field.unique) def += " UNIQUE";
|
|
2345
2322
|
if (field.default !== void 0)
|
|
@@ -2351,12 +2328,12 @@ var DatabaseController = class _DatabaseController {
|
|
|
2351
2328
|
if (field.references)
|
|
2352
2329
|
def += ` REFERENCES ${field.references} ON DELETE ${field.onDelete || "DO NOTHING"}`;
|
|
2353
2330
|
if (field.encrypted) def += " ENCRYPTED";
|
|
2354
|
-
if (
|
|
2331
|
+
if (field.constraints?.check) {
|
|
2355
2332
|
def += ` CHECK (${field.constraints.check})`;
|
|
2356
2333
|
}
|
|
2357
2334
|
return def;
|
|
2358
2335
|
}).join(", ");
|
|
2359
|
-
if (
|
|
2336
|
+
if (schema.meta?.dropExisting) {
|
|
2360
2337
|
this.dbClient.query(
|
|
2361
2338
|
`DROP TABLE IF EXISTS ${tableName} CASCADE;`
|
|
2362
2339
|
);
|
|
@@ -2575,13 +2552,13 @@ var DatabaseController = class _DatabaseController {
|
|
|
2575
2552
|
).then(
|
|
2576
2553
|
CadenzaService.createMetaTask(
|
|
2577
2554
|
"Apply Database Changes",
|
|
2578
|
-
(ctx) =>
|
|
2555
|
+
async (ctx) => {
|
|
2579
2556
|
const { ddl } = ctx;
|
|
2580
2557
|
if (ddl && ddl.length > 0) {
|
|
2581
2558
|
for (const sql of ddl) {
|
|
2582
2559
|
try {
|
|
2583
2560
|
console.log("Applying SQL", sql);
|
|
2584
|
-
|
|
2561
|
+
await this.dbClient.query(sql);
|
|
2585
2562
|
} catch (error) {
|
|
2586
2563
|
console.error(
|
|
2587
2564
|
"Error applying DDL",
|
|
@@ -2592,7 +2569,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2592
2569
|
}
|
|
2593
2570
|
console.log("DDL applied");
|
|
2594
2571
|
return true;
|
|
2595
|
-
}
|
|
2572
|
+
},
|
|
2596
2573
|
"Applies generated DDL to the database"
|
|
2597
2574
|
).then(
|
|
2598
2575
|
CadenzaService.createMetaTask(
|
|
@@ -2670,50 +2647,45 @@ var DatabaseController = class _DatabaseController {
|
|
|
2670
2647
|
reset() {
|
|
2671
2648
|
this.dbClient.end();
|
|
2672
2649
|
}
|
|
2673
|
-
getClient() {
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
client.
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
return client;
|
|
2695
|
-
});
|
|
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;
|
|
2696
2671
|
}
|
|
2697
|
-
waitForDatabase(transaction, client, context) {
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
return { rows: [] };
|
|
2709
|
-
}
|
|
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: [] };
|
|
2710
2683
|
}
|
|
2711
2684
|
}
|
|
2712
|
-
|
|
2713
|
-
|
|
2685
|
+
}
|
|
2686
|
+
throw new Error(`Timeout waiting for database to be ready`);
|
|
2714
2687
|
}
|
|
2715
2688
|
sortTablesByReferences(ctx) {
|
|
2716
|
-
var _a2, _b2;
|
|
2717
2689
|
const schema = ctx.schema;
|
|
2718
2690
|
const graph = /* @__PURE__ */ new Map();
|
|
2719
2691
|
const allTables = Object.keys(schema.tables);
|
|
@@ -2723,7 +2695,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2723
2695
|
if (field.references) {
|
|
2724
2696
|
const [refTable] = field.references.split("(");
|
|
2725
2697
|
if (refTable !== tableName && allTables.includes(refTable)) {
|
|
2726
|
-
|
|
2698
|
+
graph.get(refTable)?.add(tableName);
|
|
2727
2699
|
}
|
|
2728
2700
|
}
|
|
2729
2701
|
}
|
|
@@ -2731,7 +2703,7 @@ var DatabaseController = class _DatabaseController {
|
|
|
2731
2703
|
for (const foreignKey of table.foreignKeys) {
|
|
2732
2704
|
const refTable = foreignKey.tableName;
|
|
2733
2705
|
if (refTable !== tableName && allTables.includes(refTable)) {
|
|
2734
|
-
|
|
2706
|
+
graph.get(refTable)?.add(tableName);
|
|
2735
2707
|
}
|
|
2736
2708
|
}
|
|
2737
2709
|
}
|
|
@@ -2766,16 +2738,14 @@ var DatabaseController = class _DatabaseController {
|
|
|
2766
2738
|
}
|
|
2767
2739
|
sorted.reverse();
|
|
2768
2740
|
console.log("sorted tables", sorted, "has cycles", hasCycles);
|
|
2769
|
-
return
|
|
2770
|
-
}
|
|
2771
|
-
splitTables(ctx) {
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
}
|
|
2778
|
-
});
|
|
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
|
+
}
|
|
2779
2749
|
}
|
|
2780
2750
|
toCamelCase(rows) {
|
|
2781
2751
|
return rows.map((row) => {
|
|
@@ -2786,218 +2756,215 @@ var DatabaseController = class _DatabaseController {
|
|
|
2786
2756
|
return camelCasedRow;
|
|
2787
2757
|
});
|
|
2788
2758
|
}
|
|
2789
|
-
queryFunction(tableName, context) {
|
|
2790
|
-
|
|
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
|
-
}
|
|
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
|
+
}
|
|
2833
2803
|
}
|
|
2834
|
-
insertFunction(tableName, context) {
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
(
|
|
2849
|
-
if (
|
|
2850
|
-
|
|
2851
|
-
return `${Object.keys(row)[i]} + 1`;
|
|
2852
|
-
}
|
|
2853
|
-
if (value.__effect === "decrement") {
|
|
2854
|
-
return `${Object.keys(row)[i]} - 1`;
|
|
2855
|
-
}
|
|
2856
|
-
if (value.__effect === "set") {
|
|
2857
|
-
return `${Object.keys(row)[i]} = ${value.__value}`;
|
|
2858
|
-
}
|
|
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`;
|
|
2859
2821
|
}
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
if (onConflict) {
|
|
2866
|
-
const { target, action } = onConflict;
|
|
2867
|
-
onConflictSql += ` ON CONFLICT (${target.join(", ")})`;
|
|
2868
|
-
if (action.do === "update") {
|
|
2869
|
-
if (!action.set || Object.keys(action.set).length === 0) {
|
|
2870
|
-
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}`;
|
|
2871
2827
|
}
|
|
2872
|
-
const setClauses = Object.entries(action.set).map(
|
|
2873
|
-
([field, value]) => `${field} = ${value === "excluded" ? "excluded." + field : `$${params.length + 1}`}`
|
|
2874
|
-
).join(", ");
|
|
2875
|
-
params.push(
|
|
2876
|
-
...Object.values(action.set).filter(
|
|
2877
|
-
(v) => typeof v !== "string" || !v.startsWith("excluded.")
|
|
2878
|
-
)
|
|
2879
|
-
);
|
|
2880
|
-
onConflictSql += ` DO UPDATE SET ${setClauses}`;
|
|
2881
|
-
if (action.where) onConflictSql += ` WHERE ${action.where}`;
|
|
2882
|
-
} else {
|
|
2883
|
-
onConflictSql += ` DO NOTHING`;
|
|
2884
2828
|
}
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
const
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
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`;
|
|
2906
2853
|
}
|
|
2907
2854
|
}
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
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();
|
|
2915
2876
|
}
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
return `${(0, import_lodash_es.snakeCase)(key)} =
|
|
2941
|
-
}).join(", ");
|
|
2942
|
-
const whereClause = this.buildWhereClause(filter, params);
|
|
2943
|
-
const sql = `UPDATE ${tableName} SET ${setClause} ${whereClause} RETURNING *;`;
|
|
2944
|
-
const result = yield client.query(sql, params);
|
|
2945
|
-
if (transaction) yield client.query("COMMIT");
|
|
2946
|
-
const rows = this.toCamelCase(result.rows);
|
|
2947
|
-
if (rows.length === 0) {
|
|
2948
|
-
return __spreadProps(__spreadValues({}, context), {
|
|
2949
|
-
__updated: false
|
|
2950
|
-
});
|
|
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`;
|
|
2951
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) {
|
|
2952
2916
|
return {
|
|
2953
|
-
|
|
2954
|
-
__updated:
|
|
2917
|
+
...context,
|
|
2918
|
+
__updated: false
|
|
2955
2919
|
};
|
|
2956
|
-
} catch (error) {
|
|
2957
|
-
if (transaction) yield client.query("ROLLBACK");
|
|
2958
|
-
return __spreadProps(__spreadValues({}, context), {
|
|
2959
|
-
errored: true,
|
|
2960
|
-
__error: `Update failed: ${error.message}`
|
|
2961
|
-
});
|
|
2962
|
-
} finally {
|
|
2963
|
-
if (transaction && client) {
|
|
2964
|
-
client.release();
|
|
2965
|
-
}
|
|
2966
2920
|
}
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
if (
|
|
2973
|
-
|
|
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();
|
|
2974
2935
|
}
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
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();
|
|
2999
2966
|
}
|
|
3000
|
-
}
|
|
2967
|
+
}
|
|
3001
2968
|
}
|
|
3002
2969
|
buildWhereClause(filter, params) {
|
|
3003
2970
|
const conditions = [];
|
|
@@ -3017,75 +2984,67 @@ var DatabaseController = class _DatabaseController {
|
|
|
3017
2984
|
}
|
|
3018
2985
|
return joinSql;
|
|
3019
2986
|
}
|
|
3020
|
-
resolveNestedData(data, tableName) {
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
} else if (typeof value === "object") {
|
|
3035
|
-
resolved[key] = yield this.resolveNestedData(value, tableName);
|
|
3036
|
-
}
|
|
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);
|
|
3037
3001
|
}
|
|
3038
|
-
|
|
3039
|
-
|
|
3002
|
+
}
|
|
3003
|
+
return resolved;
|
|
3040
3004
|
}
|
|
3041
|
-
executeSubOperation(op) {
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
result =
|
|
3057
|
-
if (!result && op.return === "uuid") {
|
|
3058
|
-
result = resolvedData.uuid ? { uuid: resolvedData.uuid } : void 0;
|
|
3059
|
-
}
|
|
3060
|
-
} else if (op.subOperation === "query") {
|
|
3061
|
-
const params = [];
|
|
3062
|
-
const whereClause = this.buildWhereClause(op.filter || {}, params);
|
|
3063
|
-
const sql = `SELECT ${((_a2 = op.fields) == null ? void 0 : _a2.join(", ")) || "*"} FROM ${op.table} ${whereClause} LIMIT 1`;
|
|
3064
|
-
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;
|
|
3065
3021
|
}
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
} finally {
|
|
3072
|
-
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];
|
|
3073
3027
|
}
|
|
3074
|
-
|
|
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
|
+
}
|
|
3075
3036
|
}
|
|
3076
3037
|
createDatabaseTask(op, tableName, table, queryFunction, options) {
|
|
3077
|
-
var _a2, _b2, _c2, _d2;
|
|
3078
3038
|
const opAction = op === "query" ? "queried" : op === "insert" ? "inserted" : op === "update" ? "updated" : op === "delete" ? "deleted" : "";
|
|
3079
3039
|
const defaultSignal = `${options.isMeta ? "meta." : ""}${tableName}.${opAction}`;
|
|
3080
3040
|
const tableNameFormatted = tableName.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
3081
3041
|
CadenzaService.createThrottledTask(
|
|
3082
3042
|
`db${op.charAt(0).toUpperCase() + op.slice(1)}${tableNameFormatted}`,
|
|
3083
|
-
(context, emit) =>
|
|
3084
|
-
|
|
3085
|
-
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(
|
|
3086
3045
|
(trigger) => trigger.condition
|
|
3087
3046
|
);
|
|
3088
|
-
for (const triggerCondition of triggerConditions
|
|
3047
|
+
for (const triggerCondition of triggerConditions ?? []) {
|
|
3089
3048
|
if (triggerCondition.condition && !triggerCondition.condition(context)) {
|
|
3090
3049
|
return {
|
|
3091
3050
|
failed: true,
|
|
@@ -3093,13 +3052,13 @@ var DatabaseController = class _DatabaseController {
|
|
|
3093
3052
|
};
|
|
3094
3053
|
}
|
|
3095
3054
|
}
|
|
3096
|
-
context =
|
|
3055
|
+
context = await queryFunction(tableName, context.queryData ?? context);
|
|
3097
3056
|
if (!context.errored) {
|
|
3098
|
-
for (const signal of
|
|
3057
|
+
for (const signal of table.customSignals?.emissions?.[op] ?? []) {
|
|
3099
3058
|
if (signal.condition && !signal.condition(context)) {
|
|
3100
3059
|
continue;
|
|
3101
3060
|
}
|
|
3102
|
-
emit(
|
|
3061
|
+
emit(signal.signal ?? signal, context);
|
|
3103
3062
|
}
|
|
3104
3063
|
}
|
|
3105
3064
|
console.log(
|
|
@@ -3117,11 +3076,8 @@ var DatabaseController = class _DatabaseController {
|
|
|
3117
3076
|
delete context.limit;
|
|
3118
3077
|
delete context.offset;
|
|
3119
3078
|
return context;
|
|
3120
|
-
}),
|
|
3121
|
-
(context) => {
|
|
3122
|
-
var _a3, _b3, _c3;
|
|
3123
|
-
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";
|
|
3124
3079
|
},
|
|
3080
|
+
(context) => context?.__metadata?.__executionTraceId ?? context?.__executionTraceId ?? "default",
|
|
3125
3081
|
`Auto-generated ${op} task for ${tableName}`,
|
|
3126
3082
|
{
|
|
3127
3083
|
isMeta: options.isMeta,
|
|
@@ -3139,9 +3095,9 @@ var DatabaseController = class _DatabaseController {
|
|
|
3139
3095
|
}
|
|
3140
3096
|
}
|
|
3141
3097
|
).doOn(
|
|
3142
|
-
...
|
|
3098
|
+
...table.customSignals?.triggers?.[op]?.map((signal) => {
|
|
3143
3099
|
return typeof signal === "string" ? signal : signal.signal;
|
|
3144
|
-
})
|
|
3100
|
+
}) ?? []
|
|
3145
3101
|
).emits(defaultSignal);
|
|
3146
3102
|
}
|
|
3147
3103
|
};
|
|
@@ -3156,8 +3112,16 @@ var GraphSyncController = class _GraphSyncController {
|
|
|
3156
3112
|
return this._instance;
|
|
3157
3113
|
}
|
|
3158
3114
|
constructor() {
|
|
3159
|
-
|
|
3160
|
-
|
|
3115
|
+
CadenzaService.createDebounceMetaTask(
|
|
3116
|
+
"Debounce syncing of resources",
|
|
3117
|
+
() => true,
|
|
3118
|
+
"",
|
|
3119
|
+
500
|
|
3120
|
+
).doAfter(CadenzaService.serviceRegistry.handleInstanceUpdateTask).then(
|
|
3121
|
+
CadenzaService.broker.getSignalsTask.then(
|
|
3122
|
+
CadenzaService.registry.getAllTasks.then(CadenzaService.registry.getAllRoutines)
|
|
3123
|
+
)
|
|
3124
|
+
);
|
|
3161
3125
|
CadenzaService.createMetaTask("Split routines for registration", (ctx, emit) => {
|
|
3162
3126
|
const { __routines } = ctx;
|
|
3163
3127
|
if (!__routines) return;
|
|
@@ -3259,7 +3223,7 @@ var GraphSyncController = class _GraphSyncController {
|
|
|
3259
3223
|
taskName: task.name,
|
|
3260
3224
|
taskVersion: task.version,
|
|
3261
3225
|
taskServiceName: CadenzaService.serviceRegistry.serviceName,
|
|
3262
|
-
signalServiceName: signalServiceName
|
|
3226
|
+
signalServiceName: signalServiceName ?? CadenzaService.serviceRegistry.serviceName
|
|
3263
3227
|
}
|
|
3264
3228
|
});
|
|
3265
3229
|
});
|
|
@@ -3351,7 +3315,7 @@ var CadenzaService = class {
|
|
|
3351
3315
|
this.bootstrap();
|
|
3352
3316
|
this.validateName(routineName);
|
|
3353
3317
|
const name = `Deputy task for: ${routineName}`;
|
|
3354
|
-
options =
|
|
3318
|
+
options = {
|
|
3355
3319
|
concurrency: 0,
|
|
3356
3320
|
timeout: 0,
|
|
3357
3321
|
register: true,
|
|
@@ -3367,8 +3331,9 @@ var CadenzaService = class {
|
|
|
3367
3331
|
retryCount: 0,
|
|
3368
3332
|
retryDelay: 0,
|
|
3369
3333
|
retryDelayMax: 0,
|
|
3370
|
-
retryDelayFactor: 1
|
|
3371
|
-
|
|
3334
|
+
retryDelayFactor: 1,
|
|
3335
|
+
...options
|
|
3336
|
+
};
|
|
3372
3337
|
return new DeputyTask(
|
|
3373
3338
|
name,
|
|
3374
3339
|
routineName,
|
|
@@ -3422,7 +3387,7 @@ var CadenzaService = class {
|
|
|
3422
3387
|
this.bootstrap();
|
|
3423
3388
|
import_core3.default.validateName(signalName);
|
|
3424
3389
|
import_core3.default.validateName(serviceName);
|
|
3425
|
-
options =
|
|
3390
|
+
options = {
|
|
3426
3391
|
concurrency: 0,
|
|
3427
3392
|
timeout: 0,
|
|
3428
3393
|
register: true,
|
|
@@ -3438,8 +3403,9 @@ var CadenzaService = class {
|
|
|
3438
3403
|
retryCount: 1,
|
|
3439
3404
|
retryDelay: 0,
|
|
3440
3405
|
retryDelayMax: 0,
|
|
3441
|
-
retryDelayFactor: 1
|
|
3442
|
-
|
|
3406
|
+
retryDelayFactor: 1,
|
|
3407
|
+
...options
|
|
3408
|
+
};
|
|
3443
3409
|
options.isMeta = true;
|
|
3444
3410
|
const name = `Transmission of signal: ${signalName}`;
|
|
3445
3411
|
return new SignalTransmissionTask(
|
|
@@ -3470,10 +3436,10 @@ var CadenzaService = class {
|
|
|
3470
3436
|
import_core3.default.validateName(tableName);
|
|
3471
3437
|
import_core3.default.validateName(operation);
|
|
3472
3438
|
const tableNameFormatted = tableName.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
3473
|
-
const name = `${operation} ${tableName} in ${databaseServiceName
|
|
3474
|
-
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"}`;
|
|
3475
3441
|
const taskName = `db${operation.charAt(0).toUpperCase() + operation.slice(1)}${tableNameFormatted}`;
|
|
3476
|
-
options =
|
|
3442
|
+
options = {
|
|
3477
3443
|
concurrency: 0,
|
|
3478
3444
|
timeout: 0,
|
|
3479
3445
|
register: true,
|
|
@@ -3489,8 +3455,9 @@ var CadenzaService = class {
|
|
|
3489
3455
|
retryCount: 3,
|
|
3490
3456
|
retryDelay: 100,
|
|
3491
3457
|
retryDelayMax: 0,
|
|
3492
|
-
retryDelayFactor: 1
|
|
3493
|
-
|
|
3458
|
+
retryDelayFactor: 1,
|
|
3459
|
+
...options
|
|
3460
|
+
};
|
|
3494
3461
|
return new DatabaseTask(
|
|
3495
3462
|
name,
|
|
3496
3463
|
taskName,
|
|
@@ -3572,40 +3539,40 @@ var CadenzaService = class {
|
|
|
3572
3539
|
* @throws Error if name invalid or duplicate.
|
|
3573
3540
|
*/
|
|
3574
3541
|
static createCadenzaService(serviceName, description = "", options = {}) {
|
|
3575
|
-
var _a2, _b2, _c2, _d2, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
3576
3542
|
if (this.serviceCreated) return;
|
|
3577
3543
|
this.bootstrap();
|
|
3578
3544
|
import_core3.default.validateName(serviceName);
|
|
3579
3545
|
this.validateServiceName(serviceName);
|
|
3580
|
-
const serviceId =
|
|
3546
|
+
const serviceId = options.customServiceId ?? (0, import_uuid3.v4)();
|
|
3581
3547
|
this.serviceRegistry.serviceName = serviceName;
|
|
3582
3548
|
this.serviceRegistry.serviceInstanceId = serviceId;
|
|
3583
|
-
options =
|
|
3549
|
+
options = {
|
|
3584
3550
|
loadBalance: true,
|
|
3585
3551
|
useSocket: true,
|
|
3586
3552
|
displayName: void 0,
|
|
3587
3553
|
isMeta: false,
|
|
3588
|
-
port: parseInt(
|
|
3589
|
-
securityProfile:
|
|
3590
|
-
networkMode:
|
|
3554
|
+
port: parseInt(process.env.HTTP_PORT ?? "3000"),
|
|
3555
|
+
securityProfile: process.env.SECURITY_PROFILE ?? "medium",
|
|
3556
|
+
networkMode: process.env.NETWORK_MODE ?? "dev",
|
|
3591
3557
|
retryCount: 3,
|
|
3592
3558
|
cadenzaDB: {
|
|
3593
3559
|
connect: true,
|
|
3594
|
-
address:
|
|
3595
|
-
port: parseInt(
|
|
3560
|
+
address: process.env.CADENZA_DB_ADDRESS ?? "localhost",
|
|
3561
|
+
port: parseInt(process.env.CADENZA_DB_PORT ?? "5000")
|
|
3596
3562
|
},
|
|
3597
3563
|
relatedServices: process.env.RELATED_SERVICES ? process.env.RELATED_SERVICES.split("|").map(
|
|
3598
3564
|
(s) => s.trim().split(",")
|
|
3599
|
-
) : []
|
|
3600
|
-
|
|
3601
|
-
|
|
3565
|
+
) : [],
|
|
3566
|
+
...options
|
|
3567
|
+
};
|
|
3568
|
+
if (options.cadenzaDB?.connect) {
|
|
3602
3569
|
import_core3.default.broker.emit("meta.initializing_service", {
|
|
3603
3570
|
// Seed the CadenzaDB
|
|
3604
3571
|
serviceInstance: {
|
|
3605
3572
|
uuid: "cadenza-db",
|
|
3606
3573
|
serviceName: "CadenzaDB",
|
|
3607
|
-
address:
|
|
3608
|
-
port:
|
|
3574
|
+
address: options.cadenzaDB?.address,
|
|
3575
|
+
port: options.cadenzaDB?.port,
|
|
3609
3576
|
exposed: options.networkMode !== "dev",
|
|
3610
3577
|
numberOfRunningGraphs: 0,
|
|
3611
3578
|
isActive: true,
|
|
@@ -3616,14 +3583,13 @@ var CadenzaService = class {
|
|
|
3616
3583
|
}
|
|
3617
3584
|
});
|
|
3618
3585
|
}
|
|
3619
|
-
|
|
3620
|
-
var _a3;
|
|
3586
|
+
options.relatedServices?.forEach((service) => {
|
|
3621
3587
|
import_core3.default.broker.emit("meta.initializing_service", {
|
|
3622
3588
|
serviceInstance: {
|
|
3623
3589
|
uuid: service[0],
|
|
3624
3590
|
serviceName: service[1],
|
|
3625
3591
|
address: service[2].split(":")[0],
|
|
3626
|
-
port:
|
|
3592
|
+
port: service[2].split(":")[1] ?? 3e3,
|
|
3627
3593
|
exposed: options.networkMode !== "dev",
|
|
3628
3594
|
numberOfRunningGraphs: 0,
|
|
3629
3595
|
isActive: true,
|
|
@@ -3639,7 +3605,7 @@ var CadenzaService = class {
|
|
|
3639
3605
|
data: {
|
|
3640
3606
|
name: serviceName,
|
|
3641
3607
|
description,
|
|
3642
|
-
displayName:
|
|
3608
|
+
displayName: options.displayName ?? "",
|
|
3643
3609
|
isMeta: options.isMeta
|
|
3644
3610
|
},
|
|
3645
3611
|
__serviceName: serviceName,
|
|
@@ -3650,12 +3616,12 @@ var CadenzaService = class {
|
|
|
3650
3616
|
__securityProfile: options.securityProfile,
|
|
3651
3617
|
__networkMode: options.networkMode,
|
|
3652
3618
|
__retryCount: options.retryCount,
|
|
3653
|
-
__cadenzaDBConnect:
|
|
3619
|
+
__cadenzaDBConnect: options.cadenzaDB?.connect
|
|
3654
3620
|
};
|
|
3655
|
-
if (
|
|
3656
|
-
import_core3.default.createEphemeralMetaTask("Create service", (_, emit) =>
|
|
3621
|
+
if (options.cadenzaDB?.connect) {
|
|
3622
|
+
import_core3.default.createEphemeralMetaTask("Create service", async (_, emit) => {
|
|
3657
3623
|
emit("meta.create_service_requested", initContext);
|
|
3658
|
-
})
|
|
3624
|
+
}).doOn("meta.fetch.handshake_complete");
|
|
3659
3625
|
} else {
|
|
3660
3626
|
import_core3.default.broker.emit("meta.create_service_requested", initContext);
|
|
3661
3627
|
}
|
|
@@ -3675,29 +3641,29 @@ var CadenzaService = class {
|
|
|
3675
3641
|
this.createCadenzaService(serviceName, description, options);
|
|
3676
3642
|
}
|
|
3677
3643
|
static createDatabaseService(name, schema, description = "", options = {}) {
|
|
3678
|
-
var _a2, _b2, _c2, _d2, _e, _f;
|
|
3679
3644
|
if (this.serviceCreated) return;
|
|
3680
3645
|
this.bootstrap();
|
|
3681
3646
|
this.serviceRegistry.serviceName = name;
|
|
3682
3647
|
DatabaseController.instance;
|
|
3683
|
-
options =
|
|
3648
|
+
options = {
|
|
3684
3649
|
loadBalance: true,
|
|
3685
3650
|
useSocket: true,
|
|
3686
3651
|
displayName: void 0,
|
|
3687
3652
|
isMeta: false,
|
|
3688
|
-
port: parseInt(
|
|
3689
|
-
securityProfile:
|
|
3690
|
-
networkMode:
|
|
3653
|
+
port: parseInt(process.env.HTTP_PORT ?? "3000"),
|
|
3654
|
+
securityProfile: process.env.SECURITY_PROFILE ?? "medium",
|
|
3655
|
+
networkMode: process.env.NETWORK_MODE ?? "dev",
|
|
3691
3656
|
retryCount: 3,
|
|
3692
3657
|
cadenzaDB: {
|
|
3693
3658
|
connect: true,
|
|
3694
|
-
address:
|
|
3695
|
-
port: parseInt(
|
|
3659
|
+
address: process.env.CADENZA_DB_ADDRESS ?? "localhost",
|
|
3660
|
+
port: parseInt(process.env.CADENZA_DB_PORT ?? "5000")
|
|
3696
3661
|
},
|
|
3697
3662
|
databaseType: "postgres",
|
|
3698
3663
|
databaseName: (0, import_lodash_es2.snakeCase)(name),
|
|
3699
|
-
poolSize: parseInt(
|
|
3700
|
-
|
|
3664
|
+
poolSize: parseInt(process.env.DATABASE_POOL_SIZE ?? "10"),
|
|
3665
|
+
...options
|
|
3666
|
+
};
|
|
3701
3667
|
import_core3.default.broker.emit("meta.database_init_requested", {
|
|
3702
3668
|
schema,
|
|
3703
3669
|
databaseName: options.databaseName,
|