@crvouga/mockingbird-service-dynamodb-streams 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 +14 -2
- package/dist/{chunk-RKA7VVT3.js → chunk-CRCLRQBG.js} +139 -26
- package/dist/chunk-CRCLRQBG.js.map +7 -0
- package/dist/chunk-QOHRUYUL.js +9284 -0
- package/dist/chunk-QOHRUYUL.js.map +7 -0
- package/dist/cli.js +2 -2
- package/dist/index.d.ts +435 -121
- package/dist/index.js +1 -1
- package/dist/server.d.ts +819 -188
- package/dist/server.js +2 -2
- package/package.json +3 -2
- package/dist/chunk-RKA7VVT3.js.map +0 -7
- package/dist/chunk-YNA6KCWX.js +0 -7404
- package/dist/chunk-YNA6KCWX.js.map +0 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,85 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
type MountedAPI = FetchAPI & {
|
|
2
|
+
readonly prefix: string;
|
|
3
|
+
GET: FetchHandler;
|
|
4
|
+
POST: FetchHandler;
|
|
5
|
+
PUT: FetchHandler;
|
|
6
|
+
PATCH: FetchHandler;
|
|
7
|
+
DELETE: FetchHandler;
|
|
8
|
+
HEAD: FetchHandler;
|
|
9
|
+
OPTIONS: FetchHandler;
|
|
7
10
|
};
|
|
8
|
-
/**
|
|
9
|
-
* Prepared statement bound to a {@link SqliteClient}.
|
|
10
|
-
*
|
|
11
|
-
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
12
|
-
*/
|
|
13
|
-
interface SqliteStatement$1 {
|
|
14
|
-
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
15
|
-
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
16
|
-
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Sync SQLite client port owned by Mockingbird.
|
|
20
|
-
*
|
|
21
|
-
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
22
|
-
* `bun:sqlite` instances all work when they expose this surface.
|
|
23
|
-
*/
|
|
24
|
-
interface SqliteClient$1 {
|
|
25
|
-
exec(sql: string): void;
|
|
26
|
-
prepare(sql: string): SqliteStatement$1;
|
|
27
|
-
transaction<T>(fn: () => T): T;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
31
|
-
type Stored<T> = {
|
|
32
|
-
seq: number;
|
|
33
|
-
value: T;
|
|
34
|
-
};
|
|
35
|
-
type ListRecordsOptions<T> = {
|
|
36
|
-
/** Keep only records passing the predicate. */
|
|
37
|
-
where?: (value: T, seq: number) => boolean;
|
|
38
|
-
/** Sort order; default newest first. */
|
|
39
|
-
order?: "newest" | "oldest";
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
43
|
-
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
44
|
-
*/
|
|
45
|
-
declare class Collection<T> {
|
|
46
|
-
private readonly sqlite;
|
|
47
|
-
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
48
|
-
readonly namespace: string;
|
|
49
|
-
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
50
|
-
readonly collectionName: string;
|
|
51
|
-
constructor(sqlite: SqliteClient$1,
|
|
52
|
-
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
53
|
-
namespace: string,
|
|
54
|
-
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
55
|
-
collectionName: string);
|
|
56
|
-
private bumpCollectionSeq;
|
|
57
|
-
nextSequence(): number;
|
|
58
|
-
get(id: string): T | undefined;
|
|
59
|
-
has(id: string): boolean;
|
|
60
|
-
/** Insert a new record, assigning it the next sequence number. */
|
|
61
|
-
insert(id: string, value: T): Stored<T>;
|
|
62
|
-
/** Replace an existing record's value, keeping its position. */
|
|
63
|
-
update(id: string, value: T): Stored<T> | undefined;
|
|
64
|
-
delete(id: string): boolean;
|
|
65
|
-
/** How many records the collection holds, without reading them. */
|
|
66
|
-
count(): number;
|
|
67
|
-
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
68
|
-
id: string;
|
|
69
|
-
}>;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
74
|
-
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
75
|
-
*/
|
|
76
|
-
declare class IdSequence {
|
|
77
|
-
private readonly sqlite;
|
|
78
|
-
private readonly namespace;
|
|
79
|
-
private readonly salt;
|
|
80
|
-
constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
|
|
81
|
-
next(prefix: string, length?: number): string;
|
|
82
|
-
}
|
|
83
11
|
|
|
84
12
|
/** A stable identifier for a point in a {@link Timeline}. */
|
|
85
13
|
type CheckpointId = string;
|
|
@@ -170,6 +98,91 @@ declare class Timeline<T> {
|
|
|
170
98
|
interface FetchAPI {
|
|
171
99
|
fetch(request: Request): Promise<Response>;
|
|
172
100
|
}
|
|
101
|
+
/** A bare function form of {@link FetchAPI}, compatible with `Bun.serve`, workerd, and Deno. */
|
|
102
|
+
type FetchHandler = (request: Request) => Promise<Response>;
|
|
103
|
+
|
|
104
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
105
|
+
type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
|
|
106
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
107
|
+
type SqliteRunResult$1 = {
|
|
108
|
+
changes: number;
|
|
109
|
+
lastInsertRowid: number | bigint;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
113
|
+
*
|
|
114
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
115
|
+
*/
|
|
116
|
+
interface SqliteStatement$1 {
|
|
117
|
+
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
118
|
+
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
119
|
+
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
123
|
+
*
|
|
124
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
125
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
126
|
+
*/
|
|
127
|
+
interface SqliteClient$1 {
|
|
128
|
+
exec(sql: string): void;
|
|
129
|
+
prepare(sql: string): SqliteStatement$1;
|
|
130
|
+
transaction<T>(fn: () => T): T;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
134
|
+
type Stored<T> = {
|
|
135
|
+
seq: number;
|
|
136
|
+
value: T;
|
|
137
|
+
};
|
|
138
|
+
type ListRecordsOptions<T> = {
|
|
139
|
+
/** Keep only records passing the predicate. */
|
|
140
|
+
where?: (value: T, seq: number) => boolean;
|
|
141
|
+
/** Sort order; default newest first. */
|
|
142
|
+
order?: "newest" | "oldest";
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
146
|
+
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
147
|
+
*/
|
|
148
|
+
declare class Collection<T> {
|
|
149
|
+
private readonly sqlite;
|
|
150
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
151
|
+
readonly namespace: string;
|
|
152
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
153
|
+
readonly collectionName: string;
|
|
154
|
+
constructor(sqlite: SqliteClient$1,
|
|
155
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
156
|
+
namespace: string,
|
|
157
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
158
|
+
collectionName: string);
|
|
159
|
+
private bumpCollectionSeq;
|
|
160
|
+
nextSequence(): number;
|
|
161
|
+
get(id: string): T | undefined;
|
|
162
|
+
has(id: string): boolean;
|
|
163
|
+
/** Insert a new record, assigning it the next sequence number. */
|
|
164
|
+
insert(id: string, value: T): Stored<T>;
|
|
165
|
+
/** Replace an existing record's value, keeping its position. */
|
|
166
|
+
update(id: string, value: T): Stored<T> | undefined;
|
|
167
|
+
delete(id: string): boolean;
|
|
168
|
+
/** How many records the collection holds, without reading them. */
|
|
169
|
+
count(): number;
|
|
170
|
+
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
171
|
+
id: string;
|
|
172
|
+
}>;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
177
|
+
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
178
|
+
*/
|
|
179
|
+
declare class IdSequence {
|
|
180
|
+
private readonly sqlite;
|
|
181
|
+
private readonly namespace;
|
|
182
|
+
private readonly salt;
|
|
183
|
+
constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
|
|
184
|
+
next(prefix: string, length?: number): string;
|
|
185
|
+
}
|
|
173
186
|
|
|
174
187
|
/** Options every provider constructor accepts. */
|
|
175
188
|
type APIOptions$1 = {
|
|
@@ -231,7 +244,7 @@ type ClockState = {
|
|
|
231
244
|
now: number;
|
|
232
245
|
/** True while time does not advance on its own. */
|
|
233
246
|
frozen: boolean;
|
|
234
|
-
/** Milliseconds
|
|
247
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
235
248
|
offsetMs: number;
|
|
236
249
|
};
|
|
237
250
|
type Clock = {
|
|
@@ -267,7 +280,10 @@ type Rng = {
|
|
|
267
280
|
state(): number;
|
|
268
281
|
/** Restore a state previously returned by {@link state}. */
|
|
269
282
|
setState(state: number): void;
|
|
270
|
-
seed
|
|
283
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
284
|
+
reseed(seed: number | string): void;
|
|
285
|
+
/** The seed the stream currently starts from. */
|
|
286
|
+
readonly seed: number;
|
|
271
287
|
};
|
|
272
288
|
|
|
273
289
|
/**
|
|
@@ -276,44 +292,83 @@ type Rng = {
|
|
|
276
292
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
277
293
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
278
294
|
* appears under load, the 5xx that proves a retry path works.
|
|
295
|
+
*
|
|
296
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
297
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
298
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
279
299
|
*/
|
|
280
300
|
type FaultRule = {
|
|
281
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
301
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
282
302
|
id: string;
|
|
283
303
|
/** Fault only this operation. Omit to match every operation. */
|
|
284
304
|
operationId?: string;
|
|
285
305
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
286
306
|
method?: string;
|
|
307
|
+
/** Fault only this exact path. */
|
|
308
|
+
path?: string;
|
|
287
309
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
288
310
|
pathPrefix?: string;
|
|
311
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
312
|
+
pathPattern?: string;
|
|
313
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
314
|
+
requestHeaders?: Record<string, string>;
|
|
315
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
316
|
+
query?: Record<string, string>;
|
|
289
317
|
/**
|
|
290
318
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
291
319
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
292
320
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
293
321
|
*/
|
|
294
322
|
namespace?: string;
|
|
323
|
+
/** Let this many matching requests through first. */
|
|
324
|
+
after?: number;
|
|
325
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
326
|
+
every?: number;
|
|
327
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
328
|
+
count?: number;
|
|
329
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
330
|
+
rate?: number;
|
|
331
|
+
/**
|
|
332
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
333
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
334
|
+
*/
|
|
335
|
+
from?: Instant | Duration;
|
|
336
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
337
|
+
until?: Instant | Duration;
|
|
295
338
|
/**
|
|
296
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
297
|
-
*
|
|
298
|
-
* the request then still reaches the service.
|
|
339
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
340
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
299
341
|
*/
|
|
300
342
|
status?: number;
|
|
301
343
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
302
344
|
body?: unknown;
|
|
345
|
+
/** Response headers. */
|
|
303
346
|
headers?: Record<string, string>;
|
|
304
|
-
/**
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
rate?: number;
|
|
308
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
347
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
348
|
+
delay?: Duration;
|
|
349
|
+
/** `delay`, in milliseconds. */
|
|
309
350
|
delayMs?: number;
|
|
310
351
|
/** Alias of `delayMs`. */
|
|
311
352
|
latencyMs?: number;
|
|
353
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
354
|
+
jitter?: Duration;
|
|
355
|
+
jitterMs?: number;
|
|
356
|
+
/**
|
|
357
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
358
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
359
|
+
*/
|
|
360
|
+
timeout?: Duration;
|
|
361
|
+
timeoutMs?: number;
|
|
312
362
|
/**
|
|
313
363
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
314
364
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
315
365
|
*/
|
|
316
366
|
drop?: boolean;
|
|
367
|
+
/**
|
|
368
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
369
|
+
* the connection dies mid-body.
|
|
370
|
+
*/
|
|
371
|
+
truncate?: number;
|
|
317
372
|
/**
|
|
318
373
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
319
374
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -322,8 +377,8 @@ type FaultRule = {
|
|
|
322
377
|
effect?: string;
|
|
323
378
|
/** Parameters for `effect`. */
|
|
324
379
|
params?: Record<string, unknown>;
|
|
325
|
-
/**
|
|
326
|
-
|
|
380
|
+
/** The preset this rule was expanded from, if any. */
|
|
381
|
+
fromPreset?: string;
|
|
327
382
|
};
|
|
328
383
|
/** A fault that fired for one request. */
|
|
329
384
|
type FaultHit = {
|
|
@@ -331,6 +386,10 @@ type FaultHit = {
|
|
|
331
386
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
332
387
|
response?: Response;
|
|
333
388
|
drop?: boolean;
|
|
389
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
390
|
+
timeout?: boolean;
|
|
391
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
392
|
+
truncate?: number;
|
|
334
393
|
effect?: {
|
|
335
394
|
name: string;
|
|
336
395
|
params: Record<string, unknown>;
|
|
@@ -342,38 +401,200 @@ type FaultCandidate = {
|
|
|
342
401
|
method: string;
|
|
343
402
|
path: string;
|
|
344
403
|
namespace: string;
|
|
404
|
+
/** Request headers and query, for rules that match on them. */
|
|
405
|
+
headers?: Headers;
|
|
406
|
+
query?: URLSearchParams;
|
|
407
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
408
|
+
now?: number;
|
|
409
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
410
|
+
wait?: (ms: number) => Promise<void>;
|
|
411
|
+
};
|
|
412
|
+
/** A stored rule and how often it was consulted. */
|
|
413
|
+
type FaultListing = FaultRule & {
|
|
414
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
415
|
+
remaining: number | null;
|
|
416
|
+
/** Times the rule faulted a request. */
|
|
417
|
+
hits: number;
|
|
418
|
+
/** Requests that matched the rule, faulted or not. */
|
|
419
|
+
seen: number;
|
|
420
|
+
};
|
|
421
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
422
|
+
type FaultSnapshot = {
|
|
423
|
+
/**
|
|
424
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
425
|
+
* in the order means to a request and holds in any process.
|
|
426
|
+
*/
|
|
427
|
+
rules: (FaultListing & {
|
|
428
|
+
position?: number;
|
|
429
|
+
})[];
|
|
430
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
431
|
+
nextId?: number;
|
|
345
432
|
};
|
|
346
433
|
type FaultRegistry = {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
rules: (FaultRule & {
|
|
350
|
-
remaining: number | null;
|
|
351
|
-
hits: number;
|
|
352
|
-
position: number;
|
|
353
|
-
})[];
|
|
354
|
-
rngState: number;
|
|
355
|
-
};
|
|
434
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
435
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
356
436
|
add(rule: FaultRule): FaultRule;
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
})[];
|
|
369
|
-
rngState: number;
|
|
370
|
-
}): void;
|
|
437
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
438
|
+
nextId(namespace: string): string;
|
|
439
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
440
|
+
list(namespace?: string): FaultListing[];
|
|
441
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
442
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
443
|
+
remove(id: string, namespace?: string): boolean;
|
|
444
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
445
|
+
clear(namespace?: string): void;
|
|
446
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
447
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
371
448
|
/**
|
|
372
449
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
373
450
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
374
451
|
* each matching rule's remaining uses.
|
|
375
452
|
*/
|
|
376
453
|
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
454
|
+
/**
|
|
455
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
456
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
457
|
+
*/
|
|
458
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* The admin controls every mock shares, as one document.
|
|
463
|
+
*
|
|
464
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
465
|
+
*
|
|
466
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
467
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
468
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
469
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
470
|
+
*
|
|
471
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
472
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
473
|
+
*/
|
|
474
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
475
|
+
type Duration = number | string;
|
|
476
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
477
|
+
type Instant = number | string;
|
|
478
|
+
type ClockControls = {
|
|
479
|
+
/** Move the clock to this instant. */
|
|
480
|
+
now?: Instant;
|
|
481
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
482
|
+
frozen?: boolean;
|
|
483
|
+
/**
|
|
484
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
485
|
+
* each request still gets its own, reproducible timestamp.
|
|
486
|
+
*/
|
|
487
|
+
autoAdvance?: Duration;
|
|
488
|
+
};
|
|
489
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
490
|
+
type LatencyRuleInput = {
|
|
491
|
+
delay?: Duration;
|
|
492
|
+
jitter?: Duration;
|
|
493
|
+
};
|
|
494
|
+
type LatencyControls = LatencyRuleInput & {
|
|
495
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
496
|
+
operations?: Record<string, Duration | LatencyRuleInput>;
|
|
497
|
+
};
|
|
498
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
499
|
+
type FaultInput = (Omit<FaultRule, "id"> & {
|
|
500
|
+
id?: string;
|
|
501
|
+
}) | ({
|
|
502
|
+
preset: string;
|
|
503
|
+
} & Partial<FaultRule>);
|
|
504
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
505
|
+
declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
506
|
+
/**
|
|
507
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
508
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
509
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
510
|
+
*/
|
|
511
|
+
type SnapshotDocument = {
|
|
512
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
513
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
514
|
+
service?: string;
|
|
515
|
+
/**
|
|
516
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
517
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
518
|
+
*/
|
|
519
|
+
records: {
|
|
520
|
+
collection: string;
|
|
521
|
+
id: string;
|
|
522
|
+
seq?: number;
|
|
523
|
+
value?: unknown;
|
|
524
|
+
raw?: string;
|
|
525
|
+
}[];
|
|
526
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
527
|
+
sequences?: {
|
|
528
|
+
name: string;
|
|
529
|
+
kind: string;
|
|
530
|
+
value: number;
|
|
531
|
+
}[];
|
|
532
|
+
clock?: {
|
|
533
|
+
now: number;
|
|
534
|
+
frozen: boolean;
|
|
535
|
+
autoAdvanceMs?: number;
|
|
536
|
+
};
|
|
537
|
+
seed?: number | string;
|
|
538
|
+
/** Positions of the seeded random streams. */
|
|
539
|
+
rng?: {
|
|
540
|
+
state: number;
|
|
541
|
+
faults?: number;
|
|
542
|
+
latency?: number;
|
|
543
|
+
};
|
|
544
|
+
faults?: unknown;
|
|
545
|
+
latency?: unknown;
|
|
546
|
+
virtualTime?: boolean;
|
|
547
|
+
};
|
|
548
|
+
/** Controls for one namespace. */
|
|
549
|
+
type NamespaceControls = {
|
|
550
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
551
|
+
seed?: number | string;
|
|
552
|
+
clock?: ClockControls;
|
|
553
|
+
/**
|
|
554
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
555
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
556
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
557
|
+
*/
|
|
558
|
+
virtualTime?: boolean;
|
|
559
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
560
|
+
latency?: Duration | LatencyControls;
|
|
561
|
+
faults?: readonly FaultInput[];
|
|
562
|
+
/** Start from this state instead of the service's own seed data. */
|
|
563
|
+
snapshot?: SnapshotDocument;
|
|
564
|
+
};
|
|
565
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
566
|
+
type MockControls = NamespaceControls & {
|
|
567
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
568
|
+
credentials?: Record<string, string>;
|
|
569
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
570
|
+
namespaces?: Record<string, NamespaceControls>;
|
|
571
|
+
};
|
|
572
|
+
/** A latency rule in milliseconds. */
|
|
573
|
+
type LatencyRule = {
|
|
574
|
+
delayMs: number;
|
|
575
|
+
jitterMs: number;
|
|
576
|
+
};
|
|
577
|
+
/** Baseline latency for a namespace, normalized. */
|
|
578
|
+
type LatencyState = LatencyRule & {
|
|
579
|
+
operations: Record<string, LatencyRule>;
|
|
580
|
+
};
|
|
581
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
582
|
+
type ControlHeaderInput = {
|
|
583
|
+
namespace?: string;
|
|
584
|
+
adminKey?: string;
|
|
585
|
+
branch?: string;
|
|
586
|
+
at?: string;
|
|
587
|
+
reset?: boolean;
|
|
588
|
+
restore?: string;
|
|
589
|
+
seed?: number | string;
|
|
590
|
+
time?: Instant | Date;
|
|
591
|
+
advance?: Duration;
|
|
592
|
+
virtualTime?: boolean;
|
|
593
|
+
delay?: Duration;
|
|
594
|
+
timeout?: Duration;
|
|
595
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
596
|
+
fault?: number | string | Record<string, unknown>;
|
|
597
|
+
snapshot?: string;
|
|
377
598
|
};
|
|
378
599
|
|
|
379
600
|
/**
|
|
@@ -413,6 +634,8 @@ type RequestLog = {
|
|
|
413
634
|
unmatched: boolean;
|
|
414
635
|
/** Set when a fault rule produced the response. */
|
|
415
636
|
faultId?: string;
|
|
637
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
638
|
+
delayMs?: number;
|
|
416
639
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
417
640
|
ids?: Record<string, string>;
|
|
418
641
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -458,6 +681,12 @@ type JournalQuery = {
|
|
|
458
681
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
459
682
|
namespace?: string;
|
|
460
683
|
operationId?: string;
|
|
684
|
+
/** Only this HTTP method, case-insensitive. */
|
|
685
|
+
method?: string;
|
|
686
|
+
/** Only this exact path. */
|
|
687
|
+
path?: string;
|
|
688
|
+
/** Only requests this fault rule answered or shaped. */
|
|
689
|
+
faultId?: string;
|
|
461
690
|
status?: number;
|
|
462
691
|
/** Only entries at or after this instant (epoch ms). */
|
|
463
692
|
since?: number;
|
|
@@ -472,6 +701,50 @@ type Journal = {
|
|
|
472
701
|
clear(namespace?: string): void;
|
|
473
702
|
};
|
|
474
703
|
|
|
704
|
+
/**
|
|
705
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
706
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
707
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
708
|
+
* fault hits.
|
|
709
|
+
*/
|
|
710
|
+
type LatencyRegistry = {
|
|
711
|
+
get(namespace: string): LatencyState;
|
|
712
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
713
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
714
|
+
clear(namespace?: string): void;
|
|
715
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
716
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
/** Clock state as the admin API reports it. */
|
|
720
|
+
type ControlClock = ClockState & {
|
|
721
|
+
/** `now`, as ISO-8601. */
|
|
722
|
+
iso: string;
|
|
723
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
724
|
+
autoAdvanceMs: number;
|
|
725
|
+
};
|
|
726
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
727
|
+
type ControlState = {
|
|
728
|
+
namespace: string;
|
|
729
|
+
seed: number | string;
|
|
730
|
+
clock: ControlClock;
|
|
731
|
+
virtualTime: boolean;
|
|
732
|
+
latency: LatencyState;
|
|
733
|
+
faults: FaultListing[];
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
737
|
+
type SnapshotInfo = {
|
|
738
|
+
id: string;
|
|
739
|
+
namespace: string;
|
|
740
|
+
/** Stored rows in the snapshot. */
|
|
741
|
+
records: number;
|
|
742
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
743
|
+
at: number;
|
|
744
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
745
|
+
portable: boolean;
|
|
746
|
+
};
|
|
747
|
+
|
|
475
748
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
476
749
|
type CredentialRegistry = {
|
|
477
750
|
set(credential: string, namespace: string): void;
|
|
@@ -626,7 +899,10 @@ type ServiceInstance = FetchAPI & {
|
|
|
626
899
|
};
|
|
627
900
|
type ServiceTimelineState = Readonly<{
|
|
628
901
|
snapshot: NamespaceSnapshot;
|
|
629
|
-
clock
|
|
902
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
903
|
+
clock: Readonly<ClockState & {
|
|
904
|
+
pinned?: boolean;
|
|
905
|
+
}>;
|
|
630
906
|
rngState: number;
|
|
631
907
|
}>;
|
|
632
908
|
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
@@ -634,17 +910,23 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
634
910
|
/** Stop package-owned lifecycle timers when a supervisor closes the runtime. */
|
|
635
911
|
stop?(): void;
|
|
636
912
|
readonly name: string;
|
|
913
|
+
/** Same-origin Fetch handler and App Router method exports, sharing this runtime. */
|
|
914
|
+
mount(prefix: string): MountedAPI;
|
|
637
915
|
readonly sqlite: SqliteClient$1;
|
|
638
916
|
readonly clock: Clock;
|
|
639
917
|
readonly faults: FaultRegistry;
|
|
918
|
+
/** Baseline response time per namespace. */
|
|
919
|
+
readonly latency: LatencyRegistry;
|
|
640
920
|
readonly metrics: Metrics;
|
|
641
921
|
readonly journal: Journal;
|
|
642
922
|
readonly rng: Rng;
|
|
643
923
|
readonly credentials: CredentialRegistry;
|
|
644
924
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
645
925
|
readonly webhooks: WebhookHub | undefined;
|
|
646
|
-
/**
|
|
647
|
-
|
|
926
|
+
/**
|
|
927
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
928
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
929
|
+
*/
|
|
648
930
|
namespaceClock(namespace?: string): Clock;
|
|
649
931
|
namespaceOf(request: Request): string;
|
|
650
932
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
@@ -656,8 +938,38 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
656
938
|
instance(namespace?: string): T;
|
|
657
939
|
/** Public names of every namespace created so far. */
|
|
658
940
|
namespaces(): string[];
|
|
659
|
-
/**
|
|
941
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
660
942
|
reset(namespace?: string): Promise<void>;
|
|
943
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
944
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
945
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
946
|
+
controls(namespace?: string): ControlState & {
|
|
947
|
+
snapshots: SnapshotInfo[];
|
|
948
|
+
};
|
|
949
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
950
|
+
configure(controls: NamespaceControls, namespace?: string): ControlState & {
|
|
951
|
+
snapshots: SnapshotInfo[];
|
|
952
|
+
};
|
|
953
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
954
|
+
readonly snapshots: {
|
|
955
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
956
|
+
create(options?: {
|
|
957
|
+
name?: string;
|
|
958
|
+
namespace?: string;
|
|
959
|
+
}): SnapshotInfo;
|
|
960
|
+
list(namespace?: string): SnapshotInfo[];
|
|
961
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
962
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
963
|
+
/** Store a document as a snapshot without restoring it. */
|
|
964
|
+
import(document: SnapshotDocument, options?: {
|
|
965
|
+
name?: string;
|
|
966
|
+
namespace?: string;
|
|
967
|
+
}): SnapshotInfo;
|
|
968
|
+
restore(id: string, namespace?: string): void;
|
|
969
|
+
delete(id: string, namespace?: string): boolean;
|
|
970
|
+
};
|
|
971
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
972
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
661
973
|
snapshot(namespace?: string): NamespaceSnapshot;
|
|
662
974
|
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
663
975
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -884,6 +1196,8 @@ type RuntimeOptions = {
|
|
|
884
1196
|
accountId?: string;
|
|
885
1197
|
adminPrefix?: string;
|
|
886
1198
|
adminKey?: string;
|
|
1199
|
+
/** Admin controls to boot with: clock, latency, faults, seed, a starting snapshot. */
|
|
1200
|
+
adminControls?: MockControls;
|
|
887
1201
|
onLog?: (entry: RequestLog) => void;
|
|
888
1202
|
};
|
|
889
1203
|
type Runtime = ServiceRuntime<DynamodbStreamsAPI>;
|