@dudousxd/nestjs-durable-core 0.91.0 → 0.92.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.cjs CHANGED
@@ -161,6 +161,7 @@ __export(index_exports, {
161
161
  findStalledRuns: () => findStalledRuns,
162
162
  foldedCount: () => foldedCount,
163
163
  getRunsByIds: () => getRunsByIds,
164
+ grantsClientTenant: () => grantsClientTenant,
164
165
  handoffHandlerId: () => handoffHandlerId,
165
166
  handoffName: () => handoffName,
166
167
  hasDurableExecutionWrappers: () => hasDurableExecutionWrappers,
@@ -276,6 +277,7 @@ __export(index_exports, {
276
277
  triggeredRunsOf: () => triggeredRunsOf,
277
278
  triggeredSubscribersOf: () => triggeredSubscribersOf,
278
279
  useDurableExecution: () => useDurableExecution,
280
+ verifyWireTenant: () => verifyWireTenant,
279
281
  warn: () => warn,
280
282
  withSingletonTracked: () => withSingletonTracked,
281
283
  withoutSingletonMarkers: () => withoutSingletonMarkers,
@@ -4923,6 +4925,21 @@ function hmacTenantVerifier(secret, capabilities) {
4923
4925
  };
4924
4926
  }
4925
4927
  __name(hmacTenantVerifier, "hmacTenantVerifier");
4928
+ async function verifyWireTenant(wireTenant, verifyTenant) {
4929
+ if (!verifyTenant) return {
4930
+ tenant: wireTenant
4931
+ };
4932
+ return await verifyTenant({
4933
+ token: wireTenant,
4934
+ tenant: wireTenant
4935
+ }) ?? null;
4936
+ }
4937
+ __name(verifyWireTenant, "verifyWireTenant");
4938
+ function grantsClientTenant(verified, tenantId) {
4939
+ const capabilities = verified?.capabilities ?? [];
4940
+ return capabilities.includes("tenancy:*") || capabilities.includes(`tenancy:${tenantId}`);
4941
+ }
4942
+ __name(grantsClientTenant, "grantsClientTenant");
4926
4943
  async function resolveWireTenant(wireTenant, verifyTenant) {
4927
4944
  if (!verifyTenant) return wireTenant;
4928
4945
  const verified = await verifyTenant({
@@ -7193,20 +7210,31 @@ var WorkflowEngine = class _WorkflowEngine {
7193
7210
  }
7194
7211
  const verifyTenant = deps.verifyTenant;
7195
7212
  this.pool.onStartRun(async (message) => {
7196
- let tenant;
7213
+ let verified;
7197
7214
  try {
7198
- tenant = await resolveWireTenant(message.tenant, verifyTenant);
7215
+ verified = await verifyWireTenant(message.tenant, verifyTenant);
7199
7216
  } catch {
7200
- tenant = null;
7217
+ verified = null;
7201
7218
  }
7202
- if (tenant === null) {
7219
+ const tenant = verified?.tenant ?? null;
7220
+ if (verified === null || tenant === null) {
7203
7221
  console.warn(`[nestjs-durable] dropped a start-run for "${message.workflow}": invalid or unauthorized tenant token`);
7204
7222
  return;
7205
7223
  }
7206
7224
  await this.start(message.workflow, message.input, message.runId ?? globalThis.crypto.randomUUID(), {
7207
7225
  namespace: tenant,
7208
7226
  tags: message.tags,
7209
- searchAttributes: message.searchAttributes
7227
+ searchAttributes: message.searchAttributes,
7228
+ // The customer a tenant process claims for the run (A4): kept only when its verified
7229
+ // token grants `tenancy:*` or `tenancy:<id>`, else the run gets no customer tenant.
7230
+ ...message.tenantId !== void 0 ? grantsClientTenant(verified, message.tenantId) ? message.tenantKind !== void 0 ? {
7231
+ tenantId: message.tenantId,
7232
+ tenantKind: message.tenantKind
7233
+ } : {
7234
+ tenantId: message.tenantId
7235
+ } : {
7236
+ tenantId: null
7237
+ } : {}
7210
7238
  });
7211
7239
  });
7212
7240
  this.controlPlane?.onControl((msg) => {
@@ -9345,7 +9373,12 @@ var WorkflowEngine = class _WorkflowEngine {
9345
9373
  * invisible run reads exactly like a missing one (not found, never forbidden).
9346
9374
  */
9347
9375
  visibleRun(runId) {
9348
- const filter = this.restrictiveFilter();
9376
+ let filter;
9377
+ try {
9378
+ filter = this.restrictiveFilter();
9379
+ } catch (err) {
9380
+ return Promise.reject(err);
9381
+ }
9349
9382
  if (!filter) return this.store.getRun(runId);
9350
9383
  return this.store.getRun(runId).then((run) => run && tenantVisible(run, filter) ? run : null);
9351
9384
  }
@@ -9392,8 +9425,11 @@ var WorkflowEngine = class _WorkflowEngine {
9392
9425
  * timer, a signal, recovery or a worker. A run with no tenant, or tenancy off, runs `fn` as is.
9393
9426
  */
9394
9427
  inRunTenant(run, fn) {
9395
- const tenant = this.tenancy ? rowTenant(run) : void 0;
9396
- return tenant && this.tenancy ? this.tenancy.run(tenant, fn) : fn();
9428
+ const tenancy = this.tenancy;
9429
+ if (!tenancy) return fn();
9430
+ const tenant = rowTenant(run);
9431
+ const turn = tenant ? () => tenancy.run(tenant, fn) : fn;
9432
+ return tenancy.system ? tenancy.system(turn) : turn();
9397
9433
  }
9398
9434
  /**
9399
9435
  * Read a run's current persisted state (or null if unknown). With tenancy on, a run of another
@@ -17390,6 +17426,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17390
17426
  findStalledRuns,
17391
17427
  foldedCount,
17392
17428
  getRunsByIds,
17429
+ grantsClientTenant,
17393
17430
  handoffHandlerId,
17394
17431
  handoffName,
17395
17432
  hasDurableExecutionWrappers,
@@ -17505,6 +17542,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17505
17542
  triggeredRunsOf,
17506
17543
  triggeredSubscribersOf,
17507
17544
  useDurableExecution,
17545
+ verifyWireTenant,
17508
17546
  warn,
17509
17547
  withSingletonTracked,
17510
17548
  withoutSingletonMarkers,