@dudousxd/nestjs-durable-core 0.93.0 → 0.94.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
@@ -82,6 +82,7 @@ __export(index_exports, {
82
82
  RemoteStepTimeout: () => RemoteStepTimeout,
83
83
  RemoteWorkflowExecutor: () => RemoteWorkflowExecutor,
84
84
  RemoteWorkflowTimeout: () => RemoteWorkflowTimeout,
85
+ RunAppStamper: () => RunAppStamper,
85
86
  RunGateway: () => RunGateway,
86
87
  SINGLETON_ACTIVE_STATUSES: () => SINGLETON_ACTIVE_STATUSES,
87
88
  SINGLETON_ADMITTED_TAG: () => SINGLETON_ADMITTED_TAG,
@@ -138,6 +139,7 @@ __export(index_exports, {
138
139
  codeOf: () => codeOf,
139
140
  collectMetrics: () => collectMetrics,
140
141
  concurrencyTag: () => concurrencyTag,
142
+ controlPlaneAttribution: () => controlPlaneAttribution,
141
143
  controlPlaneDescriptor: () => controlPlaneDescriptor,
142
144
  countRuns: () => countRuns,
143
145
  coveredTokens: () => coveredTokens,
@@ -247,6 +249,7 @@ __export(index_exports, {
247
249
  runOutsideWorkflowCtx: () => runOutsideWorkflowCtx,
248
250
  runSchedules: () => runSchedules,
249
251
  runStepHandler: () => runStepHandler,
252
+ runTraceId: () => runTraceId,
250
253
  runValueFacetsFromRuns: () => runValueFacetsFromRuns,
251
254
  runWaitPrefix: () => runWaitPrefix,
252
255
  runWaitTargetOf: () => runWaitTargetOf,
@@ -272,6 +275,7 @@ __export(index_exports, {
272
275
  summarizeFan: () => summarizeFan,
273
276
  summarizeRunFromStore: () => summarizeRunFromStore,
274
277
  summarizeTimeline: () => summarizeTimeline,
278
+ tenantAppOf: () => tenantAppOf,
275
279
  tenantGroup: () => tenantGroup,
276
280
  tenantIdsFor: () => tenantIdsFor,
277
281
  tenantVisible: () => tenantVisible,
@@ -16407,6 +16411,144 @@ function collectMetrics(engine) {
16407
16411
  }
16408
16412
  __name(collectMetrics, "collectMetrics");
16409
16413
 
16414
+ // src/run-app.ts
16415
+ var import_node_crypto2 = require("crypto");
16416
+ function runTraceId(runId) {
16417
+ const hex = (0, import_node_crypto2.createHash)("sha256").update(`durable:run:${runId}`).digest("hex").slice(0, 32);
16418
+ return /^0+$/.test(hex) ? `${hex.slice(0, 31)}1` : hex;
16419
+ }
16420
+ __name(runTraceId, "runTraceId");
16421
+ function tenantAppOf(namespace, own = []) {
16422
+ if (namespace === void 0 || namespace === "" || namespace === "default") return void 0;
16423
+ const mine = Array.isArray(own) ? own.includes(namespace) : own.has(namespace);
16424
+ return mine ? void 0 : namespace;
16425
+ }
16426
+ __name(tenantAppOf, "tenantAppOf");
16427
+ function positive(value, fallback) {
16428
+ return value !== void 0 && Number.isFinite(value) ? Math.max(1, value) : fallback;
16429
+ }
16430
+ __name(positive, "positive");
16431
+ var FAILED = Symbol("failed");
16432
+ var RunAppStamper = class {
16433
+ static {
16434
+ __name(this, "RunAppStamper");
16435
+ }
16436
+ #lookup;
16437
+ #own;
16438
+ #max;
16439
+ #timeoutMs;
16440
+ /** runId → app (`null`: the control plane's own run). */
16441
+ #apps = /* @__PURE__ */ new Map();
16442
+ /** runId → the last queued event of that run, while one is waiting on a read. */
16443
+ #tails = /* @__PURE__ */ new Map();
16444
+ constructor(options) {
16445
+ this.#lookup = (runId) => options.namespaceOf(runId);
16446
+ this.#own = new Set(options.own ?? []);
16447
+ this.#max = positive(options.cacheSize, 1e4);
16448
+ this.#timeoutMs = positive(options.lookupTimeoutMs, 5e3);
16449
+ }
16450
+ /** Call `forward` with `event` stamped with its app (when it has one), now or once it is known. */
16451
+ stamp(event, forward) {
16452
+ const runId = event.runId;
16453
+ const tail = this.#tails.get(runId);
16454
+ if (tail === void 0) {
16455
+ const known = this.#known(event);
16456
+ if (known !== void 0) {
16457
+ this.#forward(event, known, forward);
16458
+ return;
16459
+ }
16460
+ }
16461
+ const next = (tail ?? Promise.resolve()).then(async () => {
16462
+ try {
16463
+ let app = this.#known(event);
16464
+ if (app === void 0) {
16465
+ const namespace = await this.#read(runId);
16466
+ if (namespace === FAILED) {
16467
+ app = null;
16468
+ } else {
16469
+ app = tenantAppOf(namespace, this.#own) ?? null;
16470
+ this.#remember(runId, app);
16471
+ }
16472
+ }
16473
+ this.#forward(event, app, forward);
16474
+ } catch {
16475
+ }
16476
+ });
16477
+ this.#tails.set(runId, next);
16478
+ void next.then(() => {
16479
+ if (this.#tails.get(runId) === next) this.#tails.delete(runId);
16480
+ });
16481
+ }
16482
+ #forward(event, app, forward) {
16483
+ try {
16484
+ forward(this.#with(event, app));
16485
+ } finally {
16486
+ if (event.type === "run.completed" || event.type === "run.failed") {
16487
+ this.#apps.delete(event.runId);
16488
+ }
16489
+ }
16490
+ }
16491
+ /** The run's namespace, or {@link FAILED} when the read threw or did not answer in time. */
16492
+ async #read(runId) {
16493
+ let timer;
16494
+ const timeout = new Promise((resolve) => {
16495
+ timer = setTimeout(() => resolve(FAILED), this.#timeoutMs);
16496
+ timer.unref?.();
16497
+ });
16498
+ try {
16499
+ return await Promise.race([
16500
+ Promise.resolve().then(() => this.#lookup(runId)).catch(() => FAILED),
16501
+ timeout
16502
+ ]);
16503
+ } finally {
16504
+ clearTimeout(timer);
16505
+ }
16506
+ }
16507
+ /** The app of `event`'s run when it is known without a read (`null`: no app). */
16508
+ #known(event) {
16509
+ if (event.namespace !== void 0) {
16510
+ const app = tenantAppOf(event.namespace, this.#own) ?? null;
16511
+ this.#remember(event.runId, app);
16512
+ return app;
16513
+ }
16514
+ return this.#apps.get(event.runId);
16515
+ }
16516
+ /** A tenant's event carries its app, and its partition as `namespace` (step events have none). */
16517
+ #with(event, app) {
16518
+ if (app === null) return event;
16519
+ return event.namespace === void 0 ? {
16520
+ ...event,
16521
+ namespace: app,
16522
+ app
16523
+ } : {
16524
+ ...event,
16525
+ app
16526
+ };
16527
+ }
16528
+ #remember(runId, app) {
16529
+ if (this.#apps.has(runId)) this.#apps.delete(runId);
16530
+ else if (this.#apps.size >= this.#max) {
16531
+ const oldest = this.#apps.keys().next().value;
16532
+ if (oldest !== void 0) this.#apps.delete(oldest);
16533
+ }
16534
+ this.#apps.set(runId, app);
16535
+ }
16536
+ };
16537
+ function controlPlaneAttribution(options, getRun) {
16538
+ if (options?.topology !== "control-plane") return void 0;
16539
+ const own = [
16540
+ options.namespace,
16541
+ options.partition
16542
+ ].filter((ns) => typeof ns === "string" && ns !== "");
16543
+ return {
16544
+ namespaceOf: /* @__PURE__ */ __name(async (runId) => (await getRun(runId))?.namespace, "namespaceOf"),
16545
+ ...own.length > 0 ? {
16546
+ own
16547
+ } : {}
16548
+ };
16549
+ }
16550
+ __name(controlPlaneAttribution, "controlPlaneAttribution");
16551
+
16410
16552
  // src/search-attributes.ts
16411
16553
  function compare(actual, filter) {
16412
16554
  if (filter.op === "in") return filter.values.some((v) => actual === v);
@@ -17538,6 +17680,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17538
17680
  RemoteStepTimeout,
17539
17681
  RemoteWorkflowExecutor,
17540
17682
  RemoteWorkflowTimeout,
17683
+ RunAppStamper,
17541
17684
  RunGateway,
17542
17685
  SINGLETON_ACTIVE_STATUSES,
17543
17686
  SINGLETON_ADMITTED_TAG,
@@ -17594,6 +17737,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17594
17737
  codeOf,
17595
17738
  collectMetrics,
17596
17739
  concurrencyTag,
17740
+ controlPlaneAttribution,
17597
17741
  controlPlaneDescriptor,
17598
17742
  countRuns,
17599
17743
  coveredTokens,
@@ -17703,6 +17847,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17703
17847
  runOutsideWorkflowCtx,
17704
17848
  runSchedules,
17705
17849
  runStepHandler,
17850
+ runTraceId,
17706
17851
  runValueFacetsFromRuns,
17707
17852
  runWaitPrefix,
17708
17853
  runWaitTargetOf,
@@ -17728,6 +17873,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17728
17873
  summarizeFan,
17729
17874
  summarizeRunFromStore,
17730
17875
  summarizeTimeline,
17876
+ tenantAppOf,
17731
17877
  tenantGroup,
17732
17878
  tenantIdsFor,
17733
17879
  tenantVisible,