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

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.
@@ -7573,6 +7573,43 @@ class KosMockFutureSimulator {
7573
7573
  }
7574
7574
  return void 0;
7575
7575
  }
7576
+ endWith(method, pattern, options) {
7577
+ const ended = /* @__PURE__ */ new Set();
7578
+ return this.deps.transformFuture(
7579
+ method,
7580
+ pattern,
7581
+ (future) => {
7582
+ if (ended.has(future.id)) {
7583
+ if (future.endState) {
7584
+ ended.delete(future.id);
7585
+ }
7586
+ return null;
7587
+ }
7588
+ if (future.endState === "CANCEL") {
7589
+ return void 0;
7590
+ }
7591
+ const reached = !!future.endState || options.atProgress !== void 0 && (future.progress ?? 0) >= options.atProgress;
7592
+ if (!reached) {
7593
+ return void 0;
7594
+ }
7595
+ if (!future.endState) {
7596
+ const simulated = this.futures.get(future.id);
7597
+ if (simulated) {
7598
+ simulated.dispose();
7599
+ } else {
7600
+ ended.add(future.id);
7601
+ }
7602
+ }
7603
+ return {
7604
+ ...future,
7605
+ endState: options.endState,
7606
+ reason: options.reason,
7607
+ remainingTimeMs: 0
7608
+ };
7609
+ },
7610
+ { description: `futures end with ${options.endState}` }
7611
+ );
7612
+ }
7576
7613
  get active() {
7577
7614
  return [...this.futures.values()].filter((handle) => !handle.done);
7578
7615
  }
@@ -8433,7 +8470,8 @@ class KosMockRegistry {
8433
8470
  state = /* @__PURE__ */ new Map();
8434
8471
  futures = new KosMockFutureSimulator({
8435
8472
  emitFrame: (frame) => this.publishFrame(frame),
8436
- ensureCancelRoutes: () => this.ensureFutureCancelRoutes()
8473
+ ensureCancelRoutes: () => this.ensureFutureCancelRoutes(),
8474
+ transformFuture: (method, pattern, transform, options) => this.transformFuture(method, pattern, transform, options)
8437
8475
  });
8438
8476
  recorder = new KosMockRecorder();
8439
8477
  config = createConfigFacade(this);
@@ -8470,13 +8508,85 @@ class KosMockRegistry {
8470
8508
  disable: remove
8471
8509
  };
8472
8510
  }
8511
+ futureTransformers = [];
8512
+ // Requests by tracker: the only link from a future frame to its endpoint.
8513
+ trackedRequests = /* @__PURE__ */ new Map();
8514
+ transformFuture(method, pattern, transform, options = {}) {
8515
+ const registered = {
8516
+ id: options.id ?? uuid(),
8517
+ method,
8518
+ pattern,
8519
+ compiled: compileRoutePattern(pattern),
8520
+ transform,
8521
+ description: options.description
8522
+ };
8523
+ this.futureTransformers.push(registered);
8524
+ const remove = () => {
8525
+ this.futureTransformers = this.futureTransformers.filter(
8526
+ (candidate) => candidate.id !== registered.id
8527
+ );
8528
+ };
8529
+ return {
8530
+ id: registered.id,
8531
+ remove,
8532
+ enable: () => void 0,
8533
+ disable: remove
8534
+ };
8535
+ }
8536
+ /** Runs a `kos.future` frame through the future transformers. */
8537
+ transformFutureFrame(frame, headers, body) {
8538
+ let future;
8539
+ try {
8540
+ future = JSON.parse(body);
8541
+ } catch {
8542
+ return frame;
8543
+ }
8544
+ const tracker = future?.tracker;
8545
+ const request = tracker ? this.trackedRequests.get(tracker) : void 0;
8546
+ if (!tracker || !request) {
8547
+ return frame;
8548
+ }
8549
+ if (future.endState) {
8550
+ this.trackedRequests.delete(tracker);
8551
+ }
8552
+ const matching = this.futureTransformers.filter(
8553
+ (transformer) => methodMatches(transformer.method, request.method) && matchRoutePattern(transformer.compiled, request.path) !== void 0
8554
+ );
8555
+ let current = future;
8556
+ let changed = false;
8557
+ for (const transformer of matching) {
8558
+ try {
8559
+ const result = transformer.transform(current, { request });
8560
+ if (result === null) {
8561
+ return void 0;
8562
+ }
8563
+ if (result !== void 0) {
8564
+ current = result;
8565
+ changed = true;
8566
+ }
8567
+ } catch (error) {
8568
+ log$L.error(
8569
+ `KosMock future transformer for ${transformer.method} ${transformer.pattern} threw`,
8570
+ error?.message
8571
+ );
8572
+ }
8573
+ }
8574
+ if (!changed) {
8575
+ return frame;
8576
+ }
8577
+ return encodeRawFrame(
8578
+ { ...headers, [KOS_MOCK_TRANSFORMED_HEADER]: "true" },
8579
+ JSON.stringify(current)
8580
+ );
8581
+ }
8473
8582
  /**
8474
- * Interceptor seam: patches a matching inbound response frame through the
8475
- * transformer chain before it reaches the app. Returns the (possibly
8476
- * modified) frame. Internal.
8583
+ * Interceptor seam: patches a matching inbound response or future frame
8584
+ * through its transformer chain before it reaches the app. Returns the
8585
+ * (possibly modified) frame, or undefined when a future transformer dropped
8586
+ * it. Internal.
8477
8587
  */
8478
8588
  transformInboundFrame(frame) {
8479
- if (this.transformers.length === 0) {
8589
+ if (this.transformers.length === 0 && this.futureTransformers.length === 0) {
8480
8590
  return frame;
8481
8591
  }
8482
8592
  let headers;
@@ -8488,7 +8598,7 @@ class KosMockRegistry {
8488
8598
  }
8489
8599
  const responseId = headers["response-id"];
8490
8600
  if (!responseId) {
8491
- return frame;
8601
+ return headers["type"] === KOS_FUTURE_TYPE && this.futureTransformers.length > 0 ? this.transformFutureFrame(frame, headers, body) : frame;
8492
8602
  }
8493
8603
  const request = this.pendingRequests.get(responseId);
8494
8604
  this.pendingRequests.delete(responseId);
@@ -8569,7 +8679,7 @@ class KosMockRegistry {
8569
8679
  const hasRequestObservers = [...this.observers].some(
8570
8680
  (observer) => observer.onRequest
8571
8681
  );
8572
- if (!hasRequestObservers && this.transformers.length === 0) {
8682
+ if (!hasRequestObservers && this.transformers.length === 0 && this.futureTransformers.length === 0) {
8573
8683
  return;
8574
8684
  }
8575
8685
  const frame = decodeRequestFrame(data);
@@ -8586,6 +8696,15 @@ class KosMockRegistry {
8586
8696
  tracker: frame.tracker,
8587
8697
  rawBody: frame.rawBody
8588
8698
  };
8699
+ if (this.futureTransformers.length > 0 && frame.tracker) {
8700
+ if (this.trackedRequests.size >= PENDING_REQUEST_CAP) {
8701
+ const oldest = this.trackedRequests.keys().next().value;
8702
+ if (oldest !== void 0) {
8703
+ this.trackedRequests.delete(oldest);
8704
+ }
8705
+ }
8706
+ this.trackedRequests.set(frame.tracker, observation);
8707
+ }
8589
8708
  if (this.transformers.length > 0) {
8590
8709
  if (this.pendingRequests.size >= PENDING_REQUEST_CAP) {
8591
8710
  const oldest = this.pendingRequests.keys().next().value;
@@ -8831,14 +8950,16 @@ class KosMockRegistry {
8831
8950
  enabled: this.enabled,
8832
8951
  standalone: this.standalone,
8833
8952
  routes: this.list(),
8834
- transformers: this.transformers.map((transformer) => {
8835
- return {
8836
- id: transformer.id,
8837
- method: transformer.method,
8838
- pattern: transformer.pattern,
8839
- description: transformer.description
8840
- };
8841
- }),
8953
+ transformers: [...this.transformers, ...this.futureTransformers].map(
8954
+ (transformer) => {
8955
+ return {
8956
+ id: transformer.id,
8957
+ method: transformer.method,
8958
+ pattern: transformer.pattern,
8959
+ description: transformer.description
8960
+ };
8961
+ }
8962
+ ),
8842
8963
  setups: this.setups.active().map((setup) => {
8843
8964
  return { name: setup.name, url: setup.url };
8844
8965
  }),
@@ -8854,6 +8975,8 @@ class KosMockRegistry {
8854
8975
  [...this.playbacks].forEach((playback) => playback.stop());
8855
8976
  this.routes = [];
8856
8977
  this.transformers = [];
8978
+ this.futureTransformers = [];
8979
+ this.trackedRequests.clear();
8857
8980
  this.pendingRequests.clear();
8858
8981
  this.cancelRoutesRegistered = false;
8859
8982
  this.emitters.forEach((timer) => clearInterval(timer));
@@ -9168,6 +9291,9 @@ class MockInterceptorTransport {
9168
9291
  */
9169
9292
  deliver(frame) {
9170
9293
  const transformed = this.registry.transformInboundFrame(frame);
9294
+ if (transformed === void 0) {
9295
+ return;
9296
+ }
9171
9297
  this.messageHandler?.({ data: transformed });
9172
9298
  this.registry.noteInboundFrame(transformed);
9173
9299
  }
@@ -25412,4 +25538,4 @@ exports.waitForRequest = waitForRequest;
25412
25538
  exports.whenReady = whenReady;
25413
25539
  exports.withCancellation = withCancellation;
25414
25540
  exports.wrapWithFutureLifecycle = wrapWithFutureLifecycle;
25415
- //# sourceMappingURL=software-info-registration-C8Yg5Z-Y.cjs.map
25541
+ //# sourceMappingURL=software-info-registration-DTbgaSeR.cjs.map