@crvouga/mockingbird-service-ecs 1.2.0 → 1.4.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 +19 -1
- package/DISCOVERY.md +23 -0
- package/README.md +20 -2
- package/dist/chunk-27EDANUF.js +4741 -0
- package/dist/chunk-27EDANUF.js.map +7 -0
- package/dist/chunk-FDEYPTYK.js +5286 -0
- package/dist/chunk-FDEYPTYK.js.map +7 -0
- package/dist/cli.js +2 -2
- package/dist/index.d.ts +354 -41
- package/dist/index.js +1 -1
- package/dist/server.d.ts +709 -82
- package/dist/server.js +2 -2
- package/package.json +3 -2
- package/dist/chunk-42CRFA4A.js +0 -3392
- package/dist/chunk-42CRFA4A.js.map +0 -7
- package/dist/chunk-EAQRG5QD.js +0 -822
- package/dist/chunk-EAQRG5QD.js.map +0 -7
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,202 @@ 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
|
+
/** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
|
|
448
|
+
webhooks?: unknown;
|
|
449
|
+
};
|
|
450
|
+
/** Controls for one namespace. */
|
|
451
|
+
type NamespaceControls$1 = {
|
|
452
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
453
|
+
seed?: number | string;
|
|
454
|
+
clock?: ClockControls$1;
|
|
455
|
+
/**
|
|
456
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
457
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
458
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
459
|
+
*/
|
|
460
|
+
virtualTime?: boolean;
|
|
461
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
462
|
+
latency?: Duration$1 | LatencyControls$1;
|
|
463
|
+
faults?: readonly FaultInput$1[];
|
|
464
|
+
/** Start from this state instead of the service's own seed data. */
|
|
465
|
+
snapshot?: SnapshotDocument$1;
|
|
466
|
+
};
|
|
467
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
468
|
+
type MockControls$1 = NamespaceControls$1 & {
|
|
469
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
470
|
+
credentials?: Record<string, string>;
|
|
471
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
472
|
+
namespaces?: Record<string, NamespaceControls$1>;
|
|
473
|
+
};
|
|
474
|
+
/** A latency rule in milliseconds. */
|
|
475
|
+
type LatencyRule$1 = {
|
|
476
|
+
delayMs: number;
|
|
477
|
+
jitterMs: number;
|
|
478
|
+
};
|
|
479
|
+
/** Baseline latency for a namespace, normalized. */
|
|
480
|
+
type LatencyState$1 = LatencyRule$1 & {
|
|
481
|
+
operations: Record<string, LatencyRule$1>;
|
|
482
|
+
};
|
|
483
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
484
|
+
type ControlHeaderInput$1 = {
|
|
485
|
+
namespace?: string;
|
|
486
|
+
adminKey?: string;
|
|
487
|
+
branch?: string;
|
|
488
|
+
at?: string;
|
|
489
|
+
reset?: boolean;
|
|
490
|
+
restore?: string;
|
|
491
|
+
seed?: number | string;
|
|
492
|
+
time?: Instant$1 | Date;
|
|
493
|
+
advance?: Duration$1;
|
|
494
|
+
virtualTime?: boolean;
|
|
495
|
+
delay?: Duration$1;
|
|
496
|
+
timeout?: Duration$1;
|
|
497
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
498
|
+
fault?: number | string | Record<string, unknown>;
|
|
499
|
+
snapshot?: string;
|
|
290
500
|
};
|
|
291
501
|
|
|
292
502
|
/**
|
|
@@ -326,6 +536,8 @@ type RequestLog$1 = {
|
|
|
326
536
|
unmatched: boolean;
|
|
327
537
|
/** Set when a fault rule produced the response. */
|
|
328
538
|
faultId?: string;
|
|
539
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
540
|
+
delayMs?: number;
|
|
329
541
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
330
542
|
ids?: Record<string, string>;
|
|
331
543
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -371,6 +583,12 @@ type JournalQuery$1 = {
|
|
|
371
583
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
372
584
|
namespace?: string;
|
|
373
585
|
operationId?: string;
|
|
586
|
+
/** Only this HTTP method, case-insensitive. */
|
|
587
|
+
method?: string;
|
|
588
|
+
/** Only this exact path. */
|
|
589
|
+
path?: string;
|
|
590
|
+
/** Only requests this fault rule answered or shaped. */
|
|
591
|
+
faultId?: string;
|
|
374
592
|
status?: number;
|
|
375
593
|
/** Only entries at or after this instant (epoch ms). */
|
|
376
594
|
since?: number;
|
|
@@ -385,6 +603,50 @@ type Journal$1 = {
|
|
|
385
603
|
clear(namespace?: string): void;
|
|
386
604
|
};
|
|
387
605
|
|
|
606
|
+
/**
|
|
607
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
608
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
609
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
610
|
+
* fault hits.
|
|
611
|
+
*/
|
|
612
|
+
type LatencyRegistry$1 = {
|
|
613
|
+
get(namespace: string): LatencyState$1;
|
|
614
|
+
set(namespace: string, state: LatencyState$1): LatencyState$1;
|
|
615
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
616
|
+
clear(namespace?: string): void;
|
|
617
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
618
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
/** Clock state as the admin API reports it. */
|
|
622
|
+
type ControlClock$1 = ClockState$1 & {
|
|
623
|
+
/** `now`, as ISO-8601. */
|
|
624
|
+
iso: string;
|
|
625
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
626
|
+
autoAdvanceMs: number;
|
|
627
|
+
};
|
|
628
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
629
|
+
type ControlState$1 = {
|
|
630
|
+
namespace: string;
|
|
631
|
+
seed: number | string;
|
|
632
|
+
clock: ControlClock$1;
|
|
633
|
+
virtualTime: boolean;
|
|
634
|
+
latency: LatencyState$1;
|
|
635
|
+
faults: FaultListing$1[];
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
639
|
+
type SnapshotInfo$1 = {
|
|
640
|
+
id: string;
|
|
641
|
+
namespace: string;
|
|
642
|
+
/** Stored rows in the snapshot. */
|
|
643
|
+
records: number;
|
|
644
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
645
|
+
at: number;
|
|
646
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
647
|
+
portable: boolean;
|
|
648
|
+
};
|
|
649
|
+
|
|
388
650
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
389
651
|
type CredentialRegistry$1 = {
|
|
390
652
|
set(credential: string, namespace: string): void;
|
|
@@ -397,6 +659,8 @@ type CredentialRegistry$1 = {
|
|
|
397
659
|
}[];
|
|
398
660
|
};
|
|
399
661
|
|
|
662
|
+
type OwnedTransport$1 = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
|
|
663
|
+
|
|
400
664
|
/**
|
|
401
665
|
* A point-in-time copy of everything a service namespace holds.
|
|
402
666
|
*
|
|
@@ -514,8 +778,16 @@ type PublishInput$1 = {
|
|
|
514
778
|
id?: string;
|
|
515
779
|
};
|
|
516
780
|
type WebhookHub$1 = {
|
|
781
|
+
observeDeliveries(listener: (event: {
|
|
782
|
+
namespace: string;
|
|
783
|
+
state: string;
|
|
784
|
+
deliveryId: string;
|
|
785
|
+
}) => void): () => void;
|
|
786
|
+
setTransportPolicy(policy: OwnedTransport$1): void;
|
|
517
787
|
pending(namespace: string): number;
|
|
518
788
|
snapshot(namespace: string): unknown;
|
|
789
|
+
exportSnapshot(handle: unknown): unknown;
|
|
790
|
+
importSnapshot(namespace: string, document: unknown): unknown;
|
|
519
791
|
restore(namespace: string, snapshot: unknown): void;
|
|
520
792
|
publish(input: PublishInput$1): WebhookMessage$1;
|
|
521
793
|
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
@@ -528,7 +800,7 @@ type WebhookHub$1 = {
|
|
|
528
800
|
/** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
|
|
529
801
|
flush(): Promise<void>;
|
|
530
802
|
/** Resolve once nothing is in flight. */
|
|
531
|
-
idle(): Promise<void>;
|
|
803
|
+
idle(namespace?: string): Promise<void>;
|
|
532
804
|
fault(namespace: string, fault: WebhookFault$1): void;
|
|
533
805
|
clear(namespace?: string): void;
|
|
534
806
|
};
|
|
@@ -539,7 +811,10 @@ type ServiceInstance$1 = FetchAPI$1 & {
|
|
|
539
811
|
};
|
|
540
812
|
type ServiceTimelineState$1 = Readonly<{
|
|
541
813
|
snapshot: NamespaceSnapshot$1;
|
|
542
|
-
clock
|
|
814
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
815
|
+
clock: Readonly<ClockState$1 & {
|
|
816
|
+
pinned?: boolean;
|
|
817
|
+
}>;
|
|
543
818
|
rngState: number;
|
|
544
819
|
}>;
|
|
545
820
|
type ServiceCheckpoint$1 = Checkpoint$1<ServiceTimelineState$1>;
|
|
@@ -552,27 +827,63 @@ type ServiceRuntime$1<T extends ServiceInstance$1> = FetchAPI$1 & {
|
|
|
552
827
|
readonly sqlite: SqliteClient$1;
|
|
553
828
|
readonly clock: Clock$1;
|
|
554
829
|
readonly faults: FaultRegistry$1;
|
|
830
|
+
/** Baseline response time per namespace. */
|
|
831
|
+
readonly latency: LatencyRegistry$1;
|
|
555
832
|
readonly metrics: Metrics$1;
|
|
556
833
|
readonly journal: Journal$1;
|
|
557
834
|
readonly rng: Rng$1;
|
|
558
835
|
readonly credentials: CredentialRegistry$1;
|
|
559
836
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
560
837
|
readonly webhooks: WebhookHub$1 | undefined;
|
|
561
|
-
/**
|
|
562
|
-
|
|
838
|
+
/**
|
|
839
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
840
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
841
|
+
*/
|
|
563
842
|
namespaceClock(namespace?: string): Clock$1;
|
|
564
843
|
namespaceOf(request: Request): string;
|
|
565
844
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
566
845
|
fleetSnapshot(namespace: string): object;
|
|
567
846
|
fleetRestore(snapshot: unknown, namespace: string): void;
|
|
847
|
+
fleetExport(snapshot: unknown): SnapshotDocument$1;
|
|
848
|
+
fleetImport(namespace: string, document: unknown): object;
|
|
568
849
|
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
569
850
|
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule$1>): FaultRule$1[];
|
|
570
851
|
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
571
852
|
instance(namespace?: string): T;
|
|
572
853
|
/** Public names of every namespace created so far. */
|
|
573
854
|
namespaces(): string[];
|
|
574
|
-
/**
|
|
855
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
575
856
|
reset(namespace?: string): Promise<void>;
|
|
857
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
858
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
859
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
860
|
+
controls(namespace?: string): ControlState$1 & {
|
|
861
|
+
snapshots: SnapshotInfo$1[];
|
|
862
|
+
};
|
|
863
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
864
|
+
configure(controls: NamespaceControls$1, namespace?: string): ControlState$1 & {
|
|
865
|
+
snapshots: SnapshotInfo$1[];
|
|
866
|
+
};
|
|
867
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
868
|
+
readonly snapshots: {
|
|
869
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
870
|
+
create(options?: {
|
|
871
|
+
name?: string;
|
|
872
|
+
namespace?: string;
|
|
873
|
+
}): SnapshotInfo$1;
|
|
874
|
+
list(namespace?: string): SnapshotInfo$1[];
|
|
875
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
876
|
+
export(id: string, namespace?: string): SnapshotDocument$1;
|
|
877
|
+
/** Store a document as a snapshot without restoring it. */
|
|
878
|
+
import(document: SnapshotDocument$1, options?: {
|
|
879
|
+
name?: string;
|
|
880
|
+
namespace?: string;
|
|
881
|
+
}): SnapshotInfo$1;
|
|
882
|
+
restore(id: string, namespace?: string): void;
|
|
883
|
+
delete(id: string, namespace?: string): boolean;
|
|
884
|
+
};
|
|
885
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
886
|
+
headers(input: ControlHeaderInput$1): Record<string, string>;
|
|
576
887
|
snapshot(namespace?: string): NamespaceSnapshot$1;
|
|
577
888
|
restore(snapshot: NamespaceSnapshot$1, namespace?: string): void;
|
|
578
889
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -615,6 +926,8 @@ type CommonServeOptions = {
|
|
|
615
926
|
adminKey: string | undefined;
|
|
616
927
|
seed: string | undefined;
|
|
617
928
|
onLog: ((entry: RequestLog$1) => void) | undefined;
|
|
929
|
+
/** Admin controls to boot with: clock, latency, faults, a starting snapshot. */
|
|
930
|
+
adminControls?: MockControls$1;
|
|
618
931
|
/** Synthetic provider fixture objects supplied by a fleet entry. */
|
|
619
932
|
fixtures?: Record<string, unknown>;
|
|
620
933
|
baseUrl?: string;
|
|
@@ -625,6 +938,7 @@ type CommonServeOptions = {
|
|
|
625
938
|
* `serveTarget`, which is also how `serve --config` finds services by name.
|
|
626
939
|
*/
|
|
627
940
|
type ServeTarget = {
|
|
941
|
+
packageVersion?: string;
|
|
628
942
|
name: string;
|
|
629
943
|
defaultPort: number;
|
|
630
944
|
/** Serve flags beyond the common ones. */
|
|
@@ -835,7 +1149,7 @@ type ClockState = {
|
|
|
835
1149
|
now: number;
|
|
836
1150
|
/** True while time does not advance on its own. */
|
|
837
1151
|
frozen: boolean;
|
|
838
|
-
/** Milliseconds
|
|
1152
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
839
1153
|
offsetMs: number;
|
|
840
1154
|
};
|
|
841
1155
|
type Clock = {
|
|
@@ -871,7 +1185,10 @@ type Rng = {
|
|
|
871
1185
|
state(): number;
|
|
872
1186
|
/** Restore a state previously returned by {@link state}. */
|
|
873
1187
|
setState(state: number): void;
|
|
874
|
-
seed
|
|
1188
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
1189
|
+
reseed(seed: number | string): void;
|
|
1190
|
+
/** The seed the stream currently starts from. */
|
|
1191
|
+
readonly seed: number;
|
|
875
1192
|
};
|
|
876
1193
|
|
|
877
1194
|
/**
|
|
@@ -880,44 +1197,83 @@ type Rng = {
|
|
|
880
1197
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
881
1198
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
882
1199
|
* appears under load, the 5xx that proves a retry path works.
|
|
1200
|
+
*
|
|
1201
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
1202
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
1203
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
883
1204
|
*/
|
|
884
1205
|
type FaultRule = {
|
|
885
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
1206
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
886
1207
|
id: string;
|
|
887
1208
|
/** Fault only this operation. Omit to match every operation. */
|
|
888
1209
|
operationId?: string;
|
|
889
1210
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
890
1211
|
method?: string;
|
|
1212
|
+
/** Fault only this exact path. */
|
|
1213
|
+
path?: string;
|
|
891
1214
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
892
1215
|
pathPrefix?: string;
|
|
1216
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
1217
|
+
pathPattern?: string;
|
|
1218
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
1219
|
+
requestHeaders?: Record<string, string>;
|
|
1220
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
1221
|
+
query?: Record<string, string>;
|
|
893
1222
|
/**
|
|
894
1223
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
895
1224
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
896
1225
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
897
1226
|
*/
|
|
898
1227
|
namespace?: string;
|
|
1228
|
+
/** Let this many matching requests through first. */
|
|
1229
|
+
after?: number;
|
|
1230
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
1231
|
+
every?: number;
|
|
1232
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
1233
|
+
count?: number;
|
|
1234
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
1235
|
+
rate?: number;
|
|
1236
|
+
/**
|
|
1237
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
1238
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
1239
|
+
*/
|
|
1240
|
+
from?: Instant | Duration;
|
|
1241
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
1242
|
+
until?: Instant | Duration;
|
|
899
1243
|
/**
|
|
900
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
901
|
-
*
|
|
902
|
-
* the request then still reaches the service.
|
|
1244
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
1245
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
903
1246
|
*/
|
|
904
1247
|
status?: number;
|
|
905
1248
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
906
1249
|
body?: unknown;
|
|
1250
|
+
/** Response headers. */
|
|
907
1251
|
headers?: Record<string, string>;
|
|
908
|
-
/**
|
|
909
|
-
|
|
910
|
-
/**
|
|
911
|
-
rate?: number;
|
|
912
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
1252
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
1253
|
+
delay?: Duration;
|
|
1254
|
+
/** `delay`, in milliseconds. */
|
|
913
1255
|
delayMs?: number;
|
|
914
1256
|
/** Alias of `delayMs`. */
|
|
915
1257
|
latencyMs?: number;
|
|
1258
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
1259
|
+
jitter?: Duration;
|
|
1260
|
+
jitterMs?: number;
|
|
1261
|
+
/**
|
|
1262
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
1263
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
1264
|
+
*/
|
|
1265
|
+
timeout?: Duration;
|
|
1266
|
+
timeoutMs?: number;
|
|
916
1267
|
/**
|
|
917
1268
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
918
1269
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
919
1270
|
*/
|
|
920
1271
|
drop?: boolean;
|
|
1272
|
+
/**
|
|
1273
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
1274
|
+
* the connection dies mid-body.
|
|
1275
|
+
*/
|
|
1276
|
+
truncate?: number;
|
|
921
1277
|
/**
|
|
922
1278
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
923
1279
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -926,8 +1282,8 @@ type FaultRule = {
|
|
|
926
1282
|
effect?: string;
|
|
927
1283
|
/** Parameters for `effect`. */
|
|
928
1284
|
params?: Record<string, unknown>;
|
|
929
|
-
/**
|
|
930
|
-
|
|
1285
|
+
/** The preset this rule was expanded from, if any. */
|
|
1286
|
+
fromPreset?: string;
|
|
931
1287
|
};
|
|
932
1288
|
/** A fault that fired for one request. */
|
|
933
1289
|
type FaultHit = {
|
|
@@ -935,6 +1291,10 @@ type FaultHit = {
|
|
|
935
1291
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
936
1292
|
response?: Response;
|
|
937
1293
|
drop?: boolean;
|
|
1294
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
1295
|
+
timeout?: boolean;
|
|
1296
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
1297
|
+
truncate?: number;
|
|
938
1298
|
effect?: {
|
|
939
1299
|
name: string;
|
|
940
1300
|
params: Record<string, unknown>;
|
|
@@ -946,38 +1306,202 @@ type FaultCandidate = {
|
|
|
946
1306
|
method: string;
|
|
947
1307
|
path: string;
|
|
948
1308
|
namespace: string;
|
|
1309
|
+
/** Request headers and query, for rules that match on them. */
|
|
1310
|
+
headers?: Headers;
|
|
1311
|
+
query?: URLSearchParams;
|
|
1312
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
1313
|
+
now?: number;
|
|
1314
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
1315
|
+
wait?: (ms: number) => Promise<void>;
|
|
1316
|
+
};
|
|
1317
|
+
/** A stored rule and how often it was consulted. */
|
|
1318
|
+
type FaultListing = FaultRule & {
|
|
1319
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
1320
|
+
remaining: number | null;
|
|
1321
|
+
/** Times the rule faulted a request. */
|
|
1322
|
+
hits: number;
|
|
1323
|
+
/** Requests that matched the rule, faulted or not. */
|
|
1324
|
+
seen: number;
|
|
1325
|
+
};
|
|
1326
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
1327
|
+
type FaultSnapshot = {
|
|
1328
|
+
/**
|
|
1329
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
1330
|
+
* in the order means to a request and holds in any process.
|
|
1331
|
+
*/
|
|
1332
|
+
rules: (FaultListing & {
|
|
1333
|
+
position?: number;
|
|
1334
|
+
})[];
|
|
1335
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
1336
|
+
nextId?: number;
|
|
949
1337
|
};
|
|
950
1338
|
type FaultRegistry = {
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
rules: (FaultRule & {
|
|
954
|
-
remaining: number | null;
|
|
955
|
-
hits: number;
|
|
956
|
-
position: number;
|
|
957
|
-
})[];
|
|
958
|
-
rngState: number;
|
|
959
|
-
};
|
|
1339
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
1340
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
960
1341
|
add(rule: FaultRule): FaultRule;
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
})[];
|
|
973
|
-
rngState: number;
|
|
974
|
-
}): void;
|
|
1342
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
1343
|
+
nextId(namespace: string): string;
|
|
1344
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
1345
|
+
list(namespace?: string): FaultListing[];
|
|
1346
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
1347
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
1348
|
+
remove(id: string, namespace?: string): boolean;
|
|
1349
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
1350
|
+
clear(namespace?: string): void;
|
|
1351
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
1352
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
975
1353
|
/**
|
|
976
1354
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
977
1355
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
978
1356
|
* each matching rule's remaining uses.
|
|
979
1357
|
*/
|
|
980
1358
|
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
1359
|
+
/**
|
|
1360
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
1361
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
1362
|
+
*/
|
|
1363
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
1364
|
+
};
|
|
1365
|
+
|
|
1366
|
+
/**
|
|
1367
|
+
* The admin controls every mock shares, as one document.
|
|
1368
|
+
*
|
|
1369
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
1370
|
+
*
|
|
1371
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
1372
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
1373
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
1374
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
1375
|
+
*
|
|
1376
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
1377
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
1378
|
+
*/
|
|
1379
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
1380
|
+
type Duration = number | string;
|
|
1381
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
1382
|
+
type Instant = number | string;
|
|
1383
|
+
type ClockControls = {
|
|
1384
|
+
/** Move the clock to this instant. */
|
|
1385
|
+
now?: Instant;
|
|
1386
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
1387
|
+
frozen?: boolean;
|
|
1388
|
+
/**
|
|
1389
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
1390
|
+
* each request still gets its own, reproducible timestamp.
|
|
1391
|
+
*/
|
|
1392
|
+
autoAdvance?: Duration;
|
|
1393
|
+
};
|
|
1394
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
1395
|
+
type LatencyRuleInput = {
|
|
1396
|
+
delay?: Duration;
|
|
1397
|
+
jitter?: Duration;
|
|
1398
|
+
};
|
|
1399
|
+
type LatencyControls = LatencyRuleInput & {
|
|
1400
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
1401
|
+
operations?: Record<string, Duration | LatencyRuleInput>;
|
|
1402
|
+
};
|
|
1403
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
1404
|
+
type FaultInput = (Omit<FaultRule, "id"> & {
|
|
1405
|
+
id?: string;
|
|
1406
|
+
}) | ({
|
|
1407
|
+
preset: string;
|
|
1408
|
+
} & Partial<FaultRule>);
|
|
1409
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
1410
|
+
declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
1411
|
+
/**
|
|
1412
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
1413
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
1414
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
1415
|
+
*/
|
|
1416
|
+
type SnapshotDocument = {
|
|
1417
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
1418
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
1419
|
+
service?: string;
|
|
1420
|
+
/**
|
|
1421
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
1422
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
1423
|
+
*/
|
|
1424
|
+
records: {
|
|
1425
|
+
collection: string;
|
|
1426
|
+
id: string;
|
|
1427
|
+
seq?: number;
|
|
1428
|
+
value?: unknown;
|
|
1429
|
+
raw?: string;
|
|
1430
|
+
}[];
|
|
1431
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
1432
|
+
sequences?: {
|
|
1433
|
+
name: string;
|
|
1434
|
+
kind: string;
|
|
1435
|
+
value: number;
|
|
1436
|
+
}[];
|
|
1437
|
+
clock?: {
|
|
1438
|
+
now: number;
|
|
1439
|
+
frozen: boolean;
|
|
1440
|
+
autoAdvanceMs?: number;
|
|
1441
|
+
};
|
|
1442
|
+
seed?: number | string;
|
|
1443
|
+
/** Positions of the seeded random streams. */
|
|
1444
|
+
rng?: {
|
|
1445
|
+
state: number;
|
|
1446
|
+
faults?: number;
|
|
1447
|
+
latency?: number;
|
|
1448
|
+
};
|
|
1449
|
+
faults?: unknown;
|
|
1450
|
+
latency?: unknown;
|
|
1451
|
+
virtualTime?: boolean;
|
|
1452
|
+
/** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
|
|
1453
|
+
webhooks?: unknown;
|
|
1454
|
+
};
|
|
1455
|
+
/** Controls for one namespace. */
|
|
1456
|
+
type NamespaceControls = {
|
|
1457
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
1458
|
+
seed?: number | string;
|
|
1459
|
+
clock?: ClockControls;
|
|
1460
|
+
/**
|
|
1461
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
1462
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
1463
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
1464
|
+
*/
|
|
1465
|
+
virtualTime?: boolean;
|
|
1466
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
1467
|
+
latency?: Duration | LatencyControls;
|
|
1468
|
+
faults?: readonly FaultInput[];
|
|
1469
|
+
/** Start from this state instead of the service's own seed data. */
|
|
1470
|
+
snapshot?: SnapshotDocument;
|
|
1471
|
+
};
|
|
1472
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
1473
|
+
type MockControls = NamespaceControls & {
|
|
1474
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
1475
|
+
credentials?: Record<string, string>;
|
|
1476
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
1477
|
+
namespaces?: Record<string, NamespaceControls>;
|
|
1478
|
+
};
|
|
1479
|
+
/** A latency rule in milliseconds. */
|
|
1480
|
+
type LatencyRule = {
|
|
1481
|
+
delayMs: number;
|
|
1482
|
+
jitterMs: number;
|
|
1483
|
+
};
|
|
1484
|
+
/** Baseline latency for a namespace, normalized. */
|
|
1485
|
+
type LatencyState = LatencyRule & {
|
|
1486
|
+
operations: Record<string, LatencyRule>;
|
|
1487
|
+
};
|
|
1488
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
1489
|
+
type ControlHeaderInput = {
|
|
1490
|
+
namespace?: string;
|
|
1491
|
+
adminKey?: string;
|
|
1492
|
+
branch?: string;
|
|
1493
|
+
at?: string;
|
|
1494
|
+
reset?: boolean;
|
|
1495
|
+
restore?: string;
|
|
1496
|
+
seed?: number | string;
|
|
1497
|
+
time?: Instant | Date;
|
|
1498
|
+
advance?: Duration;
|
|
1499
|
+
virtualTime?: boolean;
|
|
1500
|
+
delay?: Duration;
|
|
1501
|
+
timeout?: Duration;
|
|
1502
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
1503
|
+
fault?: number | string | Record<string, unknown>;
|
|
1504
|
+
snapshot?: string;
|
|
981
1505
|
};
|
|
982
1506
|
|
|
983
1507
|
/**
|
|
@@ -1017,6 +1541,8 @@ type RequestLog = {
|
|
|
1017
1541
|
unmatched: boolean;
|
|
1018
1542
|
/** Set when a fault rule produced the response. */
|
|
1019
1543
|
faultId?: string;
|
|
1544
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
1545
|
+
delayMs?: number;
|
|
1020
1546
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
1021
1547
|
ids?: Record<string, string>;
|
|
1022
1548
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -1062,6 +1588,12 @@ type JournalQuery = {
|
|
|
1062
1588
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
1063
1589
|
namespace?: string;
|
|
1064
1590
|
operationId?: string;
|
|
1591
|
+
/** Only this HTTP method, case-insensitive. */
|
|
1592
|
+
method?: string;
|
|
1593
|
+
/** Only this exact path. */
|
|
1594
|
+
path?: string;
|
|
1595
|
+
/** Only requests this fault rule answered or shaped. */
|
|
1596
|
+
faultId?: string;
|
|
1065
1597
|
status?: number;
|
|
1066
1598
|
/** Only entries at or after this instant (epoch ms). */
|
|
1067
1599
|
since?: number;
|
|
@@ -1076,6 +1608,50 @@ type Journal = {
|
|
|
1076
1608
|
clear(namespace?: string): void;
|
|
1077
1609
|
};
|
|
1078
1610
|
|
|
1611
|
+
/**
|
|
1612
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
1613
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
1614
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
1615
|
+
* fault hits.
|
|
1616
|
+
*/
|
|
1617
|
+
type LatencyRegistry = {
|
|
1618
|
+
get(namespace: string): LatencyState;
|
|
1619
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
1620
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
1621
|
+
clear(namespace?: string): void;
|
|
1622
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
1623
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
1624
|
+
};
|
|
1625
|
+
|
|
1626
|
+
/** Clock state as the admin API reports it. */
|
|
1627
|
+
type ControlClock = ClockState & {
|
|
1628
|
+
/** `now`, as ISO-8601. */
|
|
1629
|
+
iso: string;
|
|
1630
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
1631
|
+
autoAdvanceMs: number;
|
|
1632
|
+
};
|
|
1633
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
1634
|
+
type ControlState = {
|
|
1635
|
+
namespace: string;
|
|
1636
|
+
seed: number | string;
|
|
1637
|
+
clock: ControlClock;
|
|
1638
|
+
virtualTime: boolean;
|
|
1639
|
+
latency: LatencyState;
|
|
1640
|
+
faults: FaultListing[];
|
|
1641
|
+
};
|
|
1642
|
+
|
|
1643
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
1644
|
+
type SnapshotInfo = {
|
|
1645
|
+
id: string;
|
|
1646
|
+
namespace: string;
|
|
1647
|
+
/** Stored rows in the snapshot. */
|
|
1648
|
+
records: number;
|
|
1649
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
1650
|
+
at: number;
|
|
1651
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
1652
|
+
portable: boolean;
|
|
1653
|
+
};
|
|
1654
|
+
|
|
1079
1655
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
1080
1656
|
type CredentialRegistry = {
|
|
1081
1657
|
set(credential: string, namespace: string): void;
|
|
@@ -1088,6 +1664,8 @@ type CredentialRegistry = {
|
|
|
1088
1664
|
}[];
|
|
1089
1665
|
};
|
|
1090
1666
|
|
|
1667
|
+
type OwnedTransport = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
|
|
1668
|
+
|
|
1091
1669
|
/**
|
|
1092
1670
|
* A point-in-time copy of everything a service namespace holds.
|
|
1093
1671
|
*
|
|
@@ -1205,8 +1783,16 @@ type PublishInput = {
|
|
|
1205
1783
|
id?: string;
|
|
1206
1784
|
};
|
|
1207
1785
|
type WebhookHub = {
|
|
1786
|
+
observeDeliveries(listener: (event: {
|
|
1787
|
+
namespace: string;
|
|
1788
|
+
state: string;
|
|
1789
|
+
deliveryId: string;
|
|
1790
|
+
}) => void): () => void;
|
|
1791
|
+
setTransportPolicy(policy: OwnedTransport): void;
|
|
1208
1792
|
pending(namespace: string): number;
|
|
1209
1793
|
snapshot(namespace: string): unknown;
|
|
1794
|
+
exportSnapshot(handle: unknown): unknown;
|
|
1795
|
+
importSnapshot(namespace: string, document: unknown): unknown;
|
|
1210
1796
|
restore(namespace: string, snapshot: unknown): void;
|
|
1211
1797
|
publish(input: PublishInput): WebhookMessage;
|
|
1212
1798
|
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
@@ -1219,7 +1805,7 @@ type WebhookHub = {
|
|
|
1219
1805
|
/** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
|
|
1220
1806
|
flush(): Promise<void>;
|
|
1221
1807
|
/** Resolve once nothing is in flight. */
|
|
1222
|
-
idle(): Promise<void>;
|
|
1808
|
+
idle(namespace?: string): Promise<void>;
|
|
1223
1809
|
fault(namespace: string, fault: WebhookFault): void;
|
|
1224
1810
|
clear(namespace?: string): void;
|
|
1225
1811
|
};
|
|
@@ -1230,7 +1816,10 @@ type ServiceInstance = FetchAPI & {
|
|
|
1230
1816
|
};
|
|
1231
1817
|
type ServiceTimelineState = Readonly<{
|
|
1232
1818
|
snapshot: NamespaceSnapshot;
|
|
1233
|
-
clock
|
|
1819
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
1820
|
+
clock: Readonly<ClockState & {
|
|
1821
|
+
pinned?: boolean;
|
|
1822
|
+
}>;
|
|
1234
1823
|
rngState: number;
|
|
1235
1824
|
}>;
|
|
1236
1825
|
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
@@ -1243,27 +1832,63 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
1243
1832
|
readonly sqlite: SqliteClient;
|
|
1244
1833
|
readonly clock: Clock;
|
|
1245
1834
|
readonly faults: FaultRegistry;
|
|
1835
|
+
/** Baseline response time per namespace. */
|
|
1836
|
+
readonly latency: LatencyRegistry;
|
|
1246
1837
|
readonly metrics: Metrics;
|
|
1247
1838
|
readonly journal: Journal;
|
|
1248
1839
|
readonly rng: Rng;
|
|
1249
1840
|
readonly credentials: CredentialRegistry;
|
|
1250
1841
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
1251
1842
|
readonly webhooks: WebhookHub | undefined;
|
|
1252
|
-
/**
|
|
1253
|
-
|
|
1843
|
+
/**
|
|
1844
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
1845
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
1846
|
+
*/
|
|
1254
1847
|
namespaceClock(namespace?: string): Clock;
|
|
1255
1848
|
namespaceOf(request: Request): string;
|
|
1256
1849
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
1257
1850
|
fleetSnapshot(namespace: string): object;
|
|
1258
1851
|
fleetRestore(snapshot: unknown, namespace: string): void;
|
|
1852
|
+
fleetExport(snapshot: unknown): SnapshotDocument;
|
|
1853
|
+
fleetImport(namespace: string, document: unknown): object;
|
|
1259
1854
|
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
1260
1855
|
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
|
|
1261
1856
|
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
1262
1857
|
instance(namespace?: string): T;
|
|
1263
1858
|
/** Public names of every namespace created so far. */
|
|
1264
1859
|
namespaces(): string[];
|
|
1265
|
-
/**
|
|
1860
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
1266
1861
|
reset(namespace?: string): Promise<void>;
|
|
1862
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
1863
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
1864
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
1865
|
+
controls(namespace?: string): ControlState & {
|
|
1866
|
+
snapshots: SnapshotInfo[];
|
|
1867
|
+
};
|
|
1868
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
1869
|
+
configure(controls: NamespaceControls, namespace?: string): ControlState & {
|
|
1870
|
+
snapshots: SnapshotInfo[];
|
|
1871
|
+
};
|
|
1872
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
1873
|
+
readonly snapshots: {
|
|
1874
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
1875
|
+
create(options?: {
|
|
1876
|
+
name?: string;
|
|
1877
|
+
namespace?: string;
|
|
1878
|
+
}): SnapshotInfo;
|
|
1879
|
+
list(namespace?: string): SnapshotInfo[];
|
|
1880
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
1881
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
1882
|
+
/** Store a document as a snapshot without restoring it. */
|
|
1883
|
+
import(document: SnapshotDocument, options?: {
|
|
1884
|
+
name?: string;
|
|
1885
|
+
namespace?: string;
|
|
1886
|
+
}): SnapshotInfo;
|
|
1887
|
+
restore(id: string, namespace?: string): void;
|
|
1888
|
+
delete(id: string, namespace?: string): boolean;
|
|
1889
|
+
};
|
|
1890
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
1891
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
1267
1892
|
snapshot(namespace?: string): NamespaceSnapshot;
|
|
1268
1893
|
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
1269
1894
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -1358,6 +1983,8 @@ type ECSRuntimeOptions = Omit<ECSAPIOptions, "namespace" | "now"> & {
|
|
|
1358
1983
|
seed?: string | number;
|
|
1359
1984
|
adminPrefix?: string;
|
|
1360
1985
|
adminKey?: string;
|
|
1986
|
+
/** Admin controls to boot with: clock, latency, faults, seed, a starting snapshot. */
|
|
1987
|
+
adminControls?: MockControls;
|
|
1361
1988
|
onLog?: (entry: RequestLog) => void;
|
|
1362
1989
|
};
|
|
1363
1990
|
type ECSRuntime = ServiceRuntime<ECSAPI>;
|