@dudousxd/nestjs-durable-core 0.49.1 → 0.49.3

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
@@ -57,17 +57,21 @@ __export(index_exports, {
57
57
  attributeOperand: () => attributeOperand,
58
58
  bindWorkflowClass: () => bindWorkflowClass,
59
59
  breakpointToken: () => breakpointToken,
60
+ classifyWaiterToken: () => classifyWaiterToken,
60
61
  collectMetrics: () => collectMetrics,
61
62
  createStepLogger: () => createStepLogger,
62
63
  currentWorkflowCtx: () => currentWorkflowCtx,
63
64
  eventMatchOf: () => eventMatchOf,
64
65
  eventMatches: () => eventMatches,
66
+ eventNameOf: () => eventNameOf,
65
67
  eventPrefix: () => eventPrefix,
66
68
  eventToken: () => eventToken,
69
+ indexWaitersByRun: () => indexWaitersByRun,
67
70
  matchesAttributes: () => matchesAttributes,
68
71
  normalizeAttributeRows: () => normalizeAttributeRows,
69
72
  parseDuration: () => parseDuration,
70
73
  prevCronFireMs: () => prevCronFireMs,
74
+ resolveRunWaiting: () => resolveRunWaiting,
71
75
  runInWorkflowCtx: () => runInWorkflowCtx,
72
76
  runSchedules: () => runSchedules,
73
77
  runStepHandler: () => runStepHandler,
@@ -861,6 +865,14 @@ function eventToken(name, match, runId, seq) {
861
865
  return `${eventPrefix(name)}${enc(JSON.stringify(match ?? {}))}:${runId}#${seq}`;
862
866
  }
863
867
  __name(eventToken, "eventToken");
868
+ function eventNameOf(token) {
869
+ try {
870
+ return dec(token.split(":")[1] ?? "");
871
+ } catch {
872
+ return token;
873
+ }
874
+ }
875
+ __name(eventNameOf, "eventNameOf");
864
876
  function eventMatchOf(token) {
865
877
  try {
866
878
  return JSON.parse(dec(token.split(":")[2] ?? ""));
@@ -1801,6 +1813,7 @@ var WorkflowEngine = class {
1801
1813
  leaseMs;
1802
1814
  maxRecoveryAttempts;
1803
1815
  remoteAdvanceSilenceMs;
1816
+ reconcileMs;
1804
1817
  webhookUrl;
1805
1818
  traceparent;
1806
1819
  context;
@@ -1871,6 +1884,7 @@ var WorkflowEngine = class {
1871
1884
  this.leaseMs = deps.leaseMs ?? 3e4;
1872
1885
  this.maxRecoveryAttempts = deps.maxRecoveryAttempts;
1873
1886
  this.remoteAdvanceSilenceMs = deps.remoteAdvanceSilenceMs;
1887
+ this.reconcileMs = deps.reconcileMs === void 0 ? 3e5 : deps.reconcileMs || void 0;
1874
1888
  this.webhookUrl = deps.webhookUrl;
1875
1889
  this.traceparent = deps.traceparent;
1876
1890
  this.context = deps.context;
@@ -3215,7 +3229,7 @@ var WorkflowEngine = class {
3215
3229
  }
3216
3230
  await this.store.updateRun(run.id, {
3217
3231
  status: "suspended",
3218
- wakeAt: outcome.wakeAt,
3232
+ wakeAt: this.reconcileWakeAt(outcome.wakeAt),
3219
3233
  updatedAt
3220
3234
  });
3221
3235
  this.emit({
@@ -3229,6 +3243,16 @@ var WorkflowEngine = class {
3229
3243
  status: "suspended"
3230
3244
  };
3231
3245
  }
3246
+ /**
3247
+ * A wakeAt to persist on a suspend: the natural timer if the turn produced one (a `ctx.sleep`, a
3248
+ * remote-step deadline), else the {@link reconcileMs} fallback so an event-waiting run (child/signal/
3249
+ * timeout-less remote step) can't be orphaned forever if its wake is lost. `undefined` only when the
3250
+ * fallback is disabled (`reconcileMs: 0`). See {@link WorkflowEngineDeps.reconcileMs}.
3251
+ */
3252
+ reconcileWakeAt(wakeAt) {
3253
+ if (wakeAt != null) return wakeAt;
3254
+ return this.reconcileMs != null ? this.clock() + this.reconcileMs : void 0;
3255
+ }
3232
3256
  async runRemoteExecution(run, registered) {
3233
3257
  if (run.status === "cancelling") {
3234
3258
  const error = {
@@ -4364,6 +4388,42 @@ var TERMINAL_RUN_STATUSES = [
4364
4388
  "dead"
4365
4389
  ];
4366
4390
 
4391
+ // src/run-waiting.ts
4392
+ var WEBHOOK_PREFIX = "wh:";
4393
+ var CHILD_PREFIX = "child:";
4394
+ var EVENT_PREFIX = "event:";
4395
+ function classifyWaiterToken(token) {
4396
+ if (token.startsWith(WEBHOOK_PREFIX)) return {
4397
+ on: "webhook",
4398
+ name: token
4399
+ };
4400
+ if (token.startsWith(CHILD_PREFIX)) return {
4401
+ on: "child",
4402
+ name: token.slice(CHILD_PREFIX.length)
4403
+ };
4404
+ if (token.startsWith(EVENT_PREFIX)) return {
4405
+ on: "signal",
4406
+ name: eventNameOf(token)
4407
+ };
4408
+ return {
4409
+ on: "signal",
4410
+ name: token
4411
+ };
4412
+ }
4413
+ __name(classifyWaiterToken, "classifyWaiterToken");
4414
+ function resolveRunWaiting(run, waiterByRun) {
4415
+ if (run.status !== "suspended") return void 0;
4416
+ const waiter = waiterByRun.get(run.id);
4417
+ return waiter ? classifyWaiterToken(waiter.token) : void 0;
4418
+ }
4419
+ __name(resolveRunWaiting, "resolveRunWaiting");
4420
+ function indexWaitersByRun(waiters) {
4421
+ const byRun = /* @__PURE__ */ new Map();
4422
+ for (const w of waiters) if (!byRun.has(w.runId)) byRun.set(w.runId, w);
4423
+ return byRun;
4424
+ }
4425
+ __name(indexWaitersByRun, "indexWaitersByRun");
4426
+
4367
4427
  // src/codec-state-store.ts
4368
4428
  var CodecStateStore = class {
4369
4429
  static {
@@ -5119,17 +5179,21 @@ var InMemoryTransport = class {
5119
5179
  attributeOperand,
5120
5180
  bindWorkflowClass,
5121
5181
  breakpointToken,
5182
+ classifyWaiterToken,
5122
5183
  collectMetrics,
5123
5184
  createStepLogger,
5124
5185
  currentWorkflowCtx,
5125
5186
  eventMatchOf,
5126
5187
  eventMatches,
5188
+ eventNameOf,
5127
5189
  eventPrefix,
5128
5190
  eventToken,
5191
+ indexWaitersByRun,
5129
5192
  matchesAttributes,
5130
5193
  normalizeAttributeRows,
5131
5194
  parseDuration,
5132
5195
  prevCronFireMs,
5196
+ resolveRunWaiting,
5133
5197
  runInWorkflowCtx,
5134
5198
  runSchedules,
5135
5199
  runStepHandler,