@crvouga/mockingbird-service-posthog 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 +13 -4
- package/dist/chunk-CJW2IXDE.js +6085 -0
- package/dist/chunk-CJW2IXDE.js.map +7 -0
- package/dist/{chunk-CEEIMCYK.js → chunk-PVGI6M5H.js} +138 -25
- package/dist/chunk-PVGI6M5H.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-CEEIMCYK.js.map +0 -7
- package/dist/chunk-SUXAPBOI.js +0 -4205
- package/dist/chunk-SUXAPBOI.js.map +0 -7
package/dist/index.d.ts
CHANGED
|
@@ -74,88 +74,16 @@ interface FetchAPI$1 {
|
|
|
74
74
|
fetch(request: Request): Promise<Response>;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
type MountedAPI = FetchAPI & {
|
|
78
|
+
readonly prefix: string;
|
|
79
|
+
GET: FetchHandler;
|
|
80
|
+
POST: FetchHandler;
|
|
81
|
+
PUT: FetchHandler;
|
|
82
|
+
PATCH: FetchHandler;
|
|
83
|
+
DELETE: FetchHandler;
|
|
84
|
+
HEAD: FetchHandler;
|
|
85
|
+
OPTIONS: FetchHandler;
|
|
83
86
|
};
|
|
84
|
-
/**
|
|
85
|
-
* Prepared statement bound to a {@link SqliteClient}.
|
|
86
|
-
*
|
|
87
|
-
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
88
|
-
*/
|
|
89
|
-
interface SqliteStatement$1 {
|
|
90
|
-
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
91
|
-
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
92
|
-
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Sync SQLite client port owned by Mockingbird.
|
|
96
|
-
*
|
|
97
|
-
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
98
|
-
* `bun:sqlite` instances all work when they expose this surface.
|
|
99
|
-
*/
|
|
100
|
-
interface SqliteClient$1 {
|
|
101
|
-
exec(sql: string): void;
|
|
102
|
-
prepare(sql: string): SqliteStatement$1;
|
|
103
|
-
transaction<T>(fn: () => T): T;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
107
|
-
type Stored<T> = {
|
|
108
|
-
seq: number;
|
|
109
|
-
value: T;
|
|
110
|
-
};
|
|
111
|
-
type ListRecordsOptions<T> = {
|
|
112
|
-
/** Keep only records passing the predicate. */
|
|
113
|
-
where?: (value: T, seq: number) => boolean;
|
|
114
|
-
/** Sort order; default newest first. */
|
|
115
|
-
order?: "newest" | "oldest";
|
|
116
|
-
};
|
|
117
|
-
/**
|
|
118
|
-
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
119
|
-
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
120
|
-
*/
|
|
121
|
-
declare class Collection<T> {
|
|
122
|
-
private readonly sqlite;
|
|
123
|
-
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
124
|
-
readonly namespace: string;
|
|
125
|
-
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
126
|
-
readonly collectionName: string;
|
|
127
|
-
constructor(sqlite: SqliteClient$1,
|
|
128
|
-
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
129
|
-
namespace: string,
|
|
130
|
-
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
131
|
-
collectionName: string);
|
|
132
|
-
private bumpCollectionSeq;
|
|
133
|
-
nextSequence(): number;
|
|
134
|
-
get(id: string): T | undefined;
|
|
135
|
-
has(id: string): boolean;
|
|
136
|
-
/** Insert a new record, assigning it the next sequence number. */
|
|
137
|
-
insert(id: string, value: T): Stored<T>;
|
|
138
|
-
/** Replace an existing record's value, keeping its position. */
|
|
139
|
-
update(id: string, value: T): Stored<T> | undefined;
|
|
140
|
-
delete(id: string): boolean;
|
|
141
|
-
/** How many records the collection holds, without reading them. */
|
|
142
|
-
count(): number;
|
|
143
|
-
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
144
|
-
id: string;
|
|
145
|
-
}>;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
150
|
-
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
151
|
-
*/
|
|
152
|
-
declare class IdSequence {
|
|
153
|
-
private readonly sqlite;
|
|
154
|
-
private readonly namespace;
|
|
155
|
-
private readonly salt;
|
|
156
|
-
constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
|
|
157
|
-
next(prefix: string, length?: number): string;
|
|
158
|
-
}
|
|
159
87
|
|
|
160
88
|
/** A stable identifier for a point in a {@link Timeline}. */
|
|
161
89
|
type CheckpointId = string;
|
|
@@ -246,6 +174,91 @@ declare class Timeline<T> {
|
|
|
246
174
|
interface FetchAPI {
|
|
247
175
|
fetch(request: Request): Promise<Response>;
|
|
248
176
|
}
|
|
177
|
+
/** A bare function form of {@link FetchAPI}, compatible with `Bun.serve`, workerd, and Deno. */
|
|
178
|
+
type FetchHandler = (request: Request) => Promise<Response>;
|
|
179
|
+
|
|
180
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
181
|
+
type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
|
|
182
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
183
|
+
type SqliteRunResult$1 = {
|
|
184
|
+
changes: number;
|
|
185
|
+
lastInsertRowid: number | bigint;
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
189
|
+
*
|
|
190
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
191
|
+
*/
|
|
192
|
+
interface SqliteStatement$1 {
|
|
193
|
+
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
194
|
+
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
195
|
+
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
199
|
+
*
|
|
200
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
201
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
202
|
+
*/
|
|
203
|
+
interface SqliteClient$1 {
|
|
204
|
+
exec(sql: string): void;
|
|
205
|
+
prepare(sql: string): SqliteStatement$1;
|
|
206
|
+
transaction<T>(fn: () => T): T;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
210
|
+
type Stored<T> = {
|
|
211
|
+
seq: number;
|
|
212
|
+
value: T;
|
|
213
|
+
};
|
|
214
|
+
type ListRecordsOptions<T> = {
|
|
215
|
+
/** Keep only records passing the predicate. */
|
|
216
|
+
where?: (value: T, seq: number) => boolean;
|
|
217
|
+
/** Sort order; default newest first. */
|
|
218
|
+
order?: "newest" | "oldest";
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
222
|
+
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
223
|
+
*/
|
|
224
|
+
declare class Collection<T> {
|
|
225
|
+
private readonly sqlite;
|
|
226
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
227
|
+
readonly namespace: string;
|
|
228
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
229
|
+
readonly collectionName: string;
|
|
230
|
+
constructor(sqlite: SqliteClient$1,
|
|
231
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
232
|
+
namespace: string,
|
|
233
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
234
|
+
collectionName: string);
|
|
235
|
+
private bumpCollectionSeq;
|
|
236
|
+
nextSequence(): number;
|
|
237
|
+
get(id: string): T | undefined;
|
|
238
|
+
has(id: string): boolean;
|
|
239
|
+
/** Insert a new record, assigning it the next sequence number. */
|
|
240
|
+
insert(id: string, value: T): Stored<T>;
|
|
241
|
+
/** Replace an existing record's value, keeping its position. */
|
|
242
|
+
update(id: string, value: T): Stored<T> | undefined;
|
|
243
|
+
delete(id: string): boolean;
|
|
244
|
+
/** How many records the collection holds, without reading them. */
|
|
245
|
+
count(): number;
|
|
246
|
+
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
247
|
+
id: string;
|
|
248
|
+
}>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
253
|
+
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
254
|
+
*/
|
|
255
|
+
declare class IdSequence {
|
|
256
|
+
private readonly sqlite;
|
|
257
|
+
private readonly namespace;
|
|
258
|
+
private readonly salt;
|
|
259
|
+
constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
|
|
260
|
+
next(prefix: string, length?: number): string;
|
|
261
|
+
}
|
|
249
262
|
|
|
250
263
|
/** Options every provider constructor accepts. */
|
|
251
264
|
type APIOptions = {
|
|
@@ -272,7 +285,7 @@ type ClockState = {
|
|
|
272
285
|
now: number;
|
|
273
286
|
/** True while time does not advance on its own. */
|
|
274
287
|
frozen: boolean;
|
|
275
|
-
/** Milliseconds
|
|
288
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
276
289
|
offsetMs: number;
|
|
277
290
|
};
|
|
278
291
|
type Clock = {
|
|
@@ -308,7 +321,10 @@ type Rng = {
|
|
|
308
321
|
state(): number;
|
|
309
322
|
/** Restore a state previously returned by {@link state}. */
|
|
310
323
|
setState(state: number): void;
|
|
311
|
-
seed
|
|
324
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
325
|
+
reseed(seed: number | string): void;
|
|
326
|
+
/** The seed the stream currently starts from. */
|
|
327
|
+
readonly seed: number;
|
|
312
328
|
};
|
|
313
329
|
|
|
314
330
|
/**
|
|
@@ -317,44 +333,83 @@ type Rng = {
|
|
|
317
333
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
318
334
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
319
335
|
* appears under load, the 5xx that proves a retry path works.
|
|
336
|
+
*
|
|
337
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
338
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
339
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
320
340
|
*/
|
|
321
341
|
type FaultRule = {
|
|
322
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
342
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
323
343
|
id: string;
|
|
324
344
|
/** Fault only this operation. Omit to match every operation. */
|
|
325
345
|
operationId?: string;
|
|
326
346
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
327
347
|
method?: string;
|
|
348
|
+
/** Fault only this exact path. */
|
|
349
|
+
path?: string;
|
|
328
350
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
329
351
|
pathPrefix?: string;
|
|
352
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
353
|
+
pathPattern?: string;
|
|
354
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
355
|
+
requestHeaders?: Record<string, string>;
|
|
356
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
357
|
+
query?: Record<string, string>;
|
|
330
358
|
/**
|
|
331
359
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
332
360
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
333
361
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
334
362
|
*/
|
|
335
363
|
namespace?: string;
|
|
364
|
+
/** Let this many matching requests through first. */
|
|
365
|
+
after?: number;
|
|
366
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
367
|
+
every?: number;
|
|
368
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
369
|
+
count?: number;
|
|
370
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
371
|
+
rate?: number;
|
|
372
|
+
/**
|
|
373
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
374
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
375
|
+
*/
|
|
376
|
+
from?: Instant | Duration;
|
|
377
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
378
|
+
until?: Instant | Duration;
|
|
336
379
|
/**
|
|
337
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
338
|
-
*
|
|
339
|
-
* the request then still reaches the service.
|
|
380
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
381
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
340
382
|
*/
|
|
341
383
|
status?: number;
|
|
342
384
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
343
385
|
body?: unknown;
|
|
386
|
+
/** Response headers. */
|
|
344
387
|
headers?: Record<string, string>;
|
|
345
|
-
/**
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
rate?: number;
|
|
349
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
388
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
389
|
+
delay?: Duration;
|
|
390
|
+
/** `delay`, in milliseconds. */
|
|
350
391
|
delayMs?: number;
|
|
351
392
|
/** Alias of `delayMs`. */
|
|
352
393
|
latencyMs?: number;
|
|
394
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
395
|
+
jitter?: Duration;
|
|
396
|
+
jitterMs?: number;
|
|
397
|
+
/**
|
|
398
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
399
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
400
|
+
*/
|
|
401
|
+
timeout?: Duration;
|
|
402
|
+
timeoutMs?: number;
|
|
353
403
|
/**
|
|
354
404
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
355
405
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
356
406
|
*/
|
|
357
407
|
drop?: boolean;
|
|
408
|
+
/**
|
|
409
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
410
|
+
* the connection dies mid-body.
|
|
411
|
+
*/
|
|
412
|
+
truncate?: number;
|
|
358
413
|
/**
|
|
359
414
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
360
415
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -363,8 +418,8 @@ type FaultRule = {
|
|
|
363
418
|
effect?: string;
|
|
364
419
|
/** Parameters for `effect`. */
|
|
365
420
|
params?: Record<string, unknown>;
|
|
366
|
-
/**
|
|
367
|
-
|
|
421
|
+
/** The preset this rule was expanded from, if any. */
|
|
422
|
+
fromPreset?: string;
|
|
368
423
|
};
|
|
369
424
|
/** A fault that fired for one request. */
|
|
370
425
|
type FaultHit = {
|
|
@@ -372,6 +427,10 @@ type FaultHit = {
|
|
|
372
427
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
373
428
|
response?: Response;
|
|
374
429
|
drop?: boolean;
|
|
430
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
431
|
+
timeout?: boolean;
|
|
432
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
433
|
+
truncate?: number;
|
|
375
434
|
effect?: {
|
|
376
435
|
name: string;
|
|
377
436
|
params: Record<string, unknown>;
|
|
@@ -396,38 +455,200 @@ type FaultCandidate = {
|
|
|
396
455
|
method: string;
|
|
397
456
|
path: string;
|
|
398
457
|
namespace: string;
|
|
458
|
+
/** Request headers and query, for rules that match on them. */
|
|
459
|
+
headers?: Headers;
|
|
460
|
+
query?: URLSearchParams;
|
|
461
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
462
|
+
now?: number;
|
|
463
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
464
|
+
wait?: (ms: number) => Promise<void>;
|
|
465
|
+
};
|
|
466
|
+
/** A stored rule and how often it was consulted. */
|
|
467
|
+
type FaultListing = FaultRule & {
|
|
468
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
469
|
+
remaining: number | null;
|
|
470
|
+
/** Times the rule faulted a request. */
|
|
471
|
+
hits: number;
|
|
472
|
+
/** Requests that matched the rule, faulted or not. */
|
|
473
|
+
seen: number;
|
|
474
|
+
};
|
|
475
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
476
|
+
type FaultSnapshot = {
|
|
477
|
+
/**
|
|
478
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
479
|
+
* in the order means to a request and holds in any process.
|
|
480
|
+
*/
|
|
481
|
+
rules: (FaultListing & {
|
|
482
|
+
position?: number;
|
|
483
|
+
})[];
|
|
484
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
485
|
+
nextId?: number;
|
|
399
486
|
};
|
|
400
487
|
type FaultRegistry = {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
rules: (FaultRule & {
|
|
404
|
-
remaining: number | null;
|
|
405
|
-
hits: number;
|
|
406
|
-
position: number;
|
|
407
|
-
})[];
|
|
408
|
-
rngState: number;
|
|
409
|
-
};
|
|
488
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
489
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
410
490
|
add(rule: FaultRule): FaultRule;
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
})[];
|
|
423
|
-
rngState: number;
|
|
424
|
-
}): void;
|
|
491
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
492
|
+
nextId(namespace: string): string;
|
|
493
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
494
|
+
list(namespace?: string): FaultListing[];
|
|
495
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
496
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
497
|
+
remove(id: string, namespace?: string): boolean;
|
|
498
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
499
|
+
clear(namespace?: string): void;
|
|
500
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
501
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
425
502
|
/**
|
|
426
503
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
427
504
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
428
505
|
* each matching rule's remaining uses.
|
|
429
506
|
*/
|
|
430
507
|
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
508
|
+
/**
|
|
509
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
510
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
511
|
+
*/
|
|
512
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* The admin controls every mock shares, as one document.
|
|
517
|
+
*
|
|
518
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
519
|
+
*
|
|
520
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
521
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
522
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
523
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
524
|
+
*
|
|
525
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
526
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
527
|
+
*/
|
|
528
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
529
|
+
type Duration = number | string;
|
|
530
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
531
|
+
type Instant = number | string;
|
|
532
|
+
type ClockControls = {
|
|
533
|
+
/** Move the clock to this instant. */
|
|
534
|
+
now?: Instant;
|
|
535
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
536
|
+
frozen?: boolean;
|
|
537
|
+
/**
|
|
538
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
539
|
+
* each request still gets its own, reproducible timestamp.
|
|
540
|
+
*/
|
|
541
|
+
autoAdvance?: Duration;
|
|
542
|
+
};
|
|
543
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
544
|
+
type LatencyRuleInput = {
|
|
545
|
+
delay?: Duration;
|
|
546
|
+
jitter?: Duration;
|
|
547
|
+
};
|
|
548
|
+
type LatencyControls = LatencyRuleInput & {
|
|
549
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
550
|
+
operations?: Record<string, Duration | LatencyRuleInput>;
|
|
551
|
+
};
|
|
552
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
553
|
+
type FaultInput = (Omit<FaultRule, "id"> & {
|
|
554
|
+
id?: string;
|
|
555
|
+
}) | ({
|
|
556
|
+
preset: string;
|
|
557
|
+
} & Partial<FaultRule>);
|
|
558
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
559
|
+
declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
560
|
+
/**
|
|
561
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
562
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
563
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
564
|
+
*/
|
|
565
|
+
type SnapshotDocument = {
|
|
566
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
567
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
568
|
+
service?: string;
|
|
569
|
+
/**
|
|
570
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
571
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
572
|
+
*/
|
|
573
|
+
records: {
|
|
574
|
+
collection: string;
|
|
575
|
+
id: string;
|
|
576
|
+
seq?: number;
|
|
577
|
+
value?: unknown;
|
|
578
|
+
raw?: string;
|
|
579
|
+
}[];
|
|
580
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
581
|
+
sequences?: {
|
|
582
|
+
name: string;
|
|
583
|
+
kind: string;
|
|
584
|
+
value: number;
|
|
585
|
+
}[];
|
|
586
|
+
clock?: {
|
|
587
|
+
now: number;
|
|
588
|
+
frozen: boolean;
|
|
589
|
+
autoAdvanceMs?: number;
|
|
590
|
+
};
|
|
591
|
+
seed?: number | string;
|
|
592
|
+
/** Positions of the seeded random streams. */
|
|
593
|
+
rng?: {
|
|
594
|
+
state: number;
|
|
595
|
+
faults?: number;
|
|
596
|
+
latency?: number;
|
|
597
|
+
};
|
|
598
|
+
faults?: unknown;
|
|
599
|
+
latency?: unknown;
|
|
600
|
+
virtualTime?: boolean;
|
|
601
|
+
};
|
|
602
|
+
/** Controls for one namespace. */
|
|
603
|
+
type NamespaceControls = {
|
|
604
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
605
|
+
seed?: number | string;
|
|
606
|
+
clock?: ClockControls;
|
|
607
|
+
/**
|
|
608
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
609
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
610
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
611
|
+
*/
|
|
612
|
+
virtualTime?: boolean;
|
|
613
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
614
|
+
latency?: Duration | LatencyControls;
|
|
615
|
+
faults?: readonly FaultInput[];
|
|
616
|
+
/** Start from this state instead of the service's own seed data. */
|
|
617
|
+
snapshot?: SnapshotDocument;
|
|
618
|
+
};
|
|
619
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
620
|
+
type MockControls = NamespaceControls & {
|
|
621
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
622
|
+
credentials?: Record<string, string>;
|
|
623
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
624
|
+
namespaces?: Record<string, NamespaceControls>;
|
|
625
|
+
};
|
|
626
|
+
/** A latency rule in milliseconds. */
|
|
627
|
+
type LatencyRule = {
|
|
628
|
+
delayMs: number;
|
|
629
|
+
jitterMs: number;
|
|
630
|
+
};
|
|
631
|
+
/** Baseline latency for a namespace, normalized. */
|
|
632
|
+
type LatencyState = LatencyRule & {
|
|
633
|
+
operations: Record<string, LatencyRule>;
|
|
634
|
+
};
|
|
635
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
636
|
+
type ControlHeaderInput = {
|
|
637
|
+
namespace?: string;
|
|
638
|
+
adminKey?: string;
|
|
639
|
+
branch?: string;
|
|
640
|
+
at?: string;
|
|
641
|
+
reset?: boolean;
|
|
642
|
+
restore?: string;
|
|
643
|
+
seed?: number | string;
|
|
644
|
+
time?: Instant | Date;
|
|
645
|
+
advance?: Duration;
|
|
646
|
+
virtualTime?: boolean;
|
|
647
|
+
delay?: Duration;
|
|
648
|
+
timeout?: Duration;
|
|
649
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
650
|
+
fault?: number | string | Record<string, unknown>;
|
|
651
|
+
snapshot?: string;
|
|
431
652
|
};
|
|
432
653
|
|
|
433
654
|
/**
|
|
@@ -467,6 +688,8 @@ type RequestLog = {
|
|
|
467
688
|
unmatched: boolean;
|
|
468
689
|
/** Set when a fault rule produced the response. */
|
|
469
690
|
faultId?: string;
|
|
691
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
692
|
+
delayMs?: number;
|
|
470
693
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
471
694
|
ids?: Record<string, string>;
|
|
472
695
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -512,6 +735,12 @@ type JournalQuery = {
|
|
|
512
735
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
513
736
|
namespace?: string;
|
|
514
737
|
operationId?: string;
|
|
738
|
+
/** Only this HTTP method, case-insensitive. */
|
|
739
|
+
method?: string;
|
|
740
|
+
/** Only this exact path. */
|
|
741
|
+
path?: string;
|
|
742
|
+
/** Only requests this fault rule answered or shaped. */
|
|
743
|
+
faultId?: string;
|
|
515
744
|
status?: number;
|
|
516
745
|
/** Only entries at or after this instant (epoch ms). */
|
|
517
746
|
since?: number;
|
|
@@ -526,6 +755,50 @@ type Journal = {
|
|
|
526
755
|
clear(namespace?: string): void;
|
|
527
756
|
};
|
|
528
757
|
|
|
758
|
+
/**
|
|
759
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
760
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
761
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
762
|
+
* fault hits.
|
|
763
|
+
*/
|
|
764
|
+
type LatencyRegistry = {
|
|
765
|
+
get(namespace: string): LatencyState;
|
|
766
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
767
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
768
|
+
clear(namespace?: string): void;
|
|
769
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
770
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
/** Clock state as the admin API reports it. */
|
|
774
|
+
type ControlClock = ClockState & {
|
|
775
|
+
/** `now`, as ISO-8601. */
|
|
776
|
+
iso: string;
|
|
777
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
778
|
+
autoAdvanceMs: number;
|
|
779
|
+
};
|
|
780
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
781
|
+
type ControlState = {
|
|
782
|
+
namespace: string;
|
|
783
|
+
seed: number | string;
|
|
784
|
+
clock: ControlClock;
|
|
785
|
+
virtualTime: boolean;
|
|
786
|
+
latency: LatencyState;
|
|
787
|
+
faults: FaultListing[];
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
791
|
+
type SnapshotInfo = {
|
|
792
|
+
id: string;
|
|
793
|
+
namespace: string;
|
|
794
|
+
/** Stored rows in the snapshot. */
|
|
795
|
+
records: number;
|
|
796
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
797
|
+
at: number;
|
|
798
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
799
|
+
portable: boolean;
|
|
800
|
+
};
|
|
801
|
+
|
|
529
802
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
530
803
|
type CredentialRegistry = {
|
|
531
804
|
set(credential: string, namespace: string): void;
|
|
@@ -680,7 +953,10 @@ type ServiceInstance = FetchAPI & {
|
|
|
680
953
|
};
|
|
681
954
|
type ServiceTimelineState = Readonly<{
|
|
682
955
|
snapshot: NamespaceSnapshot;
|
|
683
|
-
clock
|
|
956
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
957
|
+
clock: Readonly<ClockState & {
|
|
958
|
+
pinned?: boolean;
|
|
959
|
+
}>;
|
|
684
960
|
rngState: number;
|
|
685
961
|
}>;
|
|
686
962
|
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
@@ -688,17 +964,23 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
688
964
|
/** Stop package-owned lifecycle timers when a supervisor closes the runtime. */
|
|
689
965
|
stop?(): void;
|
|
690
966
|
readonly name: string;
|
|
967
|
+
/** Same-origin Fetch handler and App Router method exports, sharing this runtime. */
|
|
968
|
+
mount(prefix: string): MountedAPI;
|
|
691
969
|
readonly sqlite: SqliteClient$1;
|
|
692
970
|
readonly clock: Clock;
|
|
693
971
|
readonly faults: FaultRegistry;
|
|
972
|
+
/** Baseline response time per namespace. */
|
|
973
|
+
readonly latency: LatencyRegistry;
|
|
694
974
|
readonly metrics: Metrics;
|
|
695
975
|
readonly journal: Journal;
|
|
696
976
|
readonly rng: Rng;
|
|
697
977
|
readonly credentials: CredentialRegistry;
|
|
698
978
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
699
979
|
readonly webhooks: WebhookHub | undefined;
|
|
700
|
-
/**
|
|
701
|
-
|
|
980
|
+
/**
|
|
981
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
982
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
983
|
+
*/
|
|
702
984
|
namespaceClock(namespace?: string): Clock;
|
|
703
985
|
namespaceOf(request: Request): string;
|
|
704
986
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
@@ -710,8 +992,38 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
|
710
992
|
instance(namespace?: string): T;
|
|
711
993
|
/** Public names of every namespace created so far. */
|
|
712
994
|
namespaces(): string[];
|
|
713
|
-
/**
|
|
995
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
714
996
|
reset(namespace?: string): Promise<void>;
|
|
997
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
998
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
999
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
1000
|
+
controls(namespace?: string): ControlState & {
|
|
1001
|
+
snapshots: SnapshotInfo[];
|
|
1002
|
+
};
|
|
1003
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
1004
|
+
configure(controls: NamespaceControls, namespace?: string): ControlState & {
|
|
1005
|
+
snapshots: SnapshotInfo[];
|
|
1006
|
+
};
|
|
1007
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
1008
|
+
readonly snapshots: {
|
|
1009
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
1010
|
+
create(options?: {
|
|
1011
|
+
name?: string;
|
|
1012
|
+
namespace?: string;
|
|
1013
|
+
}): SnapshotInfo;
|
|
1014
|
+
list(namespace?: string): SnapshotInfo[];
|
|
1015
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
1016
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
1017
|
+
/** Store a document as a snapshot without restoring it. */
|
|
1018
|
+
import(document: SnapshotDocument, options?: {
|
|
1019
|
+
name?: string;
|
|
1020
|
+
namespace?: string;
|
|
1021
|
+
}): SnapshotInfo;
|
|
1022
|
+
restore(id: string, namespace?: string): void;
|
|
1023
|
+
delete(id: string, namespace?: string): boolean;
|
|
1024
|
+
};
|
|
1025
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
1026
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
715
1027
|
snapshot(namespace?: string): NamespaceSnapshot;
|
|
716
1028
|
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
717
1029
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -1067,6 +1379,8 @@ type PostHogRuntimeOptions = {
|
|
|
1067
1379
|
seed?: number | string;
|
|
1068
1380
|
adminPrefix?: string;
|
|
1069
1381
|
adminKey?: string;
|
|
1382
|
+
/** Admin controls to boot with: clock, latency, faults, seed, a starting snapshot. */
|
|
1383
|
+
adminControls?: MockControls;
|
|
1070
1384
|
onLog?: (entry: RequestLog) => void;
|
|
1071
1385
|
/** Flags every namespace starts with (and returns to on reset). */
|
|
1072
1386
|
flags?: Record<string, FlagSpec>;
|