@crvouga/mockingbird-service-mongoatlas 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/CHANGELOG.md +11 -1
- package/DISCOVERY.md +23 -0
- package/README.md +15 -3
- package/dist/chunk-2M4V3TBY.js +6900 -0
- package/dist/chunk-2M4V3TBY.js.map +7 -0
- package/dist/{chunk-M73LWCCP.js → chunk-6Z2MR3PO.js} +33 -3
- package/dist/{chunk-M73LWCCP.js.map → chunk-6Z2MR3PO.js.map} +2 -2
- package/dist/{chunk-FRHQ7ANR.js → chunk-P52TRCEH.js} +136 -25
- package/dist/chunk-P52TRCEH.js.map +7 -0
- package/dist/cli.js +3 -3
- package/dist/crypto-II2ETGQQ.js +21 -0
- package/dist/{helpers-BMHFVPFA.js → helpers-5D4ROPWG.js} +2 -2
- package/dist/index.d.ts +361 -44
- package/dist/index.js +2 -2
- package/dist/server.d.ts +700 -84
- package/dist/server.js +3 -3
- package/package.json +3 -2
- package/dist/chunk-FRHQ7ANR.js.map +0 -7
- package/dist/chunk-T3D2FSIN.js +0 -5102
- package/dist/chunk-T3D2FSIN.js.map +0 -7
- package/dist/crypto-I5AVO7Q3.js +0 -15
- /package/dist/{crypto-I5AVO7Q3.js.map → crypto-II2ETGQQ.js.map} +0 -0
- /package/dist/{helpers-BMHFVPFA.js.map → helpers-5D4ROPWG.js.map} +0 -0
package/dist/server.d.ts
CHANGED
|
@@ -144,7 +144,7 @@ type ClockState$1 = {
|
|
|
144
144
|
now: number;
|
|
145
145
|
/** True while time does not advance on its own. */
|
|
146
146
|
frozen: boolean;
|
|
147
|
-
/** Milliseconds
|
|
147
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
148
148
|
offsetMs: number;
|
|
149
149
|
};
|
|
150
150
|
type Clock$1 = {
|
|
@@ -180,7 +180,10 @@ type Rng$1 = {
|
|
|
180
180
|
state(): number;
|
|
181
181
|
/** Restore a state previously returned by {@link state}. */
|
|
182
182
|
setState(state: number): void;
|
|
183
|
-
seed
|
|
183
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
184
|
+
reseed(seed: number | string): void;
|
|
185
|
+
/** The seed the stream currently starts from. */
|
|
186
|
+
readonly seed: number;
|
|
184
187
|
};
|
|
185
188
|
|
|
186
189
|
/**
|
|
@@ -189,44 +192,83 @@ type Rng$1 = {
|
|
|
189
192
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
190
193
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
191
194
|
* appears under load, the 5xx that proves a retry path works.
|
|
195
|
+
*
|
|
196
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
197
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
198
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
192
199
|
*/
|
|
193
200
|
type FaultRule$1 = {
|
|
194
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
201
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
195
202
|
id: string;
|
|
196
203
|
/** Fault only this operation. Omit to match every operation. */
|
|
197
204
|
operationId?: string;
|
|
198
205
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
199
206
|
method?: string;
|
|
207
|
+
/** Fault only this exact path. */
|
|
208
|
+
path?: string;
|
|
200
209
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
201
210
|
pathPrefix?: string;
|
|
211
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
212
|
+
pathPattern?: string;
|
|
213
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
214
|
+
requestHeaders?: Record<string, string>;
|
|
215
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
216
|
+
query?: Record<string, string>;
|
|
202
217
|
/**
|
|
203
218
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
204
219
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
205
220
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
206
221
|
*/
|
|
207
222
|
namespace?: string;
|
|
223
|
+
/** Let this many matching requests through first. */
|
|
224
|
+
after?: number;
|
|
225
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
226
|
+
every?: number;
|
|
227
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
228
|
+
count?: number;
|
|
229
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
230
|
+
rate?: number;
|
|
231
|
+
/**
|
|
232
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
233
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
234
|
+
*/
|
|
235
|
+
from?: Instant$1 | Duration$1;
|
|
236
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
237
|
+
until?: Instant$1 | Duration$1;
|
|
208
238
|
/**
|
|
209
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
210
|
-
*
|
|
211
|
-
* the request then still reaches the service.
|
|
239
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
240
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
212
241
|
*/
|
|
213
242
|
status?: number;
|
|
214
243
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
215
244
|
body?: unknown;
|
|
245
|
+
/** Response headers. */
|
|
216
246
|
headers?: Record<string, string>;
|
|
217
|
-
/**
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
rate?: number;
|
|
221
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
247
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
248
|
+
delay?: Duration$1;
|
|
249
|
+
/** `delay`, in milliseconds. */
|
|
222
250
|
delayMs?: number;
|
|
223
251
|
/** Alias of `delayMs`. */
|
|
224
252
|
latencyMs?: number;
|
|
253
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
254
|
+
jitter?: Duration$1;
|
|
255
|
+
jitterMs?: number;
|
|
256
|
+
/**
|
|
257
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
258
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
259
|
+
*/
|
|
260
|
+
timeout?: Duration$1;
|
|
261
|
+
timeoutMs?: number;
|
|
225
262
|
/**
|
|
226
263
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
227
264
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
228
265
|
*/
|
|
229
266
|
drop?: boolean;
|
|
267
|
+
/**
|
|
268
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
269
|
+
* the connection dies mid-body.
|
|
270
|
+
*/
|
|
271
|
+
truncate?: number;
|
|
230
272
|
/**
|
|
231
273
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
232
274
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -235,8 +277,8 @@ type FaultRule$1 = {
|
|
|
235
277
|
effect?: string;
|
|
236
278
|
/** Parameters for `effect`. */
|
|
237
279
|
params?: Record<string, unknown>;
|
|
238
|
-
/**
|
|
239
|
-
|
|
280
|
+
/** The preset this rule was expanded from, if any. */
|
|
281
|
+
fromPreset?: string;
|
|
240
282
|
};
|
|
241
283
|
/** A fault that fired for one request. */
|
|
242
284
|
type FaultHit$1 = {
|
|
@@ -244,6 +286,10 @@ type FaultHit$1 = {
|
|
|
244
286
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
245
287
|
response?: Response;
|
|
246
288
|
drop?: boolean;
|
|
289
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
290
|
+
timeout?: boolean;
|
|
291
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
292
|
+
truncate?: number;
|
|
247
293
|
effect?: {
|
|
248
294
|
name: string;
|
|
249
295
|
params: Record<string, unknown>;
|
|
@@ -255,38 +301,200 @@ type FaultCandidate$1 = {
|
|
|
255
301
|
method: string;
|
|
256
302
|
path: string;
|
|
257
303
|
namespace: string;
|
|
304
|
+
/** Request headers and query, for rules that match on them. */
|
|
305
|
+
headers?: Headers;
|
|
306
|
+
query?: URLSearchParams;
|
|
307
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
308
|
+
now?: number;
|
|
309
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
310
|
+
wait?: (ms: number) => Promise<void>;
|
|
311
|
+
};
|
|
312
|
+
/** A stored rule and how often it was consulted. */
|
|
313
|
+
type FaultListing$1 = FaultRule$1 & {
|
|
314
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
315
|
+
remaining: number | null;
|
|
316
|
+
/** Times the rule faulted a request. */
|
|
317
|
+
hits: number;
|
|
318
|
+
/** Requests that matched the rule, faulted or not. */
|
|
319
|
+
seen: number;
|
|
320
|
+
};
|
|
321
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
322
|
+
type FaultSnapshot$1 = {
|
|
323
|
+
/**
|
|
324
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
325
|
+
* in the order means to a request and holds in any process.
|
|
326
|
+
*/
|
|
327
|
+
rules: (FaultListing$1 & {
|
|
328
|
+
position?: number;
|
|
329
|
+
})[];
|
|
330
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
331
|
+
nextId?: number;
|
|
258
332
|
};
|
|
259
333
|
type FaultRegistry$1 = {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
rules: (FaultRule$1 & {
|
|
263
|
-
remaining: number | null;
|
|
264
|
-
hits: number;
|
|
265
|
-
position: number;
|
|
266
|
-
})[];
|
|
267
|
-
rngState: number;
|
|
268
|
-
};
|
|
334
|
+
snapshot(namespace: string): FaultSnapshot$1;
|
|
335
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
269
336
|
add(rule: FaultRule$1): FaultRule$1;
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
})[];
|
|
282
|
-
rngState: number;
|
|
283
|
-
}): void;
|
|
337
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
338
|
+
nextId(namespace: string): string;
|
|
339
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
340
|
+
list(namespace?: string): FaultListing$1[];
|
|
341
|
+
get(id: string, namespace?: string): FaultListing$1 | undefined;
|
|
342
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
343
|
+
remove(id: string, namespace?: string): boolean;
|
|
344
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
345
|
+
clear(namespace?: string): void;
|
|
346
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
347
|
+
restore(namespace: string, state: FaultSnapshot$1): void;
|
|
284
348
|
/**
|
|
285
349
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
286
350
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
287
351
|
* each matching rule's remaining uses.
|
|
288
352
|
*/
|
|
289
353
|
take(candidate: FaultCandidate$1): Promise<FaultHit$1[]>;
|
|
354
|
+
/**
|
|
355
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
356
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
357
|
+
*/
|
|
358
|
+
fire(rules: readonly FaultRule$1[], candidate: FaultCandidate$1): Promise<FaultHit$1[]>;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The admin controls every mock shares, as one document.
|
|
363
|
+
*
|
|
364
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
365
|
+
*
|
|
366
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
367
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
368
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
369
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
370
|
+
*
|
|
371
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
372
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
373
|
+
*/
|
|
374
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
375
|
+
type Duration$1 = number | string;
|
|
376
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
377
|
+
type Instant$1 = number | string;
|
|
378
|
+
type ClockControls$1 = {
|
|
379
|
+
/** Move the clock to this instant. */
|
|
380
|
+
now?: Instant$1;
|
|
381
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
382
|
+
frozen?: boolean;
|
|
383
|
+
/**
|
|
384
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
385
|
+
* each request still gets its own, reproducible timestamp.
|
|
386
|
+
*/
|
|
387
|
+
autoAdvance?: Duration$1;
|
|
388
|
+
};
|
|
389
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
390
|
+
type LatencyRuleInput$1 = {
|
|
391
|
+
delay?: Duration$1;
|
|
392
|
+
jitter?: Duration$1;
|
|
393
|
+
};
|
|
394
|
+
type LatencyControls$1 = LatencyRuleInput$1 & {
|
|
395
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
396
|
+
operations?: Record<string, Duration$1 | LatencyRuleInput$1>;
|
|
397
|
+
};
|
|
398
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
399
|
+
type FaultInput$1 = (Omit<FaultRule$1, "id"> & {
|
|
400
|
+
id?: string;
|
|
401
|
+
}) | ({
|
|
402
|
+
preset: string;
|
|
403
|
+
} & Partial<FaultRule$1>);
|
|
404
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
405
|
+
declare const SNAPSHOT_FORMAT$1 = "mockingbird.snapshot/v1";
|
|
406
|
+
/**
|
|
407
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
408
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
409
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
410
|
+
*/
|
|
411
|
+
type SnapshotDocument$1 = {
|
|
412
|
+
format: typeof SNAPSHOT_FORMAT$1;
|
|
413
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
414
|
+
service?: string;
|
|
415
|
+
/**
|
|
416
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
417
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
418
|
+
*/
|
|
419
|
+
records: {
|
|
420
|
+
collection: string;
|
|
421
|
+
id: string;
|
|
422
|
+
seq?: number;
|
|
423
|
+
value?: unknown;
|
|
424
|
+
raw?: string;
|
|
425
|
+
}[];
|
|
426
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
427
|
+
sequences?: {
|
|
428
|
+
name: string;
|
|
429
|
+
kind: string;
|
|
430
|
+
value: number;
|
|
431
|
+
}[];
|
|
432
|
+
clock?: {
|
|
433
|
+
now: number;
|
|
434
|
+
frozen: boolean;
|
|
435
|
+
autoAdvanceMs?: number;
|
|
436
|
+
};
|
|
437
|
+
seed?: number | string;
|
|
438
|
+
/** Positions of the seeded random streams. */
|
|
439
|
+
rng?: {
|
|
440
|
+
state: number;
|
|
441
|
+
faults?: number;
|
|
442
|
+
latency?: number;
|
|
443
|
+
};
|
|
444
|
+
faults?: unknown;
|
|
445
|
+
latency?: unknown;
|
|
446
|
+
virtualTime?: boolean;
|
|
447
|
+
};
|
|
448
|
+
/** Controls for one namespace. */
|
|
449
|
+
type NamespaceControls$1 = {
|
|
450
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
451
|
+
seed?: number | string;
|
|
452
|
+
clock?: ClockControls$1;
|
|
453
|
+
/**
|
|
454
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
455
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
456
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
457
|
+
*/
|
|
458
|
+
virtualTime?: boolean;
|
|
459
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
460
|
+
latency?: Duration$1 | LatencyControls$1;
|
|
461
|
+
faults?: readonly FaultInput$1[];
|
|
462
|
+
/** Start from this state instead of the service's own seed data. */
|
|
463
|
+
snapshot?: SnapshotDocument$1;
|
|
464
|
+
};
|
|
465
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
466
|
+
type MockControls$1 = NamespaceControls$1 & {
|
|
467
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
468
|
+
credentials?: Record<string, string>;
|
|
469
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
470
|
+
namespaces?: Record<string, NamespaceControls$1>;
|
|
471
|
+
};
|
|
472
|
+
/** A latency rule in milliseconds. */
|
|
473
|
+
type LatencyRule$1 = {
|
|
474
|
+
delayMs: number;
|
|
475
|
+
jitterMs: number;
|
|
476
|
+
};
|
|
477
|
+
/** Baseline latency for a namespace, normalized. */
|
|
478
|
+
type LatencyState$1 = LatencyRule$1 & {
|
|
479
|
+
operations: Record<string, LatencyRule$1>;
|
|
480
|
+
};
|
|
481
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
482
|
+
type ControlHeaderInput$1 = {
|
|
483
|
+
namespace?: string;
|
|
484
|
+
adminKey?: string;
|
|
485
|
+
branch?: string;
|
|
486
|
+
at?: string;
|
|
487
|
+
reset?: boolean;
|
|
488
|
+
restore?: string;
|
|
489
|
+
seed?: number | string;
|
|
490
|
+
time?: Instant$1 | Date;
|
|
491
|
+
advance?: Duration$1;
|
|
492
|
+
virtualTime?: boolean;
|
|
493
|
+
delay?: Duration$1;
|
|
494
|
+
timeout?: Duration$1;
|
|
495
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
496
|
+
fault?: number | string | Record<string, unknown>;
|
|
497
|
+
snapshot?: string;
|
|
290
498
|
};
|
|
291
499
|
|
|
292
500
|
/**
|
|
@@ -326,6 +534,8 @@ type RequestLog$1 = {
|
|
|
326
534
|
unmatched: boolean;
|
|
327
535
|
/** Set when a fault rule produced the response. */
|
|
328
536
|
faultId?: string;
|
|
537
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
538
|
+
delayMs?: number;
|
|
329
539
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
330
540
|
ids?: Record<string, string>;
|
|
331
541
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -371,6 +581,12 @@ type JournalQuery$1 = {
|
|
|
371
581
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
372
582
|
namespace?: string;
|
|
373
583
|
operationId?: string;
|
|
584
|
+
/** Only this HTTP method, case-insensitive. */
|
|
585
|
+
method?: string;
|
|
586
|
+
/** Only this exact path. */
|
|
587
|
+
path?: string;
|
|
588
|
+
/** Only requests this fault rule answered or shaped. */
|
|
589
|
+
faultId?: string;
|
|
374
590
|
status?: number;
|
|
375
591
|
/** Only entries at or after this instant (epoch ms). */
|
|
376
592
|
since?: number;
|
|
@@ -385,6 +601,50 @@ type Journal$1 = {
|
|
|
385
601
|
clear(namespace?: string): void;
|
|
386
602
|
};
|
|
387
603
|
|
|
604
|
+
/**
|
|
605
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
606
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
607
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
608
|
+
* fault hits.
|
|
609
|
+
*/
|
|
610
|
+
type LatencyRegistry$1 = {
|
|
611
|
+
get(namespace: string): LatencyState$1;
|
|
612
|
+
set(namespace: string, state: LatencyState$1): LatencyState$1;
|
|
613
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
614
|
+
clear(namespace?: string): void;
|
|
615
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
616
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
/** Clock state as the admin API reports it. */
|
|
620
|
+
type ControlClock$1 = ClockState$1 & {
|
|
621
|
+
/** `now`, as ISO-8601. */
|
|
622
|
+
iso: string;
|
|
623
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
624
|
+
autoAdvanceMs: number;
|
|
625
|
+
};
|
|
626
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
627
|
+
type ControlState$1 = {
|
|
628
|
+
namespace: string;
|
|
629
|
+
seed: number | string;
|
|
630
|
+
clock: ControlClock$1;
|
|
631
|
+
virtualTime: boolean;
|
|
632
|
+
latency: LatencyState$1;
|
|
633
|
+
faults: FaultListing$1[];
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
637
|
+
type SnapshotInfo$1 = {
|
|
638
|
+
id: string;
|
|
639
|
+
namespace: string;
|
|
640
|
+
/** Stored rows in the snapshot. */
|
|
641
|
+
records: number;
|
|
642
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
643
|
+
at: number;
|
|
644
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
645
|
+
portable: boolean;
|
|
646
|
+
};
|
|
647
|
+
|
|
388
648
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
389
649
|
type CredentialRegistry$1 = {
|
|
390
650
|
set(credential: string, namespace: string): void;
|
|
@@ -539,7 +799,10 @@ type ServiceInstance$1 = FetchAPI$1 & {
|
|
|
539
799
|
};
|
|
540
800
|
type ServiceTimelineState$1 = Readonly<{
|
|
541
801
|
snapshot: NamespaceSnapshot$1;
|
|
542
|
-
clock
|
|
802
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
803
|
+
clock: Readonly<ClockState$1 & {
|
|
804
|
+
pinned?: boolean;
|
|
805
|
+
}>;
|
|
543
806
|
rngState: number;
|
|
544
807
|
}>;
|
|
545
808
|
type ServiceCheckpoint$1 = Checkpoint$1<ServiceTimelineState$1>;
|
|
@@ -552,14 +815,18 @@ type ServiceRuntime$1<T extends ServiceInstance$1> = FetchAPI$1 & {
|
|
|
552
815
|
readonly sqlite: SqliteClient$3;
|
|
553
816
|
readonly clock: Clock$1;
|
|
554
817
|
readonly faults: FaultRegistry$1;
|
|
818
|
+
/** Baseline response time per namespace. */
|
|
819
|
+
readonly latency: LatencyRegistry$1;
|
|
555
820
|
readonly metrics: Metrics$1;
|
|
556
821
|
readonly journal: Journal$1;
|
|
557
822
|
readonly rng: Rng$1;
|
|
558
823
|
readonly credentials: CredentialRegistry$1;
|
|
559
824
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
560
825
|
readonly webhooks: WebhookHub$1 | undefined;
|
|
561
|
-
/**
|
|
562
|
-
|
|
826
|
+
/**
|
|
827
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
828
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
829
|
+
*/
|
|
563
830
|
namespaceClock(namespace?: string): Clock$1;
|
|
564
831
|
namespaceOf(request: Request): string;
|
|
565
832
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
@@ -571,8 +838,38 @@ type ServiceRuntime$1<T extends ServiceInstance$1> = FetchAPI$1 & {
|
|
|
571
838
|
instance(namespace?: string): T;
|
|
572
839
|
/** Public names of every namespace created so far. */
|
|
573
840
|
namespaces(): string[];
|
|
574
|
-
/**
|
|
841
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
575
842
|
reset(namespace?: string): Promise<void>;
|
|
843
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
844
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
845
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
846
|
+
controls(namespace?: string): ControlState$1 & {
|
|
847
|
+
snapshots: SnapshotInfo$1[];
|
|
848
|
+
};
|
|
849
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
850
|
+
configure(controls: NamespaceControls$1, namespace?: string): ControlState$1 & {
|
|
851
|
+
snapshots: SnapshotInfo$1[];
|
|
852
|
+
};
|
|
853
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
854
|
+
readonly snapshots: {
|
|
855
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
856
|
+
create(options?: {
|
|
857
|
+
name?: string;
|
|
858
|
+
namespace?: string;
|
|
859
|
+
}): SnapshotInfo$1;
|
|
860
|
+
list(namespace?: string): SnapshotInfo$1[];
|
|
861
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
862
|
+
export(id: string, namespace?: string): SnapshotDocument$1;
|
|
863
|
+
/** Store a document as a snapshot without restoring it. */
|
|
864
|
+
import(document: SnapshotDocument$1, options?: {
|
|
865
|
+
name?: string;
|
|
866
|
+
namespace?: string;
|
|
867
|
+
}): SnapshotInfo$1;
|
|
868
|
+
restore(id: string, namespace?: string): void;
|
|
869
|
+
delete(id: string, namespace?: string): boolean;
|
|
870
|
+
};
|
|
871
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
872
|
+
headers(input: ControlHeaderInput$1): Record<string, string>;
|
|
576
873
|
snapshot(namespace?: string): NamespaceSnapshot$1;
|
|
577
874
|
restore(snapshot: NamespaceSnapshot$1, namespace?: string): void;
|
|
578
875
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -615,6 +912,8 @@ type CommonServeOptions = {
|
|
|
615
912
|
adminKey: string | undefined;
|
|
616
913
|
seed: string | undefined;
|
|
617
914
|
onLog: ((entry: RequestLog$1) => void) | undefined;
|
|
915
|
+
/** Admin controls to boot with: clock, latency, faults, a starting snapshot. */
|
|
916
|
+
adminControls?: MockControls$1;
|
|
618
917
|
/** Synthetic provider fixture objects supplied by a fleet entry. */
|
|
619
918
|
fixtures?: Record<string, unknown>;
|
|
620
919
|
baseUrl?: string;
|
|
@@ -945,7 +1244,7 @@ type ClockState = {
|
|
|
945
1244
|
now: number;
|
|
946
1245
|
/** True while time does not advance on its own. */
|
|
947
1246
|
frozen: boolean;
|
|
948
|
-
/** Milliseconds
|
|
1247
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
949
1248
|
offsetMs: number;
|
|
950
1249
|
};
|
|
951
1250
|
type Clock = {
|
|
@@ -981,7 +1280,10 @@ type Rng = {
|
|
|
981
1280
|
state(): number;
|
|
982
1281
|
/** Restore a state previously returned by {@link state}. */
|
|
983
1282
|
setState(state: number): void;
|
|
984
|
-
seed
|
|
1283
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
1284
|
+
reseed(seed: number | string): void;
|
|
1285
|
+
/** The seed the stream currently starts from. */
|
|
1286
|
+
readonly seed: number;
|
|
985
1287
|
};
|
|
986
1288
|
|
|
987
1289
|
/**
|
|
@@ -990,44 +1292,83 @@ type Rng = {
|
|
|
990
1292
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
991
1293
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
992
1294
|
* appears under load, the 5xx that proves a retry path works.
|
|
1295
|
+
*
|
|
1296
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
1297
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
1298
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
993
1299
|
*/
|
|
994
1300
|
type FaultRule = {
|
|
995
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
1301
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
996
1302
|
id: string;
|
|
997
1303
|
/** Fault only this operation. Omit to match every operation. */
|
|
998
1304
|
operationId?: string;
|
|
999
1305
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
1000
1306
|
method?: string;
|
|
1307
|
+
/** Fault only this exact path. */
|
|
1308
|
+
path?: string;
|
|
1001
1309
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
1002
1310
|
pathPrefix?: string;
|
|
1311
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
1312
|
+
pathPattern?: string;
|
|
1313
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
1314
|
+
requestHeaders?: Record<string, string>;
|
|
1315
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
1316
|
+
query?: Record<string, string>;
|
|
1003
1317
|
/**
|
|
1004
1318
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
1005
1319
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
1006
1320
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
1007
1321
|
*/
|
|
1008
1322
|
namespace?: string;
|
|
1323
|
+
/** Let this many matching requests through first. */
|
|
1324
|
+
after?: number;
|
|
1325
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
1326
|
+
every?: number;
|
|
1327
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
1328
|
+
count?: number;
|
|
1329
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
1330
|
+
rate?: number;
|
|
1009
1331
|
/**
|
|
1010
|
-
*
|
|
1011
|
-
*
|
|
1012
|
-
|
|
1332
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
1333
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
1334
|
+
*/
|
|
1335
|
+
from?: Instant | Duration;
|
|
1336
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
1337
|
+
until?: Instant | Duration;
|
|
1338
|
+
/**
|
|
1339
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
1340
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
1013
1341
|
*/
|
|
1014
1342
|
status?: number;
|
|
1015
1343
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
1016
1344
|
body?: unknown;
|
|
1345
|
+
/** Response headers. */
|
|
1017
1346
|
headers?: Record<string, string>;
|
|
1018
|
-
/**
|
|
1019
|
-
|
|
1020
|
-
/**
|
|
1021
|
-
rate?: number;
|
|
1022
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
1347
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
1348
|
+
delay?: Duration;
|
|
1349
|
+
/** `delay`, in milliseconds. */
|
|
1023
1350
|
delayMs?: number;
|
|
1024
1351
|
/** Alias of `delayMs`. */
|
|
1025
1352
|
latencyMs?: number;
|
|
1353
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
1354
|
+
jitter?: Duration;
|
|
1355
|
+
jitterMs?: number;
|
|
1356
|
+
/**
|
|
1357
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
1358
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
1359
|
+
*/
|
|
1360
|
+
timeout?: Duration;
|
|
1361
|
+
timeoutMs?: number;
|
|
1026
1362
|
/**
|
|
1027
1363
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
1028
1364
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
1029
1365
|
*/
|
|
1030
1366
|
drop?: boolean;
|
|
1367
|
+
/**
|
|
1368
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
1369
|
+
* the connection dies mid-body.
|
|
1370
|
+
*/
|
|
1371
|
+
truncate?: number;
|
|
1031
1372
|
/**
|
|
1032
1373
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
1033
1374
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -1036,8 +1377,8 @@ type FaultRule = {
|
|
|
1036
1377
|
effect?: string;
|
|
1037
1378
|
/** Parameters for `effect`. */
|
|
1038
1379
|
params?: Record<string, unknown>;
|
|
1039
|
-
/**
|
|
1040
|
-
|
|
1380
|
+
/** The preset this rule was expanded from, if any. */
|
|
1381
|
+
fromPreset?: string;
|
|
1041
1382
|
};
|
|
1042
1383
|
/** A fault that fired for one request. */
|
|
1043
1384
|
type FaultHit = {
|
|
@@ -1045,6 +1386,10 @@ type FaultHit = {
|
|
|
1045
1386
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
1046
1387
|
response?: Response;
|
|
1047
1388
|
drop?: boolean;
|
|
1389
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
1390
|
+
timeout?: boolean;
|
|
1391
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
1392
|
+
truncate?: number;
|
|
1048
1393
|
effect?: {
|
|
1049
1394
|
name: string;
|
|
1050
1395
|
params: Record<string, unknown>;
|
|
@@ -1069,38 +1414,200 @@ type FaultCandidate = {
|
|
|
1069
1414
|
method: string;
|
|
1070
1415
|
path: string;
|
|
1071
1416
|
namespace: string;
|
|
1417
|
+
/** Request headers and query, for rules that match on them. */
|
|
1418
|
+
headers?: Headers;
|
|
1419
|
+
query?: URLSearchParams;
|
|
1420
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
1421
|
+
now?: number;
|
|
1422
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
1423
|
+
wait?: (ms: number) => Promise<void>;
|
|
1424
|
+
};
|
|
1425
|
+
/** A stored rule and how often it was consulted. */
|
|
1426
|
+
type FaultListing = FaultRule & {
|
|
1427
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
1428
|
+
remaining: number | null;
|
|
1429
|
+
/** Times the rule faulted a request. */
|
|
1430
|
+
hits: number;
|
|
1431
|
+
/** Requests that matched the rule, faulted or not. */
|
|
1432
|
+
seen: number;
|
|
1433
|
+
};
|
|
1434
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
1435
|
+
type FaultSnapshot = {
|
|
1436
|
+
/**
|
|
1437
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
1438
|
+
* in the order means to a request and holds in any process.
|
|
1439
|
+
*/
|
|
1440
|
+
rules: (FaultListing & {
|
|
1441
|
+
position?: number;
|
|
1442
|
+
})[];
|
|
1443
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
1444
|
+
nextId?: number;
|
|
1072
1445
|
};
|
|
1073
1446
|
type FaultRegistry = {
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
rules: (FaultRule & {
|
|
1077
|
-
remaining: number | null;
|
|
1078
|
-
hits: number;
|
|
1079
|
-
position: number;
|
|
1080
|
-
})[];
|
|
1081
|
-
rngState: number;
|
|
1082
|
-
};
|
|
1447
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
1448
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
1083
1449
|
add(rule: FaultRule): FaultRule;
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
})[];
|
|
1096
|
-
rngState: number;
|
|
1097
|
-
}): void;
|
|
1450
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
1451
|
+
nextId(namespace: string): string;
|
|
1452
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
1453
|
+
list(namespace?: string): FaultListing[];
|
|
1454
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
1455
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
1456
|
+
remove(id: string, namespace?: string): boolean;
|
|
1457
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
1458
|
+
clear(namespace?: string): void;
|
|
1459
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
1460
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
1098
1461
|
/**
|
|
1099
1462
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
1100
1463
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
1101
1464
|
* each matching rule's remaining uses.
|
|
1102
1465
|
*/
|
|
1103
1466
|
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
1467
|
+
/**
|
|
1468
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
1469
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
1470
|
+
*/
|
|
1471
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
1472
|
+
};
|
|
1473
|
+
|
|
1474
|
+
/**
|
|
1475
|
+
* The admin controls every mock shares, as one document.
|
|
1476
|
+
*
|
|
1477
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
1478
|
+
*
|
|
1479
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
1480
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
1481
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
1482
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
1483
|
+
*
|
|
1484
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
1485
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
1486
|
+
*/
|
|
1487
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
1488
|
+
type Duration = number | string;
|
|
1489
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
1490
|
+
type Instant = number | string;
|
|
1491
|
+
type ClockControls = {
|
|
1492
|
+
/** Move the clock to this instant. */
|
|
1493
|
+
now?: Instant;
|
|
1494
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
1495
|
+
frozen?: boolean;
|
|
1496
|
+
/**
|
|
1497
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
1498
|
+
* each request still gets its own, reproducible timestamp.
|
|
1499
|
+
*/
|
|
1500
|
+
autoAdvance?: Duration;
|
|
1501
|
+
};
|
|
1502
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
1503
|
+
type LatencyRuleInput = {
|
|
1504
|
+
delay?: Duration;
|
|
1505
|
+
jitter?: Duration;
|
|
1506
|
+
};
|
|
1507
|
+
type LatencyControls = LatencyRuleInput & {
|
|
1508
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
1509
|
+
operations?: Record<string, Duration | LatencyRuleInput>;
|
|
1510
|
+
};
|
|
1511
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
1512
|
+
type FaultInput = (Omit<FaultRule, "id"> & {
|
|
1513
|
+
id?: string;
|
|
1514
|
+
}) | ({
|
|
1515
|
+
preset: string;
|
|
1516
|
+
} & Partial<FaultRule>);
|
|
1517
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
1518
|
+
declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
1519
|
+
/**
|
|
1520
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
1521
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
1522
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
1523
|
+
*/
|
|
1524
|
+
type SnapshotDocument = {
|
|
1525
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
1526
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
1527
|
+
service?: string;
|
|
1528
|
+
/**
|
|
1529
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
1530
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
1531
|
+
*/
|
|
1532
|
+
records: {
|
|
1533
|
+
collection: string;
|
|
1534
|
+
id: string;
|
|
1535
|
+
seq?: number;
|
|
1536
|
+
value?: unknown;
|
|
1537
|
+
raw?: string;
|
|
1538
|
+
}[];
|
|
1539
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
1540
|
+
sequences?: {
|
|
1541
|
+
name: string;
|
|
1542
|
+
kind: string;
|
|
1543
|
+
value: number;
|
|
1544
|
+
}[];
|
|
1545
|
+
clock?: {
|
|
1546
|
+
now: number;
|
|
1547
|
+
frozen: boolean;
|
|
1548
|
+
autoAdvanceMs?: number;
|
|
1549
|
+
};
|
|
1550
|
+
seed?: number | string;
|
|
1551
|
+
/** Positions of the seeded random streams. */
|
|
1552
|
+
rng?: {
|
|
1553
|
+
state: number;
|
|
1554
|
+
faults?: number;
|
|
1555
|
+
latency?: number;
|
|
1556
|
+
};
|
|
1557
|
+
faults?: unknown;
|
|
1558
|
+
latency?: unknown;
|
|
1559
|
+
virtualTime?: boolean;
|
|
1560
|
+
};
|
|
1561
|
+
/** Controls for one namespace. */
|
|
1562
|
+
type NamespaceControls = {
|
|
1563
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
1564
|
+
seed?: number | string;
|
|
1565
|
+
clock?: ClockControls;
|
|
1566
|
+
/**
|
|
1567
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
1568
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
1569
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
1570
|
+
*/
|
|
1571
|
+
virtualTime?: boolean;
|
|
1572
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
1573
|
+
latency?: Duration | LatencyControls;
|
|
1574
|
+
faults?: readonly FaultInput[];
|
|
1575
|
+
/** Start from this state instead of the service's own seed data. */
|
|
1576
|
+
snapshot?: SnapshotDocument;
|
|
1577
|
+
};
|
|
1578
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
1579
|
+
type MockControls = NamespaceControls & {
|
|
1580
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
1581
|
+
credentials?: Record<string, string>;
|
|
1582
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
1583
|
+
namespaces?: Record<string, NamespaceControls>;
|
|
1584
|
+
};
|
|
1585
|
+
/** A latency rule in milliseconds. */
|
|
1586
|
+
type LatencyRule = {
|
|
1587
|
+
delayMs: number;
|
|
1588
|
+
jitterMs: number;
|
|
1589
|
+
};
|
|
1590
|
+
/** Baseline latency for a namespace, normalized. */
|
|
1591
|
+
type LatencyState = LatencyRule & {
|
|
1592
|
+
operations: Record<string, LatencyRule>;
|
|
1593
|
+
};
|
|
1594
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
1595
|
+
type ControlHeaderInput = {
|
|
1596
|
+
namespace?: string;
|
|
1597
|
+
adminKey?: string;
|
|
1598
|
+
branch?: string;
|
|
1599
|
+
at?: string;
|
|
1600
|
+
reset?: boolean;
|
|
1601
|
+
restore?: string;
|
|
1602
|
+
seed?: number | string;
|
|
1603
|
+
time?: Instant | Date;
|
|
1604
|
+
advance?: Duration;
|
|
1605
|
+
virtualTime?: boolean;
|
|
1606
|
+
delay?: Duration;
|
|
1607
|
+
timeout?: Duration;
|
|
1608
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
1609
|
+
fault?: number | string | Record<string, unknown>;
|
|
1610
|
+
snapshot?: string;
|
|
1104
1611
|
};
|
|
1105
1612
|
|
|
1106
1613
|
/**
|
|
@@ -1140,6 +1647,8 @@ type RequestLog = {
|
|
|
1140
1647
|
unmatched: boolean;
|
|
1141
1648
|
/** Set when a fault rule produced the response. */
|
|
1142
1649
|
faultId?: string;
|
|
1650
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
1651
|
+
delayMs?: number;
|
|
1143
1652
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
1144
1653
|
ids?: Record<string, string>;
|
|
1145
1654
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -1185,6 +1694,12 @@ type JournalQuery = {
|
|
|
1185
1694
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
1186
1695
|
namespace?: string;
|
|
1187
1696
|
operationId?: string;
|
|
1697
|
+
/** Only this HTTP method, case-insensitive. */
|
|
1698
|
+
method?: string;
|
|
1699
|
+
/** Only this exact path. */
|
|
1700
|
+
path?: string;
|
|
1701
|
+
/** Only requests this fault rule answered or shaped. */
|
|
1702
|
+
faultId?: string;
|
|
1188
1703
|
status?: number;
|
|
1189
1704
|
/** Only entries at or after this instant (epoch ms). */
|
|
1190
1705
|
since?: number;
|
|
@@ -1199,6 +1714,38 @@ type Journal = {
|
|
|
1199
1714
|
clear(namespace?: string): void;
|
|
1200
1715
|
};
|
|
1201
1716
|
|
|
1717
|
+
/**
|
|
1718
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
1719
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
1720
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
1721
|
+
* fault hits.
|
|
1722
|
+
*/
|
|
1723
|
+
type LatencyRegistry = {
|
|
1724
|
+
get(namespace: string): LatencyState;
|
|
1725
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
1726
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
1727
|
+
clear(namespace?: string): void;
|
|
1728
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
1729
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
1730
|
+
};
|
|
1731
|
+
|
|
1732
|
+
/** Clock state as the admin API reports it. */
|
|
1733
|
+
type ControlClock = ClockState & {
|
|
1734
|
+
/** `now`, as ISO-8601. */
|
|
1735
|
+
iso: string;
|
|
1736
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
1737
|
+
autoAdvanceMs: number;
|
|
1738
|
+
};
|
|
1739
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
1740
|
+
type ControlState = {
|
|
1741
|
+
namespace: string;
|
|
1742
|
+
seed: number | string;
|
|
1743
|
+
clock: ControlClock;
|
|
1744
|
+
virtualTime: boolean;
|
|
1745
|
+
latency: LatencyState;
|
|
1746
|
+
faults: FaultListing[];
|
|
1747
|
+
};
|
|
1748
|
+
|
|
1202
1749
|
type AdminRequest = {
|
|
1203
1750
|
request: Request;
|
|
1204
1751
|
url: URL;
|
|
@@ -1215,6 +1762,17 @@ type AdminRoute = (request: AdminRequest) => Response | Promise<Response>;
|
|
|
1215
1762
|
* `:param` segments — e.g. `"POST /orders/:id/transition"`.
|
|
1216
1763
|
*/
|
|
1217
1764
|
type AdminRoutes = Record<string, AdminRoute>;
|
|
1765
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
1766
|
+
type SnapshotInfo = {
|
|
1767
|
+
id: string;
|
|
1768
|
+
namespace: string;
|
|
1769
|
+
/** Stored rows in the snapshot. */
|
|
1770
|
+
records: number;
|
|
1771
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
1772
|
+
at: number;
|
|
1773
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
1774
|
+
portable: boolean;
|
|
1775
|
+
};
|
|
1218
1776
|
|
|
1219
1777
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
1220
1778
|
type CredentialRegistry = {
|
|
@@ -1459,14 +2017,26 @@ type RuntimeOptions<T extends ServiceInstance> = {
|
|
|
1459
2017
|
sqlite?: SqliteClient$2;
|
|
1460
2018
|
/** Defaults to a live clock over `Date.now`. */
|
|
1461
2019
|
clock?: Clock;
|
|
1462
|
-
/** Seeds every random choice the runtime makes (fault rates). Default `0`. */
|
|
2020
|
+
/** Seeds every random choice the runtime makes (fault rates, jitter). Default `0`. */
|
|
1463
2021
|
seed?: number | string;
|
|
2022
|
+
/**
|
|
2023
|
+
* Admin controls to boot with: clock, latency, faults, a starting snapshot, credential
|
|
2024
|
+
* mappings and per-namespace overrides. `reset` returns a namespace to exactly this.
|
|
2025
|
+
*/
|
|
2026
|
+
adminControls?: MockControls;
|
|
1464
2027
|
/** Extra `GET /__admin/health` fields, such as the loaded corpus version. */
|
|
1465
2028
|
describe?: () => Record<string, unknown>;
|
|
1466
2029
|
/** Service-specific admin routes, given the runtime so they can reach any namespace. */
|
|
1467
2030
|
admin?: (runtime: ServiceRuntime<T>) => AdminRoutes;
|
|
1468
2031
|
/** Reserved tree for every internal HTTP endpoint, UI and namespace carrier. Default /__admin. */
|
|
1469
2032
|
adminPrefix?: string;
|
|
2033
|
+
/**
|
|
2034
|
+
* A namespace's clock was moved — by `/__admin/clock`, a header, a restore, a reset, or a
|
|
2035
|
+
* wait spent in virtual time. Run whatever the service schedules by that clock, so what
|
|
2036
|
+
* came due is seen at once and not on the next request. Moving the base clock in process
|
|
2037
|
+
* (`runtime.clock`) is the caller's own doing and is not reported here.
|
|
2038
|
+
*/
|
|
2039
|
+
onClockChange?: (namespace: string) => void;
|
|
1470
2040
|
/** Invalidate transient handles before replacing an existing instance's stored state. */
|
|
1471
2041
|
beforeRestore?: (instance: T) => void;
|
|
1472
2042
|
/** Require this value in `x-mockingbird-admin-key` on admin data routes. */
|
|
@@ -1484,7 +2054,10 @@ type RuntimeOptions<T extends ServiceInstance> = {
|
|
|
1484
2054
|
* `sigV4AccessKeyId`.
|
|
1485
2055
|
*/
|
|
1486
2056
|
credential?: (request: Request) => string | undefined;
|
|
1487
|
-
/**
|
|
2057
|
+
/**
|
|
2058
|
+
* Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. They join the
|
|
2059
|
+
* standard presets every mock has; one with a standard name replaces it.
|
|
2060
|
+
*/
|
|
1488
2061
|
presets?: Record<string, FaultPreset>;
|
|
1489
2062
|
/** Outbound webhooks; adds the `/__admin/webhooks*` routes and clears on reset. */
|
|
1490
2063
|
webhooks?: WebhookHub;
|
|
@@ -1507,11 +2080,15 @@ type RuntimeOptions<T extends ServiceInstance> = {
|
|
|
1507
2080
|
type RuntimeIO = {
|
|
1508
2081
|
wallNow(): number;
|
|
1509
2082
|
monotonicNow(): number;
|
|
1510
|
-
|
|
2083
|
+
/** Wait on the wall clock. Rejects with the signal's reason when it aborts first. */
|
|
2084
|
+
sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
1511
2085
|
};
|
|
1512
2086
|
type ServiceTimelineState = Readonly<{
|
|
1513
2087
|
snapshot: NamespaceSnapshot;
|
|
1514
|
-
clock
|
|
2088
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
2089
|
+
clock: Readonly<ClockState & {
|
|
2090
|
+
pinned?: boolean;
|
|
2091
|
+
}>;
|
|
1515
2092
|
rngState: number;
|
|
1516
2093
|
}>;
|
|
1517
2094
|
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
@@ -1524,14 +2101,18 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
1524
2101
|
readonly sqlite: SqliteClient$2;
|
|
1525
2102
|
readonly clock: Clock;
|
|
1526
2103
|
readonly faults: FaultRegistry;
|
|
2104
|
+
/** Baseline response time per namespace. */
|
|
2105
|
+
readonly latency: LatencyRegistry;
|
|
1527
2106
|
readonly metrics: Metrics;
|
|
1528
2107
|
readonly journal: Journal;
|
|
1529
2108
|
readonly rng: Rng;
|
|
1530
2109
|
readonly credentials: CredentialRegistry;
|
|
1531
2110
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
1532
2111
|
readonly webhooks: WebhookHub | undefined;
|
|
1533
|
-
/**
|
|
1534
|
-
|
|
2112
|
+
/**
|
|
2113
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
2114
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
2115
|
+
*/
|
|
1535
2116
|
namespaceClock(namespace?: string): Clock;
|
|
1536
2117
|
namespaceOf(request: Request): string;
|
|
1537
2118
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
@@ -1543,8 +2124,38 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
1543
2124
|
instance(namespace?: string): T;
|
|
1544
2125
|
/** Public names of every namespace created so far. */
|
|
1545
2126
|
namespaces(): string[];
|
|
1546
|
-
/**
|
|
2127
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
1547
2128
|
reset(namespace?: string): Promise<void>;
|
|
2129
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
2130
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
2131
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
2132
|
+
controls(namespace?: string): ControlState & {
|
|
2133
|
+
snapshots: SnapshotInfo[];
|
|
2134
|
+
};
|
|
2135
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
2136
|
+
configure(controls: NamespaceControls, namespace?: string): ControlState & {
|
|
2137
|
+
snapshots: SnapshotInfo[];
|
|
2138
|
+
};
|
|
2139
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
2140
|
+
readonly snapshots: {
|
|
2141
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
2142
|
+
create(options?: {
|
|
2143
|
+
name?: string;
|
|
2144
|
+
namespace?: string;
|
|
2145
|
+
}): SnapshotInfo;
|
|
2146
|
+
list(namespace?: string): SnapshotInfo[];
|
|
2147
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
2148
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
2149
|
+
/** Store a document as a snapshot without restoring it. */
|
|
2150
|
+
import(document: SnapshotDocument, options?: {
|
|
2151
|
+
name?: string;
|
|
2152
|
+
namespace?: string;
|
|
2153
|
+
}): SnapshotInfo;
|
|
2154
|
+
restore(id: string, namespace?: string): void;
|
|
2155
|
+
delete(id: string, namespace?: string): boolean;
|
|
2156
|
+
};
|
|
2157
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
2158
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
1548
2159
|
snapshot(namespace?: string): NamespaceSnapshot;
|
|
1549
2160
|
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
1550
2161
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -1969,6 +2580,11 @@ type ProviderOptions = APIOptions & {
|
|
|
1969
2580
|
fallbackUser?: AuthFallback;
|
|
1970
2581
|
publicNamespace?: string;
|
|
1971
2582
|
adminPrefix?: string;
|
|
2583
|
+
/**
|
|
2584
|
+
* A seeded stream of `[0, 1)` that ids and tokens are minted from, so a run replays from
|
|
2585
|
+
* its seed. Default: the platform's randomness.
|
|
2586
|
+
*/
|
|
2587
|
+
random?: () => number;
|
|
1972
2588
|
};
|
|
1973
2589
|
/** Portable vendor API backed by the shared namespace; it owns no separate history or listener. */
|
|
1974
2590
|
declare class ProviderAPI {
|
|
@@ -2005,7 +2621,7 @@ declare class MongoAtlasAPI extends ProviderAPI {
|
|
|
2005
2621
|
reset(): Promise<void>;
|
|
2006
2622
|
}
|
|
2007
2623
|
|
|
2008
|
-
type MongoAtlasRuntimeOptions = Pick<RuntimeOptions<MongoAtlasAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<MongoAtlasAPIOptions, "fixtures" | "baseUrl" | "tokens">;
|
|
2624
|
+
type MongoAtlasRuntimeOptions = Pick<RuntimeOptions<MongoAtlasAPI>, "sqlite" | "clock" | "seed" | "adminPrefix" | "adminKey" | "adminControls" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<MongoAtlasAPIOptions, "fixtures" | "baseUrl" | "tokens">;
|
|
2009
2625
|
type MongoAtlasRuntime = ServiceRuntime<MongoAtlasAPI>;
|
|
2010
2626
|
|
|
2011
2627
|
declare const DEFAULT_PORT = 8905;
|