@crvouga/mockingbird-service-workos 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 +18 -4
- package/DISCOVERY.md +23 -0
- package/README.md +28 -3
- package/dist/chunk-DW6XZF4X.js +5520 -0
- package/dist/chunk-DW6XZF4X.js.map +7 -0
- package/dist/chunk-JPFEQTV6.js +4743 -0
- package/dist/chunk-JPFEQTV6.js.map +7 -0
- package/dist/cli.js +2 -2
- package/dist/index.d.ts +354 -41
- package/dist/index.js +1 -1
- package/dist/server.d.ts +709 -82
- package/dist/server.js +2 -2
- package/package.json +3 -2
- package/dist/chunk-BIGV6FI5.js +0 -824
- package/dist/chunk-BIGV6FI5.js.map +0 -7
- package/dist/chunk-JITNKKHL.js +0 -3626
- package/dist/chunk-JITNKKHL.js.map +0 -7
package/dist/server.d.ts
CHANGED
|
@@ -146,7 +146,7 @@ type ClockState$1 = {
|
|
|
146
146
|
now: number;
|
|
147
147
|
/** True while time does not advance on its own. */
|
|
148
148
|
frozen: boolean;
|
|
149
|
-
/** Milliseconds
|
|
149
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
150
150
|
offsetMs: number;
|
|
151
151
|
};
|
|
152
152
|
type Clock$1 = {
|
|
@@ -182,7 +182,10 @@ type Rng$1 = {
|
|
|
182
182
|
state(): number;
|
|
183
183
|
/** Restore a state previously returned by {@link state}. */
|
|
184
184
|
setState(state: number): void;
|
|
185
|
-
seed
|
|
185
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
186
|
+
reseed(seed: number | string): void;
|
|
187
|
+
/** The seed the stream currently starts from. */
|
|
188
|
+
readonly seed: number;
|
|
186
189
|
};
|
|
187
190
|
|
|
188
191
|
/**
|
|
@@ -191,44 +194,83 @@ type Rng$1 = {
|
|
|
191
194
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
192
195
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
193
196
|
* appears under load, the 5xx that proves a retry path works.
|
|
197
|
+
*
|
|
198
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
199
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
200
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
194
201
|
*/
|
|
195
202
|
type FaultRule$1 = {
|
|
196
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
203
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
197
204
|
id: string;
|
|
198
205
|
/** Fault only this operation. Omit to match every operation. */
|
|
199
206
|
operationId?: string;
|
|
200
207
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
201
208
|
method?: string;
|
|
209
|
+
/** Fault only this exact path. */
|
|
210
|
+
path?: string;
|
|
202
211
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
203
212
|
pathPrefix?: string;
|
|
213
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
214
|
+
pathPattern?: string;
|
|
215
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
216
|
+
requestHeaders?: Record<string, string>;
|
|
217
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
218
|
+
query?: Record<string, string>;
|
|
204
219
|
/**
|
|
205
220
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
206
221
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
207
222
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
208
223
|
*/
|
|
209
224
|
namespace?: string;
|
|
225
|
+
/** Let this many matching requests through first. */
|
|
226
|
+
after?: number;
|
|
227
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
228
|
+
every?: number;
|
|
229
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
230
|
+
count?: number;
|
|
231
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
232
|
+
rate?: number;
|
|
233
|
+
/**
|
|
234
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
235
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
236
|
+
*/
|
|
237
|
+
from?: Instant$1 | Duration$1;
|
|
238
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
239
|
+
until?: Instant$1 | Duration$1;
|
|
210
240
|
/**
|
|
211
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
212
|
-
*
|
|
213
|
-
* the request then still reaches the service.
|
|
241
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
242
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
214
243
|
*/
|
|
215
244
|
status?: number;
|
|
216
245
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
217
246
|
body?: unknown;
|
|
247
|
+
/** Response headers. */
|
|
218
248
|
headers?: Record<string, string>;
|
|
219
|
-
/**
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
rate?: number;
|
|
223
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
249
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
250
|
+
delay?: Duration$1;
|
|
251
|
+
/** `delay`, in milliseconds. */
|
|
224
252
|
delayMs?: number;
|
|
225
253
|
/** Alias of `delayMs`. */
|
|
226
254
|
latencyMs?: number;
|
|
255
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
256
|
+
jitter?: Duration$1;
|
|
257
|
+
jitterMs?: number;
|
|
258
|
+
/**
|
|
259
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
260
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
261
|
+
*/
|
|
262
|
+
timeout?: Duration$1;
|
|
263
|
+
timeoutMs?: number;
|
|
227
264
|
/**
|
|
228
265
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
229
266
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
230
267
|
*/
|
|
231
268
|
drop?: boolean;
|
|
269
|
+
/**
|
|
270
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
271
|
+
* the connection dies mid-body.
|
|
272
|
+
*/
|
|
273
|
+
truncate?: number;
|
|
232
274
|
/**
|
|
233
275
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
234
276
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -237,8 +279,8 @@ type FaultRule$1 = {
|
|
|
237
279
|
effect?: string;
|
|
238
280
|
/** Parameters for `effect`. */
|
|
239
281
|
params?: Record<string, unknown>;
|
|
240
|
-
/**
|
|
241
|
-
|
|
282
|
+
/** The preset this rule was expanded from, if any. */
|
|
283
|
+
fromPreset?: string;
|
|
242
284
|
};
|
|
243
285
|
/** A fault that fired for one request. */
|
|
244
286
|
type FaultHit$1 = {
|
|
@@ -246,6 +288,10 @@ type FaultHit$1 = {
|
|
|
246
288
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
247
289
|
response?: Response;
|
|
248
290
|
drop?: boolean;
|
|
291
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
292
|
+
timeout?: boolean;
|
|
293
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
294
|
+
truncate?: number;
|
|
249
295
|
effect?: {
|
|
250
296
|
name: string;
|
|
251
297
|
params: Record<string, unknown>;
|
|
@@ -257,38 +303,202 @@ type FaultCandidate$1 = {
|
|
|
257
303
|
method: string;
|
|
258
304
|
path: string;
|
|
259
305
|
namespace: string;
|
|
306
|
+
/** Request headers and query, for rules that match on them. */
|
|
307
|
+
headers?: Headers;
|
|
308
|
+
query?: URLSearchParams;
|
|
309
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
310
|
+
now?: number;
|
|
311
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
312
|
+
wait?: (ms: number) => Promise<void>;
|
|
313
|
+
};
|
|
314
|
+
/** A stored rule and how often it was consulted. */
|
|
315
|
+
type FaultListing$1 = FaultRule$1 & {
|
|
316
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
317
|
+
remaining: number | null;
|
|
318
|
+
/** Times the rule faulted a request. */
|
|
319
|
+
hits: number;
|
|
320
|
+
/** Requests that matched the rule, faulted or not. */
|
|
321
|
+
seen: number;
|
|
322
|
+
};
|
|
323
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
324
|
+
type FaultSnapshot$1 = {
|
|
325
|
+
/**
|
|
326
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
327
|
+
* in the order means to a request and holds in any process.
|
|
328
|
+
*/
|
|
329
|
+
rules: (FaultListing$1 & {
|
|
330
|
+
position?: number;
|
|
331
|
+
})[];
|
|
332
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
333
|
+
nextId?: number;
|
|
260
334
|
};
|
|
261
335
|
type FaultRegistry$1 = {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
rules: (FaultRule$1 & {
|
|
265
|
-
remaining: number | null;
|
|
266
|
-
hits: number;
|
|
267
|
-
position: number;
|
|
268
|
-
})[];
|
|
269
|
-
rngState: number;
|
|
270
|
-
};
|
|
336
|
+
snapshot(namespace: string): FaultSnapshot$1;
|
|
337
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
271
338
|
add(rule: FaultRule$1): FaultRule$1;
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
})[];
|
|
284
|
-
rngState: number;
|
|
285
|
-
}): void;
|
|
339
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
340
|
+
nextId(namespace: string): string;
|
|
341
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
342
|
+
list(namespace?: string): FaultListing$1[];
|
|
343
|
+
get(id: string, namespace?: string): FaultListing$1 | undefined;
|
|
344
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
345
|
+
remove(id: string, namespace?: string): boolean;
|
|
346
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
347
|
+
clear(namespace?: string): void;
|
|
348
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
349
|
+
restore(namespace: string, state: FaultSnapshot$1): void;
|
|
286
350
|
/**
|
|
287
351
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
288
352
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
289
353
|
* each matching rule's remaining uses.
|
|
290
354
|
*/
|
|
291
355
|
take(candidate: FaultCandidate$1): Promise<FaultHit$1[]>;
|
|
356
|
+
/**
|
|
357
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
358
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
359
|
+
*/
|
|
360
|
+
fire(rules: readonly FaultRule$1[], candidate: FaultCandidate$1): Promise<FaultHit$1[]>;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* The admin controls every mock shares, as one document.
|
|
365
|
+
*
|
|
366
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
367
|
+
*
|
|
368
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
369
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
370
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
371
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
372
|
+
*
|
|
373
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
374
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
375
|
+
*/
|
|
376
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
377
|
+
type Duration$1 = number | string;
|
|
378
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
379
|
+
type Instant$1 = number | string;
|
|
380
|
+
type ClockControls$1 = {
|
|
381
|
+
/** Move the clock to this instant. */
|
|
382
|
+
now?: Instant$1;
|
|
383
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
384
|
+
frozen?: boolean;
|
|
385
|
+
/**
|
|
386
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
387
|
+
* each request still gets its own, reproducible timestamp.
|
|
388
|
+
*/
|
|
389
|
+
autoAdvance?: Duration$1;
|
|
390
|
+
};
|
|
391
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
392
|
+
type LatencyRuleInput$1 = {
|
|
393
|
+
delay?: Duration$1;
|
|
394
|
+
jitter?: Duration$1;
|
|
395
|
+
};
|
|
396
|
+
type LatencyControls$1 = LatencyRuleInput$1 & {
|
|
397
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
398
|
+
operations?: Record<string, Duration$1 | LatencyRuleInput$1>;
|
|
399
|
+
};
|
|
400
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
401
|
+
type FaultInput$1 = (Omit<FaultRule$1, "id"> & {
|
|
402
|
+
id?: string;
|
|
403
|
+
}) | ({
|
|
404
|
+
preset: string;
|
|
405
|
+
} & Partial<FaultRule$1>);
|
|
406
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
407
|
+
declare const SNAPSHOT_FORMAT$1 = "mockingbird.snapshot/v1";
|
|
408
|
+
/**
|
|
409
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
410
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
411
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
412
|
+
*/
|
|
413
|
+
type SnapshotDocument$1 = {
|
|
414
|
+
format: typeof SNAPSHOT_FORMAT$1;
|
|
415
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
416
|
+
service?: string;
|
|
417
|
+
/**
|
|
418
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
419
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
420
|
+
*/
|
|
421
|
+
records: {
|
|
422
|
+
collection: string;
|
|
423
|
+
id: string;
|
|
424
|
+
seq?: number;
|
|
425
|
+
value?: unknown;
|
|
426
|
+
raw?: string;
|
|
427
|
+
}[];
|
|
428
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
429
|
+
sequences?: {
|
|
430
|
+
name: string;
|
|
431
|
+
kind: string;
|
|
432
|
+
value: number;
|
|
433
|
+
}[];
|
|
434
|
+
clock?: {
|
|
435
|
+
now: number;
|
|
436
|
+
frozen: boolean;
|
|
437
|
+
autoAdvanceMs?: number;
|
|
438
|
+
};
|
|
439
|
+
seed?: number | string;
|
|
440
|
+
/** Positions of the seeded random streams. */
|
|
441
|
+
rng?: {
|
|
442
|
+
state: number;
|
|
443
|
+
faults?: number;
|
|
444
|
+
latency?: number;
|
|
445
|
+
};
|
|
446
|
+
faults?: unknown;
|
|
447
|
+
latency?: unknown;
|
|
448
|
+
virtualTime?: boolean;
|
|
449
|
+
/** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
|
|
450
|
+
webhooks?: unknown;
|
|
451
|
+
};
|
|
452
|
+
/** Controls for one namespace. */
|
|
453
|
+
type NamespaceControls$1 = {
|
|
454
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
455
|
+
seed?: number | string;
|
|
456
|
+
clock?: ClockControls$1;
|
|
457
|
+
/**
|
|
458
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
459
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
460
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
461
|
+
*/
|
|
462
|
+
virtualTime?: boolean;
|
|
463
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
464
|
+
latency?: Duration$1 | LatencyControls$1;
|
|
465
|
+
faults?: readonly FaultInput$1[];
|
|
466
|
+
/** Start from this state instead of the service's own seed data. */
|
|
467
|
+
snapshot?: SnapshotDocument$1;
|
|
468
|
+
};
|
|
469
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
470
|
+
type MockControls$1 = NamespaceControls$1 & {
|
|
471
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
472
|
+
credentials?: Record<string, string>;
|
|
473
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
474
|
+
namespaces?: Record<string, NamespaceControls$1>;
|
|
475
|
+
};
|
|
476
|
+
/** A latency rule in milliseconds. */
|
|
477
|
+
type LatencyRule$1 = {
|
|
478
|
+
delayMs: number;
|
|
479
|
+
jitterMs: number;
|
|
480
|
+
};
|
|
481
|
+
/** Baseline latency for a namespace, normalized. */
|
|
482
|
+
type LatencyState$1 = LatencyRule$1 & {
|
|
483
|
+
operations: Record<string, LatencyRule$1>;
|
|
484
|
+
};
|
|
485
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
486
|
+
type ControlHeaderInput$1 = {
|
|
487
|
+
namespace?: string;
|
|
488
|
+
adminKey?: string;
|
|
489
|
+
branch?: string;
|
|
490
|
+
at?: string;
|
|
491
|
+
reset?: boolean;
|
|
492
|
+
restore?: string;
|
|
493
|
+
seed?: number | string;
|
|
494
|
+
time?: Instant$1 | Date;
|
|
495
|
+
advance?: Duration$1;
|
|
496
|
+
virtualTime?: boolean;
|
|
497
|
+
delay?: Duration$1;
|
|
498
|
+
timeout?: Duration$1;
|
|
499
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
500
|
+
fault?: number | string | Record<string, unknown>;
|
|
501
|
+
snapshot?: string;
|
|
292
502
|
};
|
|
293
503
|
|
|
294
504
|
/**
|
|
@@ -328,6 +538,8 @@ type RequestLog$1 = {
|
|
|
328
538
|
unmatched: boolean;
|
|
329
539
|
/** Set when a fault rule produced the response. */
|
|
330
540
|
faultId?: string;
|
|
541
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
542
|
+
delayMs?: number;
|
|
331
543
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
332
544
|
ids?: Record<string, string>;
|
|
333
545
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -373,6 +585,12 @@ type JournalQuery$1 = {
|
|
|
373
585
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
374
586
|
namespace?: string;
|
|
375
587
|
operationId?: string;
|
|
588
|
+
/** Only this HTTP method, case-insensitive. */
|
|
589
|
+
method?: string;
|
|
590
|
+
/** Only this exact path. */
|
|
591
|
+
path?: string;
|
|
592
|
+
/** Only requests this fault rule answered or shaped. */
|
|
593
|
+
faultId?: string;
|
|
376
594
|
status?: number;
|
|
377
595
|
/** Only entries at or after this instant (epoch ms). */
|
|
378
596
|
since?: number;
|
|
@@ -387,6 +605,50 @@ type Journal$1 = {
|
|
|
387
605
|
clear(namespace?: string): void;
|
|
388
606
|
};
|
|
389
607
|
|
|
608
|
+
/**
|
|
609
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
610
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
611
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
612
|
+
* fault hits.
|
|
613
|
+
*/
|
|
614
|
+
type LatencyRegistry$1 = {
|
|
615
|
+
get(namespace: string): LatencyState$1;
|
|
616
|
+
set(namespace: string, state: LatencyState$1): LatencyState$1;
|
|
617
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
618
|
+
clear(namespace?: string): void;
|
|
619
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
620
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
/** Clock state as the admin API reports it. */
|
|
624
|
+
type ControlClock$1 = ClockState$1 & {
|
|
625
|
+
/** `now`, as ISO-8601. */
|
|
626
|
+
iso: string;
|
|
627
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
628
|
+
autoAdvanceMs: number;
|
|
629
|
+
};
|
|
630
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
631
|
+
type ControlState$1 = {
|
|
632
|
+
namespace: string;
|
|
633
|
+
seed: number | string;
|
|
634
|
+
clock: ControlClock$1;
|
|
635
|
+
virtualTime: boolean;
|
|
636
|
+
latency: LatencyState$1;
|
|
637
|
+
faults: FaultListing$1[];
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
641
|
+
type SnapshotInfo$1 = {
|
|
642
|
+
id: string;
|
|
643
|
+
namespace: string;
|
|
644
|
+
/** Stored rows in the snapshot. */
|
|
645
|
+
records: number;
|
|
646
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
647
|
+
at: number;
|
|
648
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
649
|
+
portable: boolean;
|
|
650
|
+
};
|
|
651
|
+
|
|
390
652
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
391
653
|
type CredentialRegistry$1 = {
|
|
392
654
|
set(credential: string, namespace: string): void;
|
|
@@ -399,6 +661,8 @@ type CredentialRegistry$1 = {
|
|
|
399
661
|
}[];
|
|
400
662
|
};
|
|
401
663
|
|
|
664
|
+
type OwnedTransport$1 = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
|
|
665
|
+
|
|
402
666
|
/**
|
|
403
667
|
* A point-in-time copy of everything a service namespace holds.
|
|
404
668
|
*
|
|
@@ -516,8 +780,16 @@ type PublishInput$1 = {
|
|
|
516
780
|
id?: string;
|
|
517
781
|
};
|
|
518
782
|
type WebhookHub$1 = {
|
|
783
|
+
observeDeliveries(listener: (event: {
|
|
784
|
+
namespace: string;
|
|
785
|
+
state: string;
|
|
786
|
+
deliveryId: string;
|
|
787
|
+
}) => void): () => void;
|
|
788
|
+
setTransportPolicy(policy: OwnedTransport$1): void;
|
|
519
789
|
pending(namespace: string): number;
|
|
520
790
|
snapshot(namespace: string): unknown;
|
|
791
|
+
exportSnapshot(handle: unknown): unknown;
|
|
792
|
+
importSnapshot(namespace: string, document: unknown): unknown;
|
|
521
793
|
restore(namespace: string, snapshot: unknown): void;
|
|
522
794
|
publish(input: PublishInput$1): WebhookMessage$1;
|
|
523
795
|
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
@@ -530,7 +802,7 @@ type WebhookHub$1 = {
|
|
|
530
802
|
/** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
|
|
531
803
|
flush(): Promise<void>;
|
|
532
804
|
/** Resolve once nothing is in flight. */
|
|
533
|
-
idle(): Promise<void>;
|
|
805
|
+
idle(namespace?: string): Promise<void>;
|
|
534
806
|
fault(namespace: string, fault: WebhookFault$1): void;
|
|
535
807
|
clear(namespace?: string): void;
|
|
536
808
|
};
|
|
@@ -541,7 +813,10 @@ type ServiceInstance$1 = FetchAPI$2 & {
|
|
|
541
813
|
};
|
|
542
814
|
type ServiceTimelineState$1 = Readonly<{
|
|
543
815
|
snapshot: NamespaceSnapshot$1;
|
|
544
|
-
clock
|
|
816
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
817
|
+
clock: Readonly<ClockState$1 & {
|
|
818
|
+
pinned?: boolean;
|
|
819
|
+
}>;
|
|
545
820
|
rngState: number;
|
|
546
821
|
}>;
|
|
547
822
|
type ServiceCheckpoint$1 = Checkpoint$1<ServiceTimelineState$1>;
|
|
@@ -554,27 +829,63 @@ type ServiceRuntime$1<T extends ServiceInstance$1> = FetchAPI$2 & {
|
|
|
554
829
|
readonly sqlite: SqliteClient$2;
|
|
555
830
|
readonly clock: Clock$1;
|
|
556
831
|
readonly faults: FaultRegistry$1;
|
|
832
|
+
/** Baseline response time per namespace. */
|
|
833
|
+
readonly latency: LatencyRegistry$1;
|
|
557
834
|
readonly metrics: Metrics$1;
|
|
558
835
|
readonly journal: Journal$1;
|
|
559
836
|
readonly rng: Rng$1;
|
|
560
837
|
readonly credentials: CredentialRegistry$1;
|
|
561
838
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
562
839
|
readonly webhooks: WebhookHub$1 | undefined;
|
|
563
|
-
/**
|
|
564
|
-
|
|
840
|
+
/**
|
|
841
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
842
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
843
|
+
*/
|
|
565
844
|
namespaceClock(namespace?: string): Clock$1;
|
|
566
845
|
namespaceOf(request: Request): string;
|
|
567
846
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
568
847
|
fleetSnapshot(namespace: string): object;
|
|
569
848
|
fleetRestore(snapshot: unknown, namespace: string): void;
|
|
849
|
+
fleetExport(snapshot: unknown): SnapshotDocument$1;
|
|
850
|
+
fleetImport(namespace: string, document: unknown): object;
|
|
570
851
|
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
571
852
|
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule$1>): FaultRule$1[];
|
|
572
853
|
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
573
854
|
instance(namespace?: string): T;
|
|
574
855
|
/** Public names of every namespace created so far. */
|
|
575
856
|
namespaces(): string[];
|
|
576
|
-
/**
|
|
857
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
577
858
|
reset(namespace?: string): Promise<void>;
|
|
859
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
860
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
861
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
862
|
+
controls(namespace?: string): ControlState$1 & {
|
|
863
|
+
snapshots: SnapshotInfo$1[];
|
|
864
|
+
};
|
|
865
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
866
|
+
configure(controls: NamespaceControls$1, namespace?: string): ControlState$1 & {
|
|
867
|
+
snapshots: SnapshotInfo$1[];
|
|
868
|
+
};
|
|
869
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
870
|
+
readonly snapshots: {
|
|
871
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
872
|
+
create(options?: {
|
|
873
|
+
name?: string;
|
|
874
|
+
namespace?: string;
|
|
875
|
+
}): SnapshotInfo$1;
|
|
876
|
+
list(namespace?: string): SnapshotInfo$1[];
|
|
877
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
878
|
+
export(id: string, namespace?: string): SnapshotDocument$1;
|
|
879
|
+
/** Store a document as a snapshot without restoring it. */
|
|
880
|
+
import(document: SnapshotDocument$1, options?: {
|
|
881
|
+
name?: string;
|
|
882
|
+
namespace?: string;
|
|
883
|
+
}): SnapshotInfo$1;
|
|
884
|
+
restore(id: string, namespace?: string): void;
|
|
885
|
+
delete(id: string, namespace?: string): boolean;
|
|
886
|
+
};
|
|
887
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
888
|
+
headers(input: ControlHeaderInput$1): Record<string, string>;
|
|
578
889
|
snapshot(namespace?: string): NamespaceSnapshot$1;
|
|
579
890
|
restore(snapshot: NamespaceSnapshot$1, namespace?: string): void;
|
|
580
891
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -617,6 +928,8 @@ type CommonServeOptions = {
|
|
|
617
928
|
adminKey: string | undefined;
|
|
618
929
|
seed: string | undefined;
|
|
619
930
|
onLog: ((entry: RequestLog$1) => void) | undefined;
|
|
931
|
+
/** Admin controls to boot with: clock, latency, faults, a starting snapshot. */
|
|
932
|
+
adminControls?: MockControls$1;
|
|
620
933
|
/** Synthetic provider fixture objects supplied by a fleet entry. */
|
|
621
934
|
fixtures?: Record<string, unknown>;
|
|
622
935
|
baseUrl?: string;
|
|
@@ -627,6 +940,7 @@ type CommonServeOptions = {
|
|
|
627
940
|
* `serveTarget`, which is also how `serve --config` finds services by name.
|
|
628
941
|
*/
|
|
629
942
|
type ServeTarget = {
|
|
943
|
+
packageVersion?: string;
|
|
630
944
|
name: string;
|
|
631
945
|
defaultPort: number;
|
|
632
946
|
/** Serve flags beyond the common ones. */
|
|
@@ -837,7 +1151,7 @@ type ClockState = {
|
|
|
837
1151
|
now: number;
|
|
838
1152
|
/** True while time does not advance on its own. */
|
|
839
1153
|
frozen: boolean;
|
|
840
|
-
/** Milliseconds
|
|
1154
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
841
1155
|
offsetMs: number;
|
|
842
1156
|
};
|
|
843
1157
|
type Clock = {
|
|
@@ -873,7 +1187,10 @@ type Rng = {
|
|
|
873
1187
|
state(): number;
|
|
874
1188
|
/** Restore a state previously returned by {@link state}. */
|
|
875
1189
|
setState(state: number): void;
|
|
876
|
-
seed
|
|
1190
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
1191
|
+
reseed(seed: number | string): void;
|
|
1192
|
+
/** The seed the stream currently starts from. */
|
|
1193
|
+
readonly seed: number;
|
|
877
1194
|
};
|
|
878
1195
|
|
|
879
1196
|
/**
|
|
@@ -882,44 +1199,83 @@ type Rng = {
|
|
|
882
1199
|
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
883
1200
|
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
884
1201
|
* appears under load, the 5xx that proves a retry path works.
|
|
1202
|
+
*
|
|
1203
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
1204
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
1205
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
885
1206
|
*/
|
|
886
1207
|
type FaultRule = {
|
|
887
|
-
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
1208
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
888
1209
|
id: string;
|
|
889
1210
|
/** Fault only this operation. Omit to match every operation. */
|
|
890
1211
|
operationId?: string;
|
|
891
1212
|
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
892
1213
|
method?: string;
|
|
1214
|
+
/** Fault only this exact path. */
|
|
1215
|
+
path?: string;
|
|
893
1216
|
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
894
1217
|
pathPrefix?: string;
|
|
1218
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
1219
|
+
pathPattern?: string;
|
|
1220
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
1221
|
+
requestHeaders?: Record<string, string>;
|
|
1222
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
1223
|
+
query?: Record<string, string>;
|
|
895
1224
|
/**
|
|
896
1225
|
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
897
1226
|
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
898
1227
|
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
899
1228
|
*/
|
|
900
1229
|
namespace?: string;
|
|
1230
|
+
/** Let this many matching requests through first. */
|
|
1231
|
+
after?: number;
|
|
1232
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
1233
|
+
every?: number;
|
|
1234
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
1235
|
+
count?: number;
|
|
1236
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
1237
|
+
rate?: number;
|
|
1238
|
+
/**
|
|
1239
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
1240
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
1241
|
+
*/
|
|
1242
|
+
from?: Instant | Duration;
|
|
1243
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
1244
|
+
until?: Instant | Duration;
|
|
901
1245
|
/**
|
|
902
|
-
* Status of the injected response. Omit for a rule that only delays
|
|
903
|
-
*
|
|
904
|
-
* the request then still reaches the service.
|
|
1246
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
1247
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
905
1248
|
*/
|
|
906
1249
|
status?: number;
|
|
907
1250
|
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
908
1251
|
body?: unknown;
|
|
1252
|
+
/** Response headers. */
|
|
909
1253
|
headers?: Record<string, string>;
|
|
910
|
-
/**
|
|
911
|
-
|
|
912
|
-
/**
|
|
913
|
-
rate?: number;
|
|
914
|
-
/** Hold the response back this long, to exercise timeouts. */
|
|
1254
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
1255
|
+
delay?: Duration;
|
|
1256
|
+
/** `delay`, in milliseconds. */
|
|
915
1257
|
delayMs?: number;
|
|
916
1258
|
/** Alias of `delayMs`. */
|
|
917
1259
|
latencyMs?: number;
|
|
1260
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
1261
|
+
jitter?: Duration;
|
|
1262
|
+
jitterMs?: number;
|
|
1263
|
+
/**
|
|
1264
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
1265
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
1266
|
+
*/
|
|
1267
|
+
timeout?: Duration;
|
|
1268
|
+
timeoutMs?: number;
|
|
918
1269
|
/**
|
|
919
1270
|
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
920
1271
|
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
921
1272
|
*/
|
|
922
1273
|
drop?: boolean;
|
|
1274
|
+
/**
|
|
1275
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
1276
|
+
* the connection dies mid-body.
|
|
1277
|
+
*/
|
|
1278
|
+
truncate?: number;
|
|
923
1279
|
/**
|
|
924
1280
|
* A named service behaviour to switch on for the matching request instead of (or
|
|
925
1281
|
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
@@ -928,8 +1284,8 @@ type FaultRule = {
|
|
|
928
1284
|
effect?: string;
|
|
929
1285
|
/** Parameters for `effect`. */
|
|
930
1286
|
params?: Record<string, unknown>;
|
|
931
|
-
/**
|
|
932
|
-
|
|
1287
|
+
/** The preset this rule was expanded from, if any. */
|
|
1288
|
+
fromPreset?: string;
|
|
933
1289
|
};
|
|
934
1290
|
/** A fault that fired for one request. */
|
|
935
1291
|
type FaultHit = {
|
|
@@ -937,6 +1293,10 @@ type FaultHit = {
|
|
|
937
1293
|
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
938
1294
|
response?: Response;
|
|
939
1295
|
drop?: boolean;
|
|
1296
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
1297
|
+
timeout?: boolean;
|
|
1298
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
1299
|
+
truncate?: number;
|
|
940
1300
|
effect?: {
|
|
941
1301
|
name: string;
|
|
942
1302
|
params: Record<string, unknown>;
|
|
@@ -948,38 +1308,202 @@ type FaultCandidate = {
|
|
|
948
1308
|
method: string;
|
|
949
1309
|
path: string;
|
|
950
1310
|
namespace: string;
|
|
1311
|
+
/** Request headers and query, for rules that match on them. */
|
|
1312
|
+
headers?: Headers;
|
|
1313
|
+
query?: URLSearchParams;
|
|
1314
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
1315
|
+
now?: number;
|
|
1316
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
1317
|
+
wait?: (ms: number) => Promise<void>;
|
|
1318
|
+
};
|
|
1319
|
+
/** A stored rule and how often it was consulted. */
|
|
1320
|
+
type FaultListing = FaultRule & {
|
|
1321
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
1322
|
+
remaining: number | null;
|
|
1323
|
+
/** Times the rule faulted a request. */
|
|
1324
|
+
hits: number;
|
|
1325
|
+
/** Requests that matched the rule, faulted or not. */
|
|
1326
|
+
seen: number;
|
|
1327
|
+
};
|
|
1328
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
1329
|
+
type FaultSnapshot = {
|
|
1330
|
+
/**
|
|
1331
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
1332
|
+
* in the order means to a request and holds in any process.
|
|
1333
|
+
*/
|
|
1334
|
+
rules: (FaultListing & {
|
|
1335
|
+
position?: number;
|
|
1336
|
+
})[];
|
|
1337
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
1338
|
+
nextId?: number;
|
|
951
1339
|
};
|
|
952
1340
|
type FaultRegistry = {
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
rules: (FaultRule & {
|
|
956
|
-
remaining: number | null;
|
|
957
|
-
hits: number;
|
|
958
|
-
position: number;
|
|
959
|
-
})[];
|
|
960
|
-
rngState: number;
|
|
961
|
-
};
|
|
1341
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
1342
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
962
1343
|
add(rule: FaultRule): FaultRule;
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
})[];
|
|
975
|
-
rngState: number;
|
|
976
|
-
}): void;
|
|
1344
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
1345
|
+
nextId(namespace: string): string;
|
|
1346
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
1347
|
+
list(namespace?: string): FaultListing[];
|
|
1348
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
1349
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
1350
|
+
remove(id: string, namespace?: string): boolean;
|
|
1351
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
1352
|
+
clear(namespace?: string): void;
|
|
1353
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
1354
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
977
1355
|
/**
|
|
978
1356
|
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
979
1357
|
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
980
1358
|
* each matching rule's remaining uses.
|
|
981
1359
|
*/
|
|
982
1360
|
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
1361
|
+
/**
|
|
1362
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
1363
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
1364
|
+
*/
|
|
1365
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
1366
|
+
};
|
|
1367
|
+
|
|
1368
|
+
/**
|
|
1369
|
+
* The admin controls every mock shares, as one document.
|
|
1370
|
+
*
|
|
1371
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
1372
|
+
*
|
|
1373
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
1374
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
1375
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
1376
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
1377
|
+
*
|
|
1378
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
1379
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
1380
|
+
*/
|
|
1381
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
1382
|
+
type Duration = number | string;
|
|
1383
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
1384
|
+
type Instant = number | string;
|
|
1385
|
+
type ClockControls = {
|
|
1386
|
+
/** Move the clock to this instant. */
|
|
1387
|
+
now?: Instant;
|
|
1388
|
+
/** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
|
|
1389
|
+
frozen?: boolean;
|
|
1390
|
+
/**
|
|
1391
|
+
* Step the clock forward by this much before every vendor request. With a frozen clock
|
|
1392
|
+
* each request still gets its own, reproducible timestamp.
|
|
1393
|
+
*/
|
|
1394
|
+
autoAdvance?: Duration;
|
|
1395
|
+
};
|
|
1396
|
+
/** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
|
|
1397
|
+
type LatencyRuleInput = {
|
|
1398
|
+
delay?: Duration;
|
|
1399
|
+
jitter?: Duration;
|
|
1400
|
+
};
|
|
1401
|
+
type LatencyControls = LatencyRuleInput & {
|
|
1402
|
+
/** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
|
|
1403
|
+
operations?: Record<string, Duration | LatencyRuleInput>;
|
|
1404
|
+
};
|
|
1405
|
+
/** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
|
|
1406
|
+
type FaultInput = (Omit<FaultRule, "id"> & {
|
|
1407
|
+
id?: string;
|
|
1408
|
+
}) | ({
|
|
1409
|
+
preset: string;
|
|
1410
|
+
} & Partial<FaultRule>);
|
|
1411
|
+
/** Identifies a {@link SnapshotDocument}. */
|
|
1412
|
+
declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
|
|
1413
|
+
/**
|
|
1414
|
+
* A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
|
|
1415
|
+
* `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
|
|
1416
|
+
* `records` are required, so a hand-written seed file is a valid snapshot.
|
|
1417
|
+
*/
|
|
1418
|
+
type SnapshotDocument = {
|
|
1419
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
1420
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
1421
|
+
service?: string;
|
|
1422
|
+
/**
|
|
1423
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
1424
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
1425
|
+
*/
|
|
1426
|
+
records: {
|
|
1427
|
+
collection: string;
|
|
1428
|
+
id: string;
|
|
1429
|
+
seq?: number;
|
|
1430
|
+
value?: unknown;
|
|
1431
|
+
raw?: string;
|
|
1432
|
+
}[];
|
|
1433
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
1434
|
+
sequences?: {
|
|
1435
|
+
name: string;
|
|
1436
|
+
kind: string;
|
|
1437
|
+
value: number;
|
|
1438
|
+
}[];
|
|
1439
|
+
clock?: {
|
|
1440
|
+
now: number;
|
|
1441
|
+
frozen: boolean;
|
|
1442
|
+
autoAdvanceMs?: number;
|
|
1443
|
+
};
|
|
1444
|
+
seed?: number | string;
|
|
1445
|
+
/** Positions of the seeded random streams. */
|
|
1446
|
+
rng?: {
|
|
1447
|
+
state: number;
|
|
1448
|
+
faults?: number;
|
|
1449
|
+
latency?: number;
|
|
1450
|
+
};
|
|
1451
|
+
faults?: unknown;
|
|
1452
|
+
latency?: unknown;
|
|
1453
|
+
virtualTime?: boolean;
|
|
1454
|
+
/** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
|
|
1455
|
+
webhooks?: unknown;
|
|
1456
|
+
};
|
|
1457
|
+
/** Controls for one namespace. */
|
|
1458
|
+
type NamespaceControls = {
|
|
1459
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
1460
|
+
seed?: number | string;
|
|
1461
|
+
clock?: ClockControls;
|
|
1462
|
+
/**
|
|
1463
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
1464
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
1465
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
1466
|
+
*/
|
|
1467
|
+
virtualTime?: boolean;
|
|
1468
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
1469
|
+
latency?: Duration | LatencyControls;
|
|
1470
|
+
faults?: readonly FaultInput[];
|
|
1471
|
+
/** Start from this state instead of the service's own seed data. */
|
|
1472
|
+
snapshot?: SnapshotDocument;
|
|
1473
|
+
};
|
|
1474
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
1475
|
+
type MockControls = NamespaceControls & {
|
|
1476
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
1477
|
+
credentials?: Record<string, string>;
|
|
1478
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
1479
|
+
namespaces?: Record<string, NamespaceControls>;
|
|
1480
|
+
};
|
|
1481
|
+
/** A latency rule in milliseconds. */
|
|
1482
|
+
type LatencyRule = {
|
|
1483
|
+
delayMs: number;
|
|
1484
|
+
jitterMs: number;
|
|
1485
|
+
};
|
|
1486
|
+
/** Baseline latency for a namespace, normalized. */
|
|
1487
|
+
type LatencyState = LatencyRule & {
|
|
1488
|
+
operations: Record<string, LatencyRule>;
|
|
1489
|
+
};
|
|
1490
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
1491
|
+
type ControlHeaderInput = {
|
|
1492
|
+
namespace?: string;
|
|
1493
|
+
adminKey?: string;
|
|
1494
|
+
branch?: string;
|
|
1495
|
+
at?: string;
|
|
1496
|
+
reset?: boolean;
|
|
1497
|
+
restore?: string;
|
|
1498
|
+
seed?: number | string;
|
|
1499
|
+
time?: Instant | Date;
|
|
1500
|
+
advance?: Duration;
|
|
1501
|
+
virtualTime?: boolean;
|
|
1502
|
+
delay?: Duration;
|
|
1503
|
+
timeout?: Duration;
|
|
1504
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
1505
|
+
fault?: number | string | Record<string, unknown>;
|
|
1506
|
+
snapshot?: string;
|
|
983
1507
|
};
|
|
984
1508
|
|
|
985
1509
|
/**
|
|
@@ -1019,6 +1543,8 @@ type RequestLog = {
|
|
|
1019
1543
|
unmatched: boolean;
|
|
1020
1544
|
/** Set when a fault rule produced the response. */
|
|
1021
1545
|
faultId?: string;
|
|
1546
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
1547
|
+
delayMs?: number;
|
|
1022
1548
|
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
1023
1549
|
ids?: Record<string, string>;
|
|
1024
1550
|
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
@@ -1064,6 +1590,12 @@ type JournalQuery = {
|
|
|
1064
1590
|
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
1065
1591
|
namespace?: string;
|
|
1066
1592
|
operationId?: string;
|
|
1593
|
+
/** Only this HTTP method, case-insensitive. */
|
|
1594
|
+
method?: string;
|
|
1595
|
+
/** Only this exact path. */
|
|
1596
|
+
path?: string;
|
|
1597
|
+
/** Only requests this fault rule answered or shaped. */
|
|
1598
|
+
faultId?: string;
|
|
1067
1599
|
status?: number;
|
|
1068
1600
|
/** Only entries at or after this instant (epoch ms). */
|
|
1069
1601
|
since?: number;
|
|
@@ -1078,6 +1610,50 @@ type Journal = {
|
|
|
1078
1610
|
clear(namespace?: string): void;
|
|
1079
1611
|
};
|
|
1080
1612
|
|
|
1613
|
+
/**
|
|
1614
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
1615
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
1616
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
1617
|
+
* fault hits.
|
|
1618
|
+
*/
|
|
1619
|
+
type LatencyRegistry = {
|
|
1620
|
+
get(namespace: string): LatencyState;
|
|
1621
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
1622
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
1623
|
+
clear(namespace?: string): void;
|
|
1624
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
1625
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
1626
|
+
};
|
|
1627
|
+
|
|
1628
|
+
/** Clock state as the admin API reports it. */
|
|
1629
|
+
type ControlClock = ClockState & {
|
|
1630
|
+
/** `now`, as ISO-8601. */
|
|
1631
|
+
iso: string;
|
|
1632
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
1633
|
+
autoAdvanceMs: number;
|
|
1634
|
+
};
|
|
1635
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
1636
|
+
type ControlState = {
|
|
1637
|
+
namespace: string;
|
|
1638
|
+
seed: number | string;
|
|
1639
|
+
clock: ControlClock;
|
|
1640
|
+
virtualTime: boolean;
|
|
1641
|
+
latency: LatencyState;
|
|
1642
|
+
faults: FaultListing[];
|
|
1643
|
+
};
|
|
1644
|
+
|
|
1645
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
1646
|
+
type SnapshotInfo = {
|
|
1647
|
+
id: string;
|
|
1648
|
+
namespace: string;
|
|
1649
|
+
/** Stored rows in the snapshot. */
|
|
1650
|
+
records: number;
|
|
1651
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
1652
|
+
at: number;
|
|
1653
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
1654
|
+
portable: boolean;
|
|
1655
|
+
};
|
|
1656
|
+
|
|
1081
1657
|
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
1082
1658
|
type CredentialRegistry = {
|
|
1083
1659
|
set(credential: string, namespace: string): void;
|
|
@@ -1090,6 +1666,8 @@ type CredentialRegistry = {
|
|
|
1090
1666
|
}[];
|
|
1091
1667
|
};
|
|
1092
1668
|
|
|
1669
|
+
type OwnedTransport = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
|
|
1670
|
+
|
|
1093
1671
|
/**
|
|
1094
1672
|
* A point-in-time copy of everything a service namespace holds.
|
|
1095
1673
|
*
|
|
@@ -1207,8 +1785,16 @@ type PublishInput = {
|
|
|
1207
1785
|
id?: string;
|
|
1208
1786
|
};
|
|
1209
1787
|
type WebhookHub = {
|
|
1788
|
+
observeDeliveries(listener: (event: {
|
|
1789
|
+
namespace: string;
|
|
1790
|
+
state: string;
|
|
1791
|
+
deliveryId: string;
|
|
1792
|
+
}) => void): () => void;
|
|
1793
|
+
setTransportPolicy(policy: OwnedTransport): void;
|
|
1210
1794
|
pending(namespace: string): number;
|
|
1211
1795
|
snapshot(namespace: string): unknown;
|
|
1796
|
+
exportSnapshot(handle: unknown): unknown;
|
|
1797
|
+
importSnapshot(namespace: string, document: unknown): unknown;
|
|
1212
1798
|
restore(namespace: string, snapshot: unknown): void;
|
|
1213
1799
|
publish(input: PublishInput): WebhookMessage;
|
|
1214
1800
|
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
@@ -1221,7 +1807,7 @@ type WebhookHub = {
|
|
|
1221
1807
|
/** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
|
|
1222
1808
|
flush(): Promise<void>;
|
|
1223
1809
|
/** Resolve once nothing is in flight. */
|
|
1224
|
-
idle(): Promise<void>;
|
|
1810
|
+
idle(namespace?: string): Promise<void>;
|
|
1225
1811
|
fault(namespace: string, fault: WebhookFault): void;
|
|
1226
1812
|
clear(namespace?: string): void;
|
|
1227
1813
|
};
|
|
@@ -1232,7 +1818,10 @@ type ServiceInstance = FetchAPI$1 & {
|
|
|
1232
1818
|
};
|
|
1233
1819
|
type ServiceTimelineState = Readonly<{
|
|
1234
1820
|
snapshot: NamespaceSnapshot;
|
|
1235
|
-
clock
|
|
1821
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
1822
|
+
clock: Readonly<ClockState & {
|
|
1823
|
+
pinned?: boolean;
|
|
1824
|
+
}>;
|
|
1236
1825
|
rngState: number;
|
|
1237
1826
|
}>;
|
|
1238
1827
|
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
@@ -1245,27 +1834,63 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI$1 & {
|
|
|
1245
1834
|
readonly sqlite: SqliteClient$1;
|
|
1246
1835
|
readonly clock: Clock;
|
|
1247
1836
|
readonly faults: FaultRegistry;
|
|
1837
|
+
/** Baseline response time per namespace. */
|
|
1838
|
+
readonly latency: LatencyRegistry;
|
|
1248
1839
|
readonly metrics: Metrics;
|
|
1249
1840
|
readonly journal: Journal;
|
|
1250
1841
|
readonly rng: Rng;
|
|
1251
1842
|
readonly credentials: CredentialRegistry;
|
|
1252
1843
|
/** The webhook hub, when the service has outbound webhooks. */
|
|
1253
1844
|
readonly webhooks: WebhookHub | undefined;
|
|
1254
|
-
/**
|
|
1255
|
-
|
|
1845
|
+
/**
|
|
1846
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
1847
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
1848
|
+
*/
|
|
1256
1849
|
namespaceClock(namespace?: string): Clock;
|
|
1257
1850
|
namespaceOf(request: Request): string;
|
|
1258
1851
|
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
1259
1852
|
fleetSnapshot(namespace: string): object;
|
|
1260
1853
|
fleetRestore(snapshot: unknown, namespace: string): void;
|
|
1854
|
+
fleetExport(snapshot: unknown): SnapshotDocument;
|
|
1855
|
+
fleetImport(namespace: string, document: unknown): object;
|
|
1261
1856
|
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
1262
1857
|
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
|
|
1263
1858
|
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
1264
1859
|
instance(namespace?: string): T;
|
|
1265
1860
|
/** Public names of every namespace created so far. */
|
|
1266
1861
|
namespaces(): string[];
|
|
1267
|
-
/**
|
|
1862
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
1268
1863
|
reset(namespace?: string): Promise<void>;
|
|
1864
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
1865
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
1866
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
1867
|
+
controls(namespace?: string): ControlState & {
|
|
1868
|
+
snapshots: SnapshotInfo[];
|
|
1869
|
+
};
|
|
1870
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
1871
|
+
configure(controls: NamespaceControls, namespace?: string): ControlState & {
|
|
1872
|
+
snapshots: SnapshotInfo[];
|
|
1873
|
+
};
|
|
1874
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
1875
|
+
readonly snapshots: {
|
|
1876
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
1877
|
+
create(options?: {
|
|
1878
|
+
name?: string;
|
|
1879
|
+
namespace?: string;
|
|
1880
|
+
}): SnapshotInfo;
|
|
1881
|
+
list(namespace?: string): SnapshotInfo[];
|
|
1882
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
1883
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
1884
|
+
/** Store a document as a snapshot without restoring it. */
|
|
1885
|
+
import(document: SnapshotDocument, options?: {
|
|
1886
|
+
name?: string;
|
|
1887
|
+
namespace?: string;
|
|
1888
|
+
}): SnapshotInfo;
|
|
1889
|
+
restore(id: string, namespace?: string): void;
|
|
1890
|
+
delete(id: string, namespace?: string): boolean;
|
|
1891
|
+
};
|
|
1892
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
1893
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
1269
1894
|
snapshot(namespace?: string): NamespaceSnapshot;
|
|
1270
1895
|
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
1271
1896
|
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
@@ -1404,6 +2029,8 @@ type WorkOSRuntimeOptions = Omit<WorkOSAPIOptions, "namespace" | "now"> & {
|
|
|
1404
2029
|
seed?: string | number;
|
|
1405
2030
|
adminPrefix?: string;
|
|
1406
2031
|
adminKey?: string;
|
|
2032
|
+
/** Admin controls to boot with: clock, latency, faults, seed, a starting snapshot. */
|
|
2033
|
+
adminControls?: MockControls;
|
|
1407
2034
|
onLog?: (entry: RequestLog) => void;
|
|
1408
2035
|
};
|
|
1409
2036
|
type WorkOSRuntime = ServiceRuntime<WorkOSAPI>;
|