@dudousxd/nestjs-durable 0.58.0 → 0.59.1
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.cjs +256 -135
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +82 -17
- package/dist/index.d.ts +82 -17
- package/dist/index.js +257 -136
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -453,6 +453,168 @@ __name(attributesOf, "attributesOf");
|
|
|
453
453
|
var import_nestjs_durable_core3 = require("@dudousxd/nestjs-durable-core");
|
|
454
454
|
var import_common = require("@nestjs/common");
|
|
455
455
|
|
|
456
|
+
// src/tenancy.ts
|
|
457
|
+
var import_node_async_hooks = require("async_hooks");
|
|
458
|
+
|
|
459
|
+
// src/tenancy-reader.ts
|
|
460
|
+
var SLOT = Symbol.for("@dudousxd/nestjs-diagnostics:accessor");
|
|
461
|
+
function accessorOf(accessor) {
|
|
462
|
+
const slot = globalThis[SLOT];
|
|
463
|
+
return accessor ?? slot?.current ?? void 0;
|
|
464
|
+
}
|
|
465
|
+
__name(accessorOf, "accessorOf");
|
|
466
|
+
function readTenancy(option = "inherit", accessor) {
|
|
467
|
+
const a = accessorOf(accessor);
|
|
468
|
+
if (option === false || typeof a?.tenancy !== "function" || !a.tenantFilter) {
|
|
469
|
+
return {
|
|
470
|
+
enabled: false
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
const own = typeof option === "object" ? option : {};
|
|
474
|
+
const inherit = own.enabled === void 0 || own.enabled === "inherit";
|
|
475
|
+
if (!(inherit ? a.tenancy().enabled : own.enabled)) return {
|
|
476
|
+
enabled: false
|
|
477
|
+
};
|
|
478
|
+
const options = {};
|
|
479
|
+
if (own.kinds) options.kinds = own.kinds;
|
|
480
|
+
if (own.family !== void 0) options.family = own.family;
|
|
481
|
+
if (own.required !== void 0) options.required = own.required;
|
|
482
|
+
const tenantFilter = a.tenantFilter.bind(a);
|
|
483
|
+
return {
|
|
484
|
+
enabled: true,
|
|
485
|
+
nullRows: own.nullRows ?? "tenantless",
|
|
486
|
+
// Throws the context's TenantRequiredError (403) when a tenant is required and missing.
|
|
487
|
+
filter: /* @__PURE__ */ __name(() => tenantFilter(Object.keys(options).length > 0 ? options : void 0), "filter")
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
__name(readTenancy, "readTenancy");
|
|
491
|
+
|
|
492
|
+
// src/tenancy.ts
|
|
493
|
+
var durableWork = new import_node_async_hooks.AsyncLocalStorage();
|
|
494
|
+
function inDurableWork(fn) {
|
|
495
|
+
return durableWork.getStore() ? fn() : durableWork.run(true, fn);
|
|
496
|
+
}
|
|
497
|
+
__name(inDurableWork, "inDurableWork");
|
|
498
|
+
function readTenancy2(option, accessor) {
|
|
499
|
+
const own = option ?? "inherit";
|
|
500
|
+
const effective = durableWork.getStore() && own !== false ? {
|
|
501
|
+
...typeof own === "object" ? own : {},
|
|
502
|
+
required: false
|
|
503
|
+
} : own;
|
|
504
|
+
return readTenancy(effective, accessor);
|
|
505
|
+
}
|
|
506
|
+
__name(readTenancy2, "readTenancy");
|
|
507
|
+
function isTenantRuntime(x) {
|
|
508
|
+
return !!x && typeof x.withTenant === "function";
|
|
509
|
+
}
|
|
510
|
+
__name(isTenantRuntime, "isTenantRuntime");
|
|
511
|
+
var TENANCY_SLOT = Symbol.for("@dudousxd/nestjs-context:tenancy");
|
|
512
|
+
function tenancySlot() {
|
|
513
|
+
const slot = globalThis[TENANCY_SLOT];
|
|
514
|
+
return slot?.version === 1 && isTenantRuntime(slot) ? slot : void 0;
|
|
515
|
+
}
|
|
516
|
+
__name(tenancySlot, "tenancySlot");
|
|
517
|
+
function restorer(runtime) {
|
|
518
|
+
return tenancySlot() ?? runtime;
|
|
519
|
+
}
|
|
520
|
+
__name(restorer, "restorer");
|
|
521
|
+
function restore(runtime, option, tenant, fn) {
|
|
522
|
+
const family = typeof option === "object" ? option.family : void 0;
|
|
523
|
+
const ref = {
|
|
524
|
+
id: tenant.id,
|
|
525
|
+
kind: tenant.kind ?? "unknown",
|
|
526
|
+
source: "carrier"
|
|
527
|
+
};
|
|
528
|
+
return family !== void 0 ? runtime.withTenant(ref, fn, {
|
|
529
|
+
family
|
|
530
|
+
}) : runtime.withTenant(ref, fn);
|
|
531
|
+
}
|
|
532
|
+
__name(restore, "restore");
|
|
533
|
+
function buildEngineTenancy(option, accessor, runtime) {
|
|
534
|
+
if (option === false || !restorer(runtime)) return void 0;
|
|
535
|
+
return {
|
|
536
|
+
current() {
|
|
537
|
+
const t = readTenancy2(option, accessor);
|
|
538
|
+
if (!t.enabled) return void 0;
|
|
539
|
+
const f = t.filter();
|
|
540
|
+
return f.mode === "tenant" ? {
|
|
541
|
+
id: f.tenant.id,
|
|
542
|
+
kind: f.tenant.kind
|
|
543
|
+
} : void 0;
|
|
544
|
+
},
|
|
545
|
+
filter: /* @__PURE__ */ __name(() => tenantFilterNow(option, accessor), "filter"),
|
|
546
|
+
run: /* @__PURE__ */ __name((tenant, fn) => {
|
|
547
|
+
const using = restorer(runtime);
|
|
548
|
+
return using ? restore(using, option, tenant, fn) : fn();
|
|
549
|
+
}, "run"),
|
|
550
|
+
system: inDurableWork
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
__name(buildEngineTenancy, "buildEngineTenancy");
|
|
554
|
+
function tenantFilterNow(option, accessor) {
|
|
555
|
+
const t = readTenancy2(option, accessor);
|
|
556
|
+
if (!t.enabled) return {
|
|
557
|
+
mode: "system"
|
|
558
|
+
};
|
|
559
|
+
const f = t.filter();
|
|
560
|
+
if (f.mode === "tenant") return {
|
|
561
|
+
mode: "tenant",
|
|
562
|
+
tenantId: f.tenant.id,
|
|
563
|
+
nullRows: t.nullRows
|
|
564
|
+
};
|
|
565
|
+
if (f.mode === "tenantless") return {
|
|
566
|
+
mode: "tenantless"
|
|
567
|
+
};
|
|
568
|
+
return f.mode === "all" ? {
|
|
569
|
+
mode: "all"
|
|
570
|
+
} : {
|
|
571
|
+
mode: "system"
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
__name(tenantFilterNow, "tenantFilterNow");
|
|
575
|
+
function tenantFromCarrier(carrier) {
|
|
576
|
+
const ref = carrier?.tenant;
|
|
577
|
+
if (ref && typeof ref.id === "string") {
|
|
578
|
+
return typeof ref.kind === "string" ? {
|
|
579
|
+
id: ref.id,
|
|
580
|
+
kind: ref.kind
|
|
581
|
+
} : {
|
|
582
|
+
id: ref.id
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
const id = carrier?.tenantId;
|
|
586
|
+
return typeof id === "string" ? {
|
|
587
|
+
id
|
|
588
|
+
} : void 0;
|
|
589
|
+
}
|
|
590
|
+
__name(tenantFromCarrier, "tenantFromCarrier");
|
|
591
|
+
function stepTenantWrapper(option, accessor, runtime) {
|
|
592
|
+
return (execution, next) => {
|
|
593
|
+
if (execution.unit !== "step") return next();
|
|
594
|
+
return inDurableWork(() => {
|
|
595
|
+
const tenant = tenantFromCarrier(execution.context);
|
|
596
|
+
const using = restorer(runtime);
|
|
597
|
+
if (!tenant || !using || !readTenancy2(option, accessor).enabled) return next();
|
|
598
|
+
return restore(using, option, tenant, next);
|
|
599
|
+
});
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
__name(stepTenantWrapper, "stepTenantWrapper");
|
|
603
|
+
function canRestoreTenant(runtime) {
|
|
604
|
+
return tenancySlot() !== void 0 || isTenantRuntime(runtime);
|
|
605
|
+
}
|
|
606
|
+
__name(canRestoreTenant, "canRestoreTenant");
|
|
607
|
+
function currentTenant(option, accessor) {
|
|
608
|
+
const t = readTenancy2(option, accessor);
|
|
609
|
+
if (!t.enabled) return void 0;
|
|
610
|
+
const f = t.filter();
|
|
611
|
+
return f.mode === "tenant" ? {
|
|
612
|
+
id: f.tenant.id,
|
|
613
|
+
kind: f.tenant.kind
|
|
614
|
+
} : void 0;
|
|
615
|
+
}
|
|
616
|
+
__name(currentTenant, "currentTenant");
|
|
617
|
+
|
|
456
618
|
// src/tenant-contracts.ts
|
|
457
619
|
var RESOLVED = Symbol("nestjs-durable:resolved-tenant-auth");
|
|
458
620
|
function markResolved(fn) {
|
|
@@ -643,16 +805,22 @@ var DurableStartClient = class {
|
|
|
643
805
|
}
|
|
644
806
|
options;
|
|
645
807
|
deps;
|
|
808
|
+
accessor;
|
|
646
809
|
tenant;
|
|
647
810
|
tenantToken;
|
|
648
|
-
constructor(options, deps) {
|
|
811
|
+
constructor(options, deps, accessor) {
|
|
649
812
|
this.options = options;
|
|
650
813
|
this.deps = deps;
|
|
814
|
+
this.accessor = accessor;
|
|
651
815
|
this.tenant = options.partition ?? "default";
|
|
652
816
|
this.tenantToken = resolveTenantTokenSource(options.tenantToken);
|
|
653
817
|
}
|
|
654
818
|
async start(workflow, input, runId = globalThis.crypto.randomUUID(), opts) {
|
|
655
819
|
const name = (0, import_nestjs_durable_core3.workflowName)(workflow);
|
|
820
|
+
const tenant = opts?.tenantId !== void 0 ? opts.tenantId === null ? void 0 : {
|
|
821
|
+
id: opts.tenantId,
|
|
822
|
+
kind: opts.tenantKind
|
|
823
|
+
} : currentTenant(this.options.tenancy, this.accessor);
|
|
656
824
|
await startRun(this.options.connection, {
|
|
657
825
|
// The signed token (when configured) rides in the `tenant` field, as on every run request; an
|
|
658
826
|
// operator with `verifyTenant` derives the run's namespace from it.
|
|
@@ -671,6 +839,12 @@ var DurableStartClient = class {
|
|
|
671
839
|
...opts?.searchAttributes !== void 0 ? {
|
|
672
840
|
searchAttributes: opts.searchAttributes
|
|
673
841
|
} : {},
|
|
842
|
+
...tenant ? {
|
|
843
|
+
tenantId: tenant.id,
|
|
844
|
+
...tenant.kind !== void 0 ? {
|
|
845
|
+
tenantKind: tenant.kind
|
|
846
|
+
} : {}
|
|
847
|
+
} : {},
|
|
674
848
|
...this.deps !== void 0 ? {
|
|
675
849
|
deps: this.deps
|
|
676
850
|
} : {}
|
|
@@ -713,7 +887,8 @@ DurableStartClient = _ts_decorate([
|
|
|
713
887
|
_ts_metadata("design:type", Function),
|
|
714
888
|
_ts_metadata("design:paramtypes", [
|
|
715
889
|
typeof DurableModuleOptions === "undefined" ? Object : DurableModuleOptions,
|
|
716
|
-
typeof StartRunDeps === "undefined" ? Object : StartRunDeps
|
|
890
|
+
typeof StartRunDeps === "undefined" ? Object : StartRunDeps,
|
|
891
|
+
typeof ContextAccessor === "undefined" ? Object : ContextAccessor
|
|
717
892
|
])
|
|
718
893
|
], DurableStartClient);
|
|
719
894
|
function tenantUnsupported(method) {
|
|
@@ -1974,20 +2149,63 @@ var ProxyRunGateway = class {
|
|
|
1974
2149
|
timeoutMs;
|
|
1975
2150
|
capabilityTtlMs;
|
|
1976
2151
|
tenantToken;
|
|
2152
|
+
tenantFilter;
|
|
1977
2153
|
pending = /* @__PURE__ */ new Map();
|
|
1978
2154
|
/** Verb → when to ask the control plane for it again, after it answered that it does not know it.
|
|
1979
2155
|
* During a rolling upgrade the control plane is several pods of mixed versions behind one queue,
|
|
1980
2156
|
* and a reply does not say which pod sent it, so the whole control plane is assumed old until the
|
|
1981
2157
|
* entry expires. */
|
|
1982
2158
|
unsupportedUntil = /* @__PURE__ */ new Map();
|
|
1983
|
-
constructor(transport, tenant, timeoutMs = 1e4, capabilityTtlMs = 6e4, tenantToken) {
|
|
2159
|
+
constructor(transport, tenant, timeoutMs = 1e4, capabilityTtlMs = 6e4, tenantToken, tenantFilter) {
|
|
1984
2160
|
this.transport = transport;
|
|
1985
2161
|
this.tenant = tenant;
|
|
1986
2162
|
this.timeoutMs = timeoutMs;
|
|
1987
2163
|
this.capabilityTtlMs = capabilityTtlMs;
|
|
1988
2164
|
this.tenantToken = tenantToken;
|
|
2165
|
+
this.tenantFilter = tenantFilter;
|
|
1989
2166
|
this.transport.onRunReply?.((reply) => this.handleReply(reply));
|
|
1990
2167
|
}
|
|
2168
|
+
/** The request's tenant filter when it can hide runs (`tenant` / `tenantless`), else undefined. */
|
|
2169
|
+
restrictive() {
|
|
2170
|
+
const filter = this.tenantFilter?.();
|
|
2171
|
+
return filter && filter.mode !== "system" && filter.mode !== "all" ? filter : void 0;
|
|
2172
|
+
}
|
|
2173
|
+
/**
|
|
2174
|
+
* Send `query`, narrowed to the runs the request may see. A tenant-required request with no
|
|
2175
|
+
* tenant rejects (never throws synchronously).
|
|
2176
|
+
*/
|
|
2177
|
+
scoped(query, send) {
|
|
2178
|
+
let filter;
|
|
2179
|
+
try {
|
|
2180
|
+
filter = this.restrictive();
|
|
2181
|
+
} catch (err) {
|
|
2182
|
+
return Promise.reject(err);
|
|
2183
|
+
}
|
|
2184
|
+
return send(filter ? (0, import_nestjs_durable_core11.scopeTenantQuery)(query, filter) : query);
|
|
2185
|
+
}
|
|
2186
|
+
/** `run` when the request may see it, else null (an invisible run reads like a missing one). */
|
|
2187
|
+
visible(run, filter = this.restrictive()) {
|
|
2188
|
+
if (!run) return null;
|
|
2189
|
+
return !filter || (0, import_nestjs_durable_core11.tenantVisible)(run, filter) ? run : null;
|
|
2190
|
+
}
|
|
2191
|
+
/** Run a control call unless the request may not act on `runId` (then `null`, like a missing run).
|
|
2192
|
+
* Sent at once when nothing narrows the request. */
|
|
2193
|
+
guarded(runId, call) {
|
|
2194
|
+
let filter;
|
|
2195
|
+
try {
|
|
2196
|
+
filter = this.restrictive();
|
|
2197
|
+
} catch (err) {
|
|
2198
|
+
return Promise.reject(err);
|
|
2199
|
+
}
|
|
2200
|
+
if (!filter) return call();
|
|
2201
|
+
return this.hidden(runId).then((hidden) => hidden ? null : call());
|
|
2202
|
+
}
|
|
2203
|
+
/** Before a control call: true when the request may NOT act on `runId`. */
|
|
2204
|
+
async hidden(runId) {
|
|
2205
|
+
const filter = this.restrictive();
|
|
2206
|
+
if (!filter) return false;
|
|
2207
|
+
return this.visible(await this.getRunUnscoped(runId), filter) === null;
|
|
2208
|
+
}
|
|
1991
2209
|
handleReply(reply) {
|
|
1992
2210
|
const pending = this.pending.get(reply.requestId);
|
|
1993
2211
|
if (!pending) return;
|
|
@@ -2042,6 +2260,7 @@ var ProxyRunGateway = class {
|
|
|
2042
2260
|
* a summarized detail always carries `fans`, so its absence means the summary is made here.
|
|
2043
2261
|
*/
|
|
2044
2262
|
async getRunDetail(runId, options) {
|
|
2263
|
+
const filter = this.restrictive();
|
|
2045
2264
|
const detail = await this.request(options === void 0 ? {
|
|
2046
2265
|
kind: "getRunDetail",
|
|
2047
2266
|
runId
|
|
@@ -2050,6 +2269,7 @@ var ProxyRunGateway = class {
|
|
|
2050
2269
|
runId,
|
|
2051
2270
|
options
|
|
2052
2271
|
});
|
|
2272
|
+
if (detail && filter && !this.visible(detail.run, filter)) return null;
|
|
2053
2273
|
const over = options?.summarizeFansOver;
|
|
2054
2274
|
if (over === void 0 || !detail || detail.fans !== void 0) return detail;
|
|
2055
2275
|
const { timeline, fans } = (0, import_nestjs_durable_core11.summarizeTimeline)(detail.timeline, over);
|
|
@@ -2064,6 +2284,7 @@ var ProxyRunGateway = class {
|
|
|
2064
2284
|
}
|
|
2065
2285
|
/** Paged from the full detail when the control plane is too old to page a fan itself. */
|
|
2066
2286
|
async getFanMembers(runId, group, query) {
|
|
2287
|
+
if (this.restrictive() && await this.hidden(runId)) return null;
|
|
2067
2288
|
if (!this.knownUnsupported("getFanMembers")) {
|
|
2068
2289
|
try {
|
|
2069
2290
|
const page = await this.request({
|
|
@@ -2089,6 +2310,9 @@ var ProxyRunGateway = class {
|
|
|
2089
2310
|
* to know the verb: it then answers with no data, or with an unsupported-request error.
|
|
2090
2311
|
*/
|
|
2091
2312
|
async getRun(runId) {
|
|
2313
|
+
return this.visible(await this.getRunUnscoped(runId));
|
|
2314
|
+
}
|
|
2315
|
+
async getRunUnscoped(runId) {
|
|
2092
2316
|
if (!this.knownUnsupported("getRun")) {
|
|
2093
2317
|
try {
|
|
2094
2318
|
const run = await this.request({
|
|
@@ -2117,27 +2341,27 @@ var ProxyRunGateway = class {
|
|
|
2117
2341
|
/** The control plane's `StoreRunGateway` resolves each run's `waiting` descriptor; the reply carries
|
|
2118
2342
|
* it through as plain JSON, so a tenant's list rows name the wait too. */
|
|
2119
2343
|
listRuns(query) {
|
|
2120
|
-
return this.request({
|
|
2344
|
+
return this.scoped(query, (q) => this.request({
|
|
2121
2345
|
kind: "listRuns",
|
|
2122
|
-
query
|
|
2123
|
-
});
|
|
2346
|
+
query: q
|
|
2347
|
+
}));
|
|
2124
2348
|
}
|
|
2125
2349
|
/** Counted on the operator, over the same tenant-scoped predicates its `listRuns` reply obeys. */
|
|
2126
2350
|
runFacets(query) {
|
|
2127
|
-
return this.request({
|
|
2351
|
+
return this.scoped(query, (q) => this.request({
|
|
2128
2352
|
kind: "runFacets",
|
|
2129
|
-
query
|
|
2130
|
-
});
|
|
2353
|
+
query: q
|
|
2354
|
+
}));
|
|
2131
2355
|
}
|
|
2132
2356
|
/** Enumerated on the operator, over the same tenant-scoped predicates its `listRuns` reply obeys —
|
|
2133
2357
|
* so a tenant's pickers offer its own tenants/tags/attributes and never another's. */
|
|
2134
2358
|
runValueFacets(axis, query, opts) {
|
|
2135
|
-
return this.request({
|
|
2359
|
+
return this.scoped(query, (q) => this.request({
|
|
2136
2360
|
kind: "runValueFacets",
|
|
2137
2361
|
axis,
|
|
2138
|
-
query,
|
|
2362
|
+
query: q,
|
|
2139
2363
|
opts
|
|
2140
|
-
});
|
|
2364
|
+
}));
|
|
2141
2365
|
}
|
|
2142
2366
|
/** Round-trips to the operator, which scopes the result to this tenant's own `@<tenant>` groups. */
|
|
2143
2367
|
workerHealth() {
|
|
@@ -2154,39 +2378,39 @@ var ProxyRunGateway = class {
|
|
|
2154
2378
|
});
|
|
2155
2379
|
}
|
|
2156
2380
|
cancel(runId, opts) {
|
|
2157
|
-
return this.request(opts === void 0 ? {
|
|
2381
|
+
return this.guarded(runId, () => this.request(opts === void 0 ? {
|
|
2158
2382
|
kind: "cancel",
|
|
2159
2383
|
runId
|
|
2160
2384
|
} : {
|
|
2161
2385
|
kind: "cancel",
|
|
2162
2386
|
runId,
|
|
2163
2387
|
opts
|
|
2164
|
-
});
|
|
2388
|
+
}));
|
|
2165
2389
|
}
|
|
2166
2390
|
retry(runId) {
|
|
2167
|
-
return this.request({
|
|
2391
|
+
return this.guarded(runId, () => this.request({
|
|
2168
2392
|
kind: "retry",
|
|
2169
2393
|
runId
|
|
2170
|
-
});
|
|
2394
|
+
}));
|
|
2171
2395
|
}
|
|
2172
2396
|
continue(runId) {
|
|
2173
|
-
return this.request({
|
|
2397
|
+
return this.guarded(runId, () => this.request({
|
|
2174
2398
|
kind: "continue",
|
|
2175
2399
|
runId
|
|
2176
|
-
});
|
|
2400
|
+
}));
|
|
2177
2401
|
}
|
|
2178
2402
|
retryWithInput(runId, input) {
|
|
2179
|
-
return this.request({
|
|
2403
|
+
return this.guarded(runId, () => this.request({
|
|
2180
2404
|
kind: "retryWithInput",
|
|
2181
2405
|
runId,
|
|
2182
2406
|
input
|
|
2183
|
-
});
|
|
2407
|
+
}));
|
|
2184
2408
|
}
|
|
2185
2409
|
redispatchPending(runId) {
|
|
2186
|
-
return this.request({
|
|
2410
|
+
return this.guarded(runId, () => this.request({
|
|
2187
2411
|
kind: "redispatch",
|
|
2188
2412
|
runId
|
|
2189
|
-
});
|
|
2413
|
+
}));
|
|
2190
2414
|
}
|
|
2191
2415
|
/**
|
|
2192
2416
|
* Republished on the control plane: a one-run target is checked against this tenant like
|
|
@@ -2240,7 +2464,8 @@ ProxyRunGateway = _ts_decorate6([
|
|
|
2240
2464
|
String,
|
|
2241
2465
|
void 0,
|
|
2242
2466
|
void 0,
|
|
2243
|
-
typeof TenantTokenSource === "undefined" ? Object : TenantTokenSource
|
|
2467
|
+
typeof TenantTokenSource === "undefined" ? Object : TenantTokenSource,
|
|
2468
|
+
Function
|
|
2244
2469
|
])
|
|
2245
2470
|
], ProxyRunGateway);
|
|
2246
2471
|
|
|
@@ -2947,111 +3172,6 @@ StoreRunGateway = _ts_decorate9([
|
|
|
2947
3172
|
])
|
|
2948
3173
|
], StoreRunGateway);
|
|
2949
3174
|
|
|
2950
|
-
// src/tenancy.ts
|
|
2951
|
-
var SLOT = Symbol.for("@dudousxd/nestjs-diagnostics:accessor");
|
|
2952
|
-
function accessorOf(accessor) {
|
|
2953
|
-
if (accessor) return accessor;
|
|
2954
|
-
const slot = globalThis[SLOT];
|
|
2955
|
-
return slot?.current ?? void 0;
|
|
2956
|
-
}
|
|
2957
|
-
__name(accessorOf, "accessorOf");
|
|
2958
|
-
function readTenancy(option, accessor) {
|
|
2959
|
-
const a = accessorOf(accessor);
|
|
2960
|
-
if (option === false || typeof a?.tenancy !== "function" || typeof a.tenantFilter !== "function") {
|
|
2961
|
-
return {
|
|
2962
|
-
enabled: false
|
|
2963
|
-
};
|
|
2964
|
-
}
|
|
2965
|
-
const own = typeof option === "object" ? option : {};
|
|
2966
|
-
const inherit = own.enabled === void 0 || own.enabled === "inherit";
|
|
2967
|
-
if (!(inherit ? a.tenancy().enabled : own.enabled)) return {
|
|
2968
|
-
enabled: false
|
|
2969
|
-
};
|
|
2970
|
-
const kinds = own.kinds;
|
|
2971
|
-
const tenantFilter = a.tenantFilter.bind(a);
|
|
2972
|
-
return {
|
|
2973
|
-
enabled: true,
|
|
2974
|
-
nullRows: own.nullRows ?? "tenantless",
|
|
2975
|
-
filter: /* @__PURE__ */ __name(() => tenantFilter(kinds ? {
|
|
2976
|
-
kinds
|
|
2977
|
-
} : void 0), "filter")
|
|
2978
|
-
};
|
|
2979
|
-
}
|
|
2980
|
-
__name(readTenancy, "readTenancy");
|
|
2981
|
-
function isTenantRuntime(x) {
|
|
2982
|
-
return !!x && typeof x.withTenant === "function";
|
|
2983
|
-
}
|
|
2984
|
-
__name(isTenantRuntime, "isTenantRuntime");
|
|
2985
|
-
function restore(runtime, tenant, fn) {
|
|
2986
|
-
return runtime.withTenant({
|
|
2987
|
-
id: tenant.id,
|
|
2988
|
-
kind: tenant.kind ?? "unknown",
|
|
2989
|
-
source: "carrier"
|
|
2990
|
-
}, fn);
|
|
2991
|
-
}
|
|
2992
|
-
__name(restore, "restore");
|
|
2993
|
-
function buildEngineTenancy(option, accessor, runtime) {
|
|
2994
|
-
if (option === false || !runtime) return void 0;
|
|
2995
|
-
return {
|
|
2996
|
-
current() {
|
|
2997
|
-
const t = readTenancy(option, accessor);
|
|
2998
|
-
if (!t.enabled) return void 0;
|
|
2999
|
-
const f = t.filter();
|
|
3000
|
-
return f.mode === "tenant" ? {
|
|
3001
|
-
id: f.tenant.id,
|
|
3002
|
-
kind: f.tenant.kind
|
|
3003
|
-
} : void 0;
|
|
3004
|
-
},
|
|
3005
|
-
filter() {
|
|
3006
|
-
const t = readTenancy(option, accessor);
|
|
3007
|
-
if (!t.enabled) return {
|
|
3008
|
-
mode: "system"
|
|
3009
|
-
};
|
|
3010
|
-
const f = t.filter();
|
|
3011
|
-
if (f.mode === "tenant") return {
|
|
3012
|
-
mode: "tenant",
|
|
3013
|
-
tenantId: f.tenant.id,
|
|
3014
|
-
nullRows: t.nullRows
|
|
3015
|
-
};
|
|
3016
|
-
if (f.mode === "tenantless") return {
|
|
3017
|
-
mode: "tenantless"
|
|
3018
|
-
};
|
|
3019
|
-
return f.mode === "all" ? {
|
|
3020
|
-
mode: "all"
|
|
3021
|
-
} : {
|
|
3022
|
-
mode: "system"
|
|
3023
|
-
};
|
|
3024
|
-
},
|
|
3025
|
-
run: /* @__PURE__ */ __name((tenant, fn) => restore(runtime, tenant, fn), "run")
|
|
3026
|
-
};
|
|
3027
|
-
}
|
|
3028
|
-
__name(buildEngineTenancy, "buildEngineTenancy");
|
|
3029
|
-
function tenantFromCarrier(carrier) {
|
|
3030
|
-
const ref = carrier?.tenant;
|
|
3031
|
-
if (ref && typeof ref.id === "string") {
|
|
3032
|
-
return typeof ref.kind === "string" ? {
|
|
3033
|
-
id: ref.id,
|
|
3034
|
-
kind: ref.kind
|
|
3035
|
-
} : {
|
|
3036
|
-
id: ref.id
|
|
3037
|
-
};
|
|
3038
|
-
}
|
|
3039
|
-
const id = carrier?.tenantId;
|
|
3040
|
-
return typeof id === "string" ? {
|
|
3041
|
-
id
|
|
3042
|
-
} : void 0;
|
|
3043
|
-
}
|
|
3044
|
-
__name(tenantFromCarrier, "tenantFromCarrier");
|
|
3045
|
-
function stepTenantWrapper(option, accessor, runtime) {
|
|
3046
|
-
return (execution, next) => {
|
|
3047
|
-
if (execution.unit !== "step") return next();
|
|
3048
|
-
const tenant = tenantFromCarrier(execution.context);
|
|
3049
|
-
if (!tenant || !readTenancy(option, accessor).enabled) return next();
|
|
3050
|
-
return restore(runtime, tenant, next);
|
|
3051
|
-
};
|
|
3052
|
-
}
|
|
3053
|
-
__name(stepTenantWrapper, "stepTenantWrapper");
|
|
3054
|
-
|
|
3055
3175
|
// src/tenant-event-republisher.ts
|
|
3056
3176
|
function isTerminalRunEvent(event) {
|
|
3057
3177
|
return event.type === "run.completed" || event.type === "run.failed";
|
|
@@ -3848,10 +3968,10 @@ var DurableTenancyBridge = class DurableTenancyBridge2 {
|
|
|
3848
3968
|
async onApplicationBootstrap() {
|
|
3849
3969
|
if (this.options.tenancy === false) return;
|
|
3850
3970
|
const accessor = resolveAccessor(this.moduleRef);
|
|
3851
|
-
if (!
|
|
3971
|
+
if (!readTenancy2(this.options.tenancy, accessor).enabled) return;
|
|
3852
3972
|
const runtime = await resolveContextRuntime();
|
|
3853
|
-
if (!
|
|
3854
|
-
this.unregister = (0, import_nestjs_durable_core19.useDurableExecution)(stepTenantWrapper(this.options.tenancy, accessor, runtime));
|
|
3973
|
+
if (!canRestoreTenant(runtime)) return;
|
|
3974
|
+
this.unregister = (0, import_nestjs_durable_core19.useDurableExecution)(stepTenantWrapper(this.options.tenancy, accessor, isTenantRuntime(runtime) ? runtime : void 0));
|
|
3855
3975
|
}
|
|
3856
3976
|
onModuleDestroy() {
|
|
3857
3977
|
this.unregister?.();
|
|
@@ -3955,7 +4075,7 @@ var DurableModule = class _DurableModule {
|
|
|
3955
4075
|
...auth.tenantToken !== void 0 ? {
|
|
3956
4076
|
tenantToken: auth.tenantToken
|
|
3957
4077
|
} : {}
|
|
3958
|
-
});
|
|
4078
|
+
}, void 0, resolveAccessor(moduleRef));
|
|
3959
4079
|
}
|
|
3960
4080
|
if (!store) {
|
|
3961
4081
|
throw new Error("unreachable: STATE_STORE_CANONICAL must resolve a store when options.store is set");
|
|
@@ -4042,20 +4162,21 @@ var DurableModule = class _DurableModule {
|
|
|
4042
4162
|
// gateway whose every method rejects with a clear tenant error.
|
|
4043
4163
|
{
|
|
4044
4164
|
provide: import_nestjs_durable_core19.RunGateway,
|
|
4045
|
-
useFactory: /* @__PURE__ */ __name((options, store, engine, auth) => {
|
|
4165
|
+
useFactory: /* @__PURE__ */ __name((options, store, engine, auth, moduleRef) => {
|
|
4046
4166
|
if (options.store !== void 0) {
|
|
4047
4167
|
if (!store) {
|
|
4048
4168
|
throw new Error("unreachable: STATE_STORE_CANONICAL must resolve a store when options.store is set");
|
|
4049
4169
|
}
|
|
4050
4170
|
return new StoreRunGateway(store, engine);
|
|
4051
4171
|
}
|
|
4052
|
-
return options.transport ? new ProxyRunGateway(options.transport, options.partition ?? "default", options.runGatewayTimeoutMs, void 0, auth.tenantToken) : unavailableRunGateway();
|
|
4172
|
+
return options.transport ? new ProxyRunGateway(options.transport, options.partition ?? "default", options.runGatewayTimeoutMs, void 0, auth.tenantToken, options.tenancy === false ? void 0 : () => tenantFilterNow(options.tenancy, resolveAccessor(moduleRef))) : unavailableRunGateway();
|
|
4053
4173
|
}, "useFactory"),
|
|
4054
4174
|
inject: [
|
|
4055
4175
|
import_nestjs_durable_core19.DURABLE_OPTIONS_CANONICAL,
|
|
4056
4176
|
import_nestjs_durable_core19.STATE_STORE_CANONICAL,
|
|
4057
4177
|
import_nestjs_durable_core19.WorkflowEngine,
|
|
4058
|
-
TENANT_AUTH
|
|
4178
|
+
TENANT_AUTH,
|
|
4179
|
+
import_core6.ModuleRef
|
|
4059
4180
|
]
|
|
4060
4181
|
},
|
|
4061
4182
|
RunGatewayBootstrap,
|