@crvouga/mockingbird-service-wholescripts 1.7.0 → 1.8.0

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/CHANGELOG.md CHANGED
@@ -1,14 +1,31 @@
1
1
  # Changelog — @crvouga/mockingbird-service-wholescripts
2
2
 
3
- ## 1.7.0 (2026-10-01)
3
+ ## 1.8.0 (2026-10-02)
4
4
 
5
5
  ### Features
6
6
 
7
- - record SQL timelines and serve the admin explorer ([8ced17a](https://github.com/crvouga/mockingbird/commit/8ced17ac398e386ef4e9d7e01fc7744c34b95d07))
7
+ - coordinate HTTP and protocol mocks ([3168653](https://github.com/crvouga/mockingbird/commit/31686534c55a9823fdaa92afc9cc456cf741b809))
8
+
9
+ ### Fixes and improvements
8
10
 
9
- ### Dependencies
11
+ - decouple smoke types from built packages ([a4b31c6](https://github.com/crvouga/mockingbird/commit/a4b31c6205d92aa0c6f7c9de3518d5ae44d38829))
12
+ - protect child admin endpoints ([5410b94](https://github.com/crvouga/mockingbird/commit/5410b948a30dcedc21e9a70e44fd415c1ea25394))
13
+ - harden protocol startup and state ([6f5b557](https://github.com/crvouga/mockingbird/commit/6f5b557e87b48b31985949e04f3b5ef58f51d295))
14
+ - preserve checkpoints and lifecycle ([50e8c32](https://github.com/crvouga/mockingbird/commit/50e8c322feded2f7f45afce9d8cdef43c37ab9ab))
15
+ - clean branch state on reset ([3b714d4](https://github.com/crvouga/mockingbird/commit/3b714d4a5df8e75fee0bb31e98d7c7566e164381))
16
+ - type signal listener cleanup ([5279852](https://github.com/crvouga/mockingbird/commit/5279852e56ea60262ccf17aa6e46b3c31091a34a))
17
+
18
+ ## 1.7.1 (2026-10-01)
19
+
20
+ ### Fixes and improvements
10
21
 
11
- - `@crvouga/mockingbird-service-sqlite`
22
+ - freeze clock before setting time ([a054a1d](https://github.com/crvouga/mockingbird/commit/a054a1defad62539d719b1d31c15107afea623bd))
23
+
24
+ ## 1.7.0 (2026-09-30)
25
+
26
+ ### Features
27
+
28
+ - record SQL timelines and serve the admin explorer ([8ced17a](https://github.com/crvouga/mockingbird/commit/8ced17ac398e386ef4e9d7e01fc7744c34b95d07))
12
29
 
13
30
  ## 1.6.1 (2026-09-30)
14
31
 
@@ -183,29 +183,30 @@ var createControlPlane = (context) => {
183
183
  return json(200, { status: "ok", reset: target === "*" ? context.namespaces() : [target] });
184
184
  },
185
185
  "GET /namespaces": () => json(200, { default: context.defaultNamespace, namespaces: context.namespaces() }),
186
- "GET /clock": () => json(200, context.clock.state()),
187
- "POST /clock": ({ body }) => {
186
+ "GET /clock": ({ namespace }) => json(200, (context.namespaceClock?.(namespace) ?? context.clock).state()),
187
+ "POST /clock": ({ body, namespace }) => {
188
+ const clock = context.namespaceClock?.(namespace) ?? context.clock;
188
189
  if (!isRecord(body))
189
190
  return adminError(400, "expected a JSON object");
190
191
  if (body.reset === true)
191
- context.clock.reset();
192
+ clock.reset();
193
+ if (body.freeze === true)
194
+ clock.freeze();
192
195
  if (body.set !== void 0) {
193
196
  const instant = parseInstant(body.set);
194
197
  if (instant === void 0)
195
198
  return adminError(400, "set: expected epoch ms or ISO-8601");
196
- context.clock.set(instant);
199
+ clock.set(instant);
197
200
  }
198
201
  if (body.advance !== void 0) {
199
202
  const delta = parseDuration(body.advance);
200
203
  if (delta === void 0)
201
204
  return adminError(400, 'advance: expected ms or "15m"-style');
202
- context.clock.advance(delta);
205
+ clock.advance(delta);
203
206
  }
204
- if (body.freeze === true)
205
- context.clock.freeze();
206
207
  if (body.freeze === false)
207
- context.clock.unfreeze();
208
- return json(200, context.clock.state());
208
+ clock.unfreeze();
209
+ return json(200, clock.state());
209
210
  },
210
211
  "GET /faults": () => json(200, { faults: context.faults.list() }),
211
212
  "POST /faults": ({ body, namespace }) => {
@@ -485,7 +486,33 @@ var faultResponse = (rule) => {
485
486
  };
486
487
  var createFaultRegistry = (rng = createRng(0), sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))) => {
487
488
  const entries = [];
489
+ let isolated = false;
490
+ const scopedRngs = /* @__PURE__ */ new Map();
491
+ const random = (namespace) => {
492
+ if (!isolated || namespace === "default")
493
+ return rng;
494
+ let scoped = scopedRngs.get(namespace);
495
+ if (!scoped) {
496
+ scoped = createRng(rng.seed);
497
+ scopedRngs.set(namespace, scoped);
498
+ }
499
+ return scoped;
500
+ };
488
501
  return {
502
+ isolateNamespaces() {
503
+ isolated = true;
504
+ },
505
+ snapshot(namespace) {
506
+ return {
507
+ rules: structuredClone(entries.map((entry, position) => ({
508
+ ...entry.rule,
509
+ remaining: entry.remaining,
510
+ hits: entry.hits,
511
+ position
512
+ })).filter((entry) => entry.namespace === namespace)),
513
+ rngState: random(namespace).state()
514
+ };
515
+ },
489
516
  add(rule) {
490
517
  const existing = entries.findIndex((e) => e.rule.id === rule.id);
491
518
  const entry = { rule, remaining: rule.count ?? null, hits: 0 };
@@ -505,6 +532,16 @@ var createFaultRegistry = (rng = createRng(0), sleep = (ms) => new Promise((reso
505
532
  },
506
533
  clear() {
507
534
  entries.length = 0;
535
+ rng.reset();
536
+ scopedRngs.clear();
537
+ },
538
+ restore(namespace, state) {
539
+ for (let i = entries.length - 1; i >= 0; i--)
540
+ if (entries[i]?.rule.namespace === namespace)
541
+ entries.splice(i, 1);
542
+ for (const { remaining, hits, position, ...rule } of state.rules)
543
+ entries.splice(Math.min(position, entries.length), 0, { rule, remaining, hits });
544
+ random(namespace).setState(state.rngState);
508
545
  },
509
546
  async take(candidate) {
510
547
  const hits = [];
@@ -514,7 +551,7 @@ var createFaultRegistry = (rng = createRng(0), sleep = (ms) => new Promise((reso
514
551
  if (!matches(entry.rule, candidate))
515
552
  continue;
516
553
  const rate = entry.rule.rate ?? 1;
517
- if (rng.next() >= rate)
554
+ if (random(candidate.namespace).next() >= rate)
518
555
  continue;
519
556
  entry.hits++;
520
557
  if (entry.remaining !== null)
@@ -1063,7 +1100,8 @@ var annotateResponse = (response, extra) => {
1063
1100
  var responseNotes = (response) => notes.get(response);
1064
1101
 
1065
1102
  // ../core/dist/metrics.js
1066
- var createMetrics = () => {
1103
+ var createMetrics = (partition = true) => {
1104
+ const namespaces = /* @__PURE__ */ new Map();
1067
1105
  let requests = 0;
1068
1106
  let faults = 0;
1069
1107
  let totalDurationMs = 0;
@@ -1071,6 +1109,14 @@ var createMetrics = () => {
1071
1109
  const unmatched = /* @__PURE__ */ new Map();
1072
1110
  return {
1073
1111
  record(entry) {
1112
+ if (partition) {
1113
+ let scoped = namespaces.get(entry.namespace);
1114
+ if (!scoped) {
1115
+ scoped = createMetrics(false);
1116
+ namespaces.set(entry.namespace, scoped);
1117
+ }
1118
+ scoped.record(entry);
1119
+ }
1074
1120
  requests++;
1075
1121
  totalDurationMs += entry.durationMs;
1076
1122
  if (entry.faultId !== void 0)
@@ -1082,7 +1128,7 @@ var createMetrics = () => {
1082
1128
  unmatched.set(route, (unmatched.get(route) ?? 0) + 1);
1083
1129
  }
1084
1130
  },
1085
- report: () => ({
1131
+ report: (namespace) => namespace !== void 0 ? (namespaces.get(namespace) ?? createMetrics(false)).report() : {
1086
1132
  requests,
1087
1133
  byOperation: Object.fromEntries([...byOperation].sort(([a], [b]) => a.localeCompare(b))),
1088
1134
  unmatched: [...unmatched].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([route, count]) => {
@@ -1091,8 +1137,13 @@ var createMetrics = () => {
1091
1137
  }),
1092
1138
  faults,
1093
1139
  totalDurationMs
1094
- }),
1095
- reset() {
1140
+ },
1141
+ reset(namespace) {
1142
+ if (namespace !== void 0) {
1143
+ namespaces.delete(namespace);
1144
+ return;
1145
+ }
1146
+ namespaces.clear();
1096
1147
  requests = 0;
1097
1148
  faults = 0;
1098
1149
  totalDurationMs = 0;
@@ -4064,7 +4115,7 @@ var stateAdminRoutes = (routes) => {
4064
4115
  };
4065
4116
 
4066
4117
  // ../core/dist/version.js
4067
- var PACKAGE_VERSION = true ? "1.7.0" : UNRELEASED_VERSION;
4118
+ var PACKAGE_VERSION = true ? "1.8.0" : UNRELEASED_VERSION;
4068
4119
 
4069
4120
  // ../core/dist/webhooks.js
4070
4121
  var json4 = (status, body) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
@@ -4230,6 +4281,20 @@ var createRuntime = (options) => {
4230
4281
  const instances = /* @__PURE__ */ new Map();
4231
4282
  const publicNamespaces = /* @__PURE__ */ new Set();
4232
4283
  const branchRngs = /* @__PURE__ */ new Map();
4284
+ const namespaceClocks = /* @__PURE__ */ new Map();
4285
+ const namespaceForStorage = /* @__PURE__ */ new Map();
4286
+ let isolatedNamespaces = false;
4287
+ const namespaceClock = (name = DEFAULT_NAMESPACE) => {
4288
+ if (!isolatedNamespaces || name === DEFAULT_NAMESPACE)
4289
+ return clock;
4290
+ let found = namespaceClocks.get(name);
4291
+ if (!found) {
4292
+ found = createClock();
4293
+ namespaceClocks.set(name, found);
4294
+ }
4295
+ return found;
4296
+ };
4297
+ const fleetPoints = /* @__PURE__ */ new WeakMap();
4233
4298
  const timelines = /* @__PURE__ */ new Map();
4234
4299
  const branchStorage = /* @__PURE__ */ new Map();
4235
4300
  const captured = /* @__PURE__ */ new Map();
@@ -4243,17 +4308,21 @@ var createRuntime = (options) => {
4243
4308
  if (!NAMESPACE_PATTERN.test(key) || !NAMESPACE_PATTERN.test(publicNamespace)) {
4244
4309
  throw new RangeError(`namespace must match ${NAMESPACE_PATTERN}: ${JSON.stringify(publicNamespace)}`);
4245
4310
  }
4311
+ const createdContextRng = isolatedRng ?? (isolatedNamespaces && key !== DEFAULT_NAMESPACE ? createRng(options.seed ?? 0) : rng);
4246
4312
  const created = options.create({
4247
4313
  namespace: storageNamespace(key),
4248
4314
  publicNamespace,
4249
4315
  sqlite,
4250
- clock,
4251
- rng: isolatedRng ?? rng
4316
+ clock: namespaceClock(publicNamespace),
4317
+ rng: createdContextRng
4252
4318
  });
4253
4319
  instances.set(key, created);
4320
+ namespaceForStorage.set(key, publicNamespace);
4254
4321
  publicNamespaces.add(publicNamespace);
4255
4322
  if (isolatedRng)
4256
4323
  branchRngs.set(key, isolatedRng);
4324
+ else if (isolatedNamespaces && key !== DEFAULT_NAMESPACE)
4325
+ branchRngs.set(key, createdContextRng);
4257
4326
  return created;
4258
4327
  };
4259
4328
  const instance = (name = DEFAULT_NAMESPACE) => instanceFor(name);
@@ -4271,7 +4340,7 @@ var createRuntime = (options) => {
4271
4340
  captured.set(storage, snapshot2);
4272
4341
  return Object.freeze({
4273
4342
  snapshot: snapshot2,
4274
- clock: Object.freeze(clock.state()),
4343
+ clock: Object.freeze(namespaceClock(namespaceForStorage.get(storage) ?? storage).state()),
4275
4344
  rngState: (branchRngs.get(storage) ?? rng).state()
4276
4345
  });
4277
4346
  };
@@ -4281,7 +4350,7 @@ var createRuntime = (options) => {
4281
4350
  return found;
4282
4351
  instance(name);
4283
4352
  found = new Timeline({
4284
- now: clock.now,
4353
+ now: namespaceClock(name).now,
4285
4354
  ...options.maxCheckpoints !== void 0 ? { maxCheckpoints: options.maxCheckpoints } : {}
4286
4355
  });
4287
4356
  found.commit(capture(name));
@@ -4308,12 +4377,13 @@ var createRuntime = (options) => {
4308
4377
  const point = history2.checkout("main", at);
4309
4378
  restoreNamespace(sqlite, storageNamespace(namespace), point.value.snapshot);
4310
4379
  captured.set(namespace, point.value.snapshot);
4311
- rng.setState(point.value.rngState);
4312
- clock.set(point.value.clock.now);
4380
+ (branchRngs.get(namespace) ?? rng).setState(point.value.rngState);
4381
+ const selectedClock = namespaceClock(namespace);
4382
+ selectedClock.set(point.value.clock.now);
4313
4383
  if (point.value.clock.frozen)
4314
- clock.freeze();
4384
+ selectedClock.freeze();
4315
4385
  else
4316
- clock.unfreeze();
4386
+ selectedClock.unfreeze();
4317
4387
  }
4318
4388
  return namespace;
4319
4389
  }
@@ -4371,11 +4441,12 @@ var createRuntime = (options) => {
4371
4441
  const storage = ensureBranch(namespace, branchName);
4372
4442
  restoreNamespace(sqlite, storageNamespace(storage), point.value.snapshot);
4373
4443
  captured.set(storage, point.value.snapshot);
4374
- clock.set(point.value.clock.now);
4444
+ const selectedClock = namespaceClock(namespace);
4445
+ selectedClock.set(point.value.clock.now);
4375
4446
  if (point.value.clock.frozen)
4376
- clock.freeze();
4447
+ selectedClock.freeze();
4377
4448
  else
4378
- clock.unfreeze();
4449
+ selectedClock.unfreeze();
4379
4450
  (branchRngs.get(storage) ?? rng).setState(point.value.rngState);
4380
4451
  };
4381
4452
  const reset = async (name = DEFAULT_NAMESPACE) => {
@@ -4383,10 +4454,21 @@ var createRuntime = (options) => {
4383
4454
  options.webhooks?.clear();
4384
4455
  for (const each of instances.values())
4385
4456
  await each.reset();
4457
+ for (const each of branchRngs.values())
4458
+ each.reset();
4386
4459
  timelines.clear();
4460
+ for (const storage of branchStorage.values()) {
4461
+ instances.delete(storage);
4462
+ branchRngs.delete(storage);
4463
+ namespaceForStorage.delete(storage);
4464
+ }
4387
4465
  branchStorage.clear();
4388
- branchRngs.clear();
4389
4466
  captured.clear();
4467
+ faults.clear();
4468
+ clock.reset();
4469
+ rng.reset();
4470
+ for (const each of namespaceClocks.values())
4471
+ each.reset();
4390
4472
  return;
4391
4473
  }
4392
4474
  options.webhooks?.clear(name);
@@ -4404,11 +4486,18 @@ var createRuntime = (options) => {
4404
4486
  else
4405
4487
  clearNamespace(sqlite, storageNamespace(storage));
4406
4488
  branchStorage.delete(mapping);
4489
+ instances.delete(storage);
4490
+ namespaceForStorage.delete(storage);
4407
4491
  branchRngs.delete(storage);
4408
4492
  captured.delete(storage);
4409
4493
  }
4410
4494
  timelines.delete(name);
4411
4495
  captured.delete(name);
4496
+ faults.restore(name, { rules: [], rngState: createRng(options.seed ?? 0).state() });
4497
+ namespaceClock(name).reset();
4498
+ (branchRngs.get(name) ?? rng).reset();
4499
+ journal.clear(name);
4500
+ metrics.reset(name);
4412
4501
  };
4413
4502
  const snapshot = (name = DEFAULT_NAMESPACE) => {
4414
4503
  return checkpoint(name, "main").value.snapshot;
@@ -4446,6 +4535,53 @@ var createRuntime = (options) => {
4446
4535
  rng,
4447
4536
  credentials,
4448
4537
  webhooks: options.webhooks,
4538
+ isolateNamespaces() {
4539
+ if (publicNamespaces.size > 1)
4540
+ throw new Error("enable namespace isolation before creating named namespaces");
4541
+ isolatedNamespaces = true;
4542
+ faults.isolateNamespaces();
4543
+ },
4544
+ namespaceClock,
4545
+ namespaceOf(request) {
4546
+ const url = new URL(request.url);
4547
+ const prefixed = PATH_PREFIX.exec(url.pathname);
4548
+ const selected = request.headers.get(NAMESPACE_HEADER) ?? (prefixed ? decodeURIComponent(prefixed[1]) : void 0);
4549
+ const path = prefixed ? prefixed[2] ?? "/" : url.pathname;
4550
+ if (path === "/__admin" || path.startsWith("/__admin/"))
4551
+ return url.searchParams.get("namespace") ?? selected ?? DEFAULT_NAMESPACE;
4552
+ if (selected !== void 0)
4553
+ return selected;
4554
+ const credential = options.credential?.(request);
4555
+ return (credential !== void 0 ? credentials.get(credential) : void 0) ?? DEFAULT_NAMESPACE;
4556
+ },
4557
+ fleetSnapshot(namespace) {
4558
+ instance(namespace);
4559
+ const handle = {};
4560
+ fleetPoints.set(handle, {
4561
+ namespace,
4562
+ state: capture(namespace),
4563
+ faults: faults.snapshot(namespace),
4564
+ webhooks: options.webhooks?.snapshot(namespace)
4565
+ });
4566
+ return handle;
4567
+ },
4568
+ fleetRestore(handle, namespace) {
4569
+ const point = typeof handle === "object" && handle !== null ? fleetPoints.get(handle) : void 0;
4570
+ if (!point || point.namespace !== namespace)
4571
+ throw new Error("invalid fleet checkpoint");
4572
+ restoreNamespace(sqlite, storageNamespace(namespace), point.state.snapshot);
4573
+ captured.set(namespace, point.state.snapshot);
4574
+ const selectedClock = namespaceClock(namespace);
4575
+ selectedClock.freeze();
4576
+ selectedClock.set(point.state.clock.now);
4577
+ if (!point.state.clock.frozen)
4578
+ selectedClock.unfreeze();
4579
+ (branchRngs.get(namespace) ?? rng).setState(point.state.rngState);
4580
+ faults.restore(namespace, point.faults);
4581
+ if (point.webhooks !== void 0)
4582
+ options.webhooks?.restore(namespace, point.webhooks);
4583
+ timelines.delete(namespace);
4584
+ },
4449
4585
  applyPreset: (name, namespace = DEFAULT_NAMESPACE, overrides = {}) => {
4450
4586
  const preset = options.presets?.[name];
4451
4587
  if (!preset)
@@ -4621,6 +4757,7 @@ var createRuntime = (options) => {
4621
4757
  startedAt: wallNow(),
4622
4758
  wallNow,
4623
4759
  clock,
4760
+ namespaceClock,
4624
4761
  faults,
4625
4762
  metrics,
4626
4763
  journal,
@@ -5369,6 +5506,8 @@ var WholescriptsAPI = class {
5369
5506
  };
5370
5507
 
5371
5508
  export {
5509
+ createClock,
5510
+ parseDuration,
5372
5511
  MEDPAX_BOX_SKU,
5373
5512
  DEFAULT_CATALOG,
5374
5513
  document2 as document,
@@ -5381,4 +5520,4 @@ export {
5381
5520
  canonicalStatus,
5382
5521
  WholescriptsAPI
5383
5522
  };
5384
- //# sourceMappingURL=chunk-W6USLWSA.js.map
5523
+ //# sourceMappingURL=chunk-7AFJALQR.js.map