@crvouga/mockingbird-service-docker 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/DISCOVERY.md +23 -0
- package/README.md +29 -11
- package/dist/chunk-NSZYARVW.js +5989 -0
- package/dist/chunk-NSZYARVW.js.map +7 -0
- package/dist/{chunk-DMTXJ3WI.js → chunk-Y4JH3Q6Z.js} +3 -2
- package/dist/{chunk-DMTXJ3WI.js.map → chunk-Y4JH3Q6Z.js.map} +2 -2
- package/dist/cli.js +138 -26
- package/dist/cli.js.map +3 -3
- package/dist/index.d.ts +440 -113
- package/dist/index.js +1 -1
- package/dist/server.d.ts +824 -180
- package/dist/server.js +2 -2
- package/package.json +3 -2
- package/dist/chunk-JLDKQ74X.js +0 -4119
- package/dist/chunk-JLDKQ74X.js.map +0 -7
package/dist/index.d.ts
CHANGED
|
@@ -10,76 +10,16 @@ interface FetchAPI$1 {
|
|
|
10
10
|
fetch(request: Request): Promise<Response>;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
type MountedAPI = FetchAPI & {
|
|
14
|
+
readonly prefix: string;
|
|
15
|
+
GET: FetchHandler;
|
|
16
|
+
POST: FetchHandler;
|
|
17
|
+
PUT: FetchHandler;
|
|
18
|
+
PATCH: FetchHandler;
|
|
19
|
+
DELETE: FetchHandler;
|
|
20
|
+
HEAD: FetchHandler;
|
|
21
|
+
OPTIONS: FetchHandler;
|
|
19
22
|
};
|
|
20
|
-
/**
|
|
21
|
-
* Prepared statement bound to a {@link SqliteClient}.
|
|
22
|
-
*
|
|
23
|
-
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
24
|
-
*/
|
|
25
|
-
interface SqliteStatement$1 {
|
|
26
|
-
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
27
|
-
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
28
|
-
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Sync SQLite client port owned by Mockingbird.
|
|
32
|
-
*
|
|
33
|
-
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
34
|
-
* `bun:sqlite` instances all work when they expose this surface.
|
|
35
|
-
*/
|
|
36
|
-
interface SqliteClient$1 {
|
|
37
|
-
exec(sql: string): void;
|
|
38
|
-
prepare(sql: string): SqliteStatement$1;
|
|
39
|
-
transaction<T>(fn: () => T): T;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
43
|
-
type Stored<T> = {
|
|
44
|
-
seq: number;
|
|
45
|
-
value: T;
|
|
46
|
-
};
|
|
47
|
-
type ListRecordsOptions<T> = {
|
|
48
|
-
/** Keep only records passing the predicate. */
|
|
49
|
-
where?: (value: T, seq: number) => boolean;
|
|
50
|
-
/** Sort order; default newest first. */
|
|
51
|
-
order?: "newest" | "oldest";
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
55
|
-
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
56
|
-
*/
|
|
57
|
-
declare class Collection<T> {
|
|
58
|
-
private readonly sqlite;
|
|
59
|
-
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
60
|
-
readonly namespace: string;
|
|
61
|
-
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
62
|
-
readonly collectionName: string;
|
|
63
|
-
constructor(sqlite: SqliteClient$1,
|
|
64
|
-
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
65
|
-
namespace: string,
|
|
66
|
-
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
67
|
-
collectionName: string);
|
|
68
|
-
private bumpCollectionSeq;
|
|
69
|
-
nextSequence(): number;
|
|
70
|
-
get(id: string): T | undefined;
|
|
71
|
-
has(id: string): boolean;
|
|
72
|
-
/** Insert a new record, assigning it the next sequence number. */
|
|
73
|
-
insert(id: string, value: T): Stored<T>;
|
|
74
|
-
/** Replace an existing record's value, keeping its position. */
|
|
75
|
-
update(id: string, value: T): Stored<T> | undefined;
|
|
76
|
-
delete(id: string): boolean;
|
|
77
|
-
/** How many records the collection holds, without reading them. */
|
|
78
|
-
count(): number;
|
|
79
|
-
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
80
|
-
id: string;
|
|
81
|
-
}>;
|
|
82
|
-
}
|
|
83
23
|
|
|
84
24
|
/** A stable identifier for a point in a {@link Timeline}. */
|
|
85
25
|
type CheckpointId = string;
|
|
@@ -170,6 +110,79 @@ declare class Timeline<T> {
|
|
|
170
110
|
interface FetchAPI {
|
|
171
111
|
fetch(request: Request): Promise<Response>;
|
|
172
112
|
}
|
|
113
|
+
/** A bare function form of {@link FetchAPI}, compatible with `Bun.serve`, workerd, and Deno. */
|
|
114
|
+
type FetchHandler = (request: Request) => Promise<Response>;
|
|
115
|
+
|
|
116
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
117
|
+
type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
|
|
118
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
119
|
+
type SqliteRunResult$1 = {
|
|
120
|
+
changes: number;
|
|
121
|
+
lastInsertRowid: number | bigint;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
125
|
+
*
|
|
126
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
127
|
+
*/
|
|
128
|
+
interface SqliteStatement$1 {
|
|
129
|
+
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
130
|
+
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
131
|
+
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
135
|
+
*
|
|
136
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
137
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
138
|
+
*/
|
|
139
|
+
interface SqliteClient$1 {
|
|
140
|
+
exec(sql: string): void;
|
|
141
|
+
prepare(sql: string): SqliteStatement$1;
|
|
142
|
+
transaction<T>(fn: () => T): T;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
146
|
+
type Stored<T> = {
|
|
147
|
+
seq: number;
|
|
148
|
+
value: T;
|
|
149
|
+
};
|
|
150
|
+
type ListRecordsOptions<T> = {
|
|
151
|
+
/** Keep only records passing the predicate. */
|
|
152
|
+
where?: (value: T, seq: number) => boolean;
|
|
153
|
+
/** Sort order; default newest first. */
|
|
154
|
+
order?: "newest" | "oldest";
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
158
|
+
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
159
|
+
*/
|
|
160
|
+
declare class Collection<T> {
|
|
161
|
+
private readonly sqlite;
|
|
162
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
163
|
+
readonly namespace: string;
|
|
164
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
165
|
+
readonly collectionName: string;
|
|
166
|
+
constructor(sqlite: SqliteClient$1,
|
|
167
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
168
|
+
namespace: string,
|
|
169
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
170
|
+
collectionName: string);
|
|
171
|
+
private bumpCollectionSeq;
|
|
172
|
+
nextSequence(): number;
|
|
173
|
+
get(id: string): T | undefined;
|
|
174
|
+
has(id: string): boolean;
|
|
175
|
+
/** Insert a new record, assigning it the next sequence number. */
|
|
176
|
+
insert(id: string, value: T): Stored<T>;
|
|
177
|
+
/** Replace an existing record's value, keeping its position. */
|
|
178
|
+
update(id: string, value: T): Stored<T> | undefined;
|
|
179
|
+
delete(id: string): boolean;
|
|
180
|
+
/** How many records the collection holds, without reading them. */
|
|
181
|
+
count(): number;
|
|
182
|
+
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
183
|
+
id: string;
|
|
184
|
+
}>;
|
|
185
|
+
}
|
|
173
186
|
|
|
174
187
|
/**
|
|
175
188
|
* The slice of OpenAPI 3.1 (and JSON Schema 2020-12) Mockingbird understands.
|
|
@@ -361,7 +374,7 @@ type ClockState = {
|
|
|
361
374
|
now: number;
|
|
362
375
|
/** True while time does not advance on its own. */
|
|
363
376
|
frozen: boolean;
|
|
364
|
-
/** Milliseconds
|
|
377
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
365
378
|
offsetMs: number;
|
|
366
379
|
};
|
|
367
380
|
type Clock = {
|
|
@@ -397,7 +410,10 @@ type Rng = {
|
|
|
397
410
|
state(): number;
|
|
398
411
|
/** Restore a state previously returned by {@link state}. */
|
|
399
412
|
setState(state: number): void;
|
|
400
|
-
seed
|
|
413
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
414
|
+
reseed(seed: number | string): void;
|
|
415
|
+
/** The seed the stream currently starts from. */
|
|
416
|
+
readonly seed: number;
|
|
401
417
|
};
|
|
402
418
|
|
|
403
419
|
/**
|
|
@@ -406,44 +422,83 @@ type Rng = {
|
|
|
406
422
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
407
423
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
408
424
|
* appears under load, the 5xx that proves a retry path works.
|
|
425
|
+
*
|
|
426
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
427
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
428
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
409
429
|
*/
|
|
410
430
|
type FaultRule = {
|
|
411
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
431
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
412
432
|
id: string;
|
|
413
433
|
/** Fault only this operation. Omit to match every operation. */
|
|
414
434
|
operationId?: string;
|
|
415
435
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
416
436
|
method?: string;
|
|
437
|
+
/** Fault only this exact path. */
|
|
438
|
+
path?: string;
|
|
417
439
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
418
440
|
pathPrefix?: string;
|
|
441
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
442
|
+
pathPattern?: string;
|
|
443
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
444
|
+
requestHeaders?: Record<string, string>;
|
|
445
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
446
|
+
query?: Record<string, string>;
|
|
419
447
|
/**
|
|
420
448
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
421
449
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
422
450
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
423
451
|
*/
|
|
424
452
|
namespace?: string;
|
|
453
|
+
/** Let this many matching requests through first. */
|
|
454
|
+
after?: number;
|
|
455
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
456
|
+
every?: number;
|
|
457
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
458
|
+
count?: number;
|
|
459
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
460
|
+
rate?: number;
|
|
461
|
+
/**
|
|
462
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
463
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
464
|
+
*/
|
|
465
|
+
from?: Instant | Duration;
|
|
466
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
467
|
+
until?: Instant | Duration;
|
|
425
468
|
/**
|
|
426
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
427
|
-
*
|
|
428
|
-
* the request then still reaches the service.
|
|
469
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
470
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
429
471
|
*/
|
|
430
472
|
status?: number;
|
|
431
473
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
432
474
|
body?: unknown;
|
|
475
|
+
/** Response headers. */
|
|
433
476
|
headers?: Record<string, string>;
|
|
434
|
-
/**
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
rate?: number;
|
|
438
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
477
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
478
|
+
delay?: Duration;
|
|
479
|
+
/** `delay`, in milliseconds. */
|
|
439
480
|
delayMs?: number;
|
|
440
481
|
/** Alias of `delayMs`. */
|
|
441
482
|
latencyMs?: number;
|
|
483
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
484
|
+
jitter?: Duration;
|
|
485
|
+
jitterMs?: number;
|
|
486
|
+
/**
|
|
487
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
488
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
489
|
+
*/
|
|
490
|
+
timeout?: Duration;
|
|
491
|
+
timeoutMs?: number;
|
|
442
492
|
/**
|
|
443
493
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
444
494
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
445
495
|
*/
|
|
446
496
|
drop?: boolean;
|
|
497
|
+
/**
|
|
498
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
499
|
+
* the connection dies mid-body.
|
|
500
|
+
*/
|
|
501
|
+
truncate?: number;
|
|
447
502
|
/**
|
|
448
503
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
449
504
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -452,8 +507,8 @@ type FaultRule = {
|
|
|
452
507
|
effect?: string;
|
|
453
508
|
/** Parameters for `effect`. */
|
|
454
509
|
params?: Record<string, unknown>;
|
|
455
|
-
/**
|
|
456
|
-
|
|
510
|
+
/** The preset this rule was expanded from, if any. */
|
|
511
|
+
fromPreset?: string;
|
|
457
512
|
};
|
|
458
513
|
/** A fault that fired for one request. */
|
|
459
514
|
type FaultHit = {
|
|
@@ -461,6 +516,10 @@ type FaultHit = {
|
|
|
461
516
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
462
517
|
response?: Response;
|
|
463
518
|
drop?: boolean;
|
|
519
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
520
|
+
timeout?: boolean;
|
|
521
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
522
|
+
truncate?: number;
|
|
464
523
|
effect?: {
|
|
465
524
|
name: string;
|
|
466
525
|
params: Record<string, unknown>;
|
|
@@ -485,38 +544,200 @@ type FaultCandidate = {
|
|
|
485
544
|
method: string;
|
|
486
545
|
path: string;
|
|
487
546
|
namespace: string;
|
|
547
|
+
/** Request headers and query, for rules that match on them. */
|
|
548
|
+
headers?: Headers;
|
|
549
|
+
query?: URLSearchParams;
|
|
550
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
551
|
+
now?: number;
|
|
552
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
553
|
+
wait?: (ms: number) => Promise<void>;
|
|
554
|
+
};
|
|
555
|
+
/** A stored rule and how often it was consulted. */
|
|
556
|
+
type FaultListing = FaultRule & {
|
|
557
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
558
|
+
remaining: number | null;
|
|
559
|
+
/** Times the rule faulted a request. */
|
|
560
|
+
hits: number;
|
|
561
|
+
/** Requests that matched the rule, faulted or not. */
|
|
562
|
+
seen: number;
|
|
563
|
+
};
|
|
564
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
565
|
+
type FaultSnapshot = {
|
|
566
|
+
/**
|
|
567
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
568
|
+
* in the order means to a request and holds in any process.
|
|
569
|
+
*/
|
|
570
|
+
rules: (FaultListing & {
|
|
571
|
+
position?: number;
|
|
572
|
+
})[];
|
|
573
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
574
|
+
nextId?: number;
|
|
488
575
|
};
|
|
489
576
|
type FaultRegistry = {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
rules: (FaultRule & {
|
|
493
|
-
remaining: number | null;
|
|
494
|
-
hits: number;
|
|
495
|
-
position: number;
|
|
496
|
-
})[];
|
|
497
|
-
rngState: number;
|
|
498
|
-
};
|
|
577
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
578
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
499
579
|
add(rule: FaultRule): FaultRule;
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
})[];
|
|
512
|
-
rngState: number;
|
|
513
|
-
}): void;
|
|
580
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
581
|
+
nextId(namespace: string): string;
|
|
582
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
583
|
+
list(namespace?: string): FaultListing[];
|
|
584
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
585
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
586
|
+
remove(id: string, namespace?: string): boolean;
|
|
587
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
588
|
+
clear(namespace?: string): void;
|
|
589
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
590
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
514
591
|
/**
|
|
515
592
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
516
593
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
517
594
|
* each matching rule's remaining uses.
|
|
518
595
|
*/
|
|
519
596
|
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
597
|
+
/**
|
|
598
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
599
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
600
|
+
*/
|
|
601
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* The admin controls every mock shares, as one document.
|
|
606
|
+
*
|
|
607
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
608
|
+
*
|
|
609
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
610
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
611
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
612
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
613
|
+
*
|
|
614
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
615
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
616
|
+
*/
|
|
617
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
618
|
+
type Duration = number | string;
|
|
619
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
620
|
+
type Instant = number | string;
|
|
621
|
+
type ClockControls = {
|
|
622
|
+
/** Move the clock to this instant. */
|
|
623
|
+
now?: Instant;
|
|
624
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
625
|
+
frozen?: boolean;
|
|
626
|
+
/**
|
|
627
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
628
|
+
* each request still gets its own, reproducible timestamp.
|
|
629
|
+
*/
|
|
630
|
+
autoAdvance?: Duration;
|
|
631
|
+
};
|
|
632
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
633
|
+
type LatencyRuleInput = {
|
|
634
|
+
delay?: Duration;
|
|
635
|
+
jitter?: Duration;
|
|
636
|
+
};
|
|
637
|
+
type LatencyControls = LatencyRuleInput & {
|
|
638
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
639
|
+
operations?: Record<string, Duration | LatencyRuleInput>;
|
|
640
|
+
};
|
|
641
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
642
|
+
type FaultInput = (Omit<FaultRule, "id"> & {
|
|
643
|
+
id?: string;
|
|
644
|
+
}) | ({
|
|
645
|
+
preset: string;
|
|
646
|
+
} & Partial<FaultRule>);
|
|
647
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
648
|
+
declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
649
|
+
/**
|
|
650
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
651
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
652
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
653
|
+
*/
|
|
654
|
+
type SnapshotDocument = {
|
|
655
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
656
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
657
|
+
service?: string;
|
|
658
|
+
/**
|
|
659
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
660
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
661
|
+
*/
|
|
662
|
+
records: {
|
|
663
|
+
collection: string;
|
|
664
|
+
id: string;
|
|
665
|
+
seq?: number;
|
|
666
|
+
value?: unknown;
|
|
667
|
+
raw?: string;
|
|
668
|
+
}[];
|
|
669
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
670
|
+
sequences?: {
|
|
671
|
+
name: string;
|
|
672
|
+
kind: string;
|
|
673
|
+
value: number;
|
|
674
|
+
}[];
|
|
675
|
+
clock?: {
|
|
676
|
+
now: number;
|
|
677
|
+
frozen: boolean;
|
|
678
|
+
autoAdvanceMs?: number;
|
|
679
|
+
};
|
|
680
|
+
seed?: number | string;
|
|
681
|
+
/** Positions of the seeded random streams. */
|
|
682
|
+
rng?: {
|
|
683
|
+
state: number;
|
|
684
|
+
faults?: number;
|
|
685
|
+
latency?: number;
|
|
686
|
+
};
|
|
687
|
+
faults?: unknown;
|
|
688
|
+
latency?: unknown;
|
|
689
|
+
virtualTime?: boolean;
|
|
690
|
+
};
|
|
691
|
+
/** Controls for one namespace. */
|
|
692
|
+
type NamespaceControls = {
|
|
693
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
694
|
+
seed?: number | string;
|
|
695
|
+
clock?: ClockControls;
|
|
696
|
+
/**
|
|
697
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
698
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
699
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
700
|
+
*/
|
|
701
|
+
virtualTime?: boolean;
|
|
702
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
703
|
+
latency?: Duration | LatencyControls;
|
|
704
|
+
faults?: readonly FaultInput[];
|
|
705
|
+
/** Start from this state instead of the service's own seed data. */
|
|
706
|
+
snapshot?: SnapshotDocument;
|
|
707
|
+
};
|
|
708
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
709
|
+
type MockControls = NamespaceControls & {
|
|
710
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
711
|
+
credentials?: Record<string, string>;
|
|
712
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
713
|
+
namespaces?: Record<string, NamespaceControls>;
|
|
714
|
+
};
|
|
715
|
+
/** A latency rule in milliseconds. */
|
|
716
|
+
type LatencyRule = {
|
|
717
|
+
delayMs: number;
|
|
718
|
+
jitterMs: number;
|
|
719
|
+
};
|
|
720
|
+
/** Baseline latency for a namespace, normalized. */
|
|
721
|
+
type LatencyState = LatencyRule & {
|
|
722
|
+
operations: Record<string, LatencyRule>;
|
|
723
|
+
};
|
|
724
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
725
|
+
type ControlHeaderInput = {
|
|
726
|
+
namespace?: string;
|
|
727
|
+
adminKey?: string;
|
|
728
|
+
branch?: string;
|
|
729
|
+
at?: string;
|
|
730
|
+
reset?: boolean;
|
|
731
|
+
restore?: string;
|
|
732
|
+
seed?: number | string;
|
|
733
|
+
time?: Instant | Date;
|
|
734
|
+
advance?: Duration;
|
|
735
|
+
virtualTime?: boolean;
|
|
736
|
+
delay?: Duration;
|
|
737
|
+
timeout?: Duration;
|
|
738
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
739
|
+
fault?: number | string | Record<string, unknown>;
|
|
740
|
+
snapshot?: string;
|
|
520
741
|
};
|
|
521
742
|
|
|
522
743
|
/**
|
|
@@ -556,6 +777,8 @@ type RequestLog = {
|
|
|
556
777
|
unmatched: boolean;
|
|
557
778
|
/** Set when a fault rule produced the response. */
|
|
558
779
|
faultId?: string;
|
|
780
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
781
|
+
delayMs?: number;
|
|
559
782
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
560
783
|
ids?: Record<string, string>;
|
|
561
784
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -601,6 +824,12 @@ type JournalQuery = {
|
|
|
601
824
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
602
825
|
namespace?: string;
|
|
603
826
|
operationId?: string;
|
|
827
|
+
/** Only this HTTP method, case-insensitive. */
|
|
828
|
+
method?: string;
|
|
829
|
+
/** Only this exact path. */
|
|
830
|
+
path?: string;
|
|
831
|
+
/** Only requests this fault rule answered or shaped. */
|
|
832
|
+
faultId?: string;
|
|
604
833
|
status?: number;
|
|
605
834
|
/** Only entries at or after this instant (epoch ms). */
|
|
606
835
|
since?: number;
|
|
@@ -615,6 +844,38 @@ type Journal = {
|
|
|
615
844
|
clear(namespace?: string): void;
|
|
616
845
|
};
|
|
617
846
|
|
|
847
|
+
/**
|
|
848
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
849
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
850
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
851
|
+
* fault hits.
|
|
852
|
+
*/
|
|
853
|
+
type LatencyRegistry = {
|
|
854
|
+
get(namespace: string): LatencyState;
|
|
855
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
856
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
857
|
+
clear(namespace?: string): void;
|
|
858
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
859
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
/** Clock state as the admin API reports it. */
|
|
863
|
+
type ControlClock = ClockState & {
|
|
864
|
+
/** `now`, as ISO-8601. */
|
|
865
|
+
iso: string;
|
|
866
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
867
|
+
autoAdvanceMs: number;
|
|
868
|
+
};
|
|
869
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
870
|
+
type ControlState = {
|
|
871
|
+
namespace: string;
|
|
872
|
+
seed: number | string;
|
|
873
|
+
clock: ControlClock;
|
|
874
|
+
virtualTime: boolean;
|
|
875
|
+
latency: LatencyState;
|
|
876
|
+
faults: FaultListing[];
|
|
877
|
+
};
|
|
878
|
+
|
|
618
879
|
type AdminRequest = {
|
|
619
880
|
request: Request;
|
|
620
881
|
url: URL;
|
|
@@ -631,6 +892,17 @@ type AdminRoute = (request: AdminRequest) => Response | Promise<Response>;
|
|
|
631
892
|
* `:param` segments — e.g. `"POST /orders/:id/transition"`.
|
|
632
893
|
*/
|
|
633
894
|
type AdminRoutes = Record<string, AdminRoute>;
|
|
895
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
896
|
+
type SnapshotInfo = {
|
|
897
|
+
id: string;
|
|
898
|
+
namespace: string;
|
|
899
|
+
/** Stored rows in the snapshot. */
|
|
900
|
+
records: number;
|
|
901
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
902
|
+
at: number;
|
|
903
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
904
|
+
portable: boolean;
|
|
905
|
+
};
|
|
634
906
|
|
|
635
907
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
636
908
|
type CredentialRegistry = {
|
|
@@ -875,14 +1147,26 @@ type RuntimeOptions<T extends ServiceInstance> = {
|
|
|
875
1147
|
sqlite?: SqliteClient$1;
|
|
876
1148
|
/** Defaults to a live clock over `Date.now`. */
|
|
877
1149
|
clock?: Clock;
|
|
878
|
-
/** Seeds every random choice the runtime makes (fault rates). Default `0`. */
|
|
1150
|
+
/** Seeds every random choice the runtime makes (fault rates, jitter). Default `0`. */
|
|
879
1151
|
seed?: number | string;
|
|
1152
|
+
/**
|
|
1153
|
+
* Admin controls to boot with: clock, latency, faults, a starting snapshot, credential
|
|
1154
|
+
* mappings and per-namespace overrides. `reset` returns a namespace to exactly this.
|
|
1155
|
+
*/
|
|
1156
|
+
adminControls?: MockControls;
|
|
880
1157
|
/** Extra `GET /__admin/health` fields, such as the loaded corpus version. */
|
|
881
1158
|
describe?: () => Record<string, unknown>;
|
|
882
1159
|
/** Service-specific admin routes, given the runtime so they can reach any namespace. */
|
|
883
1160
|
admin?: (runtime: ServiceRuntime<T>) => AdminRoutes;
|
|
884
1161
|
/** Reserved tree for every internal HTTP endpoint, UI and namespace carrier. Default /__admin. */
|
|
885
1162
|
adminPrefix?: string;
|
|
1163
|
+
/**
|
|
1164
|
+
* A namespace's clock was moved — by `/__admin/clock`, a header, a restore, a reset, or a
|
|
1165
|
+
* wait spent in virtual time. Run whatever the service schedules by that clock, so what
|
|
1166
|
+
* came due is seen at once and not on the next request. Moving the base clock in process
|
|
1167
|
+
* (`runtime.clock`) is the caller's own doing and is not reported here.
|
|
1168
|
+
*/
|
|
1169
|
+
onClockChange?: (namespace: string) => void;
|
|
886
1170
|
/** Invalidate transient handles before replacing an existing instance's stored state. */
|
|
887
1171
|
beforeRestore?: (instance: T) => void;
|
|
888
1172
|
/** Require this value in `x-mockingbird-admin-key` on admin data routes. */
|
|
@@ -900,7 +1184,10 @@ type RuntimeOptions<T extends ServiceInstance> = {
|
|
|
900
1184
|
* `sigV4AccessKeyId`.
|
|
901
1185
|
*/
|
|
902
1186
|
credential?: (request: Request) => string | undefined;
|
|
903
|
-
/**
|
|
1187
|
+
/**
|
|
1188
|
+
* Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. They join the
|
|
1189
|
+
* standard presets every mock has; one with a standard name replaces it.
|
|
1190
|
+
*/
|
|
904
1191
|
presets?: Record<string, FaultPreset>;
|
|
905
1192
|
/** Outbound webhooks; adds the `/__admin/webhooks*` routes and clears on reset. */
|
|
906
1193
|
webhooks?: WebhookHub;
|
|
@@ -923,11 +1210,15 @@ type RuntimeOptions<T extends ServiceInstance> = {
|
|
|
923
1210
|
type RuntimeIO = {
|
|
924
1211
|
wallNow(): number;
|
|
925
1212
|
monotonicNow(): number;
|
|
926
|
-
|
|
1213
|
+
/** Wait on the wall clock. Rejects with the signal's reason when it aborts first. */
|
|
1214
|
+
sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
927
1215
|
};
|
|
928
1216
|
type ServiceTimelineState = Readonly<{
|
|
929
1217
|
snapshot: NamespaceSnapshot;
|
|
930
|
-
clock
|
|
1218
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
1219
|
+
clock: Readonly<ClockState & {
|
|
1220
|
+
pinned?: boolean;
|
|
1221
|
+
}>;
|
|
931
1222
|
rngState: number;
|
|
932
1223
|
}>;
|
|
933
1224
|
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
@@ -935,17 +1226,23 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
935
1226
|
/** Stop package-owned lifecycle timers when a supervisor closes the runtime. */
|
|
936
1227
|
stop?(): void;
|
|
937
1228
|
readonly name: string;
|
|
1229
|
+
/** Same-origin Fetch handler and App Router method exports, sharing this runtime. */
|
|
1230
|
+
mount(prefix: string): MountedAPI;
|
|
938
1231
|
readonly sqlite: SqliteClient$1;
|
|
939
1232
|
readonly clock: Clock;
|
|
940
1233
|
readonly faults: FaultRegistry;
|
|
1234
|
+
/** Baseline response time per namespace. */
|
|
1235
|
+
readonly latency: LatencyRegistry;
|
|
941
1236
|
readonly metrics: Metrics;
|
|
942
1237
|
readonly journal: Journal;
|
|
943
1238
|
readonly rng: Rng;
|
|
944
1239
|
readonly credentials: CredentialRegistry;
|
|
945
1240
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
946
1241
|
readonly webhooks: WebhookHub | undefined;
|
|
947
|
-
/**
|
|
948
|
-
|
|
1242
|
+
/**
|
|
1243
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
1244
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
1245
|
+
*/
|
|
949
1246
|
namespaceClock(namespace?: string): Clock;
|
|
950
1247
|
namespaceOf(request: Request): string;
|
|
951
1248
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
@@ -957,8 +1254,38 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
957
1254
|
instance(namespace?: string): T;
|
|
958
1255
|
/** Public names of every namespace created so far. */
|
|
959
1256
|
namespaces(): string[];
|
|
960
|
-
/**
|
|
1257
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
961
1258
|
reset(namespace?: string): Promise<void>;
|
|
1259
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
1260
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
1261
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
1262
|
+
controls(namespace?: string): ControlState & {
|
|
1263
|
+
snapshots: SnapshotInfo[];
|
|
1264
|
+
};
|
|
1265
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
1266
|
+
configure(controls: NamespaceControls, namespace?: string): ControlState & {
|
|
1267
|
+
snapshots: SnapshotInfo[];
|
|
1268
|
+
};
|
|
1269
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
1270
|
+
readonly snapshots: {
|
|
1271
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
1272
|
+
create(options?: {
|
|
1273
|
+
name?: string;
|
|
1274
|
+
namespace?: string;
|
|
1275
|
+
}): SnapshotInfo;
|
|
1276
|
+
list(namespace?: string): SnapshotInfo[];
|
|
1277
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
1278
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
1279
|
+
/** Store a document as a snapshot without restoring it. */
|
|
1280
|
+
import(document: SnapshotDocument, options?: {
|
|
1281
|
+
name?: string;
|
|
1282
|
+
namespace?: string;
|
|
1283
|
+
}): SnapshotInfo;
|
|
1284
|
+
restore(id: string, namespace?: string): void;
|
|
1285
|
+
delete(id: string, namespace?: string): boolean;
|
|
1286
|
+
};
|
|
1287
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
1288
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
962
1289
|
snapshot(namespace?: string): NamespaceSnapshot;
|
|
963
1290
|
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
964
1291
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -1271,7 +1598,7 @@ type SupportedOperationId = "ContainerList" | "ContainerCreate" | "ContainerInsp
|
|
|
1271
1598
|
declare const operationIds: readonly ["ContainerList", "ContainerCreate", "ContainerInspect", "ContainerStart", "ContainerStop", "ContainerKill", "ContainerAttach", "ContainerWait", "ContainerDelete", "SystemInfo", "SystemVersion", "SystemPing", "SystemPingHead"];
|
|
1272
1599
|
declare const supportedOperationIds: readonly ["ContainerList", "ContainerCreate", "ContainerInspect", "ContainerStart", "ContainerStop", "ContainerKill", "ContainerWait", "ContainerDelete", "SystemInfo", "SystemVersion", "SystemPing", "SystemPingHead"];
|
|
1273
1600
|
|
|
1274
|
-
type DockerRuntimeOptions = Pick<RuntimeOptions<DockerAPI>, "sqlite" | "clock" | "seed" | "adminKey" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<DockerAPIOptions, "onAttach">;
|
|
1601
|
+
type DockerRuntimeOptions = Pick<RuntimeOptions<DockerAPI>, "sqlite" | "clock" | "seed" | "adminKey" | "adminControls" | "onLog" | "journalSize" | "maxCheckpoints"> & Pick<DockerAPIOptions, "onAttach">;
|
|
1275
1602
|
type DockerRuntime = ServiceRuntime<DockerAPI> & {
|
|
1276
1603
|
close(): void;
|
|
1277
1604
|
};
|