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

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