@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kosdev-code/kos-ui-sdk",
3
- "version": "0.1.0-next.1057",
3
+ "version": "0.1.0-next.1059",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -50,7 +50,7 @@
50
50
  "react-router-dom": "^6.11.2"
51
51
  },
52
52
  "dependencies": {
53
- "@kosdev-code/kos-api-levels": "0.1.0-next.1057",
53
+ "@kosdev-code/kos-api-levels": "0.1.0-next.1059",
54
54
  "mobx": "^6.9.0",
55
55
  "mobx-react-lite": "^3.4.3",
56
56
  "date-fns": "^2.30.0",
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "kos": {
61
61
  "build": {
62
- "gitHash": "37d418c8db9cfc83be647d8ef585fe6cf87d63dd"
62
+ "gitHash": "a0ce67cfd8560c9e5a5b61192b457cfdb76dac7b"
63
63
  }
64
64
  }
65
65
  }
@@ -7572,6 +7572,43 @@ class KosMockFutureSimulator {
7572
7572
  }
7573
7573
  return void 0;
7574
7574
  }
7575
+ endWith(method, pattern, options) {
7576
+ const ended = /* @__PURE__ */ new Set();
7577
+ return this.deps.transformFuture(
7578
+ method,
7579
+ pattern,
7580
+ (future) => {
7581
+ if (ended.has(future.id)) {
7582
+ if (future.endState) {
7583
+ ended.delete(future.id);
7584
+ }
7585
+ return null;
7586
+ }
7587
+ if (future.endState === "CANCEL") {
7588
+ return void 0;
7589
+ }
7590
+ const reached = !!future.endState || options.atProgress !== void 0 && (future.progress ?? 0) >= options.atProgress;
7591
+ if (!reached) {
7592
+ return void 0;
7593
+ }
7594
+ if (!future.endState) {
7595
+ const simulated = this.futures.get(future.id);
7596
+ if (simulated) {
7597
+ simulated.dispose();
7598
+ } else {
7599
+ ended.add(future.id);
7600
+ }
7601
+ }
7602
+ return {
7603
+ ...future,
7604
+ endState: options.endState,
7605
+ reason: options.reason,
7606
+ remainingTimeMs: 0
7607
+ };
7608
+ },
7609
+ { description: `futures end with ${options.endState}` }
7610
+ );
7611
+ }
7575
7612
  get active() {
7576
7613
  return [...this.futures.values()].filter((handle) => !handle.done);
7577
7614
  }
@@ -8432,7 +8469,8 @@ class KosMockRegistry {
8432
8469
  state = /* @__PURE__ */ new Map();
8433
8470
  futures = new KosMockFutureSimulator({
8434
8471
  emitFrame: (frame) => this.publishFrame(frame),
8435
- ensureCancelRoutes: () => this.ensureFutureCancelRoutes()
8472
+ ensureCancelRoutes: () => this.ensureFutureCancelRoutes(),
8473
+ transformFuture: (method, pattern, transform, options) => this.transformFuture(method, pattern, transform, options)
8436
8474
  });
8437
8475
  recorder = new KosMockRecorder();
8438
8476
  config = createConfigFacade(this);
@@ -8469,13 +8507,85 @@ class KosMockRegistry {
8469
8507
  disable: remove
8470
8508
  };
8471
8509
  }
8510
+ futureTransformers = [];
8511
+ // Requests by tracker: the only link from a future frame to its endpoint.
8512
+ trackedRequests = /* @__PURE__ */ new Map();
8513
+ transformFuture(method, pattern, transform, options = {}) {
8514
+ const registered = {
8515
+ id: options.id ?? uuid(),
8516
+ method,
8517
+ pattern,
8518
+ compiled: compileRoutePattern(pattern),
8519
+ transform,
8520
+ description: options.description
8521
+ };
8522
+ this.futureTransformers.push(registered);
8523
+ const remove = () => {
8524
+ this.futureTransformers = this.futureTransformers.filter(
8525
+ (candidate) => candidate.id !== registered.id
8526
+ );
8527
+ };
8528
+ return {
8529
+ id: registered.id,
8530
+ remove,
8531
+ enable: () => void 0,
8532
+ disable: remove
8533
+ };
8534
+ }
8535
+ /** Runs a `kos.future` frame through the future transformers. */
8536
+ transformFutureFrame(frame, headers, body) {
8537
+ let future;
8538
+ try {
8539
+ future = JSON.parse(body);
8540
+ } catch {
8541
+ return frame;
8542
+ }
8543
+ const tracker = future?.tracker;
8544
+ const request = tracker ? this.trackedRequests.get(tracker) : void 0;
8545
+ if (!tracker || !request) {
8546
+ return frame;
8547
+ }
8548
+ if (future.endState) {
8549
+ this.trackedRequests.delete(tracker);
8550
+ }
8551
+ const matching = this.futureTransformers.filter(
8552
+ (transformer) => methodMatches(transformer.method, request.method) && matchRoutePattern(transformer.compiled, request.path) !== void 0
8553
+ );
8554
+ let current = future;
8555
+ let changed = false;
8556
+ for (const transformer of matching) {
8557
+ try {
8558
+ const result = transformer.transform(current, { request });
8559
+ if (result === null) {
8560
+ return void 0;
8561
+ }
8562
+ if (result !== void 0) {
8563
+ current = result;
8564
+ changed = true;
8565
+ }
8566
+ } catch (error) {
8567
+ log$L.error(
8568
+ `KosMock future transformer for ${transformer.method} ${transformer.pattern} threw`,
8569
+ error?.message
8570
+ );
8571
+ }
8572
+ }
8573
+ if (!changed) {
8574
+ return frame;
8575
+ }
8576
+ return encodeRawFrame(
8577
+ { ...headers, [KOS_MOCK_TRANSFORMED_HEADER]: "true" },
8578
+ JSON.stringify(current)
8579
+ );
8580
+ }
8472
8581
  /**
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.
8582
+ * Interceptor seam: patches a matching inbound response or future frame
8583
+ * through its transformer chain before it reaches the app. Returns the
8584
+ * (possibly modified) frame, or undefined when a future transformer dropped
8585
+ * it. Internal.
8476
8586
  */
8477
8587
  transformInboundFrame(frame) {
8478
- if (this.transformers.length === 0) {
8588
+ if (this.transformers.length === 0 && this.futureTransformers.length === 0) {
8479
8589
  return frame;
8480
8590
  }
8481
8591
  let headers;
@@ -8487,7 +8597,7 @@ class KosMockRegistry {
8487
8597
  }
8488
8598
  const responseId = headers["response-id"];
8489
8599
  if (!responseId) {
8490
- return frame;
8600
+ return headers["type"] === KOS_FUTURE_TYPE && this.futureTransformers.length > 0 ? this.transformFutureFrame(frame, headers, body) : frame;
8491
8601
  }
8492
8602
  const request = this.pendingRequests.get(responseId);
8493
8603
  this.pendingRequests.delete(responseId);
@@ -8568,7 +8678,7 @@ class KosMockRegistry {
8568
8678
  const hasRequestObservers = [...this.observers].some(
8569
8679
  (observer) => observer.onRequest
8570
8680
  );
8571
- if (!hasRequestObservers && this.transformers.length === 0) {
8681
+ if (!hasRequestObservers && this.transformers.length === 0 && this.futureTransformers.length === 0) {
8572
8682
  return;
8573
8683
  }
8574
8684
  const frame = decodeRequestFrame(data);
@@ -8585,6 +8695,15 @@ class KosMockRegistry {
8585
8695
  tracker: frame.tracker,
8586
8696
  rawBody: frame.rawBody
8587
8697
  };
8698
+ if (this.futureTransformers.length > 0 && frame.tracker) {
8699
+ if (this.trackedRequests.size >= PENDING_REQUEST_CAP) {
8700
+ const oldest = this.trackedRequests.keys().next().value;
8701
+ if (oldest !== void 0) {
8702
+ this.trackedRequests.delete(oldest);
8703
+ }
8704
+ }
8705
+ this.trackedRequests.set(frame.tracker, observation);
8706
+ }
8588
8707
  if (this.transformers.length > 0) {
8589
8708
  if (this.pendingRequests.size >= PENDING_REQUEST_CAP) {
8590
8709
  const oldest = this.pendingRequests.keys().next().value;
@@ -8830,14 +8949,16 @@ class KosMockRegistry {
8830
8949
  enabled: this.enabled,
8831
8950
  standalone: this.standalone,
8832
8951
  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
- }),
8952
+ transformers: [...this.transformers, ...this.futureTransformers].map(
8953
+ (transformer) => {
8954
+ return {
8955
+ id: transformer.id,
8956
+ method: transformer.method,
8957
+ pattern: transformer.pattern,
8958
+ description: transformer.description
8959
+ };
8960
+ }
8961
+ ),
8841
8962
  setups: this.setups.active().map((setup) => {
8842
8963
  return { name: setup.name, url: setup.url };
8843
8964
  }),
@@ -8853,6 +8974,8 @@ class KosMockRegistry {
8853
8974
  [...this.playbacks].forEach((playback) => playback.stop());
8854
8975
  this.routes = [];
8855
8976
  this.transformers = [];
8977
+ this.futureTransformers = [];
8978
+ this.trackedRequests.clear();
8856
8979
  this.pendingRequests.clear();
8857
8980
  this.cancelRoutesRegistered = false;
8858
8981
  this.emitters.forEach((timer) => clearInterval(timer));
@@ -9167,6 +9290,9 @@ class MockInterceptorTransport {
9167
9290
  */
9168
9291
  deliver(frame) {
9169
9292
  const transformed = this.registry.transformInboundFrame(frame);
9293
+ if (transformed === void 0) {
9294
+ return;
9295
+ }
9170
9296
  this.messageHandler?.({ data: transformed });
9171
9297
  this.registry.noteInboundFrame(transformed);
9172
9298
  }
@@ -25416,4 +25542,4 @@ export {
25416
25542
  Registration as y,
25417
25543
  getStateBeanData as z
25418
25544
  };
25419
- //# sourceMappingURL=software-info-registration-B99UrnV0.js.map
25545
+ //# sourceMappingURL=software-info-registration-BqhJP3ww.js.map