@crvouga/mockingbird-service-microsoft 0.1.0 → 0.2.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-54NQJFIT.js";
7
+ import "./chunk-JDVH3EVJ.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,200 @@ 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
+ };
1245
+ /** Controls for one namespace. */
1246
+ type NamespaceControls = {
1247
+ /** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
1248
+ seed?: number | string;
1249
+ clock?: ClockControls;
1250
+ /**
1251
+ * Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
1252
+ * wall clock: the request returns at once, the clock has moved, and the journal records
1253
+ * the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
1254
+ */
1255
+ virtualTime?: boolean;
1256
+ /** Baseline response time for every vendor request. A bare duration is its `delay`. */
1257
+ latency?: Duration | LatencyControls;
1258
+ faults?: readonly FaultInput[];
1259
+ /** Start from this state instead of the service's own seed data. */
1260
+ snapshot?: SnapshotDocument;
1261
+ };
1262
+ /** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
1263
+ type MockControls = NamespaceControls & {
1264
+ /** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
1265
+ credentials?: Record<string, string>;
1266
+ /** Overrides for named namespaces. A key given here replaces the shared value. */
1267
+ namespaces?: Record<string, NamespaceControls>;
1268
+ };
1269
+ /** A latency rule in milliseconds. */
1270
+ type LatencyRule = {
1271
+ delayMs: number;
1272
+ jitterMs: number;
1273
+ };
1274
+ /** Baseline latency for a namespace, normalized. */
1275
+ type LatencyState = LatencyRule & {
1276
+ operations: Record<string, LatencyRule>;
1277
+ };
1278
+ /** What {@link controlHeaders} turns into request headers. */
1279
+ type ControlHeaderInput = {
1280
+ namespace?: string;
1281
+ adminKey?: string;
1282
+ branch?: string;
1283
+ at?: string;
1284
+ reset?: boolean;
1285
+ restore?: string;
1286
+ seed?: number | string;
1287
+ time?: Instant | Date;
1288
+ advance?: Duration;
1289
+ virtualTime?: boolean;
1290
+ delay?: Duration;
1291
+ timeout?: Duration;
1292
+ /** A status, `"drop"`, a preset name, or a fault rule. */
1293
+ fault?: number | string | Record<string, unknown>;
1294
+ snapshot?: string;
1082
1295
  };
1083
1296
 
1084
1297
  /**
@@ -1118,6 +1331,8 @@ type RequestLog = {
1118
1331
  unmatched: boolean;
1119
1332
  /** Set when a fault rule produced the response. */
1120
1333
  faultId?: string;
1334
+ /** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
1335
+ delayMs?: number;
1121
1336
  /** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
1122
1337
  ids?: Record<string, string>;
1123
1338
  /** Set when the service created a resource the request referred to but that did not exist. */
@@ -1163,6 +1378,12 @@ type JournalQuery = {
1163
1378
  /** Only this namespace. Omit for every namespace, oldest first across all of them. */
1164
1379
  namespace?: string;
1165
1380
  operationId?: string;
1381
+ /** Only this HTTP method, case-insensitive. */
1382
+ method?: string;
1383
+ /** Only this exact path. */
1384
+ path?: string;
1385
+ /** Only requests this fault rule answered or shaped. */
1386
+ faultId?: string;
1166
1387
  status?: number;
1167
1388
  /** Only entries at or after this instant (epoch ms). */
1168
1389
  since?: number;
@@ -1177,6 +1398,38 @@ type Journal = {
1177
1398
  clear(namespace?: string): void;
1178
1399
  };
1179
1400
 
1401
+ /**
1402
+ * Baseline response time, per namespace: how long every vendor request takes before any
1403
+ * fault is considered. Jitter draws from a stream the caller owns, one per namespace and
1404
+ * separate from the fault stream, so adding latency never shifts which requests a `rate`
1405
+ * fault hits.
1406
+ */
1407
+ type LatencyRegistry = {
1408
+ get(namespace: string): LatencyState;
1409
+ set(namespace: string, state: LatencyState): LatencyState;
1410
+ /** Remove one namespace's latency, or every namespace's. */
1411
+ clear(namespace?: string): void;
1412
+ /** Milliseconds this request waits. Draws jitter, so call it once per request. */
1413
+ take(namespace: string, operationId: string | undefined): number;
1414
+ };
1415
+
1416
+ /** Clock state as the admin API reports it. */
1417
+ type ControlClock = ClockState & {
1418
+ /** `now`, as ISO-8601. */
1419
+ iso: string;
1420
+ /** Milliseconds the clock steps before every vendor request. */
1421
+ autoAdvanceMs: number;
1422
+ };
1423
+ /** One namespace's controls: the body of `GET /__admin/controls`. */
1424
+ type ControlState = {
1425
+ namespace: string;
1426
+ seed: number | string;
1427
+ clock: ControlClock;
1428
+ virtualTime: boolean;
1429
+ latency: LatencyState;
1430
+ faults: FaultListing[];
1431
+ };
1432
+
1180
1433
  type AdminRequest = {
1181
1434
  request: Request;
1182
1435
  url: URL;
@@ -1193,6 +1446,17 @@ type AdminRoute = (request: AdminRequest) => Response | Promise<Response>;
1193
1446
  * `:param` segments — e.g. `"POST /orders/:id/transition"`.
1194
1447
  */
1195
1448
  type AdminRoutes = Record<string, AdminRoute>;
1449
+ /** What `GET /__admin/snapshots` lists for each snapshot. */
1450
+ type SnapshotInfo = {
1451
+ id: string;
1452
+ namespace: string;
1453
+ /** Stored rows in the snapshot. */
1454
+ records: number;
1455
+ /** The namespace clock when it was taken, epoch ms. */
1456
+ at: number;
1457
+ /** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
1458
+ portable: boolean;
1459
+ };
1196
1460
 
1197
1461
  /** Credential → namespace mapping behind `PUT /__admin/credentials`. */
1198
1462
  type CredentialRegistry = {
@@ -1437,14 +1701,26 @@ type RuntimeOptions<T extends ServiceInstance> = {
1437
1701
  sqlite?: SqliteClient;
1438
1702
  /** Defaults to a live clock over `Date.now`. */
1439
1703
  clock?: Clock;
1440
- /** Seeds every random choice the runtime makes (fault rates). Default `0`. */
1704
+ /** Seeds every random choice the runtime makes (fault rates, jitter). Default `0`. */
1441
1705
  seed?: number | string;
1706
+ /**
1707
+ * Admin controls to boot with: clock, latency, faults, a starting snapshot, credential
1708
+ * mappings and per-namespace overrides. `reset` returns a namespace to exactly this.
1709
+ */
1710
+ adminControls?: MockControls;
1442
1711
  /** Extra `GET /__admin/health` fields, such as the loaded corpus version. */
1443
1712
  describe?: () => Record<string, unknown>;
1444
1713
  /** Service-specific admin routes, given the runtime so they can reach any namespace. */
1445
1714
  admin?: (runtime: ServiceRuntime<T>) => AdminRoutes;
1446
1715
  /** Reserved tree for every internal HTTP endpoint, UI and namespace carrier. Default /__admin. */
1447
1716
  adminPrefix?: string;
1717
+ /**
1718
+ * A namespace's clock was moved — by `/__admin/clock`, a header, a restore, a reset, or a
1719
+ * wait spent in virtual time. Run whatever the service schedules by that clock, so what
1720
+ * came due is seen at once and not on the next request. Moving the base clock in process
1721
+ * (`runtime.clock`) is the caller's own doing and is not reported here.
1722
+ */
1723
+ onClockChange?: (namespace: string) => void;
1448
1724
  /** Invalidate transient handles before replacing an existing instance's stored state. */
1449
1725
  beforeRestore?: (instance: T) => void;
1450
1726
  /** Require this value in `x-mockingbird-admin-key` on admin data routes. */
@@ -1462,7 +1738,10 @@ type RuntimeOptions<T extends ServiceInstance> = {
1462
1738
  * `sigV4AccessKeyId`.
1463
1739
  */
1464
1740
  credential?: (request: Request) => string | undefined;
1465
- /** Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. */
1741
+ /**
1742
+ * Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. They join the
1743
+ * standard presets every mock has; one with a standard name replaces it.
1744
+ */
1466
1745
  presets?: Record<string, FaultPreset>;
1467
1746
  /** Outbound webhooks; adds the `/__admin/webhooks*` routes and clears on reset. */
1468
1747
  webhooks?: WebhookHub;
@@ -1485,11 +1764,15 @@ type RuntimeOptions<T extends ServiceInstance> = {
1485
1764
  type RuntimeIO = {
1486
1765
  wallNow(): number;
1487
1766
  monotonicNow(): number;
1488
- sleep(ms: number): Promise<void>;
1767
+ /** Wait on the wall clock. Rejects with the signal's reason when it aborts first. */
1768
+ sleep(ms: number, signal?: AbortSignal): Promise<void>;
1489
1769
  };
1490
1770
  type ServiceTimelineState = Readonly<{
1491
1771
  snapshot: NamespaceSnapshot;
1492
- clock: Readonly<ReturnType<Clock["state"]>>;
1772
+ /** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
1773
+ clock: Readonly<ClockState & {
1774
+ pinned?: boolean;
1775
+ }>;
1493
1776
  rngState: number;
1494
1777
  }>;
1495
1778
  type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
@@ -1502,14 +1785,18 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1502
1785
  readonly sqlite: SqliteClient;
1503
1786
  readonly clock: Clock;
1504
1787
  readonly faults: FaultRegistry;
1788
+ /** Baseline response time per namespace. */
1789
+ readonly latency: LatencyRegistry;
1505
1790
  readonly metrics: Metrics;
1506
1791
  readonly journal: Journal;
1507
1792
  readonly rng: Rng;
1508
1793
  readonly credentials: CredentialRegistry;
1509
1794
  /** The webhook hub, when the service has outbound webhooks. */
1510
1795
  readonly webhooks: WebhookHub | undefined;
1511
- /** Enable independent namespace clocks and PRNGs before serving a fleet. */
1512
- isolateNamespaces(): void;
1796
+ /**
1797
+ * A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
1798
+ * offset and a freeze of its own, so moving one never moves another.
1799
+ */
1513
1800
  namespaceClock(namespace?: string): Clock;
1514
1801
  namespaceOf(request: Request): string;
1515
1802
  /** Opaque in-process checkpoint including records, controls and webhook history. */
@@ -1521,8 +1808,38 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1521
1808
  instance(namespace?: string): T;
1522
1809
  /** Public names of every namespace created so far. */
1523
1810
  namespaces(): string[];
1524
- /** Reset one namespace, or every namespace with `"*"`. */
1811
+ /** Return one namespace to its boot state, or every namespace with `"*"`. */
1525
1812
  reset(namespace?: string): Promise<void>;
1813
+ /** Reset a namespace and forget it, releasing everything it held. */
1814
+ dropNamespace(namespace: string): Promise<void>;
1815
+ /** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
1816
+ controls(namespace?: string): ControlState & {
1817
+ snapshots: SnapshotInfo[];
1818
+ };
1819
+ /** Apply a control document to a namespace. A given key replaces that control. */
1820
+ configure(controls: NamespaceControls, namespace?: string): ControlState & {
1821
+ snapshots: SnapshotInfo[];
1822
+ };
1823
+ /** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
1824
+ readonly snapshots: {
1825
+ /** Capture the namespace now. Reusing a name replaces that snapshot. */
1826
+ create(options?: {
1827
+ name?: string;
1828
+ namespace?: string;
1829
+ }): SnapshotInfo;
1830
+ list(namespace?: string): SnapshotInfo[];
1831
+ /** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
1832
+ export(id: string, namespace?: string): SnapshotDocument;
1833
+ /** Store a document as a snapshot without restoring it. */
1834
+ import(document: SnapshotDocument, options?: {
1835
+ name?: string;
1836
+ namespace?: string;
1837
+ }): SnapshotInfo;
1838
+ restore(id: string, namespace?: string): void;
1839
+ delete(id: string, namespace?: string): boolean;
1840
+ };
1841
+ /** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
1842
+ headers(input: ControlHeaderInput): Record<string, string>;
1526
1843
  snapshot(namespace?: string): NamespaceSnapshot;
1527
1844
  restore(snapshot: NamespaceSnapshot, namespace?: string): void;
1528
1845
  /** Capture the current branch. Mutating HTTP calls do this automatically. */
@@ -1543,7 +1860,7 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1543
1860
  state(namespace?: string): StateView;
1544
1861
  };
1545
1862
 
1546
- type MicrosoftRuntimeOptions = Pick<RuntimeOptions<MicrosoftAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<MicrosoftAPIOptions, "fixtures" | "baseUrl" | "tokens">;
1863
+ type MicrosoftRuntimeOptions = Pick<RuntimeOptions<MicrosoftAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "adminControls" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<MicrosoftAPIOptions, "fixtures" | "baseUrl" | "tokens">;
1547
1864
  type MicrosoftRuntime = ServiceRuntime<MicrosoftAPI>;
1548
1865
  declare function createRuntime(options?: MicrosoftRuntimeOptions): MicrosoftRuntime;
1549
1866
 
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-JDVH3EVJ.js";
9
+ import "./chunk-6Z2MR3PO.js";
10
10
  export {
11
11
  MICROSOFT_NAMESPACE,
12
12
  MicrosoftAPI,