@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.
package/package.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "name": "@kosdev-code/kos-ui-sdk",
3
- "version": "3.0.36",
3
+ "version": "3.0.38",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/The-Coca-Cola-Company/kos-ui-sdk.git"
7
+ },
4
8
  "type": "module",
5
9
  "main": "./index.cjs",
6
10
  "module": "./index.js",
@@ -59,7 +63,7 @@
59
63
  },
60
64
  "kos": {
61
65
  "build": {
62
- "gitHash": "e8eb7219c22d97b430af67b6b882c4449c151f91"
66
+ "gitHash": "f7038b43e53bfa550587b93601462b9d31a619bb"
63
67
  }
64
68
  }
65
69
  }
@@ -7462,6 +7462,7 @@ class KosMockFutureHandle {
7462
7462
  elapsedMs = 0;
7463
7463
  endStateValue;
7464
7464
  reasonValue;
7465
+ endFields;
7465
7466
  timer;
7466
7467
  started = false;
7467
7468
  durationMs;
@@ -7477,7 +7478,8 @@ class KosMockFutureHandle {
7477
7478
  remainingTimeMs: this.remainingTimeMs,
7478
7479
  endState: this.endStateValue,
7479
7480
  reason: this.reasonValue,
7480
- clientData: this.clientData
7481
+ clientData: this.clientData,
7482
+ ...this.endFields
7481
7483
  };
7482
7484
  }
7483
7485
  start() {
@@ -7508,14 +7510,15 @@ class KosMockFutureHandle {
7508
7510
  }
7509
7511
  this.emitFrame(encodeFutureFrame(this.snapshot()));
7510
7512
  }
7511
- complete(endState = "SUCCESS", reason) {
7513
+ complete(endState = "SUCCESS", end) {
7512
7514
  if (this.done) {
7513
7515
  return;
7514
7516
  }
7515
7517
  this.stopTimer();
7516
7518
  this.done = true;
7517
7519
  this.endStateValue = endState;
7518
- this.reasonValue = reason;
7520
+ this.endFields = typeof end === "string" ? { reason: end } : end;
7521
+ this.reasonValue = this.endFields?.reason;
7519
7522
  this.remainingTimeMs = 0;
7520
7523
  if (endState === "SUCCESS") {
7521
7524
  this.progress = 1;
@@ -7573,6 +7576,39 @@ class KosMockFutureSimulator {
7573
7576
  }
7574
7577
  return void 0;
7575
7578
  }
7579
+ endWith(method, pattern, options) {
7580
+ const ended = /* @__PURE__ */ new Set();
7581
+ return this.deps.transformFuture(
7582
+ method,
7583
+ pattern,
7584
+ (future) => {
7585
+ if (ended.has(future.id)) {
7586
+ if (future.endState) {
7587
+ ended.delete(future.id);
7588
+ }
7589
+ return null;
7590
+ }
7591
+ if (future.endState === "CANCEL") {
7592
+ return void 0;
7593
+ }
7594
+ const reached = !!future.endState || options.atProgress !== void 0 && (future.progress ?? 0) >= options.atProgress;
7595
+ if (!reached) {
7596
+ return void 0;
7597
+ }
7598
+ if (!future.endState) {
7599
+ const simulated = this.futures.get(future.id);
7600
+ if (simulated) {
7601
+ simulated.dispose();
7602
+ } else {
7603
+ ended.add(future.id);
7604
+ }
7605
+ }
7606
+ const { atProgress: _atProgress, ...end } = options;
7607
+ return { ...future, reason: void 0, remainingTimeMs: 0, ...end };
7608
+ },
7609
+ { description: `futures end with ${options.endState}` }
7610
+ );
7611
+ }
7576
7612
  get active() {
7577
7613
  return [...this.futures.values()].filter((handle) => !handle.done);
7578
7614
  }
@@ -8311,6 +8347,8 @@ const log$M = KosLog.createLogger({
8311
8347
  });
8312
8348
  const asWireValue = (value) => typeof value === "string" ? value : JSON.stringify(value);
8313
8349
  const configTopic = (beanId) => `/kos/config/${beanId}`;
8350
+ const DEVICE_CONFIG_BASE = "/api/kos/config";
8351
+ const STUDIO_CONFIG_BASE = "/api/config";
8314
8352
  const changeEnvelope = (props, previousValues = {}) => {
8315
8353
  return {
8316
8354
  changes: Object.entries(props).map(([attr, value]) => {
@@ -8334,9 +8372,10 @@ function createConfigFacade(registry) {
8334
8372
  const inject = (beanId, injection) => {
8335
8373
  const current = { ...injection.props };
8336
8374
  const schema = injection.schema ?? {};
8375
+ const base = injection.studio ? STUDIO_CONFIG_BASE : DEVICE_CONFIG_BASE;
8337
8376
  const transformerHandle = registry.transformResponse(
8338
8377
  "GET",
8339
- `/api/kos/config/details/${beanId}/:depth`,
8378
+ `${base}/details/${beanId}/:depth`,
8340
8379
  (body) => {
8341
8380
  if (!body || typeof body !== "object") {
8342
8381
  return void 0;
@@ -8364,7 +8403,7 @@ function createConfigFacade(registry) {
8364
8403
  { description: `config.inject ${beanId}` }
8365
8404
  );
8366
8405
  const routeHandle = registry.post(
8367
- `/api/kos/config/${beanId}`,
8406
+ `${base}/${beanId}`,
8368
8407
  (req) => {
8369
8408
  const requested = req.json();
8370
8409
  if (!requested) {
@@ -8430,7 +8469,8 @@ class KosMockRegistry {
8430
8469
  state = /* @__PURE__ */ new Map();
8431
8470
  futures = new KosMockFutureSimulator({
8432
8471
  emitFrame: (frame) => this.publishFrame(frame),
8433
- ensureCancelRoutes: () => this.ensureFutureCancelRoutes()
8472
+ ensureCancelRoutes: () => this.ensureFutureCancelRoutes(),
8473
+ transformFuture: (method, pattern, transform, options) => this.transformFuture(method, pattern, transform, options)
8434
8474
  });
8435
8475
  recorder = new KosMockRecorder();
8436
8476
  config = createConfigFacade(this);
@@ -8467,13 +8507,85 @@ class KosMockRegistry {
8467
8507
  disable: remove
8468
8508
  };
8469
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
+ }
8470
8581
  /**
8471
- * Interceptor seam: patches a matching inbound response frame through the
8472
- * transformer chain before it reaches the app. Returns the (possibly
8473
- * 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.
8474
8586
  */
8475
8587
  transformInboundFrame(frame) {
8476
- if (this.transformers.length === 0) {
8588
+ if (this.transformers.length === 0 && this.futureTransformers.length === 0) {
8477
8589
  return frame;
8478
8590
  }
8479
8591
  let headers;
@@ -8485,7 +8597,7 @@ class KosMockRegistry {
8485
8597
  }
8486
8598
  const responseId = headers["response-id"];
8487
8599
  if (!responseId) {
8488
- return frame;
8600
+ return headers["type"] === KOS_FUTURE_TYPE && this.futureTransformers.length > 0 ? this.transformFutureFrame(frame, headers, body) : frame;
8489
8601
  }
8490
8602
  const request = this.pendingRequests.get(responseId);
8491
8603
  this.pendingRequests.delete(responseId);
@@ -8566,7 +8678,7 @@ class KosMockRegistry {
8566
8678
  const hasRequestObservers = [...this.observers].some(
8567
8679
  (observer) => observer.onRequest
8568
8680
  );
8569
- if (!hasRequestObservers && this.transformers.length === 0) {
8681
+ if (!hasRequestObservers && this.transformers.length === 0 && this.futureTransformers.length === 0) {
8570
8682
  return;
8571
8683
  }
8572
8684
  const frame = decodeRequestFrame(data);
@@ -8583,6 +8695,15 @@ class KosMockRegistry {
8583
8695
  tracker: frame.tracker,
8584
8696
  rawBody: frame.rawBody
8585
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
+ }
8586
8707
  if (this.transformers.length > 0) {
8587
8708
  if (this.pendingRequests.size >= PENDING_REQUEST_CAP) {
8588
8709
  const oldest = this.pendingRequests.keys().next().value;
@@ -8828,14 +8949,16 @@ class KosMockRegistry {
8828
8949
  enabled: this.enabled,
8829
8950
  standalone: this.standalone,
8830
8951
  routes: this.list(),
8831
- transformers: this.transformers.map((transformer) => {
8832
- return {
8833
- id: transformer.id,
8834
- method: transformer.method,
8835
- pattern: transformer.pattern,
8836
- description: transformer.description
8837
- };
8838
- }),
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
+ ),
8839
8962
  setups: this.setups.active().map((setup) => {
8840
8963
  return { name: setup.name, url: setup.url };
8841
8964
  }),
@@ -8851,6 +8974,8 @@ class KosMockRegistry {
8851
8974
  [...this.playbacks].forEach((playback) => playback.stop());
8852
8975
  this.routes = [];
8853
8976
  this.transformers = [];
8977
+ this.futureTransformers = [];
8978
+ this.trackedRequests.clear();
8854
8979
  this.pendingRequests.clear();
8855
8980
  this.cancelRoutesRegistered = false;
8856
8981
  this.emitters.forEach((timer) => clearInterval(timer));
@@ -9165,6 +9290,9 @@ class MockInterceptorTransport {
9165
9290
  */
9166
9291
  deliver(frame) {
9167
9292
  const transformed = this.registry.transformInboundFrame(frame);
9293
+ if (transformed === void 0) {
9294
+ return;
9295
+ }
9168
9296
  this.messageHandler?.({ data: transformed });
9169
9297
  this.registry.noteInboundFrame(transformed);
9170
9298
  }
@@ -25409,4 +25537,4 @@ exports.waitForRequest = waitForRequest;
25409
25537
  exports.whenReady = whenReady;
25410
25538
  exports.withCancellation = withCancellation;
25411
25539
  exports.wrapWithFutureLifecycle = wrapWithFutureLifecycle;
25412
- //# sourceMappingURL=software-info-registration-JdgonknE.cjs.map
25540
+ //# sourceMappingURL=software-info-registration-DMOIY4c3.cjs.map