@crvouga/mockingbird-service-microsoft 0.1.0 → 0.3.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/dist/cli.js CHANGED
@@ -3,9 +3,9 @@ import {
3
3
  runCli,
4
4
  serveCommand,
5
5
  serveTarget
6
- } from "./chunk-2Q6BIW3G.js";
7
- import "./chunk-NTODMBCU.js";
8
- import "./chunk-M73LWCCP.js";
6
+ } from "./chunk-YZERYNS7.js";
7
+ import "./chunk-2S244VQQ.js";
8
+ import "./chunk-6Z2MR3PO.js";
9
9
 
10
10
  // src/cli.ts
11
11
  var code = await runCli(
@@ -0,0 +1,21 @@
1
+ import {
2
+ createHash,
3
+ createHmac,
4
+ random,
5
+ randomBytes,
6
+ randomUUID,
7
+ timingSafeEqual,
8
+ useRandom,
9
+ withRandom
10
+ } from "./chunk-6Z2MR3PO.js";
11
+ export {
12
+ createHash,
13
+ createHmac,
14
+ random,
15
+ randomBytes,
16
+ randomUUID,
17
+ timingSafeEqual,
18
+ useRandom,
19
+ withRandom
20
+ };
21
+ //# sourceMappingURL=crypto-II2ETGQQ.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  randomUUID
3
- } from "./chunk-M73LWCCP.js";
3
+ } from "./chunk-6Z2MR3PO.js";
4
4
 
5
5
  // ../../http/provider/dist/vendor/clerk/helpers.js
6
6
  function generateClerkId(prefix) {
@@ -83,4 +83,4 @@ export {
83
83
  userDisplayName
84
84
  };
85
85
  /*! Adapted from vercel-labs/emulate (Apache-2.0), modified for Mockingbird. See port.json, LICENSE_EMULATE and THIRD_PARTY_NOTICES.md. */
86
- //# sourceMappingURL=helpers-BMHFVPFA.js.map
86
+ //# sourceMappingURL=helpers-5D4ROPWG.js.map
package/dist/index.d.ts CHANGED
@@ -389,6 +389,11 @@ type ProviderOptions = APIOptions & {
389
389
  fallbackUser?: AuthFallback;
390
390
  publicNamespace?: string;
391
391
  adminPrefix?: string;
392
+ /**
393
+ * A seeded stream of `[0, 1)` that ids and tokens are minted from, so a run replays from
394
+ * its seed. Default: the platform's randomness.
395
+ */
396
+ random?: () => number;
392
397
  };
393
398
  /** Portable vendor API backed by the shared namespace; it owns no separate history or listener. */
394
399
  declare class ProviderAPI {
@@ -923,7 +928,7 @@ type ClockState = {
923
928
  now: number;
924
929
  /** True while time does not advance on its own. */
925
930
  frozen: boolean;
926
- /** Milliseconds this clock adds to its underlying source. */
931
+ /** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
927
932
  offsetMs: number;
928
933
  };
929
934
  type Clock = {
@@ -959,7 +964,10 @@ type Rng = {
959
964
  state(): number;
960
965
  /** Restore a state previously returned by {@link state}. */
961
966
  setState(state: number): void;
962
- seed: number;
967
+ /** Restart the stream from a new seed. `reset` then returns to this seed. */
968
+ reseed(seed: number | string): void;
969
+ /** The seed the stream currently starts from. */
970
+ readonly seed: number;
963
971
  };
964
972
 
965
973
  /**
@@ -968,44 +976,83 @@ type Rng = {
968
976
  * This is how a suite reaches the vendor's failure modes without the vendor: the
969
977
  * quota error that only appears when a shared sandbox is full, the 429 that only
970
978
  * appears under load, the 5xx that proves a retry path works.
979
+ *
980
+ * A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
981
+ * it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
982
+ * and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
971
983
  */
972
984
  type FaultRule = {
973
- /** Stable id, so a suite can retire exactly the rule it added. */
985
+ /** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
974
986
  id: string;
975
987
  /** Fault only this operation. Omit to match every operation. */
976
988
  operationId?: string;
977
989
  /** Fault only this HTTP method, case-insensitive. Omit to match every method. */
978
990
  method?: string;
991
+ /** Fault only this exact path. */
992
+ path?: string;
979
993
  /** Fault only paths starting with this prefix. Omit to match every path. */
980
994
  pathPrefix?: string;
995
+ /** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
996
+ pathPattern?: string;
997
+ /** Fault only requests carrying these headers. `"*"` matches any value. */
998
+ requestHeaders?: Record<string, string>;
999
+ /** Fault only requests carrying these query parameters. `"*"` matches any value. */
1000
+ query?: Record<string, string>;
981
1001
  /**
982
1002
  * Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
983
1003
  * an in-process caller usually wants, and what a parallel worker usually does not:
984
1004
  * rules added through `POST /__admin/faults` default to the calling namespace.
985
1005
  */
986
1006
  namespace?: string;
1007
+ /** Let this many matching requests through first. */
1008
+ after?: number;
1009
+ /** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
1010
+ every?: number;
1011
+ /** Retire the rule after this many faults. Omit to keep it until removed. */
1012
+ count?: number;
1013
+ /** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
1014
+ rate?: number;
987
1015
  /**
988
- * Status of the injected response. Omit for a rule that only delays (`delayMs` /
989
- * `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
990
- * the request then still reaches the service.
1016
+ * Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
1017
+ * clock when the rule was added. Listed as epoch ms.
1018
+ */
1019
+ from?: Instant | Duration;
1020
+ /** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
1021
+ until?: Instant | Duration;
1022
+ /**
1023
+ * Status of the injected response. Omit for a rule that only delays, times out, drops,
1024
+ * truncates, or switches on an `effect`: the request then still reaches the service.
991
1025
  */
992
1026
  status?: number;
993
1027
  /** Response body, serialized as JSON. A string is sent as-is. */
994
1028
  body?: unknown;
1029
+ /** Response headers. */
995
1030
  headers?: Record<string, string>;
996
- /** Retire the rule after this many faults. Omit to keep it until removed. */
997
- count?: number;
998
- /** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
999
- rate?: number;
1000
- /** Hold the response back this long, to exercise timeouts. */
1031
+ /** Hold the response back this long. Listed as `delayMs`. */
1032
+ delay?: Duration;
1033
+ /** `delay`, in milliseconds. */
1001
1034
  delayMs?: number;
1002
1035
  /** Alias of `delayMs`. */
1003
1036
  latencyMs?: number;
1037
+ /** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
1038
+ jitter?: Duration;
1039
+ jitterMs?: number;
1040
+ /**
1041
+ * Never answer: hold the request this long, then drop the connection. Listed as
1042
+ * `timeoutMs`. With virtual time the wait is spent on the mock clock.
1043
+ */
1044
+ timeout?: Duration;
1045
+ timeoutMs?: number;
1004
1046
  /**
1005
1047
  * Drop the connection instead of answering: an in-process `fetch` rejects with a
1006
1048
  * `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
1007
1049
  */
1008
1050
  drop?: boolean;
1051
+ /**
1052
+ * Cut the response body off after this many bytes: the status and headers arrive, then
1053
+ * the connection dies mid-body.
1054
+ */
1055
+ truncate?: number;
1009
1056
  /**
1010
1057
  * A named service behaviour to switch on for the matching request instead of (or
1011
1058
  * before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
@@ -1014,8 +1061,8 @@ type FaultRule = {
1014
1061
  effect?: string;
1015
1062
  /** Parameters for `effect`. */
1016
1063
  params?: Record<string, unknown>;
1017
- /** From the preset this rule was expanded from, if any. */
1018
- preset?: string;
1064
+ /** The preset this rule was expanded from, if any. */
1065
+ fromPreset?: string;
1019
1066
  };
1020
1067
  /** A fault that fired for one request. */
1021
1068
  type FaultHit = {
@@ -1023,6 +1070,10 @@ type FaultHit = {
1023
1070
  /** The injected response; absent when the rule only delays, drops, or sets an effect. */
1024
1071
  response?: Response;
1025
1072
  drop?: boolean;
1073
+ /** Set with `drop` when the rule held the request for its `timeout` first. */
1074
+ timeout?: boolean;
1075
+ /** Bytes of response body to let through before the connection dies. */
1076
+ truncate?: number;
1026
1077
  effect?: {
1027
1078
  name: string;
1028
1079
  params: Record<string, unknown>;
@@ -1047,38 +1098,202 @@ type FaultCandidate = {
1047
1098
  method: string;
1048
1099
  path: string;
1049
1100
  namespace: string;
1101
+ /** Request headers and query, for rules that match on them. */
1102
+ headers?: Headers;
1103
+ query?: URLSearchParams;
1104
+ /** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
1105
+ now?: number;
1106
+ /** How to spend a delay for this request. Default: the registry's own sleep. */
1107
+ wait?: (ms: number) => Promise<void>;
1108
+ };
1109
+ /** A stored rule and how often it was consulted. */
1110
+ type FaultListing = FaultRule & {
1111
+ /** Faults left before the rule retires. `null` when it has no `count`. */
1112
+ remaining: number | null;
1113
+ /** Times the rule faulted a request. */
1114
+ hits: number;
1115
+ /** Requests that matched the rule, faulted or not. */
1116
+ seen: number;
1117
+ };
1118
+ /** A namespace's own rules, with their counters, in the order they are tried. */
1119
+ type FaultSnapshot = {
1120
+ /**
1121
+ * `position` is how many `"*"` rules are tried before the rule, which is what its place
1122
+ * in the order means to a request and holds in any process.
1123
+ */
1124
+ rules: (FaultListing & {
1125
+ position?: number;
1126
+ })[];
1127
+ /** How many `fault_<n>` ids the namespace has handed out. */
1128
+ nextId?: number;
1050
1129
  };
1051
1130
  type FaultRegistry = {
1052
- isolateNamespaces(): void;
1053
- snapshot(namespace: string): {
1054
- rules: (FaultRule & {
1055
- remaining: number | null;
1056
- hits: number;
1057
- position: number;
1058
- })[];
1059
- rngState: number;
1060
- };
1131
+ snapshot(namespace: string): FaultSnapshot;
1132
+ /** Add a rule, or replace the rule with the same id in the same namespace. */
1061
1133
  add(rule: FaultRule): FaultRule;
1062
- list(): (FaultRule & {
1063
- remaining: number | null;
1064
- hits: number;
1065
- })[];
1066
- remove(id: string): boolean;
1067
- clear(): void;
1068
- restore(namespace: string, state: {
1069
- rules: (FaultRule & {
1070
- remaining: number | null;
1071
- hits: number;
1072
- position: number;
1073
- })[];
1074
- rngState: number;
1075
- }): void;
1134
+ /** The next unused `fault_<n>` id in `namespace`. */
1135
+ nextId(namespace: string): string;
1136
+ /** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
1137
+ list(namespace?: string): FaultListing[];
1138
+ get(id: string, namespace?: string): FaultListing | undefined;
1139
+ /** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
1140
+ remove(id: string, namespace?: string): boolean;
1141
+ /** Remove every rule, or one namespace's own rules. */
1142
+ clear(namespace?: string): void;
1143
+ /** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
1144
+ restore(namespace: string, state: FaultSnapshot): void;
1076
1145
  /**
1077
1146
  * Every fault this request should get, in rule order, stopping at the first that answers
1078
1147
  * or drops (effect-only and delay-only rules let later rules match too). Consumes one of
1079
1148
  * each matching rule's remaining uses.
1080
1149
  */
1081
1150
  take(candidate: FaultCandidate): Promise<FaultHit[]>;
1151
+ /**
1152
+ * Fire these rules at one request without storing them: no matching, no schedule, in order,
1153
+ * stopping at the first that answers or drops. Jitter draws from the namespace's stream.
1154
+ */
1155
+ fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
1156
+ };
1157
+
1158
+ /**
1159
+ * The admin controls every mock shares, as one document.
1160
+ *
1161
+ * The same shape is accepted in three places, and means the same thing in each:
1162
+ *
1163
+ * - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
1164
+ * `MOCKINGBIRD_ADMIN_CONTROLS`
1165
+ * - over HTTP: `PUT /__admin/controls`
1166
+ * - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
1167
+ *
1168
+ * Every control is deterministic: given the same seed, clock and requests, a mock answers
1169
+ * the same bytes. Nothing here reads the wall clock or `Math.random`.
1170
+ */
1171
+ /** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
1172
+ type Duration = number | string;
1173
+ /** Epoch milliseconds, or an ISO-8601 string. */
1174
+ type Instant = number | string;
1175
+ type ClockControls = {
1176
+ /** Move the clock to this instant. */
1177
+ now?: Instant;
1178
+ /** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
1179
+ frozen?: boolean;
1180
+ /**
1181
+ * Step the clock forward by this much before every vendor request. With a frozen clock
1182
+ * each request still gets its own, reproducible timestamp.
1183
+ */
1184
+ autoAdvance?: Duration;
1185
+ };
1186
+ /** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
1187
+ type LatencyRuleInput = {
1188
+ delay?: Duration;
1189
+ jitter?: Duration;
1190
+ };
1191
+ type LatencyControls = LatencyRuleInput & {
1192
+ /** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
1193
+ operations?: Record<string, Duration | LatencyRuleInput>;
1194
+ };
1195
+ /** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
1196
+ type FaultInput = (Omit<FaultRule, "id"> & {
1197
+ id?: string;
1198
+ }) | ({
1199
+ preset: string;
1200
+ } & Partial<FaultRule>);
1201
+ /** Identifies a {@link SnapshotDocument}. */
1202
+ declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
1203
+ /**
1204
+ * A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
1205
+ * `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
1206
+ * `records` are required, so a hand-written seed file is a valid snapshot.
1207
+ */
1208
+ type SnapshotDocument = {
1209
+ format: typeof SNAPSHOT_FORMAT;
1210
+ /** The service that wrote it. A snapshot is rejected by any other service. */
1211
+ service?: string;
1212
+ /**
1213
+ * Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
1214
+ * the stored text of a row that is not a plain record, and replaces `value` when present.
1215
+ */
1216
+ records: {
1217
+ collection: string;
1218
+ id: string;
1219
+ seq?: number;
1220
+ value?: unknown;
1221
+ raw?: string;
1222
+ }[];
1223
+ /** Id counters, so the next generated id continues where the snapshot left off. */
1224
+ sequences?: {
1225
+ name: string;
1226
+ kind: string;
1227
+ value: number;
1228
+ }[];
1229
+ clock?: {
1230
+ now: number;
1231
+ frozen: boolean;
1232
+ autoAdvanceMs?: number;
1233
+ };
1234
+ seed?: number | string;
1235
+ /** Positions of the seeded random streams. */
1236
+ rng?: {
1237
+ state: number;
1238
+ faults?: number;
1239
+ latency?: number;
1240
+ };
1241
+ faults?: unknown;
1242
+ latency?: unknown;
1243
+ virtualTime?: boolean;
1244
+ /** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
1245
+ webhooks?: unknown;
1246
+ };
1247
+ /** Controls for one namespace. */
1248
+ type NamespaceControls = {
1249
+ /** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
1250
+ seed?: number | string;
1251
+ clock?: ClockControls;
1252
+ /**
1253
+ * Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
1254
+ * wall clock: the request returns at once, the clock has moved, and the journal records
1255
+ * the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
1256
+ */
1257
+ virtualTime?: boolean;
1258
+ /** Baseline response time for every vendor request. A bare duration is its `delay`. */
1259
+ latency?: Duration | LatencyControls;
1260
+ faults?: readonly FaultInput[];
1261
+ /** Start from this state instead of the service's own seed data. */
1262
+ snapshot?: SnapshotDocument;
1263
+ };
1264
+ /** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
1265
+ type MockControls = NamespaceControls & {
1266
+ /** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
1267
+ credentials?: Record<string, string>;
1268
+ /** Overrides for named namespaces. A key given here replaces the shared value. */
1269
+ namespaces?: Record<string, NamespaceControls>;
1270
+ };
1271
+ /** A latency rule in milliseconds. */
1272
+ type LatencyRule = {
1273
+ delayMs: number;
1274
+ jitterMs: number;
1275
+ };
1276
+ /** Baseline latency for a namespace, normalized. */
1277
+ type LatencyState = LatencyRule & {
1278
+ operations: Record<string, LatencyRule>;
1279
+ };
1280
+ /** What {@link controlHeaders} turns into request headers. */
1281
+ type ControlHeaderInput = {
1282
+ namespace?: string;
1283
+ adminKey?: string;
1284
+ branch?: string;
1285
+ at?: string;
1286
+ reset?: boolean;
1287
+ restore?: string;
1288
+ seed?: number | string;
1289
+ time?: Instant | Date;
1290
+ advance?: Duration;
1291
+ virtualTime?: boolean;
1292
+ delay?: Duration;
1293
+ timeout?: Duration;
1294
+ /** A status, `"drop"`, a preset name, or a fault rule. */
1295
+ fault?: number | string | Record<string, unknown>;
1296
+ snapshot?: string;
1082
1297
  };
1083
1298
 
1084
1299
  /**
@@ -1118,6 +1333,8 @@ type RequestLog = {
1118
1333
  unmatched: boolean;
1119
1334
  /** Set when a fault rule produced the response. */
1120
1335
  faultId?: string;
1336
+ /** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
1337
+ delayMs?: number;
1121
1338
  /** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
1122
1339
  ids?: Record<string, string>;
1123
1340
  /** Set when the service created a resource the request referred to but that did not exist. */
@@ -1163,6 +1380,12 @@ type JournalQuery = {
1163
1380
  /** Only this namespace. Omit for every namespace, oldest first across all of them. */
1164
1381
  namespace?: string;
1165
1382
  operationId?: string;
1383
+ /** Only this HTTP method, case-insensitive. */
1384
+ method?: string;
1385
+ /** Only this exact path. */
1386
+ path?: string;
1387
+ /** Only requests this fault rule answered or shaped. */
1388
+ faultId?: string;
1166
1389
  status?: number;
1167
1390
  /** Only entries at or after this instant (epoch ms). */
1168
1391
  since?: number;
@@ -1177,6 +1400,38 @@ type Journal = {
1177
1400
  clear(namespace?: string): void;
1178
1401
  };
1179
1402
 
1403
+ /**
1404
+ * Baseline response time, per namespace: how long every vendor request takes before any
1405
+ * fault is considered. Jitter draws from a stream the caller owns, one per namespace and
1406
+ * separate from the fault stream, so adding latency never shifts which requests a `rate`
1407
+ * fault hits.
1408
+ */
1409
+ type LatencyRegistry = {
1410
+ get(namespace: string): LatencyState;
1411
+ set(namespace: string, state: LatencyState): LatencyState;
1412
+ /** Remove one namespace's latency, or every namespace's. */
1413
+ clear(namespace?: string): void;
1414
+ /** Milliseconds this request waits. Draws jitter, so call it once per request. */
1415
+ take(namespace: string, operationId: string | undefined): number;
1416
+ };
1417
+
1418
+ /** Clock state as the admin API reports it. */
1419
+ type ControlClock = ClockState & {
1420
+ /** `now`, as ISO-8601. */
1421
+ iso: string;
1422
+ /** Milliseconds the clock steps before every vendor request. */
1423
+ autoAdvanceMs: number;
1424
+ };
1425
+ /** One namespace's controls: the body of `GET /__admin/controls`. */
1426
+ type ControlState = {
1427
+ namespace: string;
1428
+ seed: number | string;
1429
+ clock: ControlClock;
1430
+ virtualTime: boolean;
1431
+ latency: LatencyState;
1432
+ faults: FaultListing[];
1433
+ };
1434
+
1180
1435
  type AdminRequest = {
1181
1436
  request: Request;
1182
1437
  url: URL;
@@ -1193,6 +1448,17 @@ type AdminRoute = (request: AdminRequest) => Response | Promise<Response>;
1193
1448
  * `:param` segments — e.g. `"POST /orders/:id/transition"`.
1194
1449
  */
1195
1450
  type AdminRoutes = Record<string, AdminRoute>;
1451
+ /** What `GET /__admin/snapshots` lists for each snapshot. */
1452
+ type SnapshotInfo = {
1453
+ id: string;
1454
+ namespace: string;
1455
+ /** Stored rows in the snapshot. */
1456
+ records: number;
1457
+ /** The namespace clock when it was taken, epoch ms. */
1458
+ at: number;
1459
+ /** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
1460
+ portable: boolean;
1461
+ };
1196
1462
 
1197
1463
  /** Credential → namespace mapping behind `PUT /__admin/credentials`. */
1198
1464
  type CredentialRegistry = {
@@ -1206,6 +1472,8 @@ type CredentialRegistry = {
1206
1472
  }[];
1207
1473
  };
1208
1474
 
1475
+ type OwnedTransport = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
1476
+
1209
1477
  /**
1210
1478
  * A point-in-time copy of everything a service namespace holds.
1211
1479
  *
@@ -1392,8 +1660,16 @@ type PublishInput = {
1392
1660
  id?: string;
1393
1661
  };
1394
1662
  type WebhookHub = {
1663
+ observeDeliveries(listener: (event: {
1664
+ namespace: string;
1665
+ state: string;
1666
+ deliveryId: string;
1667
+ }) => void): () => void;
1668
+ setTransportPolicy(policy: OwnedTransport): void;
1395
1669
  pending(namespace: string): number;
1396
1670
  snapshot(namespace: string): unknown;
1671
+ exportSnapshot(handle: unknown): unknown;
1672
+ importSnapshot(namespace: string, document: unknown): unknown;
1397
1673
  restore(namespace: string, snapshot: unknown): void;
1398
1674
  publish(input: PublishInput): WebhookMessage;
1399
1675
  /** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
@@ -1406,7 +1682,7 @@ type WebhookHub = {
1406
1682
  /** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
1407
1683
  flush(): Promise<void>;
1408
1684
  /** Resolve once nothing is in flight. */
1409
- idle(): Promise<void>;
1685
+ idle(namespace?: string): Promise<void>;
1410
1686
  fault(namespace: string, fault: WebhookFault): void;
1411
1687
  clear(namespace?: string): void;
1412
1688
  };
@@ -1437,14 +1713,26 @@ type RuntimeOptions<T extends ServiceInstance> = {
1437
1713
  sqlite?: SqliteClient;
1438
1714
  /** Defaults to a live clock over `Date.now`. */
1439
1715
  clock?: Clock;
1440
- /** Seeds every random choice the runtime makes (fault rates). Default `0`. */
1716
+ /** Seeds every random choice the runtime makes (fault rates, jitter). Default `0`. */
1441
1717
  seed?: number | string;
1718
+ /**
1719
+ * Admin controls to boot with: clock, latency, faults, a starting snapshot, credential
1720
+ * mappings and per-namespace overrides. `reset` returns a namespace to exactly this.
1721
+ */
1722
+ adminControls?: MockControls;
1442
1723
  /** Extra `GET /__admin/health` fields, such as the loaded corpus version. */
1443
1724
  describe?: () => Record<string, unknown>;
1444
1725
  /** Service-specific admin routes, given the runtime so they can reach any namespace. */
1445
1726
  admin?: (runtime: ServiceRuntime<T>) => AdminRoutes;
1446
1727
  /** Reserved tree for every internal HTTP endpoint, UI and namespace carrier. Default /__admin. */
1447
1728
  adminPrefix?: string;
1729
+ /**
1730
+ * A namespace's clock was moved — by `/__admin/clock`, a header, a restore, a reset, or a
1731
+ * wait spent in virtual time. Run whatever the service schedules by that clock, so what
1732
+ * came due is seen at once and not on the next request. Moving the base clock in process
1733
+ * (`runtime.clock`) is the caller's own doing and is not reported here.
1734
+ */
1735
+ onClockChange?: (namespace: string) => void;
1448
1736
  /** Invalidate transient handles before replacing an existing instance's stored state. */
1449
1737
  beforeRestore?: (instance: T) => void;
1450
1738
  /** Require this value in `x-mockingbird-admin-key` on admin data routes. */
@@ -1462,7 +1750,10 @@ type RuntimeOptions<T extends ServiceInstance> = {
1462
1750
  * `sigV4AccessKeyId`.
1463
1751
  */
1464
1752
  credential?: (request: Request) => string | undefined;
1465
- /** Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. */
1753
+ /**
1754
+ * Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. They join the
1755
+ * standard presets every mock has; one with a standard name replaces it.
1756
+ */
1466
1757
  presets?: Record<string, FaultPreset>;
1467
1758
  /** Outbound webhooks; adds the `/__admin/webhooks*` routes and clears on reset. */
1468
1759
  webhooks?: WebhookHub;
@@ -1485,11 +1776,15 @@ type RuntimeOptions<T extends ServiceInstance> = {
1485
1776
  type RuntimeIO = {
1486
1777
  wallNow(): number;
1487
1778
  monotonicNow(): number;
1488
- sleep(ms: number): Promise<void>;
1779
+ /** Wait on the wall clock. Rejects with the signal's reason when it aborts first. */
1780
+ sleep(ms: number, signal?: AbortSignal): Promise<void>;
1489
1781
  };
1490
1782
  type ServiceTimelineState = Readonly<{
1491
1783
  snapshot: NamespaceSnapshot;
1492
- clock: Readonly<ReturnType<Clock["state"]>>;
1784
+ /** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
1785
+ clock: Readonly<ClockState & {
1786
+ pinned?: boolean;
1787
+ }>;
1493
1788
  rngState: number;
1494
1789
  }>;
1495
1790
  type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
@@ -1502,27 +1797,63 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1502
1797
  readonly sqlite: SqliteClient;
1503
1798
  readonly clock: Clock;
1504
1799
  readonly faults: FaultRegistry;
1800
+ /** Baseline response time per namespace. */
1801
+ readonly latency: LatencyRegistry;
1505
1802
  readonly metrics: Metrics;
1506
1803
  readonly journal: Journal;
1507
1804
  readonly rng: Rng;
1508
1805
  readonly credentials: CredentialRegistry;
1509
1806
  /** The webhook hub, when the service has outbound webhooks. */
1510
1807
  readonly webhooks: WebhookHub | undefined;
1511
- /** Enable independent namespace clocks and PRNGs before serving a fleet. */
1512
- isolateNamespaces(): void;
1808
+ /**
1809
+ * A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
1810
+ * offset and a freeze of its own, so moving one never moves another.
1811
+ */
1513
1812
  namespaceClock(namespace?: string): Clock;
1514
1813
  namespaceOf(request: Request): string;
1515
1814
  /** Opaque in-process checkpoint including records, controls and webhook history. */
1516
1815
  fleetSnapshot(namespace: string): object;
1517
1816
  fleetRestore(snapshot: unknown, namespace: string): void;
1817
+ fleetExport(snapshot: unknown): SnapshotDocument;
1818
+ fleetImport(namespace: string, document: unknown): object;
1518
1819
  /** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
1519
1820
  applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
1520
1821
  /** The instance behind `namespace` (the default one when omitted), created on first use. */
1521
1822
  instance(namespace?: string): T;
1522
1823
  /** Public names of every namespace created so far. */
1523
1824
  namespaces(): string[];
1524
- /** Reset one namespace, or every namespace with `"*"`. */
1825
+ /** Return one namespace to its boot state, or every namespace with `"*"`. */
1525
1826
  reset(namespace?: string): Promise<void>;
1827
+ /** Reset a namespace and forget it, releasing everything it held. */
1828
+ dropNamespace(namespace: string): Promise<void>;
1829
+ /** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
1830
+ controls(namespace?: string): ControlState & {
1831
+ snapshots: SnapshotInfo[];
1832
+ };
1833
+ /** Apply a control document to a namespace. A given key replaces that control. */
1834
+ configure(controls: NamespaceControls, namespace?: string): ControlState & {
1835
+ snapshots: SnapshotInfo[];
1836
+ };
1837
+ /** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
1838
+ readonly snapshots: {
1839
+ /** Capture the namespace now. Reusing a name replaces that snapshot. */
1840
+ create(options?: {
1841
+ name?: string;
1842
+ namespace?: string;
1843
+ }): SnapshotInfo;
1844
+ list(namespace?: string): SnapshotInfo[];
1845
+ /** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
1846
+ export(id: string, namespace?: string): SnapshotDocument;
1847
+ /** Store a document as a snapshot without restoring it. */
1848
+ import(document: SnapshotDocument, options?: {
1849
+ name?: string;
1850
+ namespace?: string;
1851
+ }): SnapshotInfo;
1852
+ restore(id: string, namespace?: string): void;
1853
+ delete(id: string, namespace?: string): boolean;
1854
+ };
1855
+ /** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
1856
+ headers(input: ControlHeaderInput): Record<string, string>;
1526
1857
  snapshot(namespace?: string): NamespaceSnapshot;
1527
1858
  restore(snapshot: NamespaceSnapshot, namespace?: string): void;
1528
1859
  /** Capture the current branch. Mutating HTTP calls do this automatically. */
@@ -1543,7 +1874,7 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1543
1874
  state(namespace?: string): StateView;
1544
1875
  };
1545
1876
 
1546
- type MicrosoftRuntimeOptions = Pick<RuntimeOptions<MicrosoftAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<MicrosoftAPIOptions, "fixtures" | "baseUrl" | "tokens">;
1877
+ type MicrosoftRuntimeOptions = Pick<RuntimeOptions<MicrosoftAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "adminControls" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<MicrosoftAPIOptions, "fixtures" | "baseUrl" | "tokens">;
1547
1878
  type MicrosoftRuntime = ServiceRuntime<MicrosoftAPI>;
1548
1879
  declare function createRuntime(options?: MicrosoftRuntimeOptions): MicrosoftRuntime;
1549
1880
 
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@ import {
5
5
  document,
6
6
  operationIds,
7
7
  supportedOperationIds
8
- } from "./chunk-NTODMBCU.js";
9
- import "./chunk-M73LWCCP.js";
8
+ } from "./chunk-2S244VQQ.js";
9
+ import "./chunk-6Z2MR3PO.js";
10
10
  export {
11
11
  MICROSOFT_NAMESPACE,
12
12
  MicrosoftAPI,