@kosdev-code/kos-ui-sdk 3.0.36 → 3.0.37

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": "3.0.36",
3
+ "version": "3.0.37",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "kos": {
61
61
  "build": {
62
- "gitHash": "e8eb7219c22d97b430af67b6b882c4449c151f91"
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
  }
@@ -8310,6 +8347,8 @@ const log$M = KosLog.createLogger({
8310
8347
  });
8311
8348
  const asWireValue = (value) => typeof value === "string" ? value : JSON.stringify(value);
8312
8349
  const configTopic = (beanId) => `/kos/config/${beanId}`;
8350
+ const DEVICE_CONFIG_BASE = "/api/kos/config";
8351
+ const STUDIO_CONFIG_BASE = "/api/config";
8313
8352
  const changeEnvelope = (props, previousValues = {}) => {
8314
8353
  return {
8315
8354
  changes: Object.entries(props).map(([attr, value]) => {
@@ -8333,9 +8372,10 @@ function createConfigFacade(registry) {
8333
8372
  const inject = (beanId, injection) => {
8334
8373
  const current = { ...injection.props };
8335
8374
  const schema = injection.schema ?? {};
8375
+ const base = injection.studio ? STUDIO_CONFIG_BASE : DEVICE_CONFIG_BASE;
8336
8376
  const transformerHandle = registry.transformResponse(
8337
8377
  "GET",
8338
- `/api/kos/config/details/${beanId}/:depth`,
8378
+ `${base}/details/${beanId}/:depth`,
8339
8379
  (body) => {
8340
8380
  if (!body || typeof body !== "object") {
8341
8381
  return void 0;
@@ -8363,7 +8403,7 @@ function createConfigFacade(registry) {
8363
8403
  { description: `config.inject ${beanId}` }
8364
8404
  );
8365
8405
  const routeHandle = registry.post(
8366
- `/api/kos/config/${beanId}`,
8406
+ `${base}/${beanId}`,
8367
8407
  (req) => {
8368
8408
  const requested = req.json();
8369
8409
  if (!requested) {
@@ -8429,7 +8469,8 @@ class KosMockRegistry {
8429
8469
  state = /* @__PURE__ */ new Map();
8430
8470
  futures = new KosMockFutureSimulator({
8431
8471
  emitFrame: (frame) => this.publishFrame(frame),
8432
- ensureCancelRoutes: () => this.ensureFutureCancelRoutes()
8472
+ ensureCancelRoutes: () => this.ensureFutureCancelRoutes(),
8473
+ transformFuture: (method, pattern, transform, options) => this.transformFuture(method, pattern, transform, options)
8433
8474
  });
8434
8475
  recorder = new KosMockRecorder();
8435
8476
  config = createConfigFacade(this);
@@ -8466,13 +8507,85 @@ class KosMockRegistry {
8466
8507
  disable: remove
8467
8508
  };
8468
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
+ }
8469
8581
  /**
8470
- * Interceptor seam: patches a matching inbound response frame through the
8471
- * transformer chain before it reaches the app. Returns the (possibly
8472
- * 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.
8473
8586
  */
8474
8587
  transformInboundFrame(frame) {
8475
- if (this.transformers.length === 0) {
8588
+ if (this.transformers.length === 0 && this.futureTransformers.length === 0) {
8476
8589
  return frame;
8477
8590
  }
8478
8591
  let headers;
@@ -8484,7 +8597,7 @@ class KosMockRegistry {
8484
8597
  }
8485
8598
  const responseId = headers["response-id"];
8486
8599
  if (!responseId) {
8487
- return frame;
8600
+ return headers["type"] === KOS_FUTURE_TYPE && this.futureTransformers.length > 0 ? this.transformFutureFrame(frame, headers, body) : frame;
8488
8601
  }
8489
8602
  const request = this.pendingRequests.get(responseId);
8490
8603
  this.pendingRequests.delete(responseId);
@@ -8565,7 +8678,7 @@ class KosMockRegistry {
8565
8678
  const hasRequestObservers = [...this.observers].some(
8566
8679
  (observer) => observer.onRequest
8567
8680
  );
8568
- if (!hasRequestObservers && this.transformers.length === 0) {
8681
+ if (!hasRequestObservers && this.transformers.length === 0 && this.futureTransformers.length === 0) {
8569
8682
  return;
8570
8683
  }
8571
8684
  const frame = decodeRequestFrame(data);
@@ -8582,6 +8695,15 @@ class KosMockRegistry {
8582
8695
  tracker: frame.tracker,
8583
8696
  rawBody: frame.rawBody
8584
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
+ }
8585
8707
  if (this.transformers.length > 0) {
8586
8708
  if (this.pendingRequests.size >= PENDING_REQUEST_CAP) {
8587
8709
  const oldest = this.pendingRequests.keys().next().value;
@@ -8827,14 +8949,16 @@ class KosMockRegistry {
8827
8949
  enabled: this.enabled,
8828
8950
  standalone: this.standalone,
8829
8951
  routes: this.list(),
8830
- transformers: this.transformers.map((transformer) => {
8831
- return {
8832
- id: transformer.id,
8833
- method: transformer.method,
8834
- pattern: transformer.pattern,
8835
- description: transformer.description
8836
- };
8837
- }),
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
+ ),
8838
8962
  setups: this.setups.active().map((setup) => {
8839
8963
  return { name: setup.name, url: setup.url };
8840
8964
  }),
@@ -8850,6 +8974,8 @@ class KosMockRegistry {
8850
8974
  [...this.playbacks].forEach((playback) => playback.stop());
8851
8975
  this.routes = [];
8852
8976
  this.transformers = [];
8977
+ this.futureTransformers = [];
8978
+ this.trackedRequests.clear();
8853
8979
  this.pendingRequests.clear();
8854
8980
  this.cancelRoutesRegistered = false;
8855
8981
  this.emitters.forEach((timer) => clearInterval(timer));
@@ -9164,6 +9290,9 @@ class MockInterceptorTransport {
9164
9290
  */
9165
9291
  deliver(frame) {
9166
9292
  const transformed = this.registry.transformInboundFrame(frame);
9293
+ if (transformed === void 0) {
9294
+ return;
9295
+ }
9167
9296
  this.messageHandler?.({ data: transformed });
9168
9297
  this.registry.noteInboundFrame(transformed);
9169
9298
  }
@@ -25413,4 +25542,4 @@ export {
25413
25542
  Registration as y,
25414
25543
  getStateBeanData as z
25415
25544
  };
25416
- //# sourceMappingURL=software-info-registration-BhOmDkag.js.map
25545
+ //# sourceMappingURL=software-info-registration-BqhJP3ww.js.map