@kosdev-code/kos-ui-sdk 0.1.0-next.1057 → 0.1.0-next.1063

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.
@@ -7461,6 +7461,7 @@ class KosMockFutureHandle {
7461
7461
  elapsedMs = 0;
7462
7462
  endStateValue;
7463
7463
  reasonValue;
7464
+ endFields;
7464
7465
  timer;
7465
7466
  started = false;
7466
7467
  durationMs;
@@ -7476,7 +7477,8 @@ class KosMockFutureHandle {
7476
7477
  remainingTimeMs: this.remainingTimeMs,
7477
7478
  endState: this.endStateValue,
7478
7479
  reason: this.reasonValue,
7479
- clientData: this.clientData
7480
+ clientData: this.clientData,
7481
+ ...this.endFields
7480
7482
  };
7481
7483
  }
7482
7484
  start() {
@@ -7507,14 +7509,15 @@ class KosMockFutureHandle {
7507
7509
  }
7508
7510
  this.emitFrame(encodeFutureFrame(this.snapshot()));
7509
7511
  }
7510
- complete(endState = "SUCCESS", reason) {
7512
+ complete(endState = "SUCCESS", end) {
7511
7513
  if (this.done) {
7512
7514
  return;
7513
7515
  }
7514
7516
  this.stopTimer();
7515
7517
  this.done = true;
7516
7518
  this.endStateValue = endState;
7517
- this.reasonValue = reason;
7519
+ this.endFields = typeof end === "string" ? { reason: end } : end;
7520
+ this.reasonValue = this.endFields?.reason;
7518
7521
  this.remainingTimeMs = 0;
7519
7522
  if (endState === "SUCCESS") {
7520
7523
  this.progress = 1;
@@ -7572,6 +7575,39 @@ class KosMockFutureSimulator {
7572
7575
  }
7573
7576
  return void 0;
7574
7577
  }
7578
+ endWith(method, pattern, options) {
7579
+ const ended = /* @__PURE__ */ new Set();
7580
+ return this.deps.transformFuture(
7581
+ method,
7582
+ pattern,
7583
+ (future) => {
7584
+ if (ended.has(future.id)) {
7585
+ if (future.endState) {
7586
+ ended.delete(future.id);
7587
+ }
7588
+ return null;
7589
+ }
7590
+ if (future.endState === "CANCEL") {
7591
+ return void 0;
7592
+ }
7593
+ const reached = !!future.endState || options.atProgress !== void 0 && (future.progress ?? 0) >= options.atProgress;
7594
+ if (!reached) {
7595
+ return void 0;
7596
+ }
7597
+ if (!future.endState) {
7598
+ const simulated = this.futures.get(future.id);
7599
+ if (simulated) {
7600
+ simulated.dispose();
7601
+ } else {
7602
+ ended.add(future.id);
7603
+ }
7604
+ }
7605
+ const { atProgress: _atProgress, ...end } = options;
7606
+ return { ...future, reason: void 0, remainingTimeMs: 0, ...end };
7607
+ },
7608
+ { description: `futures end with ${options.endState}` }
7609
+ );
7610
+ }
7575
7611
  get active() {
7576
7612
  return [...this.futures.values()].filter((handle) => !handle.done);
7577
7613
  }
@@ -8432,7 +8468,8 @@ class KosMockRegistry {
8432
8468
  state = /* @__PURE__ */ new Map();
8433
8469
  futures = new KosMockFutureSimulator({
8434
8470
  emitFrame: (frame) => this.publishFrame(frame),
8435
- ensureCancelRoutes: () => this.ensureFutureCancelRoutes()
8471
+ ensureCancelRoutes: () => this.ensureFutureCancelRoutes(),
8472
+ transformFuture: (method, pattern, transform, options) => this.transformFuture(method, pattern, transform, options)
8436
8473
  });
8437
8474
  recorder = new KosMockRecorder();
8438
8475
  config = createConfigFacade(this);
@@ -8469,13 +8506,85 @@ class KosMockRegistry {
8469
8506
  disable: remove
8470
8507
  };
8471
8508
  }
8509
+ futureTransformers = [];
8510
+ // Requests by tracker: the only link from a future frame to its endpoint.
8511
+ trackedRequests = /* @__PURE__ */ new Map();
8512
+ transformFuture(method, pattern, transform, options = {}) {
8513
+ const registered = {
8514
+ id: options.id ?? uuid(),
8515
+ method,
8516
+ pattern,
8517
+ compiled: compileRoutePattern(pattern),
8518
+ transform,
8519
+ description: options.description
8520
+ };
8521
+ this.futureTransformers.push(registered);
8522
+ const remove = () => {
8523
+ this.futureTransformers = this.futureTransformers.filter(
8524
+ (candidate) => candidate.id !== registered.id
8525
+ );
8526
+ };
8527
+ return {
8528
+ id: registered.id,
8529
+ remove,
8530
+ enable: () => void 0,
8531
+ disable: remove
8532
+ };
8533
+ }
8534
+ /** Runs a `kos.future` frame through the future transformers. */
8535
+ transformFutureFrame(frame, headers, body) {
8536
+ let future;
8537
+ try {
8538
+ future = JSON.parse(body);
8539
+ } catch {
8540
+ return frame;
8541
+ }
8542
+ const tracker = future?.tracker;
8543
+ const request = tracker ? this.trackedRequests.get(tracker) : void 0;
8544
+ if (!tracker || !request) {
8545
+ return frame;
8546
+ }
8547
+ if (future.endState) {
8548
+ this.trackedRequests.delete(tracker);
8549
+ }
8550
+ const matching = this.futureTransformers.filter(
8551
+ (transformer) => methodMatches(transformer.method, request.method) && matchRoutePattern(transformer.compiled, request.path) !== void 0
8552
+ );
8553
+ let current = future;
8554
+ let changed = false;
8555
+ for (const transformer of matching) {
8556
+ try {
8557
+ const result = transformer.transform(current, { request });
8558
+ if (result === null) {
8559
+ return void 0;
8560
+ }
8561
+ if (result !== void 0) {
8562
+ current = result;
8563
+ changed = true;
8564
+ }
8565
+ } catch (error) {
8566
+ log$L.error(
8567
+ `KosMock future transformer for ${transformer.method} ${transformer.pattern} threw`,
8568
+ error?.message
8569
+ );
8570
+ }
8571
+ }
8572
+ if (!changed) {
8573
+ return frame;
8574
+ }
8575
+ return encodeRawFrame(
8576
+ { ...headers, [KOS_MOCK_TRANSFORMED_HEADER]: "true" },
8577
+ JSON.stringify(current)
8578
+ );
8579
+ }
8472
8580
  /**
8473
- * Interceptor seam: patches a matching inbound response frame through the
8474
- * transformer chain before it reaches the app. Returns the (possibly
8475
- * modified) frame. Internal.
8581
+ * Interceptor seam: patches a matching inbound response or future frame
8582
+ * through its transformer chain before it reaches the app. Returns the
8583
+ * (possibly modified) frame, or undefined when a future transformer dropped
8584
+ * it. Internal.
8476
8585
  */
8477
8586
  transformInboundFrame(frame) {
8478
- if (this.transformers.length === 0) {
8587
+ if (this.transformers.length === 0 && this.futureTransformers.length === 0) {
8479
8588
  return frame;
8480
8589
  }
8481
8590
  let headers;
@@ -8487,7 +8596,7 @@ class KosMockRegistry {
8487
8596
  }
8488
8597
  const responseId = headers["response-id"];
8489
8598
  if (!responseId) {
8490
- return frame;
8599
+ return headers["type"] === KOS_FUTURE_TYPE && this.futureTransformers.length > 0 ? this.transformFutureFrame(frame, headers, body) : frame;
8491
8600
  }
8492
8601
  const request = this.pendingRequests.get(responseId);
8493
8602
  this.pendingRequests.delete(responseId);
@@ -8568,7 +8677,7 @@ class KosMockRegistry {
8568
8677
  const hasRequestObservers = [...this.observers].some(
8569
8678
  (observer) => observer.onRequest
8570
8679
  );
8571
- if (!hasRequestObservers && this.transformers.length === 0) {
8680
+ if (!hasRequestObservers && this.transformers.length === 0 && this.futureTransformers.length === 0) {
8572
8681
  return;
8573
8682
  }
8574
8683
  const frame = decodeRequestFrame(data);
@@ -8585,6 +8694,15 @@ class KosMockRegistry {
8585
8694
  tracker: frame.tracker,
8586
8695
  rawBody: frame.rawBody
8587
8696
  };
8697
+ if (this.futureTransformers.length > 0 && frame.tracker) {
8698
+ if (this.trackedRequests.size >= PENDING_REQUEST_CAP) {
8699
+ const oldest = this.trackedRequests.keys().next().value;
8700
+ if (oldest !== void 0) {
8701
+ this.trackedRequests.delete(oldest);
8702
+ }
8703
+ }
8704
+ this.trackedRequests.set(frame.tracker, observation);
8705
+ }
8588
8706
  if (this.transformers.length > 0) {
8589
8707
  if (this.pendingRequests.size >= PENDING_REQUEST_CAP) {
8590
8708
  const oldest = this.pendingRequests.keys().next().value;
@@ -8830,14 +8948,16 @@ class KosMockRegistry {
8830
8948
  enabled: this.enabled,
8831
8949
  standalone: this.standalone,
8832
8950
  routes: this.list(),
8833
- transformers: this.transformers.map((transformer) => {
8834
- return {
8835
- id: transformer.id,
8836
- method: transformer.method,
8837
- pattern: transformer.pattern,
8838
- description: transformer.description
8839
- };
8840
- }),
8951
+ transformers: [...this.transformers, ...this.futureTransformers].map(
8952
+ (transformer) => {
8953
+ return {
8954
+ id: transformer.id,
8955
+ method: transformer.method,
8956
+ pattern: transformer.pattern,
8957
+ description: transformer.description
8958
+ };
8959
+ }
8960
+ ),
8841
8961
  setups: this.setups.active().map((setup) => {
8842
8962
  return { name: setup.name, url: setup.url };
8843
8963
  }),
@@ -8853,6 +8973,8 @@ class KosMockRegistry {
8853
8973
  [...this.playbacks].forEach((playback) => playback.stop());
8854
8974
  this.routes = [];
8855
8975
  this.transformers = [];
8976
+ this.futureTransformers = [];
8977
+ this.trackedRequests.clear();
8856
8978
  this.pendingRequests.clear();
8857
8979
  this.cancelRoutesRegistered = false;
8858
8980
  this.emitters.forEach((timer) => clearInterval(timer));
@@ -9167,6 +9289,9 @@ class MockInterceptorTransport {
9167
9289
  */
9168
9290
  deliver(frame) {
9169
9291
  const transformed = this.registry.transformInboundFrame(frame);
9292
+ if (transformed === void 0) {
9293
+ return;
9294
+ }
9170
9295
  this.messageHandler?.({ data: transformed });
9171
9296
  this.registry.noteInboundFrame(transformed);
9172
9297
  }
@@ -25416,4 +25541,4 @@ export {
25416
25541
  Registration as y,
25417
25542
  getStateBeanData as z
25418
25543
  };
25419
- //# sourceMappingURL=software-info-registration-B99UrnV0.js.map
25544
+ //# sourceMappingURL=software-info-registration-hfu4S9Qm.js.map