@crvouga/mockingbird-service-rxvortex 3.1.0 → 3.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 +14 -2
- package/DISCOVERY.md +23 -0
- package/README.md +14 -11
- package/dist/{chunk-QMHFS4QB.js → chunk-DAE44LUH.js} +139 -26
- package/dist/chunk-DAE44LUH.js.map +7 -0
- package/dist/chunk-U2SZUONL.js +6312 -0
- package/dist/chunk-U2SZUONL.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-EQNZ2CZA.js +0 -4432
- package/dist/chunk-EQNZ2CZA.js.map +0 -7
- package/dist/chunk-QMHFS4QB.js.map +0 -7
package/dist/index.d.ts
CHANGED
|
@@ -10,88 +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
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
86
|
-
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
87
|
-
*/
|
|
88
|
-
declare class IdSequence {
|
|
89
|
-
private readonly sqlite;
|
|
90
|
-
private readonly namespace;
|
|
91
|
-
private readonly salt;
|
|
92
|
-
constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
|
|
93
|
-
next(prefix: string, length?: number): string;
|
|
94
|
-
}
|
|
95
23
|
|
|
96
24
|
/** A stable identifier for a point in a {@link Timeline}. */
|
|
97
25
|
type CheckpointId = string;
|
|
@@ -182,6 +110,91 @@ declare class Timeline<T> {
|
|
|
182
110
|
interface FetchAPI {
|
|
183
111
|
fetch(request: Request): Promise<Response>;
|
|
184
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
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
189
|
+
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
190
|
+
*/
|
|
191
|
+
declare class IdSequence {
|
|
192
|
+
private readonly sqlite;
|
|
193
|
+
private readonly namespace;
|
|
194
|
+
private readonly salt;
|
|
195
|
+
constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
|
|
196
|
+
next(prefix: string, length?: number): string;
|
|
197
|
+
}
|
|
185
198
|
|
|
186
199
|
/** Options every provider constructor accepts. */
|
|
187
200
|
type APIOptions = {
|
|
@@ -208,7 +221,7 @@ type ClockState = {
|
|
|
208
221
|
now: number;
|
|
209
222
|
/** True while time does not advance on its own. */
|
|
210
223
|
frozen: boolean;
|
|
211
|
-
/** Milliseconds
|
|
224
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
212
225
|
offsetMs: number;
|
|
213
226
|
};
|
|
214
227
|
type Clock = {
|
|
@@ -244,7 +257,10 @@ type Rng = {
|
|
|
244
257
|
state(): number;
|
|
245
258
|
/** Restore a state previously returned by {@link state}. */
|
|
246
259
|
setState(state: number): void;
|
|
247
|
-
seed
|
|
260
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
261
|
+
reseed(seed: number | string): void;
|
|
262
|
+
/** The seed the stream currently starts from. */
|
|
263
|
+
readonly seed: number;
|
|
248
264
|
};
|
|
249
265
|
|
|
250
266
|
/**
|
|
@@ -253,44 +269,83 @@ type Rng = {
|
|
|
253
269
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
254
270
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
255
271
|
* appears under load, the 5xx that proves a retry path works.
|
|
272
|
+
*
|
|
273
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
274
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
275
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
256
276
|
*/
|
|
257
277
|
type FaultRule = {
|
|
258
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
278
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
259
279
|
id: string;
|
|
260
280
|
/** Fault only this operation. Omit to match every operation. */
|
|
261
281
|
operationId?: string;
|
|
262
282
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
263
283
|
method?: string;
|
|
284
|
+
/** Fault only this exact path. */
|
|
285
|
+
path?: string;
|
|
264
286
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
265
287
|
pathPrefix?: string;
|
|
288
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
289
|
+
pathPattern?: string;
|
|
290
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
291
|
+
requestHeaders?: Record<string, string>;
|
|
292
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
293
|
+
query?: Record<string, string>;
|
|
266
294
|
/**
|
|
267
295
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
268
296
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
269
297
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
270
298
|
*/
|
|
271
299
|
namespace?: string;
|
|
300
|
+
/** Let this many matching requests through first. */
|
|
301
|
+
after?: number;
|
|
302
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
303
|
+
every?: number;
|
|
304
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
305
|
+
count?: number;
|
|
306
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
307
|
+
rate?: number;
|
|
308
|
+
/**
|
|
309
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
310
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
311
|
+
*/
|
|
312
|
+
from?: Instant | Duration;
|
|
313
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
314
|
+
until?: Instant | Duration;
|
|
272
315
|
/**
|
|
273
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
274
|
-
*
|
|
275
|
-
* the request then still reaches the service.
|
|
316
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
317
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
276
318
|
*/
|
|
277
319
|
status?: number;
|
|
278
320
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
279
321
|
body?: unknown;
|
|
322
|
+
/** Response headers. */
|
|
280
323
|
headers?: Record<string, string>;
|
|
281
|
-
/**
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
rate?: number;
|
|
285
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
324
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
325
|
+
delay?: Duration;
|
|
326
|
+
/** `delay`, in milliseconds. */
|
|
286
327
|
delayMs?: number;
|
|
287
328
|
/** Alias of `delayMs`. */
|
|
288
329
|
latencyMs?: number;
|
|
330
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
331
|
+
jitter?: Duration;
|
|
332
|
+
jitterMs?: number;
|
|
333
|
+
/**
|
|
334
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
335
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
336
|
+
*/
|
|
337
|
+
timeout?: Duration;
|
|
338
|
+
timeoutMs?: number;
|
|
289
339
|
/**
|
|
290
340
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
291
341
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
292
342
|
*/
|
|
293
343
|
drop?: boolean;
|
|
344
|
+
/**
|
|
345
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
346
|
+
* the connection dies mid-body.
|
|
347
|
+
*/
|
|
348
|
+
truncate?: number;
|
|
294
349
|
/**
|
|
295
350
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
296
351
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -299,8 +354,8 @@ type FaultRule = {
|
|
|
299
354
|
effect?: string;
|
|
300
355
|
/** Parameters for `effect`. */
|
|
301
356
|
params?: Record<string, unknown>;
|
|
302
|
-
/**
|
|
303
|
-
|
|
357
|
+
/** The preset this rule was expanded from, if any. */
|
|
358
|
+
fromPreset?: string;
|
|
304
359
|
};
|
|
305
360
|
/** A fault that fired for one request. */
|
|
306
361
|
type FaultHit = {
|
|
@@ -308,6 +363,10 @@ type FaultHit = {
|
|
|
308
363
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
309
364
|
response?: Response;
|
|
310
365
|
drop?: boolean;
|
|
366
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
367
|
+
timeout?: boolean;
|
|
368
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
369
|
+
truncate?: number;
|
|
311
370
|
effect?: {
|
|
312
371
|
name: string;
|
|
313
372
|
params: Record<string, unknown>;
|
|
@@ -332,38 +391,200 @@ type FaultCandidate = {
|
|
|
332
391
|
method: string;
|
|
333
392
|
path: string;
|
|
334
393
|
namespace: string;
|
|
394
|
+
/** Request headers and query, for rules that match on them. */
|
|
395
|
+
headers?: Headers;
|
|
396
|
+
query?: URLSearchParams;
|
|
397
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
398
|
+
now?: number;
|
|
399
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
400
|
+
wait?: (ms: number) => Promise<void>;
|
|
401
|
+
};
|
|
402
|
+
/** A stored rule and how often it was consulted. */
|
|
403
|
+
type FaultListing = FaultRule & {
|
|
404
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
405
|
+
remaining: number | null;
|
|
406
|
+
/** Times the rule faulted a request. */
|
|
407
|
+
hits: number;
|
|
408
|
+
/** Requests that matched the rule, faulted or not. */
|
|
409
|
+
seen: number;
|
|
410
|
+
};
|
|
411
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
412
|
+
type FaultSnapshot = {
|
|
413
|
+
/**
|
|
414
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
415
|
+
* in the order means to a request and holds in any process.
|
|
416
|
+
*/
|
|
417
|
+
rules: (FaultListing & {
|
|
418
|
+
position?: number;
|
|
419
|
+
})[];
|
|
420
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
421
|
+
nextId?: number;
|
|
335
422
|
};
|
|
336
423
|
type FaultRegistry = {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
rules: (FaultRule & {
|
|
340
|
-
remaining: number | null;
|
|
341
|
-
hits: number;
|
|
342
|
-
position: number;
|
|
343
|
-
})[];
|
|
344
|
-
rngState: number;
|
|
345
|
-
};
|
|
424
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
425
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
346
426
|
add(rule: FaultRule): FaultRule;
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
})[];
|
|
359
|
-
rngState: number;
|
|
360
|
-
}): void;
|
|
427
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
428
|
+
nextId(namespace: string): string;
|
|
429
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
430
|
+
list(namespace?: string): FaultListing[];
|
|
431
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
432
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
433
|
+
remove(id: string, namespace?: string): boolean;
|
|
434
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
435
|
+
clear(namespace?: string): void;
|
|
436
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
437
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
361
438
|
/**
|
|
362
439
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
363
440
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
364
441
|
* each matching rule's remaining uses.
|
|
365
442
|
*/
|
|
366
443
|
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
444
|
+
/**
|
|
445
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
446
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
447
|
+
*/
|
|
448
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The admin controls every mock shares, as one document.
|
|
453
|
+
*
|
|
454
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
455
|
+
*
|
|
456
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
457
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
458
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
459
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
460
|
+
*
|
|
461
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
462
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
463
|
+
*/
|
|
464
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
465
|
+
type Duration = number | string;
|
|
466
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
467
|
+
type Instant = number | string;
|
|
468
|
+
type ClockControls = {
|
|
469
|
+
/** Move the clock to this instant. */
|
|
470
|
+
now?: Instant;
|
|
471
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
472
|
+
frozen?: boolean;
|
|
473
|
+
/**
|
|
474
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
475
|
+
* each request still gets its own, reproducible timestamp.
|
|
476
|
+
*/
|
|
477
|
+
autoAdvance?: Duration;
|
|
478
|
+
};
|
|
479
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
480
|
+
type LatencyRuleInput = {
|
|
481
|
+
delay?: Duration;
|
|
482
|
+
jitter?: Duration;
|
|
483
|
+
};
|
|
484
|
+
type LatencyControls = LatencyRuleInput & {
|
|
485
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
486
|
+
operations?: Record<string, Duration | LatencyRuleInput>;
|
|
487
|
+
};
|
|
488
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
489
|
+
type FaultInput = (Omit<FaultRule, "id"> & {
|
|
490
|
+
id?: string;
|
|
491
|
+
}) | ({
|
|
492
|
+
preset: string;
|
|
493
|
+
} & Partial<FaultRule>);
|
|
494
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
495
|
+
declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
496
|
+
/**
|
|
497
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
498
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
499
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
500
|
+
*/
|
|
501
|
+
type SnapshotDocument = {
|
|
502
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
503
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
504
|
+
service?: string;
|
|
505
|
+
/**
|
|
506
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
507
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
508
|
+
*/
|
|
509
|
+
records: {
|
|
510
|
+
collection: string;
|
|
511
|
+
id: string;
|
|
512
|
+
seq?: number;
|
|
513
|
+
value?: unknown;
|
|
514
|
+
raw?: string;
|
|
515
|
+
}[];
|
|
516
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
517
|
+
sequences?: {
|
|
518
|
+
name: string;
|
|
519
|
+
kind: string;
|
|
520
|
+
value: number;
|
|
521
|
+
}[];
|
|
522
|
+
clock?: {
|
|
523
|
+
now: number;
|
|
524
|
+
frozen: boolean;
|
|
525
|
+
autoAdvanceMs?: number;
|
|
526
|
+
};
|
|
527
|
+
seed?: number | string;
|
|
528
|
+
/** Positions of the seeded random streams. */
|
|
529
|
+
rng?: {
|
|
530
|
+
state: number;
|
|
531
|
+
faults?: number;
|
|
532
|
+
latency?: number;
|
|
533
|
+
};
|
|
534
|
+
faults?: unknown;
|
|
535
|
+
latency?: unknown;
|
|
536
|
+
virtualTime?: boolean;
|
|
537
|
+
};
|
|
538
|
+
/** Controls for one namespace. */
|
|
539
|
+
type NamespaceControls = {
|
|
540
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
541
|
+
seed?: number | string;
|
|
542
|
+
clock?: ClockControls;
|
|
543
|
+
/**
|
|
544
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
545
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
546
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
547
|
+
*/
|
|
548
|
+
virtualTime?: boolean;
|
|
549
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
550
|
+
latency?: Duration | LatencyControls;
|
|
551
|
+
faults?: readonly FaultInput[];
|
|
552
|
+
/** Start from this state instead of the service's own seed data. */
|
|
553
|
+
snapshot?: SnapshotDocument;
|
|
554
|
+
};
|
|
555
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
556
|
+
type MockControls = NamespaceControls & {
|
|
557
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
558
|
+
credentials?: Record<string, string>;
|
|
559
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
560
|
+
namespaces?: Record<string, NamespaceControls>;
|
|
561
|
+
};
|
|
562
|
+
/** A latency rule in milliseconds. */
|
|
563
|
+
type LatencyRule = {
|
|
564
|
+
delayMs: number;
|
|
565
|
+
jitterMs: number;
|
|
566
|
+
};
|
|
567
|
+
/** Baseline latency for a namespace, normalized. */
|
|
568
|
+
type LatencyState = LatencyRule & {
|
|
569
|
+
operations: Record<string, LatencyRule>;
|
|
570
|
+
};
|
|
571
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
572
|
+
type ControlHeaderInput = {
|
|
573
|
+
namespace?: string;
|
|
574
|
+
adminKey?: string;
|
|
575
|
+
branch?: string;
|
|
576
|
+
at?: string;
|
|
577
|
+
reset?: boolean;
|
|
578
|
+
restore?: string;
|
|
579
|
+
seed?: number | string;
|
|
580
|
+
time?: Instant | Date;
|
|
581
|
+
advance?: Duration;
|
|
582
|
+
virtualTime?: boolean;
|
|
583
|
+
delay?: Duration;
|
|
584
|
+
timeout?: Duration;
|
|
585
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
586
|
+
fault?: number | string | Record<string, unknown>;
|
|
587
|
+
snapshot?: string;
|
|
367
588
|
};
|
|
368
589
|
|
|
369
590
|
/**
|
|
@@ -403,6 +624,8 @@ type RequestLog = {
|
|
|
403
624
|
unmatched: boolean;
|
|
404
625
|
/** Set when a fault rule produced the response. */
|
|
405
626
|
faultId?: string;
|
|
627
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
628
|
+
delayMs?: number;
|
|
406
629
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
407
630
|
ids?: Record<string, string>;
|
|
408
631
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -448,6 +671,12 @@ type JournalQuery = {
|
|
|
448
671
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
449
672
|
namespace?: string;
|
|
450
673
|
operationId?: string;
|
|
674
|
+
/** Only this HTTP method, case-insensitive. */
|
|
675
|
+
method?: string;
|
|
676
|
+
/** Only this exact path. */
|
|
677
|
+
path?: string;
|
|
678
|
+
/** Only requests this fault rule answered or shaped. */
|
|
679
|
+
faultId?: string;
|
|
451
680
|
status?: number;
|
|
452
681
|
/** Only entries at or after this instant (epoch ms). */
|
|
453
682
|
since?: number;
|
|
@@ -462,6 +691,50 @@ type Journal = {
|
|
|
462
691
|
clear(namespace?: string): void;
|
|
463
692
|
};
|
|
464
693
|
|
|
694
|
+
/**
|
|
695
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
696
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
697
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
698
|
+
* fault hits.
|
|
699
|
+
*/
|
|
700
|
+
type LatencyRegistry = {
|
|
701
|
+
get(namespace: string): LatencyState;
|
|
702
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
703
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
704
|
+
clear(namespace?: string): void;
|
|
705
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
706
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
/** Clock state as the admin API reports it. */
|
|
710
|
+
type ControlClock = ClockState & {
|
|
711
|
+
/** `now`, as ISO-8601. */
|
|
712
|
+
iso: string;
|
|
713
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
714
|
+
autoAdvanceMs: number;
|
|
715
|
+
};
|
|
716
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
717
|
+
type ControlState = {
|
|
718
|
+
namespace: string;
|
|
719
|
+
seed: number | string;
|
|
720
|
+
clock: ControlClock;
|
|
721
|
+
virtualTime: boolean;
|
|
722
|
+
latency: LatencyState;
|
|
723
|
+
faults: FaultListing[];
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
727
|
+
type SnapshotInfo = {
|
|
728
|
+
id: string;
|
|
729
|
+
namespace: string;
|
|
730
|
+
/** Stored rows in the snapshot. */
|
|
731
|
+
records: number;
|
|
732
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
733
|
+
at: number;
|
|
734
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
735
|
+
portable: boolean;
|
|
736
|
+
};
|
|
737
|
+
|
|
465
738
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
466
739
|
type CredentialRegistry = {
|
|
467
740
|
set(credential: string, namespace: string): void;
|
|
@@ -616,7 +889,10 @@ type ServiceInstance = FetchAPI & {
|
|
|
616
889
|
};
|
|
617
890
|
type ServiceTimelineState = Readonly<{
|
|
618
891
|
snapshot: NamespaceSnapshot;
|
|
619
|
-
clock
|
|
892
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
893
|
+
clock: Readonly<ClockState & {
|
|
894
|
+
pinned?: boolean;
|
|
895
|
+
}>;
|
|
620
896
|
rngState: number;
|
|
621
897
|
}>;
|
|
622
898
|
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
@@ -624,17 +900,23 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
624
900
|
/** Stop package-owned lifecycle timers when a supervisor closes the runtime. */
|
|
625
901
|
stop?(): void;
|
|
626
902
|
readonly name: string;
|
|
903
|
+
/** Same-origin Fetch handler and App Router method exports, sharing this runtime. */
|
|
904
|
+
mount(prefix: string): MountedAPI;
|
|
627
905
|
readonly sqlite: SqliteClient$1;
|
|
628
906
|
readonly clock: Clock;
|
|
629
907
|
readonly faults: FaultRegistry;
|
|
908
|
+
/** Baseline response time per namespace. */
|
|
909
|
+
readonly latency: LatencyRegistry;
|
|
630
910
|
readonly metrics: Metrics;
|
|
631
911
|
readonly journal: Journal;
|
|
632
912
|
readonly rng: Rng;
|
|
633
913
|
readonly credentials: CredentialRegistry;
|
|
634
914
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
635
915
|
readonly webhooks: WebhookHub | undefined;
|
|
636
|
-
/**
|
|
637
|
-
|
|
916
|
+
/**
|
|
917
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
918
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
919
|
+
*/
|
|
638
920
|
namespaceClock(namespace?: string): Clock;
|
|
639
921
|
namespaceOf(request: Request): string;
|
|
640
922
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
@@ -646,8 +928,38 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
646
928
|
instance(namespace?: string): T;
|
|
647
929
|
/** Public names of every namespace created so far. */
|
|
648
930
|
namespaces(): string[];
|
|
649
|
-
/**
|
|
931
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
650
932
|
reset(namespace?: string): Promise<void>;
|
|
933
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
934
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
935
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
936
|
+
controls(namespace?: string): ControlState & {
|
|
937
|
+
snapshots: SnapshotInfo[];
|
|
938
|
+
};
|
|
939
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
940
|
+
configure(controls: NamespaceControls, namespace?: string): ControlState & {
|
|
941
|
+
snapshots: SnapshotInfo[];
|
|
942
|
+
};
|
|
943
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
944
|
+
readonly snapshots: {
|
|
945
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
946
|
+
create(options?: {
|
|
947
|
+
name?: string;
|
|
948
|
+
namespace?: string;
|
|
949
|
+
}): SnapshotInfo;
|
|
950
|
+
list(namespace?: string): SnapshotInfo[];
|
|
951
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
952
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
953
|
+
/** Store a document as a snapshot without restoring it. */
|
|
954
|
+
import(document: SnapshotDocument, options?: {
|
|
955
|
+
name?: string;
|
|
956
|
+
namespace?: string;
|
|
957
|
+
}): SnapshotInfo;
|
|
958
|
+
restore(id: string, namespace?: string): void;
|
|
959
|
+
delete(id: string, namespace?: string): boolean;
|
|
960
|
+
};
|
|
961
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
962
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
651
963
|
snapshot(namespace?: string): NamespaceSnapshot;
|
|
652
964
|
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
653
965
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -983,6 +1295,8 @@ type RxVortexRuntimeOptions = {
|
|
|
983
1295
|
seed?: number | string;
|
|
984
1296
|
adminPrefix?: string;
|
|
985
1297
|
adminKey?: string;
|
|
1298
|
+
/** Admin controls to boot with: clock, latency, faults, seed, a starting snapshot. */
|
|
1299
|
+
adminControls?: MockControls;
|
|
986
1300
|
onLog?: (entry: RequestLog) => void;
|
|
987
1301
|
/**
|
|
988
1302
|
* Rows every namespace starts with, and returns to on `POST /__admin/reset` (`serve
|