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