@crvouga/mockingbird-service-google 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-S6TECUBL.js";
7
- import "./chunk-KRPPUJIW.js";
8
- import "./chunk-M73LWCCP.js";
6
+ } from "./chunk-HNG6OQSB.js";
7
+ import "./chunk-PF2EJVUZ.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
@@ -470,6 +470,11 @@ type ProviderOptions = APIOptions & {
470
470
  fallbackUser?: AuthFallback;
471
471
  publicNamespace?: string;
472
472
  adminPrefix?: string;
473
+ /**
474
+ * A seeded stream of `[0, 1)` that ids and tokens are minted from, so a run replays from
475
+ * its seed. Default: the platform's randomness.
476
+ */
477
+ random?: () => number;
473
478
  };
474
479
  /** Portable vendor API backed by the shared namespace; it owns no separate history or listener. */
475
480
  declare class ProviderAPI {
@@ -1004,7 +1009,7 @@ type ClockState = {
1004
1009
  now: number;
1005
1010
  /** True while time does not advance on its own. */
1006
1011
  frozen: boolean;
1007
- /** Milliseconds this clock adds to its underlying source. */
1012
+ /** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
1008
1013
  offsetMs: number;
1009
1014
  };
1010
1015
  type Clock = {
@@ -1040,7 +1045,10 @@ type Rng = {
1040
1045
  state(): number;
1041
1046
  /** Restore a state previously returned by {@link state}. */
1042
1047
  setState(state: number): void;
1043
- seed: number;
1048
+ /** Restart the stream from a new seed. `reset` then returns to this seed. */
1049
+ reseed(seed: number | string): void;
1050
+ /** The seed the stream currently starts from. */
1051
+ readonly seed: number;
1044
1052
  };
1045
1053
 
1046
1054
  /**
@@ -1049,44 +1057,83 @@ type Rng = {
1049
1057
  * This is how a suite reaches the vendor's failure modes without the vendor: the
1050
1058
  * quota error that only appears when a shared sandbox is full, the 429 that only
1051
1059
  * appears under load, the 5xx that proves a retry path works.
1060
+ *
1061
+ * A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
1062
+ * it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
1063
+ * and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
1052
1064
  */
1053
1065
  type FaultRule = {
1054
- /** Stable id, so a suite can retire exactly the rule it added. */
1066
+ /** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
1055
1067
  id: string;
1056
1068
  /** Fault only this operation. Omit to match every operation. */
1057
1069
  operationId?: string;
1058
1070
  /** Fault only this HTTP method, case-insensitive. Omit to match every method. */
1059
1071
  method?: string;
1072
+ /** Fault only this exact path. */
1073
+ path?: string;
1060
1074
  /** Fault only paths starting with this prefix. Omit to match every path. */
1061
1075
  pathPrefix?: string;
1076
+ /** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
1077
+ pathPattern?: string;
1078
+ /** Fault only requests carrying these headers. `"*"` matches any value. */
1079
+ requestHeaders?: Record<string, string>;
1080
+ /** Fault only requests carrying these query parameters. `"*"` matches any value. */
1081
+ query?: Record<string, string>;
1062
1082
  /**
1063
1083
  * Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
1064
1084
  * an in-process caller usually wants, and what a parallel worker usually does not:
1065
1085
  * rules added through `POST /__admin/faults` default to the calling namespace.
1066
1086
  */
1067
1087
  namespace?: string;
1088
+ /** Let this many matching requests through first. */
1089
+ after?: number;
1090
+ /** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
1091
+ every?: number;
1092
+ /** Retire the rule after this many faults. Omit to keep it until removed. */
1093
+ count?: number;
1094
+ /** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
1095
+ rate?: number;
1068
1096
  /**
1069
- * Status of the injected response. Omit for a rule that only delays (`delayMs` /
1070
- * `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
1071
- * the request then still reaches the service.
1097
+ * Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
1098
+ * clock when the rule was added. Listed as epoch ms.
1099
+ */
1100
+ from?: Instant | Duration;
1101
+ /** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
1102
+ until?: Instant | Duration;
1103
+ /**
1104
+ * Status of the injected response. Omit for a rule that only delays, times out, drops,
1105
+ * truncates, or switches on an `effect`: the request then still reaches the service.
1072
1106
  */
1073
1107
  status?: number;
1074
1108
  /** Response body, serialized as JSON. A string is sent as-is. */
1075
1109
  body?: unknown;
1110
+ /** Response headers. */
1076
1111
  headers?: Record<string, string>;
1077
- /** Retire the rule after this many faults. Omit to keep it until removed. */
1078
- count?: number;
1079
- /** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
1080
- rate?: number;
1081
- /** Hold the response back this long, to exercise timeouts. */
1112
+ /** Hold the response back this long. Listed as `delayMs`. */
1113
+ delay?: Duration;
1114
+ /** `delay`, in milliseconds. */
1082
1115
  delayMs?: number;
1083
1116
  /** Alias of `delayMs`. */
1084
1117
  latencyMs?: number;
1118
+ /** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
1119
+ jitter?: Duration;
1120
+ jitterMs?: number;
1121
+ /**
1122
+ * Never answer: hold the request this long, then drop the connection. Listed as
1123
+ * `timeoutMs`. With virtual time the wait is spent on the mock clock.
1124
+ */
1125
+ timeout?: Duration;
1126
+ timeoutMs?: number;
1085
1127
  /**
1086
1128
  * Drop the connection instead of answering: an in-process `fetch` rejects with a
1087
1129
  * `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
1088
1130
  */
1089
1131
  drop?: boolean;
1132
+ /**
1133
+ * Cut the response body off after this many bytes: the status and headers arrive, then
1134
+ * the connection dies mid-body.
1135
+ */
1136
+ truncate?: number;
1090
1137
  /**
1091
1138
  * A named service behaviour to switch on for the matching request instead of (or
1092
1139
  * before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
@@ -1095,8 +1142,8 @@ type FaultRule = {
1095
1142
  effect?: string;
1096
1143
  /** Parameters for `effect`. */
1097
1144
  params?: Record<string, unknown>;
1098
- /** From the preset this rule was expanded from, if any. */
1099
- preset?: string;
1145
+ /** The preset this rule was expanded from, if any. */
1146
+ fromPreset?: string;
1100
1147
  };
1101
1148
  /** A fault that fired for one request. */
1102
1149
  type FaultHit = {
@@ -1104,6 +1151,10 @@ type FaultHit = {
1104
1151
  /** The injected response; absent when the rule only delays, drops, or sets an effect. */
1105
1152
  response?: Response;
1106
1153
  drop?: boolean;
1154
+ /** Set with `drop` when the rule held the request for its `timeout` first. */
1155
+ timeout?: boolean;
1156
+ /** Bytes of response body to let through before the connection dies. */
1157
+ truncate?: number;
1107
1158
  effect?: {
1108
1159
  name: string;
1109
1160
  params: Record<string, unknown>;
@@ -1128,38 +1179,202 @@ type FaultCandidate = {
1128
1179
  method: string;
1129
1180
  path: string;
1130
1181
  namespace: string;
1182
+ /** Request headers and query, for rules that match on them. */
1183
+ headers?: Headers;
1184
+ query?: URLSearchParams;
1185
+ /** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
1186
+ now?: number;
1187
+ /** How to spend a delay for this request. Default: the registry's own sleep. */
1188
+ wait?: (ms: number) => Promise<void>;
1189
+ };
1190
+ /** A stored rule and how often it was consulted. */
1191
+ type FaultListing = FaultRule & {
1192
+ /** Faults left before the rule retires. `null` when it has no `count`. */
1193
+ remaining: number | null;
1194
+ /** Times the rule faulted a request. */
1195
+ hits: number;
1196
+ /** Requests that matched the rule, faulted or not. */
1197
+ seen: number;
1198
+ };
1199
+ /** A namespace's own rules, with their counters, in the order they are tried. */
1200
+ type FaultSnapshot = {
1201
+ /**
1202
+ * `position` is how many `"*"` rules are tried before the rule, which is what its place
1203
+ * in the order means to a request and holds in any process.
1204
+ */
1205
+ rules: (FaultListing & {
1206
+ position?: number;
1207
+ })[];
1208
+ /** How many `fault_<n>` ids the namespace has handed out. */
1209
+ nextId?: number;
1131
1210
  };
1132
1211
  type FaultRegistry = {
1133
- isolateNamespaces(): void;
1134
- snapshot(namespace: string): {
1135
- rules: (FaultRule & {
1136
- remaining: number | null;
1137
- hits: number;
1138
- position: number;
1139
- })[];
1140
- rngState: number;
1141
- };
1212
+ snapshot(namespace: string): FaultSnapshot;
1213
+ /** Add a rule, or replace the rule with the same id in the same namespace. */
1142
1214
  add(rule: FaultRule): FaultRule;
1143
- list(): (FaultRule & {
1144
- remaining: number | null;
1145
- hits: number;
1146
- })[];
1147
- remove(id: string): boolean;
1148
- clear(): void;
1149
- restore(namespace: string, state: {
1150
- rules: (FaultRule & {
1151
- remaining: number | null;
1152
- hits: number;
1153
- position: number;
1154
- })[];
1155
- rngState: number;
1156
- }): void;
1215
+ /** The next unused `fault_<n>` id in `namespace`. */
1216
+ nextId(namespace: string): string;
1217
+ /** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
1218
+ list(namespace?: string): FaultListing[];
1219
+ get(id: string, namespace?: string): FaultListing | undefined;
1220
+ /** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
1221
+ remove(id: string, namespace?: string): boolean;
1222
+ /** Remove every rule, or one namespace's own rules. */
1223
+ clear(namespace?: string): void;
1224
+ /** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
1225
+ restore(namespace: string, state: FaultSnapshot): void;
1157
1226
  /**
1158
1227
  * Every fault this request should get, in rule order, stopping at the first that answers
1159
1228
  * or drops (effect-only and delay-only rules let later rules match too). Consumes one of
1160
1229
  * each matching rule's remaining uses.
1161
1230
  */
1162
1231
  take(candidate: FaultCandidate): Promise<FaultHit[]>;
1232
+ /**
1233
+ * Fire these rules at one request without storing them: no matching, no schedule, in order,
1234
+ * stopping at the first that answers or drops. Jitter draws from the namespace's stream.
1235
+ */
1236
+ fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
1237
+ };
1238
+
1239
+ /**
1240
+ * The admin controls every mock shares, as one document.
1241
+ *
1242
+ * The same shape is accepted in three places, and means the same thing in each:
1243
+ *
1244
+ * - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
1245
+ * `MOCKINGBIRD_ADMIN_CONTROLS`
1246
+ * - over HTTP: `PUT /__admin/controls`
1247
+ * - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
1248
+ *
1249
+ * Every control is deterministic: given the same seed, clock and requests, a mock answers
1250
+ * the same bytes. Nothing here reads the wall clock or `Math.random`.
1251
+ */
1252
+ /** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
1253
+ type Duration = number | string;
1254
+ /** Epoch milliseconds, or an ISO-8601 string. */
1255
+ type Instant = number | string;
1256
+ type ClockControls = {
1257
+ /** Move the clock to this instant. */
1258
+ now?: Instant;
1259
+ /** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
1260
+ frozen?: boolean;
1261
+ /**
1262
+ * Step the clock forward by this much before every vendor request. With a frozen clock
1263
+ * each request still gets its own, reproducible timestamp.
1264
+ */
1265
+ autoAdvance?: Duration;
1266
+ };
1267
+ /** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
1268
+ type LatencyRuleInput = {
1269
+ delay?: Duration;
1270
+ jitter?: Duration;
1271
+ };
1272
+ type LatencyControls = LatencyRuleInput & {
1273
+ /** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
1274
+ operations?: Record<string, Duration | LatencyRuleInput>;
1275
+ };
1276
+ /** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
1277
+ type FaultInput = (Omit<FaultRule, "id"> & {
1278
+ id?: string;
1279
+ }) | ({
1280
+ preset: string;
1281
+ } & Partial<FaultRule>);
1282
+ /** Identifies a {@link SnapshotDocument}. */
1283
+ declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
1284
+ /**
1285
+ * A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
1286
+ * `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
1287
+ * `records` are required, so a hand-written seed file is a valid snapshot.
1288
+ */
1289
+ type SnapshotDocument = {
1290
+ format: typeof SNAPSHOT_FORMAT;
1291
+ /** The service that wrote it. A snapshot is rejected by any other service. */
1292
+ service?: string;
1293
+ /**
1294
+ * Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
1295
+ * the stored text of a row that is not a plain record, and replaces `value` when present.
1296
+ */
1297
+ records: {
1298
+ collection: string;
1299
+ id: string;
1300
+ seq?: number;
1301
+ value?: unknown;
1302
+ raw?: string;
1303
+ }[];
1304
+ /** Id counters, so the next generated id continues where the snapshot left off. */
1305
+ sequences?: {
1306
+ name: string;
1307
+ kind: string;
1308
+ value: number;
1309
+ }[];
1310
+ clock?: {
1311
+ now: number;
1312
+ frozen: boolean;
1313
+ autoAdvanceMs?: number;
1314
+ };
1315
+ seed?: number | string;
1316
+ /** Positions of the seeded random streams. */
1317
+ rng?: {
1318
+ state: number;
1319
+ faults?: number;
1320
+ latency?: number;
1321
+ };
1322
+ faults?: unknown;
1323
+ latency?: unknown;
1324
+ virtualTime?: boolean;
1325
+ /** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
1326
+ webhooks?: unknown;
1327
+ };
1328
+ /** Controls for one namespace. */
1329
+ type NamespaceControls = {
1330
+ /** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
1331
+ seed?: number | string;
1332
+ clock?: ClockControls;
1333
+ /**
1334
+ * Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
1335
+ * wall clock: the request returns at once, the clock has moved, and the journal records
1336
+ * the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
1337
+ */
1338
+ virtualTime?: boolean;
1339
+ /** Baseline response time for every vendor request. A bare duration is its `delay`. */
1340
+ latency?: Duration | LatencyControls;
1341
+ faults?: readonly FaultInput[];
1342
+ /** Start from this state instead of the service's own seed data. */
1343
+ snapshot?: SnapshotDocument;
1344
+ };
1345
+ /** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
1346
+ type MockControls = NamespaceControls & {
1347
+ /** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
1348
+ credentials?: Record<string, string>;
1349
+ /** Overrides for named namespaces. A key given here replaces the shared value. */
1350
+ namespaces?: Record<string, NamespaceControls>;
1351
+ };
1352
+ /** A latency rule in milliseconds. */
1353
+ type LatencyRule = {
1354
+ delayMs: number;
1355
+ jitterMs: number;
1356
+ };
1357
+ /** Baseline latency for a namespace, normalized. */
1358
+ type LatencyState = LatencyRule & {
1359
+ operations: Record<string, LatencyRule>;
1360
+ };
1361
+ /** What {@link controlHeaders} turns into request headers. */
1362
+ type ControlHeaderInput = {
1363
+ namespace?: string;
1364
+ adminKey?: string;
1365
+ branch?: string;
1366
+ at?: string;
1367
+ reset?: boolean;
1368
+ restore?: string;
1369
+ seed?: number | string;
1370
+ time?: Instant | Date;
1371
+ advance?: Duration;
1372
+ virtualTime?: boolean;
1373
+ delay?: Duration;
1374
+ timeout?: Duration;
1375
+ /** A status, `"drop"`, a preset name, or a fault rule. */
1376
+ fault?: number | string | Record<string, unknown>;
1377
+ snapshot?: string;
1163
1378
  };
1164
1379
 
1165
1380
  /**
@@ -1199,6 +1414,8 @@ type RequestLog = {
1199
1414
  unmatched: boolean;
1200
1415
  /** Set when a fault rule produced the response. */
1201
1416
  faultId?: string;
1417
+ /** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
1418
+ delayMs?: number;
1202
1419
  /** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
1203
1420
  ids?: Record<string, string>;
1204
1421
  /** Set when the service created a resource the request referred to but that did not exist. */
@@ -1244,6 +1461,12 @@ type JournalQuery = {
1244
1461
  /** Only this namespace. Omit for every namespace, oldest first across all of them. */
1245
1462
  namespace?: string;
1246
1463
  operationId?: string;
1464
+ /** Only this HTTP method, case-insensitive. */
1465
+ method?: string;
1466
+ /** Only this exact path. */
1467
+ path?: string;
1468
+ /** Only requests this fault rule answered or shaped. */
1469
+ faultId?: string;
1247
1470
  status?: number;
1248
1471
  /** Only entries at or after this instant (epoch ms). */
1249
1472
  since?: number;
@@ -1258,6 +1481,38 @@ type Journal = {
1258
1481
  clear(namespace?: string): void;
1259
1482
  };
1260
1483
 
1484
+ /**
1485
+ * Baseline response time, per namespace: how long every vendor request takes before any
1486
+ * fault is considered. Jitter draws from a stream the caller owns, one per namespace and
1487
+ * separate from the fault stream, so adding latency never shifts which requests a `rate`
1488
+ * fault hits.
1489
+ */
1490
+ type LatencyRegistry = {
1491
+ get(namespace: string): LatencyState;
1492
+ set(namespace: string, state: LatencyState): LatencyState;
1493
+ /** Remove one namespace's latency, or every namespace's. */
1494
+ clear(namespace?: string): void;
1495
+ /** Milliseconds this request waits. Draws jitter, so call it once per request. */
1496
+ take(namespace: string, operationId: string | undefined): number;
1497
+ };
1498
+
1499
+ /** Clock state as the admin API reports it. */
1500
+ type ControlClock = ClockState & {
1501
+ /** `now`, as ISO-8601. */
1502
+ iso: string;
1503
+ /** Milliseconds the clock steps before every vendor request. */
1504
+ autoAdvanceMs: number;
1505
+ };
1506
+ /** One namespace's controls: the body of `GET /__admin/controls`. */
1507
+ type ControlState = {
1508
+ namespace: string;
1509
+ seed: number | string;
1510
+ clock: ControlClock;
1511
+ virtualTime: boolean;
1512
+ latency: LatencyState;
1513
+ faults: FaultListing[];
1514
+ };
1515
+
1261
1516
  type AdminRequest = {
1262
1517
  request: Request;
1263
1518
  url: URL;
@@ -1274,6 +1529,17 @@ type AdminRoute = (request: AdminRequest) => Response | Promise<Response>;
1274
1529
  * `:param` segments — e.g. `"POST /orders/:id/transition"`.
1275
1530
  */
1276
1531
  type AdminRoutes = Record<string, AdminRoute>;
1532
+ /** What `GET /__admin/snapshots` lists for each snapshot. */
1533
+ type SnapshotInfo = {
1534
+ id: string;
1535
+ namespace: string;
1536
+ /** Stored rows in the snapshot. */
1537
+ records: number;
1538
+ /** The namespace clock when it was taken, epoch ms. */
1539
+ at: number;
1540
+ /** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
1541
+ portable: boolean;
1542
+ };
1277
1543
 
1278
1544
  /** Credential → namespace mapping behind `PUT /__admin/credentials`. */
1279
1545
  type CredentialRegistry = {
@@ -1287,6 +1553,8 @@ type CredentialRegistry = {
1287
1553
  }[];
1288
1554
  };
1289
1555
 
1556
+ type OwnedTransport = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
1557
+
1290
1558
  /**
1291
1559
  * A point-in-time copy of everything a service namespace holds.
1292
1560
  *
@@ -1473,8 +1741,16 @@ type PublishInput = {
1473
1741
  id?: string;
1474
1742
  };
1475
1743
  type WebhookHub = {
1744
+ observeDeliveries(listener: (event: {
1745
+ namespace: string;
1746
+ state: string;
1747
+ deliveryId: string;
1748
+ }) => void): () => void;
1749
+ setTransportPolicy(policy: OwnedTransport): void;
1476
1750
  pending(namespace: string): number;
1477
1751
  snapshot(namespace: string): unknown;
1752
+ exportSnapshot(handle: unknown): unknown;
1753
+ importSnapshot(namespace: string, document: unknown): unknown;
1478
1754
  restore(namespace: string, snapshot: unknown): void;
1479
1755
  publish(input: PublishInput): WebhookMessage;
1480
1756
  /** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
@@ -1487,7 +1763,7 @@ type WebhookHub = {
1487
1763
  /** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
1488
1764
  flush(): Promise<void>;
1489
1765
  /** Resolve once nothing is in flight. */
1490
- idle(): Promise<void>;
1766
+ idle(namespace?: string): Promise<void>;
1491
1767
  fault(namespace: string, fault: WebhookFault): void;
1492
1768
  clear(namespace?: string): void;
1493
1769
  };
@@ -1518,14 +1794,26 @@ type RuntimeOptions<T extends ServiceInstance> = {
1518
1794
  sqlite?: SqliteClient;
1519
1795
  /** Defaults to a live clock over `Date.now`. */
1520
1796
  clock?: Clock;
1521
- /** Seeds every random choice the runtime makes (fault rates). Default `0`. */
1797
+ /** Seeds every random choice the runtime makes (fault rates, jitter). Default `0`. */
1522
1798
  seed?: number | string;
1799
+ /**
1800
+ * Admin controls to boot with: clock, latency, faults, a starting snapshot, credential
1801
+ * mappings and per-namespace overrides. `reset` returns a namespace to exactly this.
1802
+ */
1803
+ adminControls?: MockControls;
1523
1804
  /** Extra `GET /__admin/health` fields, such as the loaded corpus version. */
1524
1805
  describe?: () => Record<string, unknown>;
1525
1806
  /** Service-specific admin routes, given the runtime so they can reach any namespace. */
1526
1807
  admin?: (runtime: ServiceRuntime<T>) => AdminRoutes;
1527
1808
  /** Reserved tree for every internal HTTP endpoint, UI and namespace carrier. Default /__admin. */
1528
1809
  adminPrefix?: string;
1810
+ /**
1811
+ * A namespace's clock was moved — by `/__admin/clock`, a header, a restore, a reset, or a
1812
+ * wait spent in virtual time. Run whatever the service schedules by that clock, so what
1813
+ * came due is seen at once and not on the next request. Moving the base clock in process
1814
+ * (`runtime.clock`) is the caller's own doing and is not reported here.
1815
+ */
1816
+ onClockChange?: (namespace: string) => void;
1529
1817
  /** Invalidate transient handles before replacing an existing instance's stored state. */
1530
1818
  beforeRestore?: (instance: T) => void;
1531
1819
  /** Require this value in `x-mockingbird-admin-key` on admin data routes. */
@@ -1543,7 +1831,10 @@ type RuntimeOptions<T extends ServiceInstance> = {
1543
1831
  * `sigV4AccessKeyId`.
1544
1832
  */
1545
1833
  credential?: (request: Request) => string | undefined;
1546
- /** Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. */
1834
+ /**
1835
+ * Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. They join the
1836
+ * standard presets every mock has; one with a standard name replaces it.
1837
+ */
1547
1838
  presets?: Record<string, FaultPreset>;
1548
1839
  /** Outbound webhooks; adds the `/__admin/webhooks*` routes and clears on reset. */
1549
1840
  webhooks?: WebhookHub;
@@ -1566,11 +1857,15 @@ type RuntimeOptions<T extends ServiceInstance> = {
1566
1857
  type RuntimeIO = {
1567
1858
  wallNow(): number;
1568
1859
  monotonicNow(): number;
1569
- sleep(ms: number): Promise<void>;
1860
+ /** Wait on the wall clock. Rejects with the signal's reason when it aborts first. */
1861
+ sleep(ms: number, signal?: AbortSignal): Promise<void>;
1570
1862
  };
1571
1863
  type ServiceTimelineState = Readonly<{
1572
1864
  snapshot: NamespaceSnapshot;
1573
- clock: Readonly<ReturnType<Clock["state"]>>;
1865
+ /** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
1866
+ clock: Readonly<ClockState & {
1867
+ pinned?: boolean;
1868
+ }>;
1574
1869
  rngState: number;
1575
1870
  }>;
1576
1871
  type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
@@ -1583,27 +1878,63 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1583
1878
  readonly sqlite: SqliteClient;
1584
1879
  readonly clock: Clock;
1585
1880
  readonly faults: FaultRegistry;
1881
+ /** Baseline response time per namespace. */
1882
+ readonly latency: LatencyRegistry;
1586
1883
  readonly metrics: Metrics;
1587
1884
  readonly journal: Journal;
1588
1885
  readonly rng: Rng;
1589
1886
  readonly credentials: CredentialRegistry;
1590
1887
  /** The webhook hub, when the service has outbound webhooks. */
1591
1888
  readonly webhooks: WebhookHub | undefined;
1592
- /** Enable independent namespace clocks and PRNGs before serving a fleet. */
1593
- isolateNamespaces(): void;
1889
+ /**
1890
+ * A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
1891
+ * offset and a freeze of its own, so moving one never moves another.
1892
+ */
1594
1893
  namespaceClock(namespace?: string): Clock;
1595
1894
  namespaceOf(request: Request): string;
1596
1895
  /** Opaque in-process checkpoint including records, controls and webhook history. */
1597
1896
  fleetSnapshot(namespace: string): object;
1598
1897
  fleetRestore(snapshot: unknown, namespace: string): void;
1898
+ fleetExport(snapshot: unknown): SnapshotDocument;
1899
+ fleetImport(namespace: string, document: unknown): object;
1599
1900
  /** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
1600
1901
  applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
1601
1902
  /** The instance behind `namespace` (the default one when omitted), created on first use. */
1602
1903
  instance(namespace?: string): T;
1603
1904
  /** Public names of every namespace created so far. */
1604
1905
  namespaces(): string[];
1605
- /** Reset one namespace, or every namespace with `"*"`. */
1906
+ /** Return one namespace to its boot state, or every namespace with `"*"`. */
1606
1907
  reset(namespace?: string): Promise<void>;
1908
+ /** Reset a namespace and forget it, releasing everything it held. */
1909
+ dropNamespace(namespace: string): Promise<void>;
1910
+ /** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
1911
+ controls(namespace?: string): ControlState & {
1912
+ snapshots: SnapshotInfo[];
1913
+ };
1914
+ /** Apply a control document to a namespace. A given key replaces that control. */
1915
+ configure(controls: NamespaceControls, namespace?: string): ControlState & {
1916
+ snapshots: SnapshotInfo[];
1917
+ };
1918
+ /** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
1919
+ readonly snapshots: {
1920
+ /** Capture the namespace now. Reusing a name replaces that snapshot. */
1921
+ create(options?: {
1922
+ name?: string;
1923
+ namespace?: string;
1924
+ }): SnapshotInfo;
1925
+ list(namespace?: string): SnapshotInfo[];
1926
+ /** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
1927
+ export(id: string, namespace?: string): SnapshotDocument;
1928
+ /** Store a document as a snapshot without restoring it. */
1929
+ import(document: SnapshotDocument, options?: {
1930
+ name?: string;
1931
+ namespace?: string;
1932
+ }): SnapshotInfo;
1933
+ restore(id: string, namespace?: string): void;
1934
+ delete(id: string, namespace?: string): boolean;
1935
+ };
1936
+ /** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
1937
+ headers(input: ControlHeaderInput): Record<string, string>;
1607
1938
  snapshot(namespace?: string): NamespaceSnapshot;
1608
1939
  restore(snapshot: NamespaceSnapshot, namespace?: string): void;
1609
1940
  /** Capture the current branch. Mutating HTTP calls do this automatically. */
@@ -1624,7 +1955,7 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1624
1955
  state(namespace?: string): StateView;
1625
1956
  };
1626
1957
 
1627
- type GoogleRuntimeOptions = Pick<RuntimeOptions<GoogleAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<GoogleAPIOptions, "fixtures" | "baseUrl" | "tokens">;
1958
+ type GoogleRuntimeOptions = Pick<RuntimeOptions<GoogleAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "adminControls" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<GoogleAPIOptions, "fixtures" | "baseUrl" | "tokens">;
1628
1959
  type GoogleRuntime = ServiceRuntime<GoogleAPI>;
1629
1960
  declare function createRuntime(options?: GoogleRuntimeOptions): GoogleRuntime;
1630
1961
 
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@ import {
5
5
  document,
6
6
  operationIds,
7
7
  supportedOperationIds
8
- } from "./chunk-KRPPUJIW.js";
9
- import "./chunk-M73LWCCP.js";
8
+ } from "./chunk-PF2EJVUZ.js";
9
+ import "./chunk-6Z2MR3PO.js";
10
10
  export {
11
11
  GOOGLE_NAMESPACE,
12
12
  GoogleAPI,