@dudousxd/nestjs-durable 0.68.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.cjs CHANGED
@@ -1045,21 +1045,116 @@ var import_nestjs_durable_core9 = require("@dudousxd/nestjs-durable-core");
1045
1045
  var import_common3 = require("@nestjs/common");
1046
1046
  var import_core = require("@nestjs/core");
1047
1047
 
1048
- // src/schema-parse.ts
1048
+ // src/tokens.ts
1049
1049
  var import_nestjs_durable_core4 = require("@dudousxd/nestjs-durable-core");
1050
+ var CONTEXT_ACCESSOR = Symbol.for("@dudousxd/nestjs-context:accessor");
1051
+ var TENANT_AUTH = Symbol("nestjs-durable:tenant-auth");
1052
+ var RUN_GATEWAY = import_nestjs_durable_core4.RunGateway;
1053
+
1054
+ // src/context-runtime.ts
1055
+ function resolveAccessor(moduleRef) {
1056
+ try {
1057
+ return moduleRef.get(CONTEXT_ACCESSOR, {
1058
+ strict: false
1059
+ });
1060
+ } catch {
1061
+ return void 0;
1062
+ }
1063
+ }
1064
+ __name(resolveAccessor, "resolveAccessor");
1065
+ function contextSlotPresent() {
1066
+ const slot = globalThis[Symbol.for("@dudousxd/nestjs-diagnostics:accessor")];
1067
+ return !!slot?.current;
1068
+ }
1069
+ __name(contextSlotPresent, "contextSlotPresent");
1070
+ function isContextRuntime(x) {
1071
+ return !!x && typeof x.deserialize === "function";
1072
+ }
1073
+ __name(isContextRuntime, "isContextRuntime");
1074
+ async function resolveContextRuntime() {
1075
+ try {
1076
+ const specifier = "@dudousxd/nestjs-context";
1077
+ const mod = await import(specifier);
1078
+ return isContextRuntime(mod.Context) ? mod.Context : void 0;
1079
+ } catch {
1080
+ return void 0;
1081
+ }
1082
+ }
1083
+ __name(resolveContextRuntime, "resolveContextRuntime");
1084
+ async function resolveContextSerializer(accessor) {
1085
+ try {
1086
+ const specifier = "@dudousxd/nestjs-context";
1087
+ const mod = await import(specifier);
1088
+ const serialize = mod.Context?.serialize;
1089
+ if (typeof serialize !== "function" || mod.contextAccessor !== accessor) return void 0;
1090
+ return () => serialize.call(mod.Context);
1091
+ } catch {
1092
+ return void 0;
1093
+ }
1094
+ }
1095
+ __name(resolveContextSerializer, "resolveContextSerializer");
1096
+ var CONTEXT_MODULE_OPTIONS = Symbol.for("@dudousxd/nestjs-context:options");
1097
+ function contextHasCustomSerialize(moduleRef) {
1098
+ try {
1099
+ const options = moduleRef.get(CONTEXT_MODULE_OPTIONS, {
1100
+ strict: false
1101
+ });
1102
+ return typeof options?.serialize === "function";
1103
+ } catch {
1104
+ return false;
1105
+ }
1106
+ }
1107
+ __name(contextHasCustomSerialize, "contextHasCustomSerialize");
1108
+ function rehydrateBridge(runtime) {
1109
+ return (carrier, fn) => carrier && Object.keys(carrier).length > 0 ? runtime.deserialize(carrier, fn) : fn();
1110
+ }
1111
+ __name(rehydrateBridge, "rehydrateBridge");
1112
+ async function resolveContextRehydrate(moduleRef) {
1113
+ if (resolveAccessor(moduleRef) === void 0) return void 0;
1114
+ const runtime = await resolveContextRuntime();
1115
+ return runtime ? rehydrateBridge(runtime) : void 0;
1116
+ }
1117
+ __name(resolveContextRehydrate, "resolveContextRehydrate");
1118
+ function carrierFromContext(accessor, serialize, customSerialize) {
1119
+ if (serialize) {
1120
+ const carrier2 = {
1121
+ ...serialize() ?? {}
1122
+ };
1123
+ for (const key of Object.keys(carrier2)) if (carrier2[key] === void 0) delete carrier2[key];
1124
+ const tenant2 = customSerialize ? void 0 : accessor.tenant?.();
1125
+ if (carrier2.tenantId !== void 0 && tenant2 !== void 0 && tenant2.source !== "legacy") {
1126
+ carrier2.tenant = tenant2;
1127
+ }
1128
+ return carrier2;
1129
+ }
1130
+ const carrier = {};
1131
+ const traceId = accessor.traceId();
1132
+ if (traceId !== void 0) carrier.traceId = traceId;
1133
+ const tenantId = accessor.tenantId();
1134
+ if (tenantId !== void 0) carrier.tenantId = tenantId;
1135
+ const tenant = accessor.tenant?.();
1136
+ if (tenant !== void 0 && tenant.source !== "legacy") carrier.tenant = tenant;
1137
+ const userRef = accessor.userRef();
1138
+ if (userRef !== void 0) carrier.userRef = userRef;
1139
+ return carrier;
1140
+ }
1141
+ __name(carrierFromContext, "carrierFromContext");
1142
+
1143
+ // src/schema-parse.ts
1144
+ var import_nestjs_durable_core5 = require("@dudousxd/nestjs-durable-core");
1050
1145
  function schemaParser(schema, kind, label) {
1051
1146
  const code = kind === "input" ? "invalid_input" : "invalid_output";
1052
1147
  if (isStandardSchema(schema)) {
1053
1148
  return async (value) => {
1054
1149
  const result = await schema["~standard"].validate(value);
1055
- if (result.issues) throw new import_nestjs_durable_core4.SchemaValidationError(label, result.issues, code);
1150
+ if (result.issues) throw new import_nestjs_durable_core5.SchemaValidationError(label, result.issues, code);
1056
1151
  return result.value;
1057
1152
  };
1058
1153
  }
1059
1154
  if (isZodLike(schema)) {
1060
1155
  return async (value) => {
1061
1156
  const result = schema.safeParse(value);
1062
- if (!result.success) throw new import_nestjs_durable_core4.SchemaValidationError(label, result.error.issues, code);
1157
+ if (!result.success) throw new import_nestjs_durable_core5.SchemaValidationError(label, result.error.issues, code);
1063
1158
  return result.data;
1064
1159
  };
1065
1160
  }
@@ -1079,7 +1174,7 @@ function classValidatorParser(cls, code, label) {
1079
1174
  }
1080
1175
  if (value === null || typeof value !== "object" || Array.isArray(value)) {
1081
1176
  const got = value === null ? "null" : Array.isArray(value) ? "an array" : typeof value;
1082
- throw new import_nestjs_durable_core4.SchemaValidationError(label, [
1177
+ throw new import_nestjs_durable_core5.SchemaValidationError(label, [
1083
1178
  {
1084
1179
  message: `expected an object, got ${got}`
1085
1180
  }
@@ -1099,7 +1194,7 @@ function classValidatorParser(cls, code, label) {
1099
1194
  ]
1100
1195
  }));
1101
1196
  const summary = issues.map((i) => i.message).join("; ");
1102
- throw new import_nestjs_durable_core4.SchemaValidationError(label, issues, code, summary);
1197
+ throw new import_nestjs_durable_core5.SchemaValidationError(label, issues, code, summary);
1103
1198
  }
1104
1199
  return instance;
1105
1200
  };
@@ -1154,7 +1249,7 @@ function scanSteps(discovery, scanner, register) {
1154
1249
  __name(scanSteps, "scanSteps");
1155
1250
 
1156
1251
  // src/entity.ts
1157
- var import_nestjs_durable_core5 = require("@dudousxd/nestjs-durable-core");
1252
+ var import_nestjs_durable_core6 = require("@dudousxd/nestjs-durable-core");
1158
1253
  var import_common2 = require("@nestjs/common");
1159
1254
  var import_reflect_metadata2 = require("reflect-metadata");
1160
1255
  function _ts_decorate2(decorators, target, key, desc) {
@@ -1237,15 +1332,15 @@ EntityService = _ts_decorate2([
1237
1332
  (0, import_common2.Injectable)(),
1238
1333
  _ts_metadata2("design:type", Function),
1239
1334
  _ts_metadata2("design:paramtypes", [
1240
- typeof import_nestjs_durable_core5.WorkflowEngine === "undefined" ? Object : import_nestjs_durable_core5.WorkflowEngine
1335
+ typeof import_nestjs_durable_core6.WorkflowEngine === "undefined" ? Object : import_nestjs_durable_core6.WorkflowEngine
1241
1336
  ])
1242
1337
  ], EntityService);
1243
1338
 
1244
1339
  // src/runtime-registration.ts
1245
- var import_nestjs_durable_core7 = require("@dudousxd/nestjs-durable-core");
1340
+ var import_nestjs_durable_core8 = require("@dudousxd/nestjs-durable-core");
1246
1341
 
1247
1342
  // src/on-error-handlers.ts
1248
- var import_nestjs_durable_core6 = require("@dudousxd/nestjs-durable-core");
1343
+ var import_nestjs_durable_core7 = require("@dudousxd/nestjs-durable-core");
1249
1344
  function onErrorHandlersOf(meta, instance, scanner) {
1250
1345
  const handlers = [];
1251
1346
  const routes = [];
@@ -1266,7 +1361,7 @@ function onErrorHandlersOf(meta, instance, scanner) {
1266
1361
  for (const r of meta.onError ?? []) {
1267
1362
  routes.push({
1268
1363
  code: r.code === void 0 || typeof r.code === "string" ? r.code : r.code.code,
1269
- workflow: (0, import_nestjs_durable_core6.workflowName)(r.workflow)
1364
+ workflow: (0, import_nestjs_durable_core7.workflowName)(r.workflow)
1270
1365
  });
1271
1366
  }
1272
1367
  return {
@@ -1336,7 +1431,7 @@ function announcedOptions(meta) {
1336
1431
  ...meta.tags
1337
1432
  ];
1338
1433
  if (meta.executionTimeout !== void 0) {
1339
- options.executionTimeoutMs = (0, import_nestjs_durable_core7.parseDuration)(meta.executionTimeout);
1434
+ options.executionTimeoutMs = (0, import_nestjs_durable_core8.parseDuration)(meta.executionTimeout);
1340
1435
  }
1341
1436
  if (meta.singleton !== void 0) {
1342
1437
  options.singleton = {
@@ -1368,12 +1463,6 @@ function isStepInterceptor(target) {
1368
1463
  }
1369
1464
  __name(isStepInterceptor, "isStepInterceptor");
1370
1465
 
1371
- // src/tokens.ts
1372
- var import_nestjs_durable_core8 = require("@dudousxd/nestjs-durable-core");
1373
- var CONTEXT_ACCESSOR = Symbol.for("@dudousxd/nestjs-context:accessor");
1374
- var TENANT_AUTH = Symbol("nestjs-durable:tenant-auth");
1375
- var RUN_GATEWAY = import_nestjs_durable_core8.RunGateway;
1376
-
1377
1466
  // src/durable-worker.module.ts
1378
1467
  function _ts_decorate3(decorators, target, key, desc) {
1379
1468
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -1593,6 +1682,9 @@ var ThinWorkerBootstrap = class {
1593
1682
  ...options.concurrency !== void 0 ? {
1594
1683
  concurrency: options.concurrency
1595
1684
  } : {},
1685
+ ...options.concurrencyByHandler !== void 0 ? {
1686
+ concurrencyByHandler: options.concurrencyByHandler
1687
+ } : {},
1596
1688
  ...options.lockDuration !== void 0 ? {
1597
1689
  lockDuration: options.lockDuration
1598
1690
  } : {},
@@ -1698,16 +1790,24 @@ function thinWorkerProviders() {
1698
1790
  // The role gate lives HERE, not just in the consumers: constructing the runtime resolves the
1699
1791
  // optional `@dudousxd/durable-worker` peer, so an operator that never runs a worker must not
1700
1792
  // reach this branch at all.
1701
- useFactory: /* @__PURE__ */ __name((options) => isPureThinWorker(options) ? createWorkerRuntime({
1702
- workflowGroup: options.partition ?? "",
1703
- // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
1704
- // module's `compensationRetries` as the engine does.
1705
- ...options.compensationRetries !== void 0 ? {
1706
- compensationRetries: options.compensationRetries
1707
- } : {}
1708
- }) : null, "useFactory"),
1793
+ useFactory: /* @__PURE__ */ __name(async (options, moduleRef) => {
1794
+ if (!isPureThinWorker(options)) return null;
1795
+ const rehydrate = await resolveContextRehydrate(moduleRef);
1796
+ return createWorkerRuntime({
1797
+ workflowGroup: options.partition ?? "",
1798
+ // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
1799
+ // module's `compensationRetries` as the engine does.
1800
+ ...options.compensationRetries !== void 0 ? {
1801
+ compensationRetries: options.compensationRetries
1802
+ } : {},
1803
+ ...rehydrate !== void 0 ? {
1804
+ rehydrate
1805
+ } : {}
1806
+ });
1807
+ }, "useFactory"),
1709
1808
  inject: [
1710
- import_nestjs_durable_core9.DURABLE_OPTIONS_CANONICAL
1809
+ import_nestjs_durable_core9.DURABLE_OPTIONS_CANONICAL,
1810
+ import_core.ModuleRef
1711
1811
  ]
1712
1812
  },
1713
1813
  {
@@ -1957,6 +2057,9 @@ var InAppWorkerBootstrap = class {
1957
2057
  ...options.concurrency !== void 0 ? {
1958
2058
  concurrency: options.concurrency
1959
2059
  } : {},
2060
+ ...options.concurrencyByHandler !== void 0 ? {
2061
+ concurrencyByHandler: options.concurrencyByHandler
2062
+ } : {},
1960
2063
  ...options.lockDuration !== void 0 ? {
1961
2064
  lockDuration: options.lockDuration
1962
2065
  } : {},
@@ -2013,16 +2116,24 @@ function inAppWorkerProviders() {
2013
2116
  // The role gate lives HERE, not just in the consumer: constructing the runtime resolves the
2014
2117
  // optional `@dudousxd/durable-worker` peer, so an operator that never runs a worker must not
2015
2118
  // reach this branch at all.
2016
- useFactory: /* @__PURE__ */ __name((options) => isCoLocatedWorker(options) ? createWorkerRuntime({
2017
- workflowGroup: options.partition ?? "",
2018
- // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
2019
- // module's `compensationRetries` as the engine does.
2020
- ...options.compensationRetries !== void 0 ? {
2021
- compensationRetries: options.compensationRetries
2022
- } : {}
2023
- }) : null, "useFactory"),
2119
+ useFactory: /* @__PURE__ */ __name(async (options, moduleRef) => {
2120
+ if (!isCoLocatedWorker(options)) return null;
2121
+ const rehydrate = await resolveContextRehydrate(moduleRef);
2122
+ return createWorkerRuntime({
2123
+ workflowGroup: options.partition ?? "",
2124
+ // The worker unwinds a failed turn's `localStep` undos itself, so it takes the
2125
+ // module's `compensationRetries` as the engine does.
2126
+ ...options.compensationRetries !== void 0 ? {
2127
+ compensationRetries: options.compensationRetries
2128
+ } : {},
2129
+ ...rehydrate !== void 0 ? {
2130
+ rehydrate
2131
+ } : {}
2132
+ });
2133
+ }, "useFactory"),
2024
2134
  inject: [
2025
- import_nestjs_durable_core11.DURABLE_OPTIONS_CANONICAL
2135
+ import_nestjs_durable_core11.DURABLE_OPTIONS_CANONICAL,
2136
+ import_core3.ModuleRef
2026
2137
  ]
2027
2138
  },
2028
2139
  {
@@ -4459,48 +4570,6 @@ function _ts_param9(paramIndex, decorator) {
4459
4570
  };
4460
4571
  }
4461
4572
  __name(_ts_param9, "_ts_param");
4462
- function resolveAccessor(moduleRef) {
4463
- try {
4464
- return moduleRef.get(CONTEXT_ACCESSOR, {
4465
- strict: false
4466
- });
4467
- } catch {
4468
- return void 0;
4469
- }
4470
- }
4471
- __name(resolveAccessor, "resolveAccessor");
4472
- function carrierFromAccessor(accessor) {
4473
- const carrier = {};
4474
- const traceId = accessor.traceId();
4475
- if (traceId !== void 0) carrier.traceId = traceId;
4476
- const tenantId = accessor.tenantId();
4477
- if (tenantId !== void 0) carrier.tenantId = tenantId;
4478
- const tenant = accessor.tenant?.();
4479
- if (tenant !== void 0 && tenant.source !== "legacy") carrier.tenant = tenant;
4480
- const userRef = accessor.userRef();
4481
- if (userRef !== void 0) carrier.userRef = userRef;
4482
- return carrier;
4483
- }
4484
- __name(carrierFromAccessor, "carrierFromAccessor");
4485
- function contextSlotPresent() {
4486
- const slot = globalThis[Symbol.for("@dudousxd/nestjs-diagnostics:accessor")];
4487
- return !!slot?.current;
4488
- }
4489
- __name(contextSlotPresent, "contextSlotPresent");
4490
- function isContextRuntime(x) {
4491
- return !!x && typeof x.deserialize === "function";
4492
- }
4493
- __name(isContextRuntime, "isContextRuntime");
4494
- async function resolveContextRuntime() {
4495
- try {
4496
- const specifier = "@dudousxd/nestjs-context";
4497
- const mod = await import(specifier);
4498
- return isContextRuntime(mod.Context) ? mod.Context : void 0;
4499
- } catch {
4500
- return void 0;
4501
- }
4502
- }
4503
- __name(resolveContextRuntime, "resolveContextRuntime");
4504
4573
  function isControlPlane(x) {
4505
4574
  return !!x && typeof x.publishControl === "function" && typeof x.onControl === "function";
4506
4575
  }
@@ -4818,10 +4887,12 @@ var DurableModule = class _DurableModule {
4818
4887
  }
4819
4888
  const primary = transport ?? options.transports?.[0]?.transport;
4820
4889
  const accessor = resolveAccessor(moduleRef);
4821
- const context = options.context ?? (accessor ? () => carrierFromAccessor(accessor) : void 0);
4822
4890
  const runtimeNeeded = accessor !== void 0 || contextSlotPresent();
4823
4891
  const runtime = runtimeNeeded ? await resolveContextRuntime() : void 0;
4824
- const rehydrate = runtime && ((carrier, fn) => carrier && Object.keys(carrier).length > 0 ? runtime.deserialize(carrier, fn) : fn());
4892
+ const rehydrate = runtime && rehydrateBridge(runtime);
4893
+ const serialize = options.context === void 0 && accessor ? await resolveContextSerializer(accessor) : void 0;
4894
+ const customSerialize = contextHasCustomSerialize(moduleRef);
4895
+ const context = options.context ?? (accessor ? () => carrierFromContext(accessor, serialize, customSerialize) : void 0);
4825
4896
  if (options.consumers === false) {
4826
4897
  for (const t of [
4827
4898
  transport,