@dudousxd/nestjs-durable 0.69.0 → 0.70.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.d.cts CHANGED
@@ -1050,9 +1050,11 @@ interface DurableModuleSharedOptions {
1050
1050
  * inspects its shape.
1051
1051
  *
1052
1052
  * **Auto-feed**: if you omit this AND `@dudousxd/nestjs-context` is installed (its accessor is bound
1053
- * to the shared `CONTEXT_ACCESSOR` token), DurableModule defaults this to a reader that builds
1054
- * `{ traceId, tenantId, userRef }` from the accessor — so a workflow dispatched within a request
1055
- * automatically carries the originating context across process boundaries. Pass your own reader to
1053
+ * to the shared `CONTEXT_ACCESSOR` token), DurableModule defaults this to nestjs-context's
1054
+ * `Context.serialize()` — the fields its `carrier: [...]` allowlist names, `traceId`, `tenantId` and
1055
+ * `userRef` by default — so a workflow dispatched within a request automatically carries the
1056
+ * originating context across process boundaries, and nothing the allowlist leaves out reaches the
1057
+ * broker. (An app's own accessor bound to the token is read directly instead.) Pass your own reader to
1056
1058
  * override the auto-feed; with neither, the carrier is omitted (unchanged behavior).
1057
1059
  *
1058
1060
  * Re-evaluated at each (re)dispatch — including a retry or a crash/scale-down resume that the engine
package/dist/index.d.ts CHANGED
@@ -1050,9 +1050,11 @@ interface DurableModuleSharedOptions {
1050
1050
  * inspects its shape.
1051
1051
  *
1052
1052
  * **Auto-feed**: if you omit this AND `@dudousxd/nestjs-context` is installed (its accessor is bound
1053
- * to the shared `CONTEXT_ACCESSOR` token), DurableModule defaults this to a reader that builds
1054
- * `{ traceId, tenantId, userRef }` from the accessor — so a workflow dispatched within a request
1055
- * automatically carries the originating context across process boundaries. Pass your own reader to
1053
+ * to the shared `CONTEXT_ACCESSOR` token), DurableModule defaults this to nestjs-context's
1054
+ * `Context.serialize()` — the fields its `carrier: [...]` allowlist names, `traceId`, `tenantId` and
1055
+ * `userRef` by default — so a workflow dispatched within a request automatically carries the
1056
+ * originating context across process boundaries, and nothing the allowlist leaves out reaches the
1057
+ * broker. (An app's own accessor bound to the token is read directly instead.) Pass your own reader to
1056
1058
  * override the auto-feed; with neither, the carrier is omitted (unchanged behavior).
1057
1059
  *
1058
1060
  * Re-evaluated at each (re)dispatch — including a retry or a crash/scale-down resume that the engine
package/dist/index.js CHANGED
@@ -931,7 +931,102 @@ __name(tenantUnsupported, "tenantUnsupported");
931
931
  // src/durable-worker.module.ts
932
932
  import { DURABLE_OPTIONS_CANONICAL, WorkflowEngine as WorkflowEngine2, bindWorkflowClass, resolveTenantToken as resolveTenantToken2, workflowName as workflowName3 } from "@dudousxd/nestjs-durable-core";
933
933
  import { Inject, Injectable as Injectable3 } from "@nestjs/common";
934
- import { DiscoveryService, MetadataScanner } from "@nestjs/core";
934
+ import { DiscoveryService, MetadataScanner, ModuleRef } from "@nestjs/core";
935
+
936
+ // src/tokens.ts
937
+ import { RunGateway } from "@dudousxd/nestjs-durable-core";
938
+ var CONTEXT_ACCESSOR = Symbol.for("@dudousxd/nestjs-context:accessor");
939
+ var TENANT_AUTH = Symbol("nestjs-durable:tenant-auth");
940
+ var RUN_GATEWAY = RunGateway;
941
+
942
+ // src/context-runtime.ts
943
+ function resolveAccessor(moduleRef) {
944
+ try {
945
+ return moduleRef.get(CONTEXT_ACCESSOR, {
946
+ strict: false
947
+ });
948
+ } catch {
949
+ return void 0;
950
+ }
951
+ }
952
+ __name(resolveAccessor, "resolveAccessor");
953
+ function contextSlotPresent() {
954
+ const slot = globalThis[Symbol.for("@dudousxd/nestjs-diagnostics:accessor")];
955
+ return !!slot?.current;
956
+ }
957
+ __name(contextSlotPresent, "contextSlotPresent");
958
+ function isContextRuntime(x) {
959
+ return !!x && typeof x.deserialize === "function";
960
+ }
961
+ __name(isContextRuntime, "isContextRuntime");
962
+ async function resolveContextRuntime() {
963
+ try {
964
+ const specifier = "@dudousxd/nestjs-context";
965
+ const mod = await import(specifier);
966
+ return isContextRuntime(mod.Context) ? mod.Context : void 0;
967
+ } catch {
968
+ return void 0;
969
+ }
970
+ }
971
+ __name(resolveContextRuntime, "resolveContextRuntime");
972
+ async function resolveContextSerializer(accessor) {
973
+ try {
974
+ const specifier = "@dudousxd/nestjs-context";
975
+ const mod = await import(specifier);
976
+ const serialize = mod.Context?.serialize;
977
+ if (typeof serialize !== "function" || mod.contextAccessor !== accessor) return void 0;
978
+ return () => serialize.call(mod.Context);
979
+ } catch {
980
+ return void 0;
981
+ }
982
+ }
983
+ __name(resolveContextSerializer, "resolveContextSerializer");
984
+ var CONTEXT_MODULE_OPTIONS = Symbol.for("@dudousxd/nestjs-context:options");
985
+ function contextHasCustomSerialize(moduleRef) {
986
+ try {
987
+ const options = moduleRef.get(CONTEXT_MODULE_OPTIONS, {
988
+ strict: false
989
+ });
990
+ return typeof options?.serialize === "function";
991
+ } catch {
992
+ return false;
993
+ }
994
+ }
995
+ __name(contextHasCustomSerialize, "contextHasCustomSerialize");
996
+ function rehydrateBridge(runtime) {
997
+ return (carrier, fn) => carrier && Object.keys(carrier).length > 0 ? runtime.deserialize(carrier, fn) : fn();
998
+ }
999
+ __name(rehydrateBridge, "rehydrateBridge");
1000
+ async function resolveContextRehydrate(moduleRef) {
1001
+ if (resolveAccessor(moduleRef) === void 0) return void 0;
1002
+ const runtime = await resolveContextRuntime();
1003
+ return runtime ? rehydrateBridge(runtime) : void 0;
1004
+ }
1005
+ __name(resolveContextRehydrate, "resolveContextRehydrate");
1006
+ function carrierFromContext(accessor, serialize, customSerialize) {
1007
+ if (serialize) {
1008
+ const carrier2 = {
1009
+ ...serialize() ?? {}
1010
+ };
1011
+ for (const key of Object.keys(carrier2)) if (carrier2[key] === void 0) delete carrier2[key];
1012
+ const tenant2 = customSerialize ? void 0 : accessor.tenant?.();
1013
+ if (carrier2.tenantId !== void 0 && tenant2 !== void 0 && tenant2.source !== "legacy") {
1014
+ carrier2.tenant = tenant2;
1015
+ }
1016
+ return carrier2;
1017
+ }
1018
+ const carrier = {};
1019
+ const traceId = accessor.traceId();
1020
+ if (traceId !== void 0) carrier.traceId = traceId;
1021
+ const tenantId = accessor.tenantId();
1022
+ if (tenantId !== void 0) carrier.tenantId = tenantId;
1023
+ const tenant = accessor.tenant?.();
1024
+ if (tenant !== void 0 && tenant.source !== "legacy") carrier.tenant = tenant;
1025
+ const userRef = accessor.userRef();
1026
+ if (userRef !== void 0) carrier.userRef = userRef;
1027
+ return carrier;
1028
+ }
1029
+ __name(carrierFromContext, "carrierFromContext");
935
1030
 
936
1031
  // src/schema-parse.ts
937
1032
  import { SchemaValidationError } from "@dudousxd/nestjs-durable-core";
@@ -1256,12 +1351,6 @@ function isStepInterceptor(target) {
1256
1351
  }
1257
1352
  __name(isStepInterceptor, "isStepInterceptor");
1258
1353
 
1259
- // src/tokens.ts
1260
- import { RunGateway } from "@dudousxd/nestjs-durable-core";
1261
- var CONTEXT_ACCESSOR = Symbol.for("@dudousxd/nestjs-context:accessor");
1262
- var TENANT_AUTH = Symbol("nestjs-durable:tenant-auth");
1263
- var RUN_GATEWAY = RunGateway;
1264
-
1265
1354
  // src/durable-worker.module.ts
1266
1355
  function _ts_decorate3(decorators, target, key, desc) {
1267
1356
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -1589,16 +1678,24 @@ function thinWorkerProviders() {
1589
1678
  // The role gate lives HERE, not just in the consumers: constructing the runtime resolves the
1590
1679
  // optional `@dudousxd/durable-worker` peer, so an operator that never runs a worker must not
1591
1680
  // reach this branch at all.
1592
- useFactory: /* @__PURE__ */ __name((options) => isPureThinWorker(options) ? createWorkerRuntime({
1593
- workflowGroup: options.partition ?? "",
1594
- // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
1595
- // module's `compensationRetries` as the engine does.
1596
- ...options.compensationRetries !== void 0 ? {
1597
- compensationRetries: options.compensationRetries
1598
- } : {}
1599
- }) : null, "useFactory"),
1681
+ useFactory: /* @__PURE__ */ __name(async (options, moduleRef) => {
1682
+ if (!isPureThinWorker(options)) return null;
1683
+ const rehydrate = await resolveContextRehydrate(moduleRef);
1684
+ return createWorkerRuntime({
1685
+ workflowGroup: options.partition ?? "",
1686
+ // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
1687
+ // module's `compensationRetries` as the engine does.
1688
+ ...options.compensationRetries !== void 0 ? {
1689
+ compensationRetries: options.compensationRetries
1690
+ } : {},
1691
+ ...rehydrate !== void 0 ? {
1692
+ rehydrate
1693
+ } : {}
1694
+ });
1695
+ }, "useFactory"),
1600
1696
  inject: [
1601
- DURABLE_OPTIONS_CANONICAL
1697
+ DURABLE_OPTIONS_CANONICAL,
1698
+ ModuleRef
1602
1699
  ]
1603
1700
  },
1604
1701
  {
@@ -1670,7 +1767,7 @@ __name(unavailableRunGateway, "unavailableRunGateway");
1670
1767
  // src/durable.module.ts
1671
1768
  import { DURABLE_OPTIONS, DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL8, RunGateway as RunGateway2, STATE_STORE, STATE_STORE_CANONICAL as STATE_STORE_CANONICAL4, TRANSPORT, TRANSPORT_CANONICAL as TRANSPORT_CANONICAL3, WorkflowEngine as WorkflowEngine7, sealDurableCopies, useDurableExecution } from "@dudousxd/nestjs-durable-core";
1672
1769
  import { Inject as Inject9, Injectable as Injectable13, Module } from "@nestjs/common";
1673
- import { DiscoveryModule, ModuleRef as ModuleRef2 } from "@nestjs/core";
1770
+ import { DiscoveryModule, ModuleRef as ModuleRef4 } from "@nestjs/core";
1674
1771
 
1675
1772
  // src/durable-step.registrar.ts
1676
1773
  import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL2, TRANSPORT_CANONICAL } from "@dudousxd/nestjs-durable-core";
@@ -1755,7 +1852,7 @@ DurableStepRegistrar = _ts_decorate4([
1755
1852
  // src/in-app-worker.ts
1756
1853
  import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL3, TRANSPORT_CANONICAL as TRANSPORT_CANONICAL2 } from "@dudousxd/nestjs-durable-core";
1757
1854
  import { Inject as Inject3, Injectable as Injectable5 } from "@nestjs/common";
1758
- import { DiscoveryService as DiscoveryService3, MetadataScanner as MetadataScanner3 } from "@nestjs/core";
1855
+ import { DiscoveryService as DiscoveryService3, MetadataScanner as MetadataScanner3, ModuleRef as ModuleRef2 } from "@nestjs/core";
1759
1856
  function _ts_decorate5(decorators, target, key, desc) {
1760
1857
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1761
1858
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
@@ -1907,16 +2004,24 @@ function inAppWorkerProviders() {
1907
2004
  // The role gate lives HERE, not just in the consumer: constructing the runtime resolves the
1908
2005
  // optional `@dudousxd/durable-worker` peer, so an operator that never runs a worker must not
1909
2006
  // reach this branch at all.
1910
- useFactory: /* @__PURE__ */ __name((options) => isCoLocatedWorker(options) ? createWorkerRuntime({
1911
- workflowGroup: options.partition ?? "",
1912
- // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
1913
- // module's `compensationRetries` as the engine does.
1914
- ...options.compensationRetries !== void 0 ? {
1915
- compensationRetries: options.compensationRetries
1916
- } : {}
1917
- }) : null, "useFactory"),
2007
+ useFactory: /* @__PURE__ */ __name(async (options, moduleRef) => {
2008
+ if (!isCoLocatedWorker(options)) return null;
2009
+ const rehydrate = await resolveContextRehydrate(moduleRef);
2010
+ return createWorkerRuntime({
2011
+ workflowGroup: options.partition ?? "",
2012
+ // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
2013
+ // module's `compensationRetries` as the engine does.
2014
+ ...options.compensationRetries !== void 0 ? {
2015
+ compensationRetries: options.compensationRetries
2016
+ } : {},
2017
+ ...rehydrate !== void 0 ? {
2018
+ rehydrate
2019
+ } : {}
2020
+ });
2021
+ }, "useFactory"),
1918
2022
  inject: [
1919
- DURABLE_OPTIONS_CANONICAL3
2023
+ DURABLE_OPTIONS_CANONICAL3,
2024
+ ModuleRef2
1920
2025
  ]
1921
2026
  },
1922
2027
  {
@@ -2941,7 +3046,7 @@ __name(settleWithin, "settleWithin");
2941
3046
  // src/sweep-leases.ts
2942
3047
  import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL4 } from "@dudousxd/nestjs-durable-core";
2943
3048
  import { Inject as Inject4, Injectable as Injectable7, Logger, Optional } from "@nestjs/common";
2944
- import { ModuleRef } from "@nestjs/core";
3049
+ import { ModuleRef as ModuleRef3 } from "@nestjs/core";
2945
3050
  function _ts_decorate7(decorators, target, key, desc) {
2946
3051
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2947
3052
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
@@ -3040,7 +3145,7 @@ SweepLeases = _ts_decorate7([
3040
3145
  _ts_param4(1, Inject4(DURABLE_OPTIONS_CANONICAL4)),
3041
3146
  _ts_metadata7("design:type", Function),
3042
3147
  _ts_metadata7("design:paramtypes", [
3043
- typeof ModuleRef === "undefined" ? Object : ModuleRef,
3148
+ typeof ModuleRef3 === "undefined" ? Object : ModuleRef3,
3044
3149
  typeof DurableModuleOptions === "undefined" ? Object : DurableModuleOptions
3045
3150
  ])
3046
3151
  ], SweepLeases);
@@ -4353,48 +4458,6 @@ function _ts_param9(paramIndex, decorator) {
4353
4458
  };
4354
4459
  }
4355
4460
  __name(_ts_param9, "_ts_param");
4356
- function resolveAccessor(moduleRef) {
4357
- try {
4358
- return moduleRef.get(CONTEXT_ACCESSOR, {
4359
- strict: false
4360
- });
4361
- } catch {
4362
- return void 0;
4363
- }
4364
- }
4365
- __name(resolveAccessor, "resolveAccessor");
4366
- function carrierFromAccessor(accessor) {
4367
- const carrier = {};
4368
- const traceId = accessor.traceId();
4369
- if (traceId !== void 0) carrier.traceId = traceId;
4370
- const tenantId = accessor.tenantId();
4371
- if (tenantId !== void 0) carrier.tenantId = tenantId;
4372
- const tenant = accessor.tenant?.();
4373
- if (tenant !== void 0 && tenant.source !== "legacy") carrier.tenant = tenant;
4374
- const userRef = accessor.userRef();
4375
- if (userRef !== void 0) carrier.userRef = userRef;
4376
- return carrier;
4377
- }
4378
- __name(carrierFromAccessor, "carrierFromAccessor");
4379
- function contextSlotPresent() {
4380
- const slot = globalThis[Symbol.for("@dudousxd/nestjs-diagnostics:accessor")];
4381
- return !!slot?.current;
4382
- }
4383
- __name(contextSlotPresent, "contextSlotPresent");
4384
- function isContextRuntime(x) {
4385
- return !!x && typeof x.deserialize === "function";
4386
- }
4387
- __name(isContextRuntime, "isContextRuntime");
4388
- async function resolveContextRuntime() {
4389
- try {
4390
- const specifier = "@dudousxd/nestjs-context";
4391
- const mod = await import(specifier);
4392
- return isContextRuntime(mod.Context) ? mod.Context : void 0;
4393
- } catch {
4394
- return void 0;
4395
- }
4396
- }
4397
- __name(resolveContextRuntime, "resolveContextRuntime");
4398
4461
  function isControlPlane(x) {
4399
4462
  return !!x && typeof x.publishControl === "function" && typeof x.onControl === "function";
4400
4463
  }
@@ -4594,7 +4657,7 @@ DurableTenancyBridge = _ts_decorate13([
4594
4657
  _ts_metadata13("design:type", Function),
4595
4658
  _ts_metadata13("design:paramtypes", [
4596
4659
  typeof DurableModuleOptions === "undefined" ? Object : DurableModuleOptions,
4597
- typeof ModuleRef2 === "undefined" ? Object : ModuleRef2
4660
+ typeof ModuleRef4 === "undefined" ? Object : ModuleRef4
4598
4661
  ])
4599
4662
  ], DurableTenancyBridge);
4600
4663
  var DurableModule = class _DurableModule {
@@ -4660,7 +4723,7 @@ var DurableModule = class _DurableModule {
4660
4723
  }), "useFactory"),
4661
4724
  inject: [
4662
4725
  DURABLE_OPTIONS_CANONICAL8,
4663
- ModuleRef2
4726
+ ModuleRef4
4664
4727
  ]
4665
4728
  },
4666
4729
  // Deprecated tokens that published releases still import resolve to the same instances:
@@ -4712,10 +4775,12 @@ var DurableModule = class _DurableModule {
4712
4775
  }
4713
4776
  const primary = transport ?? options.transports?.[0]?.transport;
4714
4777
  const accessor = resolveAccessor(moduleRef);
4715
- const context = options.context ?? (accessor ? () => carrierFromAccessor(accessor) : void 0);
4716
4778
  const runtimeNeeded = accessor !== void 0 || contextSlotPresent();
4717
4779
  const runtime = runtimeNeeded ? await resolveContextRuntime() : void 0;
4718
- const rehydrate = runtime && ((carrier, fn) => carrier && Object.keys(carrier).length > 0 ? runtime.deserialize(carrier, fn) : fn());
4780
+ const rehydrate = runtime && rehydrateBridge(runtime);
4781
+ const serialize = options.context === void 0 && accessor ? await resolveContextSerializer(accessor) : void 0;
4782
+ const customSerialize = contextHasCustomSerialize(moduleRef);
4783
+ const context = options.context ?? (accessor ? () => carrierFromContext(accessor, serialize, customSerialize) : void 0);
4719
4784
  if (options.consumers === false) {
4720
4785
  for (const t of [
4721
4786
  transport,
@@ -4776,7 +4841,7 @@ var DurableModule = class _DurableModule {
4776
4841
  DURABLE_OPTIONS_CANONICAL8,
4777
4842
  STATE_STORE_CANONICAL4,
4778
4843
  TRANSPORT_CANONICAL3,
4779
- ModuleRef2,
4844
+ ModuleRef4,
4780
4845
  TENANT_AUTH
4781
4846
  ]
4782
4847
  },
@@ -4806,7 +4871,7 @@ var DurableModule = class _DurableModule {
4806
4871
  STATE_STORE_CANONICAL4,
4807
4872
  WorkflowEngine7,
4808
4873
  TENANT_AUTH,
4809
- ModuleRef2
4874
+ ModuleRef4
4810
4875
  ]
4811
4876
  },
4812
4877
  RunGatewayBootstrap,