@crvouga/mockingbird-runtime 0.0.0-reserved → 0.0.1
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/README.md +130 -1
- package/dist/adapter-node.d.ts +2521 -0
- package/dist/adapter-node.js +6920 -0
- package/dist/adapter-node.js.map +7 -0
- package/dist/admin-ui.js +25 -0
- package/dist/admin-ui.js.map +7 -0
- package/dist/chunk-2KDYLZXC.js +118 -0
- package/dist/chunk-2KDYLZXC.js.map +7 -0
- package/dist/chunk-2Y25NPCV.js +1289 -0
- package/dist/chunk-2Y25NPCV.js.map +7 -0
- package/dist/chunk-4SAK6MQT.js +246 -0
- package/dist/chunk-4SAK6MQT.js.map +7 -0
- package/dist/chunk-ANCCZFF2.js +778 -0
- package/dist/chunk-ANCCZFF2.js.map +7 -0
- package/dist/chunk-EBGYPQPA.js +198 -0
- package/dist/chunk-EBGYPQPA.js.map +7 -0
- package/dist/chunk-EQLY2YOA.js +274 -0
- package/dist/chunk-EQLY2YOA.js.map +7 -0
- package/dist/chunk-G62YDIIJ.js +7664 -0
- package/dist/chunk-G62YDIIJ.js.map +7 -0
- package/dist/chunk-HU6IOAFC.js +437 -0
- package/dist/chunk-HU6IOAFC.js.map +7 -0
- package/dist/chunk-RNAHPNUJ.js +97 -0
- package/dist/chunk-RNAHPNUJ.js.map +7 -0
- package/dist/chunk-UP4QOSFD.js +87 -0
- package/dist/chunk-UP4QOSFD.js.map +7 -0
- package/dist/chunk-XNS34BPF.js +2489 -0
- package/dist/chunk-XNS34BPF.js.map +7 -0
- package/dist/core.d.ts +356 -0
- package/dist/core.js +51 -0
- package/dist/core.js.map +7 -0
- package/dist/crypto-XS5XCMRJ.js +21 -0
- package/dist/crypto-XS5XCMRJ.js.map +7 -0
- package/dist/helpers-UD7CG4WQ.js +18 -0
- package/dist/helpers-UD7CG4WQ.js.map +7 -0
- package/dist/http-codec.d.ts +72 -0
- package/dist/http-codec.js +25 -0
- package/dist/http-codec.js.map +7 -0
- package/dist/http-provider.d.ts +3243 -0
- package/dist/http-provider.js +37920 -0
- package/dist/http-provider.js.map +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +7 -0
- package/dist/mqtt-broker/node.d.ts +632 -0
- package/dist/mqtt-broker/node.js +355 -0
- package/dist/mqtt-broker/node.js.map +7 -0
- package/dist/mqtt-broker.d.ts +990 -0
- package/dist/mqtt-broker.js +913 -0
- package/dist/mqtt-broker.js.map +7 -0
- package/dist/openapi-metadata.d.ts +290 -0
- package/dist/openapi-metadata.js +28 -0
- package/dist/openapi-metadata.js.map +7 -0
- package/dist/openapi.d.ts +252 -0
- package/dist/openapi.js +45 -0
- package/dist/openapi.js.map +7 -0
- package/dist/preview/artifacts.d.ts +19 -0
- package/dist/preview/artifacts.js +40 -0
- package/dist/preview/artifacts.js.map +7 -0
- package/dist/preview/bridge.d.ts +21 -0
- package/dist/preview/bridge.js +9 -0
- package/dist/preview/bridge.js.map +7 -0
- package/dist/preview/harness.d.ts +12 -0
- package/dist/preview/harness.js +65 -0
- package/dist/preview/harness.js.map +7 -0
- package/dist/preview/host.d.ts +30 -0
- package/dist/preview/host.js +70 -0
- package/dist/preview/host.js.map +7 -0
- package/dist/service/admin.d.ts +106 -0
- package/dist/service/admin.js +340 -0
- package/dist/service/admin.js.map +7 -0
- package/dist/service.d.ts +2665 -0
- package/dist/service.js +264 -0
- package/dist/service.js.map +7 -0
- package/dist/sqlite.d.ts +69 -0
- package/dist/sqlite.js +19 -0
- package/dist/sqlite.js.map +7 -0
- package/dist/ui.d.ts +37 -0
- package/dist/ui.js +702 -0
- package/dist/ui.js.map +7 -0
- package/package.json +130 -6
|
@@ -0,0 +1,2665 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
|
|
3
|
+
/** Trusted mount metadata stays on the Request, never in a spoofable HTTP header. */
|
|
4
|
+
declare const requestBaseUrl: (request: Request) => string;
|
|
5
|
+
type MountedAPI = FetchAPI & {
|
|
6
|
+
readonly prefix: string;
|
|
7
|
+
GET: FetchHandler;
|
|
8
|
+
POST: FetchHandler;
|
|
9
|
+
PUT: FetchHandler;
|
|
10
|
+
PATCH: FetchHandler;
|
|
11
|
+
DELETE: FetchHandler;
|
|
12
|
+
HEAD: FetchHandler;
|
|
13
|
+
OPTIONS: FetchHandler;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** A stable identifier for a point in a {@link Timeline}. */
|
|
17
|
+
type CheckpointId = string;
|
|
18
|
+
/** An immutable node in a timeline's checkpoint DAG. */
|
|
19
|
+
type Checkpoint<T> = Readonly<{
|
|
20
|
+
id: CheckpointId;
|
|
21
|
+
branch: string;
|
|
22
|
+
parent: CheckpointId | null;
|
|
23
|
+
/** Logical time supplied by the timeline's injected clock. */
|
|
24
|
+
at: number;
|
|
25
|
+
value: T;
|
|
26
|
+
}>;
|
|
27
|
+
type TimelineOptions = {
|
|
28
|
+
/** Logical clock used to stamp checkpoints. Defaults to a deterministic counter. */
|
|
29
|
+
now?: () => number;
|
|
30
|
+
/** Maximum retained checkpoints. Branch heads are never collected. Default 1,000. */
|
|
31
|
+
maxCheckpoints?: number;
|
|
32
|
+
/** Customize deterministic checkpoint IDs. */
|
|
33
|
+
id?: (sequence: number) => CheckpointId;
|
|
34
|
+
};
|
|
35
|
+
type CommitOptions = {
|
|
36
|
+
branch?: string;
|
|
37
|
+
/** Parent checkpoint. Defaults to the selected branch's current head. */
|
|
38
|
+
parent?: CheckpointId | null;
|
|
39
|
+
};
|
|
40
|
+
type ForkOptions = {
|
|
41
|
+
/** Checkpoint to fork from. Defaults to the main branch's head. */
|
|
42
|
+
from?: CheckpointId;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Small, storage-agnostic checkpoint DAG shared by service runtimes. Its values may be immutable
|
|
46
|
+
* records, namespace images, or copy-on-write SQL engine snapshots.
|
|
47
|
+
*
|
|
48
|
+
* Values are retained by reference. Engines can therefore use persistent/COW snapshots while
|
|
49
|
+
* simpler services can use immutable values. IDs and GC order are deterministic, and all IO
|
|
50
|
+
* (the logical clock) is injected.
|
|
51
|
+
*/
|
|
52
|
+
declare class Timeline<T> {
|
|
53
|
+
readonly maxCheckpoints: number;
|
|
54
|
+
private readonly now;
|
|
55
|
+
private readonly makeId;
|
|
56
|
+
private readonly nodes;
|
|
57
|
+
private readonly heads;
|
|
58
|
+
/** Unreferenced nodes in the exact order they became collectible. */
|
|
59
|
+
private readonly evictable;
|
|
60
|
+
/** Branch heads plus explicit retainers. Absent means zero. */
|
|
61
|
+
private readonly references;
|
|
62
|
+
private readonly explicitPins;
|
|
63
|
+
private sequence;
|
|
64
|
+
constructor(options?: TimelineOptions);
|
|
65
|
+
/** Capture a new immutable value and move `branch` to it. */
|
|
66
|
+
commit(value: T, options?: CommitOptions): Checkpoint<T>;
|
|
67
|
+
/** Create a branch pointer without copying its checkpoint value. */
|
|
68
|
+
fork(branch: string, options?: ForkOptions): Checkpoint<T> | undefined;
|
|
69
|
+
/** Move a branch pointer to an existing checkpoint. */
|
|
70
|
+
checkout(branch: string, id: CheckpointId): Checkpoint<T>;
|
|
71
|
+
get(id: CheckpointId): Checkpoint<T>;
|
|
72
|
+
head(branch?: string): Checkpoint<T> | undefined;
|
|
73
|
+
hasBranch(branch: string): boolean;
|
|
74
|
+
branches(): Readonly<Record<string, CheckpointId>>;
|
|
75
|
+
checkpoints(): readonly Checkpoint<T>[];
|
|
76
|
+
/** Number of retained checkpoints without allocating an array. */
|
|
77
|
+
get size(): number;
|
|
78
|
+
/** Pin a checkpoint independently of branch heads (used by compatibility snapshot handles). */
|
|
79
|
+
retain(id: CheckpointId): Checkpoint<T>;
|
|
80
|
+
/** Release one explicit pin. Branch heads remain pinned until moved or deleted. */
|
|
81
|
+
release(id: CheckpointId): boolean;
|
|
82
|
+
deleteBranch(branch: string): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Deterministically discard oldest unpinned checkpoints. Collection is O(number removed):
|
|
85
|
+
* commits never scan pinned nodes or the retained history. Parents are metadata rather than a
|
|
86
|
+
* storage dependency, so a retained node remains usable after pruning.
|
|
87
|
+
*/
|
|
88
|
+
gc(max?: number): CheckpointId[];
|
|
89
|
+
private collect;
|
|
90
|
+
private moveHead;
|
|
91
|
+
private addReference;
|
|
92
|
+
private removeReference;
|
|
93
|
+
private assertBranch;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
98
|
+
*
|
|
99
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
100
|
+
* It is the only contract shared across the whole graph.
|
|
101
|
+
*/
|
|
102
|
+
interface FetchAPI {
|
|
103
|
+
fetch(request: Request): Promise<Response>;
|
|
104
|
+
}
|
|
105
|
+
/** A bare function form of {@link FetchAPI}, compatible with `Bun.serve`, workerd, and Deno. */
|
|
106
|
+
type FetchHandler = (request: Request) => Promise<Response>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The single source of time for a service.
|
|
110
|
+
*
|
|
111
|
+
* Every timestamp a mock writes reads from here, so a suite moves time instead of
|
|
112
|
+
* sleeping: appointment windows, result delays and expiries become reachable in
|
|
113
|
+
* milliseconds. A frozen clock also makes timestamps reproducible from a seed.
|
|
114
|
+
*/
|
|
115
|
+
type ClockState = {
|
|
116
|
+
/** Current epoch milliseconds. */
|
|
117
|
+
now: number;
|
|
118
|
+
/** True while time does not advance on its own. */
|
|
119
|
+
frozen: boolean;
|
|
120
|
+
/** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
|
|
121
|
+
offsetMs: number;
|
|
122
|
+
};
|
|
123
|
+
type Clock = {
|
|
124
|
+
now(): number;
|
|
125
|
+
/** Pin the clock to an exact instant, keeping it frozen if it already was. */
|
|
126
|
+
set(epochMs: number): void;
|
|
127
|
+
/** Move the clock forward, or back with a negative delta. */
|
|
128
|
+
advance(deltaMs: number): void;
|
|
129
|
+
/** Stop time at the current instant. */
|
|
130
|
+
freeze(): void;
|
|
131
|
+
/** Resume from the current instant. */
|
|
132
|
+
unfreeze(): void;
|
|
133
|
+
/** Drop back to the underlying source, live. */
|
|
134
|
+
reset(): void;
|
|
135
|
+
state(): ClockState;
|
|
136
|
+
};
|
|
137
|
+
/** A {@link Clock} over `source` (default `Date.now`), live and unfrozen. */
|
|
138
|
+
declare const createClock: (source?: () => number) => Clock;
|
|
139
|
+
/** A clock over a base clock, which can tell its own freeze from the base's. */
|
|
140
|
+
type DerivedClock = Clock & {
|
|
141
|
+
/** True while this clock itself is frozen, and so no longer follows the base. */
|
|
142
|
+
pinned(): boolean;
|
|
143
|
+
/** Put the clock at an instant: pinned there, or following the base from there. */
|
|
144
|
+
place(state: {
|
|
145
|
+
now: number;
|
|
146
|
+
pinned: boolean;
|
|
147
|
+
}): void;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* A clock that follows `base` — its time, its freezes, its jumps — plus an offset and a
|
|
151
|
+
* freeze of its own. Moving it never moves `base`, so a namespace can travel in time alone.
|
|
152
|
+
* It reports `frozen` while either clock is, and no offset while it stands still. `moved` is
|
|
153
|
+
* called after anything moves it, for whatever keeps a schedule by this clock.
|
|
154
|
+
*/
|
|
155
|
+
declare const deriveClock: (base: Clock, moved?: () => void) => DerivedClock;
|
|
156
|
+
/**
|
|
157
|
+
* Put a clock at an instant, frozen or running. Frozen first, so the instant is set exactly:
|
|
158
|
+
* setting a live clock and freezing it afterwards reads the source again and can land a tick late.
|
|
159
|
+
*/
|
|
160
|
+
declare const restoreClock: (clock: Clock, state: {
|
|
161
|
+
now: number;
|
|
162
|
+
frozen: boolean;
|
|
163
|
+
}) => void;
|
|
164
|
+
|
|
165
|
+
type UpstreamState = {
|
|
166
|
+
mode: "emulated" | "forward";
|
|
167
|
+
baseUrl: string;
|
|
168
|
+
};
|
|
169
|
+
type UpstreamOptions = {
|
|
170
|
+
fetch: (request: Request) => Response | Promise<Response>;
|
|
171
|
+
baseUrl: string;
|
|
172
|
+
mode?: "emulated" | "forward";
|
|
173
|
+
rewriteAuth?: (request: Request) => Request | Promise<Request>;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
|
|
178
|
+
* request a percentage fault hits — is reproducible from a seed.
|
|
179
|
+
*
|
|
180
|
+
* mulberry32: small, fast, and stable across runtimes, which matters more here
|
|
181
|
+
* than statistical quality.
|
|
182
|
+
*/
|
|
183
|
+
type Rng = {
|
|
184
|
+
/** Next value in `[0, 1)`. */
|
|
185
|
+
next(): number;
|
|
186
|
+
/** Next integer in `[min, max]`. */
|
|
187
|
+
int(min: number, max: number): number;
|
|
188
|
+
/** Restart the stream from its seed. */
|
|
189
|
+
reset(): void;
|
|
190
|
+
/** Serializable engine state used by deterministic checkpoints. */
|
|
191
|
+
state(): number;
|
|
192
|
+
/** Restore a state previously returned by {@link state}. */
|
|
193
|
+
setState(state: number): void;
|
|
194
|
+
/** Restart the stream from a new seed. `reset` then returns to this seed. */
|
|
195
|
+
reseed(seed: number | string): void;
|
|
196
|
+
/** The seed the stream currently starts from. */
|
|
197
|
+
readonly seed: number;
|
|
198
|
+
};
|
|
199
|
+
/** Hash an arbitrary string into a 32-bit seed, so callers can seed by name. */
|
|
200
|
+
declare const seedFrom: (value: string) => number;
|
|
201
|
+
declare const createRng: (seed?: number | string) => Rng;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* A deliberate failure injected in front of an operation.
|
|
205
|
+
*
|
|
206
|
+
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
207
|
+
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
208
|
+
* appears under load, the 5xx that proves a retry path works.
|
|
209
|
+
*
|
|
210
|
+
* A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
|
|
211
|
+
* it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
|
|
212
|
+
* and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
|
|
213
|
+
*/
|
|
214
|
+
type FaultRule = {
|
|
215
|
+
/** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
|
|
216
|
+
id: string;
|
|
217
|
+
/** Fault only this operation. Omit to match every operation. */
|
|
218
|
+
operationId?: string;
|
|
219
|
+
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
220
|
+
method?: string;
|
|
221
|
+
/** Fault only this exact path. */
|
|
222
|
+
path?: string;
|
|
223
|
+
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
224
|
+
pathPrefix?: string;
|
|
225
|
+
/** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
|
|
226
|
+
pathPattern?: string;
|
|
227
|
+
/** Fault only requests carrying these headers. `"*"` matches any value. */
|
|
228
|
+
requestHeaders?: Record<string, string>;
|
|
229
|
+
/** Fault only requests carrying these query parameters. `"*"` matches any value. */
|
|
230
|
+
query?: Record<string, string>;
|
|
231
|
+
/**
|
|
232
|
+
* Fault only requests whose JSON body carries these fields. Each key is a dotted path into
|
|
233
|
+
* the parsed body (`"patient_details.email"`; a number steps into an array, `"items.0.sku"`)
|
|
234
|
+
* and each value is what the field must equal: a string, number, boolean or `null`, compared
|
|
235
|
+
* strictly (`3` is not `"3"`). `"*"` matches any value that is present. A request whose body
|
|
236
|
+
* is missing, is not JSON, or is not a JSON object never matches. Not `body`, which is the
|
|
237
|
+
* response a rule injects.
|
|
238
|
+
*/
|
|
239
|
+
requestBody?: Record<string, RequestBodyValue>;
|
|
240
|
+
/**
|
|
241
|
+
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
242
|
+
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
243
|
+
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
244
|
+
*/
|
|
245
|
+
namespace?: string;
|
|
246
|
+
/** Let this many matching requests through first. */
|
|
247
|
+
after?: number;
|
|
248
|
+
/** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
|
|
249
|
+
every?: number;
|
|
250
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
251
|
+
count?: number;
|
|
252
|
+
/** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
|
|
253
|
+
rate?: number;
|
|
254
|
+
/**
|
|
255
|
+
* Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
|
|
256
|
+
* clock when the rule was added. Listed as epoch ms.
|
|
257
|
+
*/
|
|
258
|
+
from?: Instant | Duration;
|
|
259
|
+
/** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
|
|
260
|
+
until?: Instant | Duration;
|
|
261
|
+
/**
|
|
262
|
+
* Status of the injected response. Omit for a rule that only delays, times out, drops,
|
|
263
|
+
* truncates, or switches on an `effect`: the request then still reaches the service.
|
|
264
|
+
*/
|
|
265
|
+
status?: number;
|
|
266
|
+
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
267
|
+
body?: unknown;
|
|
268
|
+
/** Response headers. */
|
|
269
|
+
headers?: Record<string, string>;
|
|
270
|
+
/** Hold the response back this long. Listed as `delayMs`. */
|
|
271
|
+
delay?: Duration;
|
|
272
|
+
/** `delay`, in milliseconds. */
|
|
273
|
+
delayMs?: number;
|
|
274
|
+
/** Alias of `delayMs`. */
|
|
275
|
+
latencyMs?: number;
|
|
276
|
+
/** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
|
|
277
|
+
jitter?: Duration;
|
|
278
|
+
jitterMs?: number;
|
|
279
|
+
/**
|
|
280
|
+
* Never answer: hold the request this long, then drop the connection. Listed as
|
|
281
|
+
* `timeoutMs`. With virtual time the wait is spent on the mock clock.
|
|
282
|
+
*/
|
|
283
|
+
timeout?: Duration;
|
|
284
|
+
timeoutMs?: number;
|
|
285
|
+
/**
|
|
286
|
+
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
287
|
+
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
288
|
+
*/
|
|
289
|
+
drop?: boolean;
|
|
290
|
+
/**
|
|
291
|
+
* Cut the response body off after this many bytes: the status and headers arrive, then
|
|
292
|
+
* the connection dies mid-body.
|
|
293
|
+
*/
|
|
294
|
+
truncate?: number;
|
|
295
|
+
/**
|
|
296
|
+
* A named service behaviour to switch on for the matching request instead of (or
|
|
297
|
+
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
298
|
+
* read it with `faultEffects(request)`.
|
|
299
|
+
*/
|
|
300
|
+
effect?: string;
|
|
301
|
+
/** Parameters for `effect`. */
|
|
302
|
+
params?: Record<string, unknown>;
|
|
303
|
+
/** The preset this rule was expanded from, if any. */
|
|
304
|
+
fromPreset?: string;
|
|
305
|
+
};
|
|
306
|
+
/** What a field of the request body must equal in {@link FaultRule.requestBody}. */
|
|
307
|
+
type RequestBodyValue = string | number | boolean | null;
|
|
308
|
+
/** A fault that fired for one request. */
|
|
309
|
+
type FaultHit = {
|
|
310
|
+
id: string;
|
|
311
|
+
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
312
|
+
response?: Response;
|
|
313
|
+
drop?: boolean;
|
|
314
|
+
/** Set with `drop` when the rule held the request for its `timeout` first. */
|
|
315
|
+
timeout?: boolean;
|
|
316
|
+
/** Bytes of response body to let through before the connection dies. */
|
|
317
|
+
truncate?: number;
|
|
318
|
+
effect?: {
|
|
319
|
+
name: string;
|
|
320
|
+
params: Record<string, unknown>;
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* A named, documented fault a suite switches on by name
|
|
325
|
+
* (`POST /__admin/faults {"preset": "rate_limited"}`): one or more rules, and optionally a
|
|
326
|
+
* webhook delivery fault.
|
|
327
|
+
*/
|
|
328
|
+
type FaultPreset = {
|
|
329
|
+
description: string;
|
|
330
|
+
rules?: Omit<FaultRule, "id">[];
|
|
331
|
+
webhook?: {
|
|
332
|
+
mode: "duplicate" | "reorder" | "drop";
|
|
333
|
+
count?: number;
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
/**
|
|
337
|
+
* Presets every mock answers to, so a suite uses one vocabulary across vendors. A service
|
|
338
|
+
* preset of the same name replaces the standard one with the vendor's own error body.
|
|
339
|
+
*/
|
|
340
|
+
declare const STANDARD_FAULT_PRESETS: Record<string, FaultPreset>;
|
|
341
|
+
/**
|
|
342
|
+
* Body of `GET /__admin/faults/presets`.
|
|
343
|
+
* `presets` is a list of named presets. A record keyed by name is not this type:
|
|
344
|
+
* the admin page maps the list.
|
|
345
|
+
*/
|
|
346
|
+
type FaultPresetList = {
|
|
347
|
+
presets: readonly {
|
|
348
|
+
name: string;
|
|
349
|
+
}[];
|
|
350
|
+
};
|
|
351
|
+
/** What a request looks like to the fault matcher. */
|
|
352
|
+
type FaultCandidate = {
|
|
353
|
+
operationId: string | undefined;
|
|
354
|
+
method: string;
|
|
355
|
+
path: string;
|
|
356
|
+
namespace: string;
|
|
357
|
+
/** Request headers and query, for rules that match on them. */
|
|
358
|
+
headers?: Headers;
|
|
359
|
+
query?: URLSearchParams;
|
|
360
|
+
/**
|
|
361
|
+
* The request body parsed as JSON, read only when a rule matches on `requestBody`. Resolve
|
|
362
|
+
* `undefined` for a body that is missing or not JSON. It must leave the request readable.
|
|
363
|
+
*/
|
|
364
|
+
readJson?: () => Promise<unknown>;
|
|
365
|
+
/** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
|
|
366
|
+
now?: number;
|
|
367
|
+
/** How to spend a delay for this request. Default: the registry's own sleep. */
|
|
368
|
+
wait?: (ms: number) => Promise<void>;
|
|
369
|
+
};
|
|
370
|
+
/** A stored rule and how often it was consulted. */
|
|
371
|
+
type FaultListing = FaultRule & {
|
|
372
|
+
/** Faults left before the rule retires. `null` when it has no `count`. */
|
|
373
|
+
remaining: number | null;
|
|
374
|
+
/** Times the rule faulted a request. */
|
|
375
|
+
hits: number;
|
|
376
|
+
/** Requests that matched the rule, faulted or not. */
|
|
377
|
+
seen: number;
|
|
378
|
+
};
|
|
379
|
+
/** A namespace's own rules, with their counters, in the order they are tried. */
|
|
380
|
+
type FaultSnapshot = {
|
|
381
|
+
/**
|
|
382
|
+
* `position` is how many `"*"` rules are tried before the rule, which is what its place
|
|
383
|
+
* in the order means to a request and holds in any process.
|
|
384
|
+
*/
|
|
385
|
+
rules: (FaultListing & {
|
|
386
|
+
position?: number;
|
|
387
|
+
})[];
|
|
388
|
+
/** How many `fault_<n>` ids the namespace has handed out. */
|
|
389
|
+
nextId?: number;
|
|
390
|
+
};
|
|
391
|
+
type FaultRegistry = {
|
|
392
|
+
snapshot(namespace: string): FaultSnapshot;
|
|
393
|
+
/** Add a rule, or replace the rule with the same id in the same namespace. */
|
|
394
|
+
add(rule: FaultRule): FaultRule;
|
|
395
|
+
/** The next unused `fault_<n>` id in `namespace`. */
|
|
396
|
+
nextId(namespace: string): string;
|
|
397
|
+
/** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
|
|
398
|
+
list(namespace?: string): FaultListing[];
|
|
399
|
+
get(id: string, namespace?: string): FaultListing | undefined;
|
|
400
|
+
/** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
|
|
401
|
+
remove(id: string, namespace?: string): boolean;
|
|
402
|
+
/** Remove every rule, or one namespace's own rules. */
|
|
403
|
+
clear(namespace?: string): void;
|
|
404
|
+
/** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
|
|
405
|
+
restore(namespace: string, state: FaultSnapshot): void;
|
|
406
|
+
/**
|
|
407
|
+
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
408
|
+
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
409
|
+
* each matching rule's remaining uses.
|
|
410
|
+
*/
|
|
411
|
+
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
412
|
+
/**
|
|
413
|
+
* Fire these rules at one request without storing them: no matching, no schedule, in order,
|
|
414
|
+
* stopping at the first that answers or drops. Jitter draws from the namespace's stream.
|
|
415
|
+
*/
|
|
416
|
+
fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
417
|
+
};
|
|
418
|
+
/**
|
|
419
|
+
* Check a fault rule and put it in its stored form: durations in milliseconds
|
|
420
|
+
* (`delayMs`, `jitterMs`, `timeoutMs`), windows as epoch ms. Throws {@link ControlError}.
|
|
421
|
+
*
|
|
422
|
+
* `strict` rejects unknown keys, so a typo in a request body is an error and not a rule
|
|
423
|
+
* that silently never matches.
|
|
424
|
+
*/
|
|
425
|
+
declare const normalizeFault: (input: unknown, context?: {
|
|
426
|
+
id?: string;
|
|
427
|
+
namespace?: string;
|
|
428
|
+
now?: number;
|
|
429
|
+
strict?: boolean;
|
|
430
|
+
}) => FaultRule;
|
|
431
|
+
/**
|
|
432
|
+
* Rules are matched in the order they were added, so a narrow rule added first
|
|
433
|
+
* wins over a later catch-all. `rate` and `jitter` draw from a seeded stream per namespace,
|
|
434
|
+
* so a partial-failure run replays identically whatever other namespaces are doing.
|
|
435
|
+
*
|
|
436
|
+
* `random` is that stream: a function from namespace to stream, owned by the caller, which
|
|
437
|
+
* the registry draws from and never seeds or rewinds. A single stream is accepted too, for a
|
|
438
|
+
* registry used on its own: it serves `default`, and every other namespace gets a stream of
|
|
439
|
+
* its own from the same seed.
|
|
440
|
+
*/
|
|
441
|
+
declare const createFaultRegistry: (random?: Rng | ((namespace: string) => Rng), sleep?: (ms: number) => Promise<void>) => FaultRegistry;
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* The admin controls every mock shares, as one document.
|
|
445
|
+
*
|
|
446
|
+
* The same shape is accepted in three places, and means the same thing in each:
|
|
447
|
+
*
|
|
448
|
+
* - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
|
|
449
|
+
* `MOCKINGBIRD_ADMIN_CONTROLS`
|
|
450
|
+
* - over HTTP: `PUT /__admin/controls`
|
|
451
|
+
* - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
|
|
452
|
+
*
|
|
453
|
+
* Every control is deterministic: given the same seed, clock and requests, a mock answers
|
|
454
|
+
* the same bytes. Nothing here reads the wall clock or `Math.random`.
|
|
455
|
+
*/
|
|
456
|
+
/** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
|
|
457
|
+
type Duration = number | string;
|
|
458
|
+
/** Epoch milliseconds, or an ISO-8601 string. */
|
|
459
|
+
type Instant = number | string;
|
|
460
|
+
/** A control that cannot be applied: a bad value, an unknown key, a missing snapshot. */
|
|
461
|
+
declare class ControlError extends RangeError {
|
|
462
|
+
constructor(message: string);
|
|
463
|
+
}
|
|
464
|
+
/** Milliseconds from a number, a numeric string, or a duration like `"90s"`, `"15m"`, `"2h"`. */
|
|
465
|
+
declare const parseDuration: (value: unknown) => number | undefined;
|
|
466
|
+
/** Epoch milliseconds from a number, a string of digits, or an ISO-8601 string. */
|
|
467
|
+
declare const parseInstant: (value: unknown) => number | undefined;
|
|
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
|
+
upstream?: UpstreamState;
|
|
503
|
+
format: typeof SNAPSHOT_FORMAT;
|
|
504
|
+
/** The service that wrote it. A snapshot is rejected by any other service. */
|
|
505
|
+
service?: string;
|
|
506
|
+
/**
|
|
507
|
+
* Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
|
|
508
|
+
* the stored text of a row that is not a plain record, and replaces `value` when present.
|
|
509
|
+
*/
|
|
510
|
+
records: {
|
|
511
|
+
collection: string;
|
|
512
|
+
id: string;
|
|
513
|
+
seq?: number;
|
|
514
|
+
value?: unknown;
|
|
515
|
+
raw?: string;
|
|
516
|
+
}[];
|
|
517
|
+
/** Id counters, so the next generated id continues where the snapshot left off. */
|
|
518
|
+
sequences?: {
|
|
519
|
+
name: string;
|
|
520
|
+
kind: string;
|
|
521
|
+
value: number;
|
|
522
|
+
}[];
|
|
523
|
+
clock?: {
|
|
524
|
+
now: number;
|
|
525
|
+
frozen: boolean;
|
|
526
|
+
autoAdvanceMs?: number;
|
|
527
|
+
};
|
|
528
|
+
seed?: number | string;
|
|
529
|
+
/** Positions of the seeded random streams. */
|
|
530
|
+
rng?: {
|
|
531
|
+
state: number;
|
|
532
|
+
faults?: number;
|
|
533
|
+
latency?: number;
|
|
534
|
+
};
|
|
535
|
+
faults?: unknown;
|
|
536
|
+
latency?: unknown;
|
|
537
|
+
virtualTime?: boolean;
|
|
538
|
+
/** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
|
|
539
|
+
webhooks?: unknown;
|
|
540
|
+
};
|
|
541
|
+
/** Controls for one namespace. */
|
|
542
|
+
type NamespaceControls$1 = {
|
|
543
|
+
/** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
|
|
544
|
+
seed?: number | string;
|
|
545
|
+
clock?: ClockControls;
|
|
546
|
+
/**
|
|
547
|
+
* Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
|
|
548
|
+
* wall clock: the request returns at once, the clock has moved, and the journal records
|
|
549
|
+
* the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
|
|
550
|
+
*/
|
|
551
|
+
virtualTime?: boolean;
|
|
552
|
+
/** Baseline response time for every vendor request. A bare duration is its `delay`. */
|
|
553
|
+
latency?: Duration | LatencyControls;
|
|
554
|
+
faults?: readonly FaultInput[];
|
|
555
|
+
/** Start from this state instead of the service's own seed data. */
|
|
556
|
+
snapshot?: SnapshotDocument;
|
|
557
|
+
};
|
|
558
|
+
/** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
|
|
559
|
+
type MockControls = NamespaceControls$1 & {
|
|
560
|
+
/** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
|
|
561
|
+
credentials?: Record<string, string>;
|
|
562
|
+
/** Overrides for named namespaces. A key given here replaces the shared value. */
|
|
563
|
+
namespaces?: Record<string, NamespaceControls$1>;
|
|
564
|
+
};
|
|
565
|
+
/** A latency rule in milliseconds. */
|
|
566
|
+
type LatencyRule = {
|
|
567
|
+
delayMs: number;
|
|
568
|
+
jitterMs: number;
|
|
569
|
+
};
|
|
570
|
+
/** Baseline latency for a namespace, normalized. */
|
|
571
|
+
type LatencyState = LatencyRule & {
|
|
572
|
+
operations: Record<string, LatencyRule>;
|
|
573
|
+
};
|
|
574
|
+
/** A clock control, normalized. Absent keys leave that part of the clock alone. */
|
|
575
|
+
type ClockPlan = {
|
|
576
|
+
now?: number;
|
|
577
|
+
frozen?: boolean;
|
|
578
|
+
autoAdvanceMs?: number;
|
|
579
|
+
};
|
|
580
|
+
/** {@link NamespaceControls} with every value checked and every duration in milliseconds. */
|
|
581
|
+
type ControlPlan = {
|
|
582
|
+
seed?: number | string;
|
|
583
|
+
clock?: ClockPlan;
|
|
584
|
+
virtualTime?: boolean;
|
|
585
|
+
latency?: LatencyState;
|
|
586
|
+
faults?: readonly FaultInput[];
|
|
587
|
+
snapshot?: SnapshotDocument;
|
|
588
|
+
};
|
|
589
|
+
/** Check a snapshot document's shape. The runtime checks `service` when it restores one. */
|
|
590
|
+
declare const assertSnapshotDocument: (value: unknown, label?: string) => SnapshotDocument;
|
|
591
|
+
/** Validate one namespace's controls. Fault rules are checked when they are added. */
|
|
592
|
+
declare const normalizeControls: (value: unknown, label?: string) => ControlPlan;
|
|
593
|
+
/** Boot controls, checked once when the runtime is created. */
|
|
594
|
+
type BootPlan = {
|
|
595
|
+
shared: ControlPlan;
|
|
596
|
+
namespaces: Record<string, ControlPlan>;
|
|
597
|
+
credentials: Record<string, string>;
|
|
598
|
+
};
|
|
599
|
+
/** Validate a boot {@link MockControls} document. */
|
|
600
|
+
declare const normalizeBootControls: (value: unknown, label?: string) => BootPlan;
|
|
601
|
+
/**
|
|
602
|
+
* Every header a request can carry a control in. Selection headers pick what a request
|
|
603
|
+
* reads; the rest act exactly like the matching admin call made just before the request.
|
|
604
|
+
*/
|
|
605
|
+
declare const CONTROL_HEADERS: {
|
|
606
|
+
readonly namespace: "x-mockingbird-namespace";
|
|
607
|
+
readonly adminKey: "x-mockingbird-admin-key";
|
|
608
|
+
readonly branch: "x-mockingbird-branch";
|
|
609
|
+
readonly at: "x-mockingbird-at";
|
|
610
|
+
readonly reset: "x-mockingbird-reset";
|
|
611
|
+
readonly upstream: "x-mockingbird-upstream";
|
|
612
|
+
readonly restore: "x-mockingbird-restore";
|
|
613
|
+
readonly seed: "x-mockingbird-seed";
|
|
614
|
+
readonly time: "x-mockingbird-time";
|
|
615
|
+
readonly advance: "x-mockingbird-advance";
|
|
616
|
+
readonly virtualTime: "x-mockingbird-virtual-time";
|
|
617
|
+
readonly fault: "x-mockingbird-fault";
|
|
618
|
+
readonly delay: "x-mockingbird-delay";
|
|
619
|
+
readonly timeout: "x-mockingbird-timeout";
|
|
620
|
+
readonly snapshot: "x-mockingbird-snapshot";
|
|
621
|
+
};
|
|
622
|
+
type ControlHeaderName = keyof typeof CONTROL_HEADERS;
|
|
623
|
+
/**
|
|
624
|
+
* What each control header does, in the order the runtime applies them. This list is the
|
|
625
|
+
* source for `GET /__admin` and is checked against the guide in `docs/ADMIN_CONTROLS.md`.
|
|
626
|
+
*/
|
|
627
|
+
declare const CONTROL_HEADER_REFERENCE: readonly {
|
|
628
|
+
header: string;
|
|
629
|
+
value: string;
|
|
630
|
+
/** `select` picks what is read; `namespace` changes it and persists; `request` is one request. */
|
|
631
|
+
scope: "select" | "namespace" | "request";
|
|
632
|
+
/** Needs `x-mockingbird-admin-key` when the mock has an admin key. */
|
|
633
|
+
guarded: boolean;
|
|
634
|
+
summary: string;
|
|
635
|
+
}[];
|
|
636
|
+
/** The controls one request carries in its headers, parsed and checked. */
|
|
637
|
+
type RequestControls = {
|
|
638
|
+
upstream?: "emulated" | "forward";
|
|
639
|
+
reset?: true;
|
|
640
|
+
restore?: string;
|
|
641
|
+
seed?: number | string;
|
|
642
|
+
time?: number;
|
|
643
|
+
advance?: number;
|
|
644
|
+
virtualTime?: boolean;
|
|
645
|
+
delay?: number;
|
|
646
|
+
timeout?: number;
|
|
647
|
+
fault?: Record<string, unknown>;
|
|
648
|
+
snapshot?: string;
|
|
649
|
+
};
|
|
650
|
+
/**
|
|
651
|
+
* Parse an `x-mockingbird-fault` value into a fault rule (or preset reference):
|
|
652
|
+
*
|
|
653
|
+
* - `503` — answer with that status
|
|
654
|
+
* - `drop` — drop the connection
|
|
655
|
+
* - `rate_limited` — a preset, by name
|
|
656
|
+
* - `status=503; delay=2s; header.retry-after=5` — `key=value` pairs
|
|
657
|
+
* - `{"status": 503, "body": {…}}` — a rule as JSON
|
|
658
|
+
*/
|
|
659
|
+
declare const parseFaultHeader: (value: string, header?: string) => Record<string, unknown>;
|
|
660
|
+
/** Read a request's control headers. `undefined` when it carries none. Throws {@link ControlError}. */
|
|
661
|
+
declare const parseControlHeaders: (headers: Headers) => RequestControls | undefined;
|
|
662
|
+
/** What {@link controlHeaders} turns into request headers. */
|
|
663
|
+
type ControlHeaderInput = {
|
|
664
|
+
upstream?: "emulated" | "forward";
|
|
665
|
+
namespace?: string;
|
|
666
|
+
adminKey?: string;
|
|
667
|
+
branch?: string;
|
|
668
|
+
at?: string;
|
|
669
|
+
reset?: boolean;
|
|
670
|
+
restore?: string;
|
|
671
|
+
seed?: number | string;
|
|
672
|
+
time?: Instant | Date;
|
|
673
|
+
advance?: Duration;
|
|
674
|
+
virtualTime?: boolean;
|
|
675
|
+
delay?: Duration;
|
|
676
|
+
timeout?: Duration;
|
|
677
|
+
/** A status, `"drop"`, a preset name, or a fault rule. */
|
|
678
|
+
fault?: number | string | Record<string, unknown>;
|
|
679
|
+
snapshot?: string;
|
|
680
|
+
};
|
|
681
|
+
/**
|
|
682
|
+
* Build the request headers for a set of controls, to spread into a `fetch` or hand to an
|
|
683
|
+
* SDK's default headers. Values are checked, so a typo fails here and not as a `400` later.
|
|
684
|
+
*/
|
|
685
|
+
declare const controlHeaders: (input: ControlHeaderInput) => Record<string, string>;
|
|
686
|
+
|
|
687
|
+
type DecodedBody = {
|
|
688
|
+
kind: "empty";
|
|
689
|
+
} | {
|
|
690
|
+
kind: "json";
|
|
691
|
+
value: unknown;
|
|
692
|
+
} | {
|
|
693
|
+
kind: "form";
|
|
694
|
+
value: Record<string, unknown>;
|
|
695
|
+
} | {
|
|
696
|
+
kind: "text";
|
|
697
|
+
value: string;
|
|
698
|
+
} | {
|
|
699
|
+
kind: "bytes";
|
|
700
|
+
value: Uint8Array;
|
|
701
|
+
} | {
|
|
702
|
+
kind: "invalid";
|
|
703
|
+
mediaType: string;
|
|
704
|
+
text: string;
|
|
705
|
+
error: string;
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Rails/PHP/Stripe-style bracket notation for `application/x-www-form-urlencoded` bodies and
|
|
710
|
+
* query strings:
|
|
711
|
+
*
|
|
712
|
+
* address[city]=Paris -> { address: { city: "Paris" } }
|
|
713
|
+
* items[0][name]=a -> { items: [{ name: "a" }] }
|
|
714
|
+
* tags[]=x&tags[]=y -> { tags: ["x", "y"] }
|
|
715
|
+
* metadata[k]=v -> { metadata: { k: "v" } }
|
|
716
|
+
*
|
|
717
|
+
* Decoding yields only strings, arrays and plain objects — coercion is the caller's concern,
|
|
718
|
+
* exactly like a real HTTP server.
|
|
719
|
+
*/
|
|
720
|
+
type FormValue = string | FormValue[] | {
|
|
721
|
+
[key: string]: FormValue;
|
|
722
|
+
};
|
|
723
|
+
type FormObject = {
|
|
724
|
+
[key: string]: FormValue;
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* The slice of OpenAPI 3.1 (and JSON Schema 2020-12) Mockingbird understands.
|
|
729
|
+
* Unknown keys (including `x-*` extensions) are preserved on every object.
|
|
730
|
+
*/
|
|
731
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
732
|
+
type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
733
|
+
[key: string]: JsonValue;
|
|
734
|
+
};
|
|
735
|
+
type ReferenceObject = {
|
|
736
|
+
$ref: string;
|
|
737
|
+
description?: string;
|
|
738
|
+
summary?: string;
|
|
739
|
+
};
|
|
740
|
+
type SchemaType = "string" | "number" | "integer" | "boolean" | "object" | "array" | "null";
|
|
741
|
+
type SchemaObject = {
|
|
742
|
+
$ref?: string;
|
|
743
|
+
type?: SchemaType | SchemaType[];
|
|
744
|
+
title?: string;
|
|
745
|
+
description?: string;
|
|
746
|
+
format?: string;
|
|
747
|
+
enum?: JsonValue[];
|
|
748
|
+
const?: JsonValue;
|
|
749
|
+
default?: JsonValue;
|
|
750
|
+
example?: JsonValue;
|
|
751
|
+
examples?: JsonValue[];
|
|
752
|
+
nullable?: boolean;
|
|
753
|
+
deprecated?: boolean;
|
|
754
|
+
readOnly?: boolean;
|
|
755
|
+
writeOnly?: boolean;
|
|
756
|
+
minimum?: number;
|
|
757
|
+
maximum?: number;
|
|
758
|
+
exclusiveMinimum?: number;
|
|
759
|
+
exclusiveMaximum?: number;
|
|
760
|
+
multipleOf?: number;
|
|
761
|
+
minLength?: number;
|
|
762
|
+
maxLength?: number;
|
|
763
|
+
pattern?: string;
|
|
764
|
+
minItems?: number;
|
|
765
|
+
maxItems?: number;
|
|
766
|
+
uniqueItems?: boolean;
|
|
767
|
+
items?: SchemaObject;
|
|
768
|
+
prefixItems?: SchemaObject[];
|
|
769
|
+
minProperties?: number;
|
|
770
|
+
maxProperties?: number;
|
|
771
|
+
required?: string[];
|
|
772
|
+
properties?: Record<string, SchemaObject>;
|
|
773
|
+
additionalProperties?: boolean | SchemaObject;
|
|
774
|
+
propertyNames?: SchemaObject;
|
|
775
|
+
oneOf?: SchemaObject[];
|
|
776
|
+
anyOf?: SchemaObject[];
|
|
777
|
+
allOf?: SchemaObject[];
|
|
778
|
+
not?: SchemaObject;
|
|
779
|
+
discriminator?: {
|
|
780
|
+
propertyName: string;
|
|
781
|
+
mapping?: Record<string, string>;
|
|
782
|
+
};
|
|
783
|
+
[extension: `x-${string}`]: unknown;
|
|
784
|
+
};
|
|
785
|
+
type ParameterLocation = "path" | "query" | "header" | "cookie";
|
|
786
|
+
type ParameterObject = {
|
|
787
|
+
name: string;
|
|
788
|
+
in: ParameterLocation;
|
|
789
|
+
description?: string;
|
|
790
|
+
required?: boolean;
|
|
791
|
+
deprecated?: boolean;
|
|
792
|
+
style?: string;
|
|
793
|
+
explode?: boolean;
|
|
794
|
+
schema?: SchemaObject;
|
|
795
|
+
content?: Record<string, MediaTypeObject>;
|
|
796
|
+
example?: JsonValue;
|
|
797
|
+
[extension: `x-${string}`]: unknown;
|
|
798
|
+
};
|
|
799
|
+
type MediaTypeObject = {
|
|
800
|
+
schema?: SchemaObject;
|
|
801
|
+
example?: JsonValue;
|
|
802
|
+
examples?: Record<string, unknown>;
|
|
803
|
+
encoding?: Record<string, unknown>;
|
|
804
|
+
[extension: `x-${string}`]: unknown;
|
|
805
|
+
};
|
|
806
|
+
type RequestBodyObject = {
|
|
807
|
+
description?: string;
|
|
808
|
+
required?: boolean;
|
|
809
|
+
content: Record<string, MediaTypeObject>;
|
|
810
|
+
[extension: `x-${string}`]: unknown;
|
|
811
|
+
};
|
|
812
|
+
type HeaderObject = {
|
|
813
|
+
description?: string;
|
|
814
|
+
required?: boolean;
|
|
815
|
+
schema?: SchemaObject;
|
|
816
|
+
[extension: `x-${string}`]: unknown;
|
|
817
|
+
};
|
|
818
|
+
type ResponseObject = {
|
|
819
|
+
description: string;
|
|
820
|
+
headers?: Record<string, HeaderObject | ReferenceObject>;
|
|
821
|
+
content?: Record<string, MediaTypeObject>;
|
|
822
|
+
[extension: `x-${string}`]: unknown;
|
|
823
|
+
};
|
|
824
|
+
type ResponsesObject = Record<string, ResponseObject | ReferenceObject>;
|
|
825
|
+
type SecurityRequirementObject = Record<string, string[]>;
|
|
826
|
+
type OperationObject = {
|
|
827
|
+
operationId?: string;
|
|
828
|
+
summary?: string;
|
|
829
|
+
description?: string;
|
|
830
|
+
tags?: string[];
|
|
831
|
+
deprecated?: boolean;
|
|
832
|
+
parameters?: Array<ParameterObject | ReferenceObject>;
|
|
833
|
+
requestBody?: RequestBodyObject | ReferenceObject;
|
|
834
|
+
responses: ResponsesObject;
|
|
835
|
+
security?: SecurityRequirementObject[];
|
|
836
|
+
[extension: `x-${string}`]: unknown;
|
|
837
|
+
};
|
|
838
|
+
declare const HTTP_METHODS: readonly ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
839
|
+
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
840
|
+
type PathItemObject = {
|
|
841
|
+
summary?: string;
|
|
842
|
+
description?: string;
|
|
843
|
+
parameters?: Array<ParameterObject | ReferenceObject>;
|
|
844
|
+
[extension: `x-${string}`]: unknown;
|
|
845
|
+
} & Partial<Record<HttpMethod, OperationObject>>;
|
|
846
|
+
type SecuritySchemeObject = {
|
|
847
|
+
type: "apiKey" | "http" | "oauth2" | "openIdConnect" | "mutualTLS";
|
|
848
|
+
description?: string;
|
|
849
|
+
name?: string;
|
|
850
|
+
in?: ParameterLocation;
|
|
851
|
+
scheme?: string;
|
|
852
|
+
bearerFormat?: string;
|
|
853
|
+
flows?: Record<string, unknown>;
|
|
854
|
+
openIdConnectUrl?: string;
|
|
855
|
+
[extension: `x-${string}`]: unknown;
|
|
856
|
+
};
|
|
857
|
+
type ComponentsObject = {
|
|
858
|
+
schemas?: Record<string, SchemaObject>;
|
|
859
|
+
responses?: Record<string, ResponseObject>;
|
|
860
|
+
parameters?: Record<string, ParameterObject>;
|
|
861
|
+
requestBodies?: Record<string, RequestBodyObject>;
|
|
862
|
+
headers?: Record<string, HeaderObject>;
|
|
863
|
+
securitySchemes?: Record<string, SecuritySchemeObject>;
|
|
864
|
+
[extension: `x-${string}`]: unknown;
|
|
865
|
+
};
|
|
866
|
+
type ServerObject = {
|
|
867
|
+
url: string;
|
|
868
|
+
description?: string;
|
|
869
|
+
variables?: Record<string, unknown>;
|
|
870
|
+
[extension: `x-${string}`]: unknown;
|
|
871
|
+
};
|
|
872
|
+
type InfoObject = {
|
|
873
|
+
title: string;
|
|
874
|
+
version: string;
|
|
875
|
+
description?: string;
|
|
876
|
+
[extension: `x-${string}`]: unknown;
|
|
877
|
+
};
|
|
878
|
+
type OpenAPIDocument = {
|
|
879
|
+
openapi: string;
|
|
880
|
+
info: InfoObject;
|
|
881
|
+
servers?: ServerObject[];
|
|
882
|
+
paths: Record<string, PathItemObject>;
|
|
883
|
+
components?: ComponentsObject;
|
|
884
|
+
security?: SecurityRequirementObject[];
|
|
885
|
+
tags?: Array<{
|
|
886
|
+
name: string;
|
|
887
|
+
description?: string;
|
|
888
|
+
}>;
|
|
889
|
+
[extension: `x-${string}`]: unknown;
|
|
890
|
+
};
|
|
891
|
+
/** One concrete HTTP operation discovered in a document. */
|
|
892
|
+
type Operation = {
|
|
893
|
+
operationId: string;
|
|
894
|
+
method: HttpMethod;
|
|
895
|
+
/** OpenAPI path template, e.g. `/v1/customers/{customer}`. */
|
|
896
|
+
path: string;
|
|
897
|
+
operation: OperationObject;
|
|
898
|
+
/** Path-level parameters merged with operation-level ones (operation wins), `$ref`s resolved. */
|
|
899
|
+
parameters: ParameterObject[];
|
|
900
|
+
requestBody: RequestBodyObject | undefined;
|
|
901
|
+
responses: Record<string, ResponseObject>;
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
905
|
+
type SqliteValue = null | number | bigint | string | Uint8Array | boolean;
|
|
906
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
907
|
+
type SqliteRunResult = {
|
|
908
|
+
changes: number;
|
|
909
|
+
lastInsertRowid: number | bigint;
|
|
910
|
+
};
|
|
911
|
+
/**
|
|
912
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
913
|
+
*
|
|
914
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
915
|
+
*/
|
|
916
|
+
interface SqliteStatement {
|
|
917
|
+
run(...params: SqliteValue[]): SqliteRunResult;
|
|
918
|
+
all<T = Record<string, unknown>>(...params: SqliteValue[]): T[];
|
|
919
|
+
get<T = Record<string, unknown>>(...params: SqliteValue[]): T | undefined;
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
923
|
+
*
|
|
924
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
925
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
926
|
+
*/
|
|
927
|
+
interface SqliteClient {
|
|
928
|
+
exec(sql: string): void;
|
|
929
|
+
prepare(sql: string): SqliteStatement;
|
|
930
|
+
transaction<T>(fn: () => T): T;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/** Options every provider constructor accepts. */
|
|
934
|
+
type APIOptions = {
|
|
935
|
+
/** Sync SQLite client. Defaults to `@crvouga/mockingbird-service-sqlite`. */
|
|
936
|
+
sqlite?: SqliteClient;
|
|
937
|
+
/** Clock used for `created`-style fields. Default `Date.now`. */
|
|
938
|
+
now?: () => number;
|
|
939
|
+
/**
|
|
940
|
+
* Storage namespace for this instance's records. Instances sharing one SQLite
|
|
941
|
+
* client stay isolated when their namespaces differ. Defaults to the service name.
|
|
942
|
+
*/
|
|
943
|
+
namespace?: string;
|
|
944
|
+
};
|
|
945
|
+
type OperationContext = {
|
|
946
|
+
request: Request;
|
|
947
|
+
url: URL;
|
|
948
|
+
/** Path parameters. */
|
|
949
|
+
params: Record<string, string>;
|
|
950
|
+
/** Query string decoded with bracket notation (`created[gte]=1` -> `{ created: { gte: "1" } }`). */
|
|
951
|
+
query: FormObject;
|
|
952
|
+
body: DecodedBody;
|
|
953
|
+
/** Shared SQLite client for this service (already migrated). */
|
|
954
|
+
sqlite: SqliteClient;
|
|
955
|
+
/** Service namespace used for records / sequences. */
|
|
956
|
+
namespace: string;
|
|
957
|
+
operation: Operation;
|
|
958
|
+
/** The vendor contract the service was built from. */
|
|
959
|
+
document: OpenAPIDocument;
|
|
960
|
+
now: () => number;
|
|
961
|
+
};
|
|
962
|
+
type OperationHandler = (context: OperationContext) => Promise<Response> | Response;
|
|
963
|
+
type OperationHandlers = Record<string, OperationHandler>;
|
|
964
|
+
/** Identity helper that keeps handler maps type-checked against a fixed set of operation ids. */
|
|
965
|
+
declare const defineOperations: <Id extends string>(handlers: Record<Id, OperationHandler>) => Record<Id, OperationHandler>;
|
|
966
|
+
declare class OperationRegistryError extends Error {
|
|
967
|
+
readonly problems: string[];
|
|
968
|
+
constructor(problems: string[]);
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Cross-check handlers against the document: every supported operation needs exactly one
|
|
972
|
+
* handler, no handler may target an unknown or unsupported operation, no duplicate ids.
|
|
973
|
+
*/
|
|
974
|
+
declare const verifyOperations: (document: OpenAPIDocument, handlers: OperationHandlers) => string[];
|
|
975
|
+
type ServiceOptions = {
|
|
976
|
+
document: OpenAPIDocument;
|
|
977
|
+
handlers: OperationHandlers;
|
|
978
|
+
sqlite: SqliteClient;
|
|
979
|
+
/** Namespace isolating this service's records and sequences. */
|
|
980
|
+
namespace: string;
|
|
981
|
+
now?: (() => number) | undefined;
|
|
982
|
+
/** Response for paths/methods outside the contract. */
|
|
983
|
+
notFound: (request: Request) => Response | Promise<Response>;
|
|
984
|
+
/** Response for operations declared but marked `supported: false`. Default: `notFound`. */
|
|
985
|
+
unsupported?: (request: Request, operation: Operation) => Response | Promise<Response>;
|
|
986
|
+
/** Convert handler exceptions into a provider-shaped response. */
|
|
987
|
+
onError: (error: unknown, request: Request) => Response | Promise<Response>;
|
|
988
|
+
/** Runs before every operation; return a Response to short-circuit (e.g. authentication). */
|
|
989
|
+
before?: (context: OperationContext) => Promise<Response | undefined> | Response | undefined;
|
|
990
|
+
};
|
|
991
|
+
type Service = FetchAPI & {
|
|
992
|
+
app: Hono;
|
|
993
|
+
sqlite: SqliteClient;
|
|
994
|
+
namespace: string;
|
|
995
|
+
/** Delete every record and sequence in the service namespace. */
|
|
996
|
+
reset(): Promise<void>;
|
|
997
|
+
};
|
|
998
|
+
/**
|
|
999
|
+
* Resolve optional `sqlite`, run core migrations, and return the ready client.
|
|
1000
|
+
* Call this from every provider constructor before building state.
|
|
1001
|
+
*/
|
|
1002
|
+
declare const bootSqlite: (sqlite?: SqliteClient) => SqliteClient;
|
|
1003
|
+
/** Build a Fetch-native service whose routes are exactly the document's operations. */
|
|
1004
|
+
declare const createService: (options: ServiceOptions) => Service;
|
|
1005
|
+
|
|
1006
|
+
/**
|
|
1007
|
+
* One problem with a request body, at a dotted path (`patient.address.city`, `items.0.sku`).
|
|
1008
|
+
* `kind` is `media_type` when the body is present but its media type is not one the operation
|
|
1009
|
+
* accepts (a `415` on most vendors), `syntax` when it could not be decoded, `required` when
|
|
1010
|
+
* the operation needs a body and none arrived, and `schema` for a contract violation.
|
|
1011
|
+
*/
|
|
1012
|
+
type BodyIssue = {
|
|
1013
|
+
path: string;
|
|
1014
|
+
message: string;
|
|
1015
|
+
kind?: "media_type" | "syntax" | "required" | "schema";
|
|
1016
|
+
};
|
|
1017
|
+
/** What the runtime records about a request the mock rejected: never the body itself. */
|
|
1018
|
+
type UnsupportedMediaType = {
|
|
1019
|
+
/** The request's media type (`text/plain`), or `null` when it sent no `content-type`. */
|
|
1020
|
+
mediaType: string | null;
|
|
1021
|
+
/** The media types the operation's contract accepts, as written (`application/*+json`). */
|
|
1022
|
+
accepted: string[];
|
|
1023
|
+
};
|
|
1024
|
+
/**
|
|
1025
|
+
* The reason a request body cannot be read by this operation: its `content-type` is missing
|
|
1026
|
+
* or names a media type the contract does not list. Undefined when the operation declares no
|
|
1027
|
+
* body, the media type is accepted, or the body is empty (unless `includeEmpty`: ASP.NET
|
|
1028
|
+
* Core picks the input formatter from the header before looking at the body, so even an
|
|
1029
|
+
* empty body with no `content-type` is 415 there). A JSON body sent as `text/plain` or with
|
|
1030
|
+
* no header is the common case: ASP.NET Core, Spring and Rails answer it `415`, and it must
|
|
1031
|
+
* never read as "request body is required".
|
|
1032
|
+
*/
|
|
1033
|
+
declare const unsupportedMediaType: (context: OperationContext, options?: {
|
|
1034
|
+
includeEmpty?: boolean;
|
|
1035
|
+
}) => UnsupportedMediaType | undefined;
|
|
1036
|
+
/** The issues `bodyIssues` last found for a request, if any. */
|
|
1037
|
+
declare const recordedIssues: (request: Request) => BodyIssue[] | undefined;
|
|
1038
|
+
/**
|
|
1039
|
+
* Validate the decoded body against the operation's `requestBody` schema in the vendor
|
|
1040
|
+
* contract, for the media type the request sent (or `contentType` when it sent none that the
|
|
1041
|
+
* contract lists). Returns `[]` when valid, or when the operation declares no such body.
|
|
1042
|
+
* Services turn the issues into the vendor's own validation error shape; a `media_type`
|
|
1043
|
+
* issue is a body the operation cannot read at all (see `unsupportedMediaType`).
|
|
1044
|
+
*/
|
|
1045
|
+
declare const bodyIssues: (context: OperationContext, contentType?: string) => BodyIssue[];
|
|
1046
|
+
/** Issues grouped Laravel-style: `{ "patient.email": ["…"] }`. */
|
|
1047
|
+
declare const issuesByField: (issues: BodyIssue[]) => Record<string, string[]>;
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* What the mock received from a request it rejected, so a 4xx can be attributed after the
|
|
1051
|
+
* fact (an empty body, a missing `content-type`, a schema violation) without logging the body.
|
|
1052
|
+
*/
|
|
1053
|
+
type RejectedRequest = {
|
|
1054
|
+
/** The raw `content-type` header, or `null` when the request sent none. */
|
|
1055
|
+
contentType: string | null;
|
|
1056
|
+
/** Bytes of body received from `content-length` (`0` for none), or `null` when the client sent no length (a streamed / chunked body). */
|
|
1057
|
+
bodyBytes: number | null;
|
|
1058
|
+
/** The raw `transfer-encoding` header (`chunked`), or `null`. */
|
|
1059
|
+
transferEncoding: string | null;
|
|
1060
|
+
};
|
|
1061
|
+
/** One handled request, as the structured log sees it. */
|
|
1062
|
+
type RequestLog = {
|
|
1063
|
+
upstream?: boolean;
|
|
1064
|
+
service: string;
|
|
1065
|
+
namespace: string;
|
|
1066
|
+
operationId: string | undefined;
|
|
1067
|
+
method: string;
|
|
1068
|
+
path: string;
|
|
1069
|
+
status: number;
|
|
1070
|
+
durationMs: number;
|
|
1071
|
+
/** True when the path matched no operation in the contract. */
|
|
1072
|
+
unmatched: boolean;
|
|
1073
|
+
/** Set when a fault rule produced the response. */
|
|
1074
|
+
faultId?: string;
|
|
1075
|
+
/** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
|
|
1076
|
+
delayMs?: number;
|
|
1077
|
+
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
1078
|
+
ids?: Record<string, string>;
|
|
1079
|
+
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
1080
|
+
adopted?: boolean;
|
|
1081
|
+
/** Explicit durable acceptance, independent of response delivery. */
|
|
1082
|
+
accepted?: boolean;
|
|
1083
|
+
/** Checkpoint captured at explicit acceptance. */
|
|
1084
|
+
checkpoint?: string;
|
|
1085
|
+
/** Set on a rejection (status ≥ 400) the mock produced itself, never on a scripted fault. */
|
|
1086
|
+
request?: RejectedRequest;
|
|
1087
|
+
/** The validation issues behind a rejection, when the service found any. */
|
|
1088
|
+
issues?: BodyIssue[];
|
|
1089
|
+
};
|
|
1090
|
+
type MetricsReport = {
|
|
1091
|
+
requests: number;
|
|
1092
|
+
/** Counts keyed `<operationId> <status>`. */
|
|
1093
|
+
byOperation: Record<string, number>;
|
|
1094
|
+
/**
|
|
1095
|
+
* Paths that matched no operation, most frequent first.
|
|
1096
|
+
*
|
|
1097
|
+
* This is the early-warning signal: a consumer calling something the mock does
|
|
1098
|
+
* not implement shows up here as a count, before it fails a suite as a 404.
|
|
1099
|
+
*/
|
|
1100
|
+
unmatched: {
|
|
1101
|
+
method: string;
|
|
1102
|
+
path: string;
|
|
1103
|
+
count: number;
|
|
1104
|
+
}[];
|
|
1105
|
+
faults: number;
|
|
1106
|
+
totalDurationMs: number;
|
|
1107
|
+
};
|
|
1108
|
+
type Metrics = {
|
|
1109
|
+
record(entry: RequestLog): void;
|
|
1110
|
+
report(namespace?: string): MetricsReport;
|
|
1111
|
+
reset(namespace?: string): void;
|
|
1112
|
+
};
|
|
1113
|
+
declare const createMetrics: (partition?: boolean) => Metrics;
|
|
1114
|
+
|
|
1115
|
+
/** One journal entry: a request log stamped with when (on the mock clock) it was handled. */
|
|
1116
|
+
type JournalEntry = RequestLog & {
|
|
1117
|
+
at: string;
|
|
1118
|
+
};
|
|
1119
|
+
type JournalQuery = {
|
|
1120
|
+
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
1121
|
+
namespace?: string;
|
|
1122
|
+
operationId?: string;
|
|
1123
|
+
/** Only this HTTP method, case-insensitive. */
|
|
1124
|
+
method?: string;
|
|
1125
|
+
/** Only this exact path. */
|
|
1126
|
+
path?: string;
|
|
1127
|
+
/** Only requests this fault rule answered or shaped. */
|
|
1128
|
+
faultId?: string;
|
|
1129
|
+
status?: number;
|
|
1130
|
+
/** Only entries at or after this instant (epoch ms). */
|
|
1131
|
+
since?: number;
|
|
1132
|
+
/** At most this many, the most recent kept. */
|
|
1133
|
+
limit?: number;
|
|
1134
|
+
};
|
|
1135
|
+
type Journal = {
|
|
1136
|
+
readonly size: number;
|
|
1137
|
+
record(entry: JournalEntry): void;
|
|
1138
|
+
list(query?: JournalQuery): JournalEntry[];
|
|
1139
|
+
/** Forget one namespace's entries, or every namespace's. */
|
|
1140
|
+
clear(namespace?: string): void;
|
|
1141
|
+
};
|
|
1142
|
+
/** Default number of entries each namespace keeps. */
|
|
1143
|
+
declare const DEFAULT_JOURNAL_SIZE = 1000;
|
|
1144
|
+
/**
|
|
1145
|
+
* The last `size` requests per namespace, in a ring buffer: what the mock actually saw,
|
|
1146
|
+
* so a test can prove a request arrived (or never did) without reading logs.
|
|
1147
|
+
*/
|
|
1148
|
+
declare const createJournal: (size?: number) => Journal;
|
|
1149
|
+
/** What a service knows about a request that the runtime cannot see from outside. */
|
|
1150
|
+
type ResponseNotes = {
|
|
1151
|
+
/** Node upgrade admission: actual wire status; the Fetch carrier remains 200. */
|
|
1152
|
+
wireStatus?: 101;
|
|
1153
|
+
/** Resource ids the handler touched, e.g. `{ userId, orderId }`. */
|
|
1154
|
+
ids?: Record<string, string>;
|
|
1155
|
+
/** Set when the handler created a resource the request referred to but did not exist. */
|
|
1156
|
+
adopted?: boolean;
|
|
1157
|
+
/** The body validation issues behind a rejection; the runtime logs them, never the body. */
|
|
1158
|
+
issues?: BodyIssue[];
|
|
1159
|
+
};
|
|
1160
|
+
/**
|
|
1161
|
+
* Attach notes to a response for the request journal and structured log. They travel
|
|
1162
|
+
* beside the response, never in it, so nothing the vendor would not send reaches a client.
|
|
1163
|
+
*/
|
|
1164
|
+
declare const annotateResponse: (response: Response, extra: ResponseNotes) => Response;
|
|
1165
|
+
declare const responseNotes: (response: Response) => ResponseNotes | undefined;
|
|
1166
|
+
|
|
1167
|
+
/**
|
|
1168
|
+
* Baseline response time, per namespace: how long every vendor request takes before any
|
|
1169
|
+
* fault is considered. Jitter draws from a stream the caller owns, one per namespace and
|
|
1170
|
+
* separate from the fault stream, so adding latency never shifts which requests a `rate`
|
|
1171
|
+
* fault hits.
|
|
1172
|
+
*/
|
|
1173
|
+
type LatencyRegistry = {
|
|
1174
|
+
get(namespace: string): LatencyState;
|
|
1175
|
+
set(namespace: string, state: LatencyState): LatencyState;
|
|
1176
|
+
/** Remove one namespace's latency, or every namespace's. */
|
|
1177
|
+
clear(namespace?: string): void;
|
|
1178
|
+
/** Milliseconds this request waits. Draws jitter, so call it once per request. */
|
|
1179
|
+
take(namespace: string, operationId: string | undefined): number;
|
|
1180
|
+
};
|
|
1181
|
+
/** `random` is the namespace's jitter stream. The registry never seeds or rewinds it. */
|
|
1182
|
+
declare const createLatencyRegistry: (random: (namespace: string) => Rng) => LatencyRegistry;
|
|
1183
|
+
|
|
1184
|
+
/** The controls of a namespace that are plain settings, not registries. */
|
|
1185
|
+
type ControlSettings = {
|
|
1186
|
+
seed: number | string;
|
|
1187
|
+
virtualTime: boolean;
|
|
1188
|
+
autoAdvanceMs: number;
|
|
1189
|
+
};
|
|
1190
|
+
/** Clock state as the admin API reports it. */
|
|
1191
|
+
type ControlClock = ClockState & {
|
|
1192
|
+
/** `now`, as ISO-8601. */
|
|
1193
|
+
iso: string;
|
|
1194
|
+
/** Milliseconds the clock steps before every vendor request. */
|
|
1195
|
+
autoAdvanceMs: number;
|
|
1196
|
+
};
|
|
1197
|
+
/** One namespace's controls: the body of `GET /__admin/controls`. */
|
|
1198
|
+
type ControlState = {
|
|
1199
|
+
namespace: string;
|
|
1200
|
+
seed: number | string;
|
|
1201
|
+
clock: ControlClock;
|
|
1202
|
+
virtualTime: boolean;
|
|
1203
|
+
latency: LatencyState;
|
|
1204
|
+
faults: FaultListing[];
|
|
1205
|
+
};
|
|
1206
|
+
/**
|
|
1207
|
+
* A namespace's controls as a snapshot holds them. Every part is optional: restoring puts
|
|
1208
|
+
* back what is there and leaves the rest of the namespace as it is.
|
|
1209
|
+
*/
|
|
1210
|
+
type ControlsSnapshot = {
|
|
1211
|
+
settings?: Partial<ControlSettings>;
|
|
1212
|
+
/** Where the fault and latency streams stand. */
|
|
1213
|
+
streams?: {
|
|
1214
|
+
faults?: number;
|
|
1215
|
+
latency?: number;
|
|
1216
|
+
};
|
|
1217
|
+
faults?: FaultSnapshot;
|
|
1218
|
+
latency?: LatencyState;
|
|
1219
|
+
};
|
|
1220
|
+
/** A change to a clock: {@link ClockPlan} plus the one-shot moves `POST /clock` accepts. */
|
|
1221
|
+
type ClockChange = ClockPlan & {
|
|
1222
|
+
advanceMs?: number;
|
|
1223
|
+
reset?: boolean;
|
|
1224
|
+
};
|
|
1225
|
+
/** What a reboot puts back: everything a boot snapshot holds, or only its controls. */
|
|
1226
|
+
type BootScope = "all" | "controls";
|
|
1227
|
+
type NamespaceControls = {
|
|
1228
|
+
readonly faults: FaultRegistry;
|
|
1229
|
+
readonly latency: LatencyRegistry;
|
|
1230
|
+
/** Standard presets, with the service's own taking precedence. */
|
|
1231
|
+
readonly presets: Readonly<Record<string, FaultPreset>>;
|
|
1232
|
+
/** Boot the namespace the first time it is used. Idempotent. */
|
|
1233
|
+
ensure(namespace: string): void;
|
|
1234
|
+
settings(namespace: string): Readonly<ControlSettings>;
|
|
1235
|
+
clock(namespace: string): ControlClock;
|
|
1236
|
+
describe(namespace: string): ControlState;
|
|
1237
|
+
/** Throw {@link ControlError} unless every part of `plan` can be applied. Changes nothing. */
|
|
1238
|
+
check(plan: ControlPlan): void;
|
|
1239
|
+
/**
|
|
1240
|
+
* Apply seed, clock, virtual time, latency and faults. A given key replaces that control.
|
|
1241
|
+
* The whole plan is checked first, so a plan that is rejected has changed nothing.
|
|
1242
|
+
*/
|
|
1243
|
+
apply(namespace: string, plan: ControlPlan): void;
|
|
1244
|
+
changeClock(namespace: string, change: ClockChange): ControlClock;
|
|
1245
|
+
/** Add one fault from an untrusted body: a rule, or `{"preset": "<name>", …overrides}`. */
|
|
1246
|
+
addFault(namespace: string, input: unknown): FaultRule[];
|
|
1247
|
+
/** The rules one request scripts for itself in its headers. Nothing is stored. */
|
|
1248
|
+
scriptFaults(namespace: string, request: {
|
|
1249
|
+
fault?: Record<string, unknown>;
|
|
1250
|
+
timeoutMs?: number;
|
|
1251
|
+
}): FaultRule[];
|
|
1252
|
+
/** Restart every random stream of a namespace from `seed`. */
|
|
1253
|
+
reseed(namespace: string, seed: number | string): void;
|
|
1254
|
+
/** Put a namespace back to what it was at boot: all of it, or only its controls. */
|
|
1255
|
+
reboot(namespace: string, scope?: BootScope): void;
|
|
1256
|
+
/** Forget every namespace; each boots again when next used. */
|
|
1257
|
+
rebootAll(): void;
|
|
1258
|
+
snapshot(namespace: string): Required<ControlsSnapshot>;
|
|
1259
|
+
restore(namespace: string, snapshot: ControlsSnapshot): void;
|
|
1260
|
+
/** Drop a namespace's controls entirely. */
|
|
1261
|
+
forget(namespace: string): void;
|
|
1262
|
+
};
|
|
1263
|
+
type NamespaceControlsOptions = {
|
|
1264
|
+
/** What every stream starts from, unless a namespace is given a seed of its own. */
|
|
1265
|
+
seed: number | string;
|
|
1266
|
+
clock(namespace: string): Clock;
|
|
1267
|
+
boot?: BootPlan;
|
|
1268
|
+
/** The service's presets. The standard ones fill in behind them. */
|
|
1269
|
+
presets?: Readonly<Record<string, FaultPreset>>;
|
|
1270
|
+
/** How a fault waits on the wall clock, when the request does not say. */
|
|
1271
|
+
sleep?(ms: number): Promise<void>;
|
|
1272
|
+
/** Reseed whatever else draws from the namespace's seed (the service's own stream). */
|
|
1273
|
+
onReseed?(namespace: string, seed: number | string): void;
|
|
1274
|
+
/** A preset was switched on: do the part of it that is not a fault rule. */
|
|
1275
|
+
onPreset?(preset: FaultPreset, namespace: string, overrides: Partial<FaultRule>): void;
|
|
1276
|
+
/** Put a snapshot document into a namespace: the boot `snapshot` control. */
|
|
1277
|
+
restoreDocument?(namespace: string, document: SnapshotDocument, scope: BootScope): void;
|
|
1278
|
+
};
|
|
1279
|
+
/**
|
|
1280
|
+
* Per-namespace seed, clock settings, latency and faults, and the one place a control
|
|
1281
|
+
* document is checked and applied — at boot, from `PUT /__admin/controls`, and from request
|
|
1282
|
+
* headers. It owns the seeded streams too: a namespace's seed lives here and nowhere else.
|
|
1283
|
+
*/
|
|
1284
|
+
declare const createNamespaceControls: (options: NamespaceControlsOptions) => NamespaceControls;
|
|
1285
|
+
|
|
1286
|
+
/** Unauthenticated readiness probe, served ahead of every vendor auth gate. */
|
|
1287
|
+
declare const HEALTH_PATH = "/__admin/health";
|
|
1288
|
+
/** Prefix of every control-plane route; never part of a vendor contract. */
|
|
1289
|
+
declare const ADMIN_PREFIX = "/__admin";
|
|
1290
|
+
/** Resolve once at construction; ambiguous URL forms are never accepted. */
|
|
1291
|
+
declare const resolveAdminPrefix: (prefix?: string) => string;
|
|
1292
|
+
/** Segment-boundary matching prevents /__administer from entering the control plane. */
|
|
1293
|
+
declare const isAdminPath: (path: string, prefix: string) => boolean;
|
|
1294
|
+
/** SDK base URL namespace carrier, entirely inside the configured internal route tree. */
|
|
1295
|
+
declare const matchNamespacePath: (path: string, prefix: string) => RegExpExecArray | null;
|
|
1296
|
+
/** Fail before serving when the declared vendor contract overlaps the reserved tree. */
|
|
1297
|
+
declare const assertAdminPrefixAvailable: (prefix: string, paths: readonly string[]) => void;
|
|
1298
|
+
/** Carries the admin key, which is separate from any vendor credential. */
|
|
1299
|
+
declare const ADMIN_KEY_HEADER: "x-mockingbird-admin-key";
|
|
1300
|
+
/** Selects the isolated namespace a request reads and writes. */
|
|
1301
|
+
declare const NAMESPACE_HEADER: "x-mockingbird-namespace";
|
|
1302
|
+
/** Where the admin controls are documented, for people and agents. */
|
|
1303
|
+
declare const ADMIN_DOCS_URL = "https://mockingbird.chrisvouga.dev/docs/admin-controls";
|
|
1304
|
+
/**
|
|
1305
|
+
* The guide's page for each control, with the shared routes (by first path segment) and the
|
|
1306
|
+
* request headers that page defines. `GET /__admin` links every route and header to its page,
|
|
1307
|
+
* and the guide in `docs/admin-controls/` is checked against this list.
|
|
1308
|
+
*/
|
|
1309
|
+
declare const ADMIN_CONTROL_PAGES: readonly {
|
|
1310
|
+
page: string;
|
|
1311
|
+
title: string;
|
|
1312
|
+
routes: readonly string[];
|
|
1313
|
+
headers: readonly string[];
|
|
1314
|
+
}[];
|
|
1315
|
+
type AdminRequest = {
|
|
1316
|
+
request: Request;
|
|
1317
|
+
url: URL;
|
|
1318
|
+
/** `:param` segments of the matched route. */
|
|
1319
|
+
params: Record<string, string>;
|
|
1320
|
+
/** Namespace the request targets: `?namespace=`, then the header, then the default. */
|
|
1321
|
+
namespace: string;
|
|
1322
|
+
/** Parsed JSON body, or `undefined` when there is none. */
|
|
1323
|
+
body: unknown;
|
|
1324
|
+
};
|
|
1325
|
+
type AdminRoute = (request: AdminRequest) => Response | Promise<Response>;
|
|
1326
|
+
/**
|
|
1327
|
+
* Service-specific admin routes, keyed `METHOD /path` relative to `/__admin`, with
|
|
1328
|
+
* `:param` segments — e.g. `"POST /orders/:id/transition"`.
|
|
1329
|
+
*/
|
|
1330
|
+
type AdminRoutes = Record<string, AdminRoute>;
|
|
1331
|
+
/** What `GET /__admin/snapshots` lists for each snapshot. */
|
|
1332
|
+
type SnapshotInfo = {
|
|
1333
|
+
id: string;
|
|
1334
|
+
namespace: string;
|
|
1335
|
+
/** Stored rows in the snapshot. */
|
|
1336
|
+
records: number;
|
|
1337
|
+
/** The namespace clock when it was taken, epoch ms. */
|
|
1338
|
+
at: number;
|
|
1339
|
+
/** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
|
|
1340
|
+
portable: boolean;
|
|
1341
|
+
};
|
|
1342
|
+
/** Named snapshots of a namespace. Ids are unique within a namespace, never across them. */
|
|
1343
|
+
type SnapshotStore = {
|
|
1344
|
+
/** Capture the namespace now. Reusing an id replaces that snapshot. */
|
|
1345
|
+
create(namespace: string, id?: string): SnapshotInfo;
|
|
1346
|
+
list(namespace: string): SnapshotInfo[];
|
|
1347
|
+
get(namespace: string, id: string): SnapshotInfo | undefined;
|
|
1348
|
+
/** The snapshot as portable JSON; `undefined` when there is none or it cannot leave the process. */
|
|
1349
|
+
export(namespace: string, id: string): SnapshotDocument | undefined;
|
|
1350
|
+
/** Store a document as a snapshot without restoring it. Throws {@link ControlError}. */
|
|
1351
|
+
import(namespace: string, id: string | undefined, document: SnapshotDocument): SnapshotInfo;
|
|
1352
|
+
/** Replace the namespace with the snapshot. `false` when there is none. */
|
|
1353
|
+
restore(namespace: string, id: string): boolean;
|
|
1354
|
+
remove(namespace: string, id: string): boolean;
|
|
1355
|
+
/** Forget one namespace's snapshots, or every namespace's. */
|
|
1356
|
+
clear(namespace?: string): void;
|
|
1357
|
+
};
|
|
1358
|
+
type ControlContext = {
|
|
1359
|
+
name: string;
|
|
1360
|
+
startedAt: number;
|
|
1361
|
+
wallNow: () => number;
|
|
1362
|
+
clock: Clock;
|
|
1363
|
+
namespaceClock?(namespace: string): Clock;
|
|
1364
|
+
metrics: Metrics;
|
|
1365
|
+
journal: Journal;
|
|
1366
|
+
defaultNamespace: string;
|
|
1367
|
+
namespaces(): string[];
|
|
1368
|
+
reset(namespace: string | "*"): Promise<void>;
|
|
1369
|
+
timeTravel: {
|
|
1370
|
+
checkpoint(namespace: string, branch: string): {
|
|
1371
|
+
id: string;
|
|
1372
|
+
branch: string;
|
|
1373
|
+
parent: string | null;
|
|
1374
|
+
at: number;
|
|
1375
|
+
records?: number;
|
|
1376
|
+
};
|
|
1377
|
+
branch(name: string, options: {
|
|
1378
|
+
namespace: string;
|
|
1379
|
+
at?: string;
|
|
1380
|
+
}): {
|
|
1381
|
+
id: string;
|
|
1382
|
+
branch: string;
|
|
1383
|
+
parent: string | null;
|
|
1384
|
+
at: number;
|
|
1385
|
+
};
|
|
1386
|
+
checkout(checkpoint: string, options: {
|
|
1387
|
+
namespace: string;
|
|
1388
|
+
branch: string;
|
|
1389
|
+
}): void;
|
|
1390
|
+
retain(namespace: string, checkpoint: string): void;
|
|
1391
|
+
release(namespace: string, checkpoint: string): boolean;
|
|
1392
|
+
inspect(namespace: string): {
|
|
1393
|
+
branches: Readonly<Record<string, string>>;
|
|
1394
|
+
checkpoints: readonly {
|
|
1395
|
+
id: string;
|
|
1396
|
+
branch: string;
|
|
1397
|
+
parent: string | null;
|
|
1398
|
+
at: number;
|
|
1399
|
+
}[];
|
|
1400
|
+
};
|
|
1401
|
+
};
|
|
1402
|
+
/** Extra fields for `GET /__admin/health`, such as the loaded corpus version. */
|
|
1403
|
+
describe(): Record<string, unknown>;
|
|
1404
|
+
routes: AdminRoutes;
|
|
1405
|
+
upstream?: {
|
|
1406
|
+
get: NonNullable<AdminRoutes[string]>;
|
|
1407
|
+
set: NonNullable<AdminRoutes[string]>;
|
|
1408
|
+
};
|
|
1409
|
+
adminPrefix?: string;
|
|
1410
|
+
adminKey: string | undefined;
|
|
1411
|
+
/** Reported by `GET /__admin`. */
|
|
1412
|
+
version?: string;
|
|
1413
|
+
/**
|
|
1414
|
+
* Per-namespace seed, clock settings, latency and faults. Default: controls over this
|
|
1415
|
+
* context's clock, with the standard presets and no boot controls.
|
|
1416
|
+
*/
|
|
1417
|
+
controls?: NamespaceControls;
|
|
1418
|
+
/** Named snapshots. Default: pinned `timeTravel` checkpoints, which are not portable. */
|
|
1419
|
+
snapshots?: SnapshotStore;
|
|
1420
|
+
/** Apply a control document, including its `snapshot`. Default: `controls.apply`. */
|
|
1421
|
+
applyControls?(namespace: string, plan: ControlPlan): void;
|
|
1422
|
+
/** Reset a namespace and forget it. Default: reset. */
|
|
1423
|
+
dropNamespace?(namespace: string): Promise<void>;
|
|
1424
|
+
};
|
|
1425
|
+
type ControlPlane = {
|
|
1426
|
+
/** The control-plane response for `request`, or `undefined` for a vendor request. */
|
|
1427
|
+
handle(request: Request): Promise<Response | undefined>;
|
|
1428
|
+
/**
|
|
1429
|
+
* Namespace a vendor request targets, from {@link NAMESPACE_HEADER}. Only admin routes
|
|
1430
|
+
* also accept `?namespace=`, so a vendor query parameter of that name can never
|
|
1431
|
+
* silently reroute a request.
|
|
1432
|
+
*/
|
|
1433
|
+
namespaceOf(request: Request): string;
|
|
1434
|
+
/** The per-namespace controls behind the routes. */
|
|
1435
|
+
readonly controls: NamespaceControls;
|
|
1436
|
+
readonly snapshots: SnapshotStore;
|
|
1437
|
+
};
|
|
1438
|
+
declare const createControlPlane: (context: ControlContext) => ControlPlane;
|
|
1439
|
+
|
|
1440
|
+
/**
|
|
1441
|
+
* Reading the vendor credential a request carries.
|
|
1442
|
+
*
|
|
1443
|
+
* Several vendor SDKs (Stripe, AWS, PostHog, Twilio) cannot add a namespace header, so a
|
|
1444
|
+
* runtime can also pick a request's namespace from its credential: a suite maps each
|
|
1445
|
+
* worker's API key, token or account SID to a namespace through `PUT /__admin/credentials`.
|
|
1446
|
+
* These helpers pull the credential out of the usual carriers.
|
|
1447
|
+
*/
|
|
1448
|
+
/** The token after `Bearer `, or `undefined`. */
|
|
1449
|
+
declare const bearerToken: (request: Request) => string | undefined;
|
|
1450
|
+
type BasicCredentials = {
|
|
1451
|
+
username: string;
|
|
1452
|
+
password: string;
|
|
1453
|
+
};
|
|
1454
|
+
/** `{ username, password }` from `Authorization: Basic …`, or `undefined`. */
|
|
1455
|
+
declare const basicAuth: (request: Request) => BasicCredentials | undefined;
|
|
1456
|
+
/**
|
|
1457
|
+
* The access key id of an AWS SigV4-signed request (`Credential=AKID/date/region/service/…`),
|
|
1458
|
+
* from the `Authorization` header or a presigned `X-Amz-Credential` query parameter.
|
|
1459
|
+
*/
|
|
1460
|
+
declare const sigV4AccessKeyId: (request: Request) => string | undefined;
|
|
1461
|
+
/** The credential in any of the common carriers: Bearer, Basic username, SigV4, or `x-api-key`. */
|
|
1462
|
+
declare const anyCredential: (request: Request) => string | undefined;
|
|
1463
|
+
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
1464
|
+
type CredentialRegistry = {
|
|
1465
|
+
set(credential: string, namespace: string): void;
|
|
1466
|
+
get(credential: string): string | undefined;
|
|
1467
|
+
remove(credential: string): boolean;
|
|
1468
|
+
clear(): void;
|
|
1469
|
+
entries(): {
|
|
1470
|
+
credential: string;
|
|
1471
|
+
namespace: string;
|
|
1472
|
+
}[];
|
|
1473
|
+
};
|
|
1474
|
+
declare const createCredentialRegistry: () => CredentialRegistry;
|
|
1475
|
+
/** A credential shown in admin output: enough to recognise it, never the whole secret. */
|
|
1476
|
+
declare const maskCredential: (credential: string) => string;
|
|
1477
|
+
|
|
1478
|
+
/**
|
|
1479
|
+
* A point-in-time copy of everything a service namespace holds.
|
|
1480
|
+
*
|
|
1481
|
+
* All service state lives in the two core tables keyed by namespace, so a snapshot
|
|
1482
|
+
* is generic: any service gets per-test rollback without knowing its own schema.
|
|
1483
|
+
* Restoring is much cheaper than rebuilding a namespace from a corpus.
|
|
1484
|
+
*/
|
|
1485
|
+
type NamespaceSnapshot = {
|
|
1486
|
+
namespace: string;
|
|
1487
|
+
records: {
|
|
1488
|
+
collection: string;
|
|
1489
|
+
id: string;
|
|
1490
|
+
seq: number;
|
|
1491
|
+
value: string;
|
|
1492
|
+
}[];
|
|
1493
|
+
sequences: {
|
|
1494
|
+
name: string;
|
|
1495
|
+
kind: string;
|
|
1496
|
+
value: number;
|
|
1497
|
+
}[];
|
|
1498
|
+
};
|
|
1499
|
+
/**
|
|
1500
|
+
* @deprecated Low-level Timeline payload capture retained for API compatibility. Provider code
|
|
1501
|
+
* must use the runtime Timeline or `withNamespaceRollback`.
|
|
1502
|
+
*/
|
|
1503
|
+
declare const snapshotNamespace: (sqlite: SqliteClient, namespace: string) => NamespaceSnapshot;
|
|
1504
|
+
/**
|
|
1505
|
+
* Replace a namespace's contents with `snapshot`. The namespace is emptied first,
|
|
1506
|
+
* so restoring is an assignment, not a merge — records created since the snapshot
|
|
1507
|
+
* are gone afterwards.
|
|
1508
|
+
*
|
|
1509
|
+
* @deprecated Low-level Timeline payload restore retained for API compatibility. Provider code
|
|
1510
|
+
* must use the runtime Timeline or `withNamespaceRollback`.
|
|
1511
|
+
*/
|
|
1512
|
+
declare const restoreNamespace: (sqlite: SqliteClient, namespace: string, snapshot: NamespaceSnapshot) => void;
|
|
1513
|
+
/**
|
|
1514
|
+
* Execute asynchronous service work atomically using the canonical Timeline rollback primitive.
|
|
1515
|
+
* This is the prescribed escape hatch when the SQLite adapter cannot hold a transaction across
|
|
1516
|
+
* `await`; service code should not coordinate raw namespace snapshots itself.
|
|
1517
|
+
*/
|
|
1518
|
+
declare const withNamespaceRollback: <T>(sqlite: SqliteClient, namespace: string, run: () => Promise<T>) => Promise<T>;
|
|
1519
|
+
|
|
1520
|
+
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
1521
|
+
type Stored<T> = {
|
|
1522
|
+
seq: number;
|
|
1523
|
+
value: T;
|
|
1524
|
+
};
|
|
1525
|
+
type ListRecordsOptions<T> = {
|
|
1526
|
+
/** Keep only records passing the predicate. */
|
|
1527
|
+
where?: (value: T, seq: number) => boolean;
|
|
1528
|
+
/** Sort order; default newest first. */
|
|
1529
|
+
order?: "newest" | "oldest";
|
|
1530
|
+
};
|
|
1531
|
+
/**
|
|
1532
|
+
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
1533
|
+
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
1534
|
+
*/
|
|
1535
|
+
declare class Collection<T> {
|
|
1536
|
+
private readonly sqlite;
|
|
1537
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
1538
|
+
readonly namespace: string;
|
|
1539
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
1540
|
+
readonly collectionName: string;
|
|
1541
|
+
private readonly statements;
|
|
1542
|
+
private prepare;
|
|
1543
|
+
constructor(sqlite: SqliteClient,
|
|
1544
|
+
/** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
|
|
1545
|
+
namespace: string,
|
|
1546
|
+
/** Table name inside the namespace. Stable across resets of the rows themselves. */
|
|
1547
|
+
collectionName: string);
|
|
1548
|
+
private bumpCollectionSeq;
|
|
1549
|
+
nextSequence(): number;
|
|
1550
|
+
get(id: string): T | undefined;
|
|
1551
|
+
has(id: string): boolean;
|
|
1552
|
+
/** Insert a new record, assigning it the next sequence number. */
|
|
1553
|
+
insert(id: string, value: T): Stored<T>;
|
|
1554
|
+
/** Replace an existing record's value, keeping its position. */
|
|
1555
|
+
update(id: string, value: T): Stored<T> | undefined;
|
|
1556
|
+
delete(id: string): boolean;
|
|
1557
|
+
/** How many records the collection holds, without reading them. */
|
|
1558
|
+
count(): number;
|
|
1559
|
+
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
1560
|
+
id: string;
|
|
1561
|
+
}>;
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
declare const STATE_FIELD_KINDS: readonly ["string", "number", "boolean", "null", "object", "array", "unknown"];
|
|
1565
|
+
type StateFieldKind = (typeof STATE_FIELD_KINDS)[number];
|
|
1566
|
+
/** JSON-safe annotations a bespoke admin UI may read. The default UI shows them as text. */
|
|
1567
|
+
type StateMeta = Readonly<Record<string, string | number | boolean | null>>;
|
|
1568
|
+
type StateField = {
|
|
1569
|
+
name: string;
|
|
1570
|
+
kind: StateFieldKind;
|
|
1571
|
+
optional: boolean;
|
|
1572
|
+
description?: string;
|
|
1573
|
+
/** One level of nested object fields. */
|
|
1574
|
+
fields?: StateField[];
|
|
1575
|
+
meta?: StateMeta;
|
|
1576
|
+
};
|
|
1577
|
+
/**
|
|
1578
|
+
* What a service knows about a collection before any row exists.
|
|
1579
|
+
* Stored rows still appear, including fields this declaration does not mention.
|
|
1580
|
+
*/
|
|
1581
|
+
type StateDeclaration = {
|
|
1582
|
+
name: string;
|
|
1583
|
+
label?: string;
|
|
1584
|
+
description?: string;
|
|
1585
|
+
fields?: readonly StateField[];
|
|
1586
|
+
meta?: StateMeta;
|
|
1587
|
+
};
|
|
1588
|
+
type StateCollectionView = {
|
|
1589
|
+
name: string;
|
|
1590
|
+
label: string;
|
|
1591
|
+
description?: string;
|
|
1592
|
+
count: number;
|
|
1593
|
+
/** `mixed` when a declaration and stored rows both contribute fields. */
|
|
1594
|
+
source: "declared" | "inferred" | "mixed";
|
|
1595
|
+
fields: StateField[];
|
|
1596
|
+
meta?: StateMeta;
|
|
1597
|
+
};
|
|
1598
|
+
/** The shape of one namespace, for an admin UI that has never seen the service before. */
|
|
1599
|
+
type StateView = {
|
|
1600
|
+
namespace: string;
|
|
1601
|
+
storageNamespace: string;
|
|
1602
|
+
collections: StateCollectionView[];
|
|
1603
|
+
};
|
|
1604
|
+
type StateRecord = {
|
|
1605
|
+
id: string;
|
|
1606
|
+
seq: number;
|
|
1607
|
+
value: unknown;
|
|
1608
|
+
};
|
|
1609
|
+
type StatePage = {
|
|
1610
|
+
collection: string;
|
|
1611
|
+
count: number;
|
|
1612
|
+
records: StateRecord[];
|
|
1613
|
+
next: number | null;
|
|
1614
|
+
};
|
|
1615
|
+
type StateScope = {
|
|
1616
|
+
sqlite: SqliteClient;
|
|
1617
|
+
/** Public namespace name a request selects. */
|
|
1618
|
+
namespace: string;
|
|
1619
|
+
/** Namespace key used in `mockingbird_records`. */
|
|
1620
|
+
storageNamespace: string;
|
|
1621
|
+
/** Service instance, walked for Collection fields. */
|
|
1622
|
+
root: unknown;
|
|
1623
|
+
declarations?: readonly StateDeclaration[];
|
|
1624
|
+
};
|
|
1625
|
+
/** Describe every collection in a namespace. Empty declared collections are included. */
|
|
1626
|
+
declare const inspectState: (scope: StateScope) => StateView;
|
|
1627
|
+
|
|
1628
|
+
/**
|
|
1629
|
+
* What every mock can be called with. A service may accept more fields — accounts,
|
|
1630
|
+
* catalogs, webhook endpoints — and those stay optional so `createRuntime()` and
|
|
1631
|
+
* `createRuntime({ seed, adminKey })` work everywhere.
|
|
1632
|
+
*/
|
|
1633
|
+
type MockCreateOptions = {
|
|
1634
|
+
sqlite?: SqliteClient;
|
|
1635
|
+
clock?: Clock;
|
|
1636
|
+
seed?: number | string;
|
|
1637
|
+
/** All internal HTTP paths live below this prefix. Default /__admin. */
|
|
1638
|
+
adminPrefix?: string;
|
|
1639
|
+
adminKey?: string;
|
|
1640
|
+
/**
|
|
1641
|
+
* Admin controls to boot with: clock, latency, faults, seed, a starting snapshot, and
|
|
1642
|
+
* per-namespace overrides. `POST /__admin/reset` returns to exactly this.
|
|
1643
|
+
*/
|
|
1644
|
+
adminControls?: MockControls;
|
|
1645
|
+
};
|
|
1646
|
+
/** A named snapshot, as the admin API and `runtime.snapshots` list it. */
|
|
1647
|
+
type MockSnapshotInfo = {
|
|
1648
|
+
readonly id: string;
|
|
1649
|
+
readonly namespace: string;
|
|
1650
|
+
readonly records: number;
|
|
1651
|
+
readonly at: number;
|
|
1652
|
+
readonly portable: boolean;
|
|
1653
|
+
};
|
|
1654
|
+
/** One namespace's controls, as `GET /__admin/controls` and `runtime.controls()` report them. */
|
|
1655
|
+
type MockControlState = {
|
|
1656
|
+
readonly namespace: string;
|
|
1657
|
+
readonly seed: number | string;
|
|
1658
|
+
readonly clock: ClockState & {
|
|
1659
|
+
iso: string;
|
|
1660
|
+
autoAdvanceMs: number;
|
|
1661
|
+
};
|
|
1662
|
+
readonly virtualTime: boolean;
|
|
1663
|
+
readonly latency: unknown;
|
|
1664
|
+
readonly faults: readonly unknown[];
|
|
1665
|
+
readonly snapshots: readonly MockSnapshotInfo[];
|
|
1666
|
+
};
|
|
1667
|
+
/** A checkpoint head the admin client can show. Services may return a richer object. */
|
|
1668
|
+
type MockCheckpoint = {
|
|
1669
|
+
readonly id: string;
|
|
1670
|
+
readonly branch: string;
|
|
1671
|
+
readonly parent: string | null;
|
|
1672
|
+
readonly at: number;
|
|
1673
|
+
};
|
|
1674
|
+
/** The instance behind one namespace. Vendor methods sit beside these, not instead of them. */
|
|
1675
|
+
type MockInstanceSurface = {
|
|
1676
|
+
fetch(request: Request): Promise<Response>;
|
|
1677
|
+
reset(): Promise<void>;
|
|
1678
|
+
};
|
|
1679
|
+
/**
|
|
1680
|
+
* The interface every mock runtime implements.
|
|
1681
|
+
*
|
|
1682
|
+
* Assignability is one way: a service may add methods (`tick`, a typed webhook hub,
|
|
1683
|
+
* account directories) and narrower property types. Removing or renaming a member
|
|
1684
|
+
* fails the typecheck. The HTTP control plane (`/__admin/health`, `/__admin/*`) is the same
|
|
1685
|
+
* contract on the wire; {@link STANDARD_ADMIN_ROUTES} is that list.
|
|
1686
|
+
*/
|
|
1687
|
+
type MockSurface = {
|
|
1688
|
+
readonly name: string;
|
|
1689
|
+
fetch(request: Request): Promise<Response>;
|
|
1690
|
+
readonly clock: Clock;
|
|
1691
|
+
readonly faults: FaultRegistry;
|
|
1692
|
+
/** Baseline response time per namespace. */
|
|
1693
|
+
readonly latency: LatencyRegistry;
|
|
1694
|
+
readonly metrics: Metrics;
|
|
1695
|
+
readonly journal: Journal;
|
|
1696
|
+
readonly rng: Rng;
|
|
1697
|
+
readonly credentials: CredentialRegistry;
|
|
1698
|
+
/** Present on every runtime. `undefined` when the service has no outbound webhooks. */
|
|
1699
|
+
readonly webhooks: unknown;
|
|
1700
|
+
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
|
|
1701
|
+
instance(namespace?: string): MockInstanceSurface;
|
|
1702
|
+
namespaces(): string[];
|
|
1703
|
+
reset(namespace?: string): Promise<void>;
|
|
1704
|
+
/** A namespace's clock: the base clock for `default`, its own for any other. */
|
|
1705
|
+
namespaceClock(namespace?: string): Clock;
|
|
1706
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
1707
|
+
controls(namespace?: string): MockControlState;
|
|
1708
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
1709
|
+
configure(controls: NamespaceControls$1, namespace?: string): MockControlState;
|
|
1710
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
1711
|
+
readonly snapshots: {
|
|
1712
|
+
create(options?: {
|
|
1713
|
+
name?: string;
|
|
1714
|
+
namespace?: string;
|
|
1715
|
+
}): MockSnapshotInfo;
|
|
1716
|
+
list(namespace?: string): MockSnapshotInfo[];
|
|
1717
|
+
restore(id: string, namespace?: string): void;
|
|
1718
|
+
delete(id: string, namespace?: string): boolean;
|
|
1719
|
+
};
|
|
1720
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
1721
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
1722
|
+
snapshot(namespace?: string): NamespaceSnapshot;
|
|
1723
|
+
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
1724
|
+
checkpoint(namespace?: string, branch?: string): MockCheckpoint;
|
|
1725
|
+
branch(name: string, options?: {
|
|
1726
|
+
namespace?: string;
|
|
1727
|
+
at?: string;
|
|
1728
|
+
}): MockCheckpoint;
|
|
1729
|
+
checkout(checkpoint: string, options?: {
|
|
1730
|
+
namespace?: string;
|
|
1731
|
+
branch?: string;
|
|
1732
|
+
}): void;
|
|
1733
|
+
timeline(namespace?: string): {
|
|
1734
|
+
head(branch?: string): {
|
|
1735
|
+
readonly id: string;
|
|
1736
|
+
} | undefined;
|
|
1737
|
+
branches(): Readonly<Record<string, string>>;
|
|
1738
|
+
};
|
|
1739
|
+
/** Collections in `namespace`, declared by the service and inferred from stored rows. */
|
|
1740
|
+
state(namespace?: string): StateView;
|
|
1741
|
+
readonly sqlite: SqliteClient;
|
|
1742
|
+
};
|
|
1743
|
+
/**
|
|
1744
|
+
* Keep a service's own runtime type while proving it is a {@link MockSurface}.
|
|
1745
|
+
* Extra members stay. A missing member is a type error at the definition.
|
|
1746
|
+
*/
|
|
1747
|
+
declare const defineMock: <T extends MockSurface>(runtime: T) => T;
|
|
1748
|
+
/**
|
|
1749
|
+
* What each shared admin route does. `GET /__admin` serves this list, and the guide in
|
|
1750
|
+
* `docs/ADMIN_CONTROLS.md` is checked against it.
|
|
1751
|
+
*/
|
|
1752
|
+
declare const ADMIN_ROUTE_REFERENCE: {
|
|
1753
|
+
readonly "GET /upstream": "Read this namespace’s emulated or forward mode and upstream base URL.";
|
|
1754
|
+
readonly "PUT /upstream": "Set upstream mode or base URL; forward requires an injected transport.";
|
|
1755
|
+
readonly "GET /": "This index: every route and control header, with a one-line summary each.";
|
|
1756
|
+
readonly "GET /health": "Readiness. Never needs the admin key or a vendor credential.";
|
|
1757
|
+
readonly "GET /controls": "The namespace's controls in one document: seed, clock, virtual time, latency, faults, snapshots.";
|
|
1758
|
+
readonly "PUT /controls": "Apply a control document. Each key given replaces that control; `snapshot` replaces the records.";
|
|
1759
|
+
readonly "DELETE /controls": "Put the namespace's controls back to boot. Records are untouched.";
|
|
1760
|
+
readonly "POST /reset": "Return the namespace to its boot state: records, controls, snapshots, journal, metrics. `?all=1` resets every namespace.";
|
|
1761
|
+
readonly "GET /namespaces": "The isolation keys in use.";
|
|
1762
|
+
readonly "DELETE /namespaces/:name": "Reset a namespace and forget it.";
|
|
1763
|
+
readonly "GET /clock": "The namespace clock: `now`, `iso`, `frozen`, `offsetMs`, `autoAdvanceMs`.";
|
|
1764
|
+
readonly "POST /clock": "Move time: `now` (or `set`), `advance`, `frozen` (or `freeze`), `autoAdvance`, `reset`.";
|
|
1765
|
+
readonly "GET /latency": "The namespace's baseline response time.";
|
|
1766
|
+
readonly "PUT /latency": "Set it: `delay`, seeded `jitter`, and per-operation `operations`.";
|
|
1767
|
+
readonly "DELETE /latency": "Remove it.";
|
|
1768
|
+
readonly "GET /faults": "Fault rules that can hit this namespace. `?all=1` lists every namespace's.";
|
|
1769
|
+
readonly "POST /faults": "Add a fault rule, or a preset with `{\"preset\": \"<name>\"}`.";
|
|
1770
|
+
readonly "PUT /faults": "Replace the namespace's fault rules with `{\"faults\": [...]}`.";
|
|
1771
|
+
readonly "DELETE /faults": "Remove the namespace's fault rules. `?id=` removes one; `?all=1` every namespace's.";
|
|
1772
|
+
readonly "GET /faults/presets": "Named faults this emulator answers to.";
|
|
1773
|
+
readonly "POST /faults/presets/:name": "Switch a preset on. The body overrides its fields.";
|
|
1774
|
+
readonly "GET /faults/:id": "One fault rule, with its `hits`, `seen` and `remaining` counters.";
|
|
1775
|
+
readonly "DELETE /faults/:id": "Remove one fault rule.";
|
|
1776
|
+
readonly "GET /snapshots": "The namespace's snapshots.";
|
|
1777
|
+
readonly "POST /snapshots": "Snapshot the namespace: records, clock, random streams, faults, latency. `{\"name\"}` names it.";
|
|
1778
|
+
readonly "GET /snapshots/:id": "A snapshot as portable JSON.";
|
|
1779
|
+
readonly "PUT /snapshots/:id": "Store a snapshot document under this id, without restoring it.";
|
|
1780
|
+
readonly "POST /snapshots/:id/restore": "Replace the namespace with a snapshot.";
|
|
1781
|
+
readonly "DELETE /snapshots/:id": "Delete a snapshot.";
|
|
1782
|
+
readonly "GET /timeline": "The namespace's checkpoint history and branches.";
|
|
1783
|
+
readonly "POST /checkpoints": "Checkpoint a branch now. Successful mutations do this automatically.";
|
|
1784
|
+
readonly "POST /branches/:name": "Create a copy-on-write branch, optionally from a checkpoint (`at`).";
|
|
1785
|
+
readonly "POST /branches/:name/checkout": "Move a branch to a checkpoint.";
|
|
1786
|
+
readonly "GET /requests": "The request journal. Filter with `operationId`, `method`, `path`, `status`, `faultId`, `since`, `limit`; `?all=1` for every namespace.";
|
|
1787
|
+
readonly "DELETE /requests": "Clear the journal.";
|
|
1788
|
+
readonly "GET /metrics": "Request counts and unmatched paths, for every namespace or the one named.";
|
|
1789
|
+
readonly "DELETE /metrics": "Clear them.";
|
|
1790
|
+
readonly "GET /credentials": "Vendor credential → namespace mappings, masked.";
|
|
1791
|
+
readonly "PUT /credentials": "Map vendor credentials to namespaces, for SDKs that cannot send a header.";
|
|
1792
|
+
readonly "DELETE /credentials": "Remove one mapping (`?credential=`) or all.";
|
|
1793
|
+
readonly "GET /state": "The namespace's collections: declared shape and stored rows.";
|
|
1794
|
+
readonly "GET /state/:collection": "A page of rows.";
|
|
1795
|
+
readonly "GET /state/:collection/:id": "One row.";
|
|
1796
|
+
readonly "POST /state/:collection": "Create a row from `{id?, value}`.";
|
|
1797
|
+
readonly "PUT /state/:collection/:id": "Replace a row.";
|
|
1798
|
+
readonly "PATCH /state/:collection/:id": "Shallow-merge into a row.";
|
|
1799
|
+
readonly "DELETE /state/:collection/:id": "Delete a row.";
|
|
1800
|
+
readonly "GET /ui": "The admin UI.";
|
|
1801
|
+
readonly "GET /ui/": "The admin UI.";
|
|
1802
|
+
readonly "GET /ui/manifest": "The admin UI's panels and extensions.";
|
|
1803
|
+
};
|
|
1804
|
+
/**
|
|
1805
|
+
* Admin routes every mock answers. A service adds its own keys beside these.
|
|
1806
|
+
* A key in this list keeps the shared handler, so the state API and the admin UI
|
|
1807
|
+
* stay the same when a service also ships vendor-specific controls.
|
|
1808
|
+
*/
|
|
1809
|
+
declare const STANDARD_ADMIN_ROUTES: readonly (keyof typeof ADMIN_ROUTE_REFERENCE)[];
|
|
1810
|
+
type StandardAdminRoute = (typeof STANDARD_ADMIN_ROUTES)[number];
|
|
1811
|
+
/**
|
|
1812
|
+
* A contribution the admin shell mounts beside the shared views.
|
|
1813
|
+
* `panel` is author-owned markup. `sql` turns on the built-in table explorer
|
|
1814
|
+
* and query runner, which call `GET /sql/tables` and `POST /sql/query`.
|
|
1815
|
+
* `route` presents an admin action with the shell's prebuilt form and response view.
|
|
1816
|
+
*/
|
|
1817
|
+
type AdminExtension = {
|
|
1818
|
+
kind: "route";
|
|
1819
|
+
id: string;
|
|
1820
|
+
title: string;
|
|
1821
|
+
description?: string;
|
|
1822
|
+
/** An existing admin route, including its method and any path parameters. */
|
|
1823
|
+
route: string;
|
|
1824
|
+
/** Initial JSON body shown in the request form. */
|
|
1825
|
+
body?: unknown;
|
|
1826
|
+
} | {
|
|
1827
|
+
kind: "panel";
|
|
1828
|
+
id: string;
|
|
1829
|
+
title: string;
|
|
1830
|
+
description?: string;
|
|
1831
|
+
html: string;
|
|
1832
|
+
script?: string;
|
|
1833
|
+
} | {
|
|
1834
|
+
kind: "sql";
|
|
1835
|
+
/** DOM id suffix. Default `sql`. */
|
|
1836
|
+
id?: string;
|
|
1837
|
+
title?: string;
|
|
1838
|
+
description?: string;
|
|
1839
|
+
};
|
|
1840
|
+
/** A panel the default admin shell mounts after the shared views. */
|
|
1841
|
+
type AdminPanel = {
|
|
1842
|
+
/** DOM id suffix. Lowercase letters, digits, and hyphens. */
|
|
1843
|
+
id: string;
|
|
1844
|
+
title: string;
|
|
1845
|
+
description?: string;
|
|
1846
|
+
/** Markup placed inside the panel body. The service author owns it. */
|
|
1847
|
+
html: string;
|
|
1848
|
+
/**
|
|
1849
|
+
* Function body called as `(root, api) => void` once the shell has loaded.
|
|
1850
|
+
* `root` is the panel body. `api` is `{ get, send, namespace }` against `/__admin`.
|
|
1851
|
+
*/
|
|
1852
|
+
script?: string;
|
|
1853
|
+
};
|
|
1854
|
+
/**
|
|
1855
|
+
* How a service customizes the admin UI without leaving the shared shell.
|
|
1856
|
+
* `panels` add views. `render` replaces the document and can call `defaultHtml()`
|
|
1857
|
+
* to wrap the shared shell instead of discarding it.
|
|
1858
|
+
*/
|
|
1859
|
+
type AdminUi = {
|
|
1860
|
+
panels?: readonly AdminPanel[];
|
|
1861
|
+
/** First-class shell contributions. `sql` mounts the shared database explorer. */
|
|
1862
|
+
extensions?: readonly AdminExtension[];
|
|
1863
|
+
render?: (input: {
|
|
1864
|
+
service: string;
|
|
1865
|
+
defaultHtml: () => string;
|
|
1866
|
+
}) => string;
|
|
1867
|
+
};
|
|
1868
|
+
/** Reject a panel or extension the shell cannot mount. Called when the runtime is created. */
|
|
1869
|
+
declare const assertAdminUi: (ui: AdminUi | undefined) => void;
|
|
1870
|
+
|
|
1871
|
+
/** One service's vendor chip: what `GET <adminPrefix>/ui/brands.json` returns under its name. */
|
|
1872
|
+
type AdminBrand = {
|
|
1873
|
+
vendor: string;
|
|
1874
|
+
website: string;
|
|
1875
|
+
/** Vendor API reference, when there is one. */
|
|
1876
|
+
docs: string | null;
|
|
1877
|
+
/** The service's page on the docs site. A link only; the admin never fetches it. */
|
|
1878
|
+
guide: string;
|
|
1879
|
+
/** A `data:image/…;base64,` URI, so the chip loads nothing from the network; "" for none. */
|
|
1880
|
+
logo: string;
|
|
1881
|
+
color: string | null;
|
|
1882
|
+
description: string | null;
|
|
1883
|
+
};
|
|
1884
|
+
|
|
1885
|
+
/** Portable MD5 for AWS wire checksums (not a security primitive). */
|
|
1886
|
+
declare const awsMd5: (value: string | Uint8Array) => string;
|
|
1887
|
+
|
|
1888
|
+
/** Deterministic, opaque-looking alphanumeric token of `length` characters derived from `input`. */
|
|
1889
|
+
declare const opaqueToken: (input: string, length: number) => string;
|
|
1890
|
+
/**
|
|
1891
|
+
* What names an id base: a non-negative safe integer (or its decimal string) or `"boot"`,
|
|
1892
|
+
* which derives the base from the wall clock when the runtime is created.
|
|
1893
|
+
*/
|
|
1894
|
+
type IdBase = number | string;
|
|
1895
|
+
/**
|
|
1896
|
+
* Resolve an id base. `"boot"` is the wall clock in epoch milliseconds, so every boot gets
|
|
1897
|
+
* a different base. Anything else must be a non-negative safe integer or a string of digits.
|
|
1898
|
+
*/
|
|
1899
|
+
declare const normalizeIdBase: (value: unknown, label?: string, wallNow?: () => number) => number;
|
|
1900
|
+
/**
|
|
1901
|
+
* Give every {@link IdSequence} over `sqlite` this base. `0` is the default and changes
|
|
1902
|
+
* nothing; any other base yields a different, unrelated run of ids from the same counters.
|
|
1903
|
+
*/
|
|
1904
|
+
declare const setIdBase: (sqlite: SqliteClient, base: number) => void;
|
|
1905
|
+
/** The id base in force over `sqlite` (`0` unless one was set). */
|
|
1906
|
+
declare const idBaseOf: (sqlite: SqliteClient) => number;
|
|
1907
|
+
/**
|
|
1908
|
+
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
1909
|
+
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
1910
|
+
* A runtime's `idBase` is mixed in, so a different base reissues none of another's ids.
|
|
1911
|
+
*/
|
|
1912
|
+
declare class IdSequence {
|
|
1913
|
+
private readonly sqlite;
|
|
1914
|
+
private readonly namespace;
|
|
1915
|
+
private readonly salt;
|
|
1916
|
+
constructor(sqlite: SqliteClient, namespace: string, salt?: string);
|
|
1917
|
+
next(prefix: string, length?: number): string;
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
type AwsInput = Record<string, unknown>;
|
|
1921
|
+
declare class AwsError extends Error {
|
|
1922
|
+
readonly code: string;
|
|
1923
|
+
readonly status: number;
|
|
1924
|
+
constructor(code: string, message: string, status?: number);
|
|
1925
|
+
}
|
|
1926
|
+
type AwsProtocolOptions = APIOptions & {
|
|
1927
|
+
region?: string;
|
|
1928
|
+
accountId?: string;
|
|
1929
|
+
};
|
|
1930
|
+
type AwsOperation = {
|
|
1931
|
+
operation: string;
|
|
1932
|
+
input: AwsInput;
|
|
1933
|
+
request: Request;
|
|
1934
|
+
url: URL;
|
|
1935
|
+
};
|
|
1936
|
+
declare const awsRecord: (value: unknown) => AwsInput;
|
|
1937
|
+
declare const awsList: (value: unknown) => unknown[];
|
|
1938
|
+
declare const awsRequired: (input: AwsInput, key: string) => string;
|
|
1939
|
+
declare const awsPage: <T>(values: T[], input: AwsInput, tokenKey?: string, limitKey?: string) => {
|
|
1940
|
+
token?: string;
|
|
1941
|
+
items: T[];
|
|
1942
|
+
};
|
|
1943
|
+
/** Fetch-native AWS wire transport. Service subclasses own operations and persisted state. */
|
|
1944
|
+
declare abstract class AwsProtocolAPI {
|
|
1945
|
+
readonly service: string;
|
|
1946
|
+
readonly sqlite: SqliteClient;
|
|
1947
|
+
readonly namespace: string;
|
|
1948
|
+
readonly now: () => number;
|
|
1949
|
+
readonly region: string;
|
|
1950
|
+
readonly accountId: string;
|
|
1951
|
+
readonly ids: IdSequence;
|
|
1952
|
+
private readonly collections;
|
|
1953
|
+
constructor(service: string, options?: AwsProtocolOptions);
|
|
1954
|
+
collection(name: string): Collection<AwsInput>;
|
|
1955
|
+
arn(type: string, name: string, service?: string): string;
|
|
1956
|
+
reset(): Promise<void>;
|
|
1957
|
+
abstract dispatch(context: AwsOperation): unknown | Promise<unknown>;
|
|
1958
|
+
route(_method: string, _pathname: string): {
|
|
1959
|
+
operation: string;
|
|
1960
|
+
params: AwsInput;
|
|
1961
|
+
} | undefined;
|
|
1962
|
+
fetch(request: Request): Promise<Response>;
|
|
1963
|
+
unsupported(operation: string): never;
|
|
1964
|
+
get(collection: string, id: string, code?: string): AwsInput;
|
|
1965
|
+
}
|
|
1966
|
+
declare function decodeAwsQuery(params: URLSearchParams): AwsInput;
|
|
1967
|
+
|
|
1968
|
+
/** AWS XML uses string scalars; callers convert numbers according to the service model. */
|
|
1969
|
+
declare const awsParseXml: (source: string) => Record<string, unknown>;
|
|
1970
|
+
declare const awsXmlEscape: (value: unknown) => string;
|
|
1971
|
+
declare const awsXml: (value: unknown, listTag?: string) => string;
|
|
1972
|
+
|
|
1973
|
+
/**
|
|
1974
|
+
* Why a form value did not fit its schema. `path` uses bracket notation (`shipping[address][line1]`)
|
|
1975
|
+
* because that is how form-encoded APIs name parameters in their errors.
|
|
1976
|
+
*/
|
|
1977
|
+
type FormIssue = {
|
|
1978
|
+
kind: "unknown";
|
|
1979
|
+
path: string;
|
|
1980
|
+
} | {
|
|
1981
|
+
kind: "missing";
|
|
1982
|
+
path: string;
|
|
1983
|
+
} | {
|
|
1984
|
+
kind: "empty";
|
|
1985
|
+
path: string;
|
|
1986
|
+
} | {
|
|
1987
|
+
kind: "invalid-integer" | "invalid-number" | "invalid-boolean";
|
|
1988
|
+
path: string;
|
|
1989
|
+
raw: string;
|
|
1990
|
+
} | {
|
|
1991
|
+
kind: "invalid-object" | "invalid-array";
|
|
1992
|
+
path: string;
|
|
1993
|
+
raw: string;
|
|
1994
|
+
} | {
|
|
1995
|
+
kind: "invalid-enum";
|
|
1996
|
+
path: string;
|
|
1997
|
+
raw: string;
|
|
1998
|
+
allowed: string[];
|
|
1999
|
+
} | {
|
|
2000
|
+
kind: "too-long";
|
|
2001
|
+
path: string;
|
|
2002
|
+
raw: string;
|
|
2003
|
+
limit: number;
|
|
2004
|
+
} | {
|
|
2005
|
+
kind: "too-many-items";
|
|
2006
|
+
path: string;
|
|
2007
|
+
count: number;
|
|
2008
|
+
limit: number;
|
|
2009
|
+
} | {
|
|
2010
|
+
kind: "below-minimum";
|
|
2011
|
+
path: string;
|
|
2012
|
+
raw: string;
|
|
2013
|
+
limit: number;
|
|
2014
|
+
} | {
|
|
2015
|
+
kind: "above-maximum";
|
|
2016
|
+
path: string;
|
|
2017
|
+
raw: string;
|
|
2018
|
+
limit: number;
|
|
2019
|
+
};
|
|
2020
|
+
type ParsedForm = {
|
|
2021
|
+
value: unknown;
|
|
2022
|
+
issues: FormIssue[];
|
|
2023
|
+
};
|
|
2024
|
+
declare const sortIssues: (issues: FormIssue[]) => FormIssue[];
|
|
2025
|
+
/**
|
|
2026
|
+
* Coerce a bracket-decoded form value against an OpenAPI schema the way form-encoded APIs do:
|
|
2027
|
+
* every leaf arrives as a string, so integers/numbers/booleans are parsed, unknown keys are
|
|
2028
|
+
* rejected, required keys are enforced and constraints are checked. Empty strings stay `""`
|
|
2029
|
+
* for string-typed leaves (providers decide whether that means "unset"), become `[]` for arrays
|
|
2030
|
+
* when the schema permits it, and are reported as invalid for numeric/boolean leaves.
|
|
2031
|
+
*
|
|
2032
|
+
* `anyOf`/`oneOf` picks the branch matching the value's shape (object/array/scalar); among
|
|
2033
|
+
* scalar branches, one whose `enum` names the value exactly wins over the first.
|
|
2034
|
+
*/
|
|
2035
|
+
declare const parseForm: (document: OpenAPIDocument, schema: SchemaObject, raw: FormValue | undefined, path?: string) => ParsedForm;
|
|
2036
|
+
|
|
2037
|
+
/** JSON response with a normalised content type. */
|
|
2038
|
+
declare const jsonRes: (status: number, body: unknown, headers?: Record<string, string>) => Response;
|
|
2039
|
+
/** Thrown by handlers to produce a provider-shaped error response via `onError`. */
|
|
2040
|
+
declare class HttpError extends Error {
|
|
2041
|
+
readonly status: number;
|
|
2042
|
+
readonly body: unknown;
|
|
2043
|
+
readonly headers: Record<string, string>;
|
|
2044
|
+
constructor(status: number, body: unknown, headers?: Record<string, string>);
|
|
2045
|
+
toResponse(): Response;
|
|
2046
|
+
}
|
|
2047
|
+
type FieldResult<T> = {
|
|
2048
|
+
ok: true;
|
|
2049
|
+
value: T;
|
|
2050
|
+
} | {
|
|
2051
|
+
ok: false;
|
|
2052
|
+
reason: string;
|
|
2053
|
+
};
|
|
2054
|
+
/** Form bodies decode to strings; these coerce the way HTTP servers do, reporting why not. */
|
|
2055
|
+
declare const coerce: {
|
|
2056
|
+
string(value: unknown): FieldResult<string>;
|
|
2057
|
+
integer(value: unknown): FieldResult<number>;
|
|
2058
|
+
boolean(value: unknown): FieldResult<boolean>;
|
|
2059
|
+
enumeration<T extends string>(value: unknown, allowed: readonly T[]): FieldResult<T>;
|
|
2060
|
+
/** Flat string-to-string map, the shape of Stripe-style `metadata`. */
|
|
2061
|
+
stringMap(value: unknown): FieldResult<Record<string, string>>;
|
|
2062
|
+
};
|
|
2063
|
+
/** Count Unicode code points, the way JSON Schema and most APIs measure string length. */
|
|
2064
|
+
declare const codePointLength: (value: string) => number;
|
|
2065
|
+
|
|
2066
|
+
type IdempotencyStoreOptions = {
|
|
2067
|
+
/** The clock key ages are measured on; pass the runtime's so a test can advance it. */
|
|
2068
|
+
now?: () => number;
|
|
2069
|
+
/**
|
|
2070
|
+
* How long a key is kept, in ms. A reuse at or after this age runs as a new request, as
|
|
2071
|
+
* Stripe does once it prunes a key ("at least 24 hours old"). Default: keys never expire.
|
|
2072
|
+
*/
|
|
2073
|
+
ttlMs?: number;
|
|
2074
|
+
};
|
|
2075
|
+
type IdempotencyErrors = {
|
|
2076
|
+
/** Same key, different parameters. */
|
|
2077
|
+
mismatch: () => Response;
|
|
2078
|
+
/** Same key while the first request is still being handled. */
|
|
2079
|
+
conflict: () => Response;
|
|
2080
|
+
};
|
|
2081
|
+
declare class IdempotencyStore {
|
|
2082
|
+
private readonly namespace;
|
|
2083
|
+
private readonly options;
|
|
2084
|
+
private readonly responses;
|
|
2085
|
+
constructor(sqlite: SqliteClient, namespace: string, name?: string, options?: IdempotencyStoreOptions);
|
|
2086
|
+
/**
|
|
2087
|
+
* Run `handler` once per `key`. `fingerprint` identifies the request's parameters (e.g.
|
|
2088
|
+
* the method, path and canonical body). Only `replayable` responses are stored (default:
|
|
2089
|
+
* every status below 500), so a transient failure can be retried. `replayable` also sees the
|
|
2090
|
+
* response, for vendors that decide from more than the status.
|
|
2091
|
+
* Stripe also saves endpoint-produced 500 responses.
|
|
2092
|
+
*/
|
|
2093
|
+
run(key: string, fingerprint: string, errors: IdempotencyErrors, handler: () => Promise<Response> | Response, replayable?: (status: number, response: Response) => boolean): Promise<Response>;
|
|
2094
|
+
private expired;
|
|
2095
|
+
}
|
|
2096
|
+
/** A stable fingerprint of a request's method, path and parsed body. */
|
|
2097
|
+
declare const requestFingerprint: (method: string, path: string, body: unknown) => string;
|
|
2098
|
+
declare const stableStringify: (value: unknown) => string;
|
|
2099
|
+
|
|
2100
|
+
/** Policy only for runtime-owned HTTP sends; it does not sandbox DNS or arbitrary sockets. */
|
|
2101
|
+
type OutboundPolicy = {
|
|
2102
|
+
mode: "offline" | "allowlist";
|
|
2103
|
+
origins?: string[];
|
|
2104
|
+
maxRedirects?: number;
|
|
2105
|
+
maxBodyBytes?: number;
|
|
2106
|
+
onReject?(event: {
|
|
2107
|
+
namespace: string;
|
|
2108
|
+
policy: string;
|
|
2109
|
+
reason: string;
|
|
2110
|
+
}): void;
|
|
2111
|
+
};
|
|
2112
|
+
type OwnedTransport = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
|
|
2113
|
+
declare function ownedTransport(policy: OutboundPolicy): OwnedTransport;
|
|
2114
|
+
|
|
2115
|
+
/**
|
|
2116
|
+
* Outbound webhooks, shared by every service that has them.
|
|
2117
|
+
*
|
|
2118
|
+
* A service publishes a message (the exact bytes the vendor would send); the hub fans it out
|
|
2119
|
+
* to every matching endpoint, signs each delivery with the vendor's scheme, retries on the
|
|
2120
|
+
* vendor's schedule, and keeps a record a suite can read, replay or flush through
|
|
2121
|
+
* `/__admin/webhooks*`. Signature timestamps use the wall clock unless a service injects
|
|
2122
|
+
* `now`: receivers check them against their own time, and an advanced mock clock would
|
|
2123
|
+
* otherwise fail every delivery. A service whose caller injected the clock (Stripe) passes it,
|
|
2124
|
+
* so a virtual-time receiver verifies at the mock's time.
|
|
2125
|
+
*/
|
|
2126
|
+
/** What a signer sees for one delivery attempt. */
|
|
2127
|
+
type SignInput = {
|
|
2128
|
+
messageId: string;
|
|
2129
|
+
/** Exact bytes about to be sent. */
|
|
2130
|
+
body: string;
|
|
2131
|
+
/** Unix seconds of this attempt: wall clock, or the hub's injected `now`. */
|
|
2132
|
+
timestampSeconds: number;
|
|
2133
|
+
/** Where the delivery is posted. */
|
|
2134
|
+
url: string;
|
|
2135
|
+
/** The endpoint's secret, if it has one. */
|
|
2136
|
+
secret: string | undefined;
|
|
2137
|
+
/** The endpoint's `signUrl` (the public URL the receiver verifies against), else `url`. */
|
|
2138
|
+
signUrl: string;
|
|
2139
|
+
/** Form parameters, when the body is form-encoded (Twilio signs these, not the body). */
|
|
2140
|
+
form: Record<string, string> | undefined;
|
|
2141
|
+
/** Stable event type and metadata captured with the message; avoids signer side-channel state. */
|
|
2142
|
+
type: string;
|
|
2143
|
+
tags: Readonly<Record<string, string>>;
|
|
2144
|
+
};
|
|
2145
|
+
/** Headers to add to one delivery. */
|
|
2146
|
+
type WebhookSigner = (input: SignInput) => Record<string, string> | Promise<Record<string, string>>;
|
|
2147
|
+
/** Named signers for the vendor schemes the catalog needs. */
|
|
2148
|
+
declare const signers: {
|
|
2149
|
+
/** No signature. */
|
|
2150
|
+
none: () => WebhookSigner;
|
|
2151
|
+
/** Svix (`svix-id`, `svix-timestamp`, `svix-signature: v1,<b64>`): Junction, Flex, Resend. */
|
|
2152
|
+
svix: (options?: {
|
|
2153
|
+
prefix?: "svix" | "webhook";
|
|
2154
|
+
}) => WebhookSigner;
|
|
2155
|
+
/** `<header>: t=<unix>,v1=<hex HMAC-SHA256(secret, "t.body")>`: Stripe, Persona, Fullscript. */
|
|
2156
|
+
timestamped: (header?: string) => WebhookSigner;
|
|
2157
|
+
/** `X-Twilio-Signature` over the public URL plus the sorted form parameters. */
|
|
2158
|
+
twilio: () => WebhookSigner;
|
|
2159
|
+
/** The secret itself in a header (optionally templated): RxVortex, Pharmetika, AHA `Token <s>`. */
|
|
2160
|
+
header: (header: string, format?: (secret: string) => string) => WebhookSigner;
|
|
2161
|
+
/** Anything else: the service computes the headers itself. */
|
|
2162
|
+
custom: (sign: WebhookSigner) => WebhookSigner;
|
|
2163
|
+
};
|
|
2164
|
+
type WebhookEndpoint = {
|
|
2165
|
+
/** Stable id; generated when omitted. */
|
|
2166
|
+
id?: string;
|
|
2167
|
+
url: string;
|
|
2168
|
+
secret?: string;
|
|
2169
|
+
/** Event types to deliver; omit or include `"*"` for every type. */
|
|
2170
|
+
events?: string[];
|
|
2171
|
+
/** Deliver only messages whose tags include all of these (e.g. `{ account: "mso" }`). */
|
|
2172
|
+
tags?: Record<string, string>;
|
|
2173
|
+
/** The public URL the receiver verifies signatures against (Twilio), when it differs. */
|
|
2174
|
+
signUrl?: string;
|
|
2175
|
+
headers?: Record<string, string>;
|
|
2176
|
+
};
|
|
2177
|
+
type WebhookMessage = {
|
|
2178
|
+
id: string;
|
|
2179
|
+
namespace: string;
|
|
2180
|
+
type: string;
|
|
2181
|
+
body: string;
|
|
2182
|
+
contentType: string;
|
|
2183
|
+
tags: Record<string, string>;
|
|
2184
|
+
headers?: Record<string, string>;
|
|
2185
|
+
/** Wall-clock ISO-8601 time of publication. */
|
|
2186
|
+
publishedAt: string;
|
|
2187
|
+
};
|
|
2188
|
+
type WebhookAttempt = {
|
|
2189
|
+
attempt: number;
|
|
2190
|
+
at: string;
|
|
2191
|
+
status: number | null;
|
|
2192
|
+
error: string | null;
|
|
2193
|
+
durationMs: number;
|
|
2194
|
+
/** Exact receiver response body, when one was returned. */
|
|
2195
|
+
responseBody?: string | null;
|
|
2196
|
+
};
|
|
2197
|
+
type WebhookDelivery = {
|
|
2198
|
+
id: string;
|
|
2199
|
+
messageId: string;
|
|
2200
|
+
namespace: string;
|
|
2201
|
+
type: string;
|
|
2202
|
+
endpointId: string;
|
|
2203
|
+
url: string;
|
|
2204
|
+
state: "pending" | "delivered" | "failed" | "dropped";
|
|
2205
|
+
attempts: WebhookAttempt[];
|
|
2206
|
+
};
|
|
2207
|
+
/** A delivery-level fault: what happens to the next `count` messages in a namespace. */
|
|
2208
|
+
type WebhookFault = {
|
|
2209
|
+
mode: "duplicate" | "reorder" | "drop";
|
|
2210
|
+
/** Messages affected; default 1. */
|
|
2211
|
+
count?: number;
|
|
2212
|
+
};
|
|
2213
|
+
type PublishInput = {
|
|
2214
|
+
namespace: string;
|
|
2215
|
+
type: string;
|
|
2216
|
+
/** Exact body; objects are JSON-encoded. */
|
|
2217
|
+
body: string | Record<string, unknown> | unknown[];
|
|
2218
|
+
/** Default `application/json`, or form-encoded when `form` is given. */
|
|
2219
|
+
contentType?: string;
|
|
2220
|
+
/** Form parameters, when the vendor posts `application/x-www-form-urlencoded`. */
|
|
2221
|
+
form?: Record<string, string>;
|
|
2222
|
+
tags?: Record<string, string>;
|
|
2223
|
+
/** Message-specific delivery headers, captured as part of durable message state. */
|
|
2224
|
+
headers?: Record<string, string>;
|
|
2225
|
+
/** Message id; generated when omitted. */
|
|
2226
|
+
id?: string;
|
|
2227
|
+
};
|
|
2228
|
+
type WebhookHubOptions = {
|
|
2229
|
+
signer: WebhookSigner;
|
|
2230
|
+
/**
|
|
2231
|
+
* Delay before each attempt, in ms; the first entry delays the first attempt. Default:
|
|
2232
|
+
* immediately, then 5 s, 5 min, 30 min, 2 h. An empty list means one immediate attempt and no
|
|
2233
|
+
* retries (never zero attempts).
|
|
2234
|
+
*/
|
|
2235
|
+
retryDelaysMs?: readonly number[];
|
|
2236
|
+
/** Abort an attempt after this long. Default 15 s. */
|
|
2237
|
+
timeoutMs?: number;
|
|
2238
|
+
/** Which receiver statuses count as delivered. Default 2xx. */
|
|
2239
|
+
delivered?: (status: number) => boolean;
|
|
2240
|
+
/** Endpoints every namespace delivers to (from `--webhook-url`). */
|
|
2241
|
+
endpoints?: WebhookEndpoint[];
|
|
2242
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
2243
|
+
/** Called in-process for every published message, delivered or not. */
|
|
2244
|
+
onMessage?: (message: WebhookMessage) => void;
|
|
2245
|
+
/** Messages kept per namespace for `GET /__admin/webhooks/events`. Default 500. */
|
|
2246
|
+
keep?: number;
|
|
2247
|
+
/** Injectable clock (default wall clock) for signatures and attempt records. */
|
|
2248
|
+
now?: () => number;
|
|
2249
|
+
/** Injectable deterministic identifier source. Receives `"msg_"` or `"dlv_"`. */
|
|
2250
|
+
id?: (prefix: string) => string;
|
|
2251
|
+
/** Injectable scheduler used by timeouts and retries. */
|
|
2252
|
+
schedule?: (callback: () => void, delayMs: number) => unknown;
|
|
2253
|
+
cancel?: (handle: unknown) => void;
|
|
2254
|
+
};
|
|
2255
|
+
type WebhookHub = {
|
|
2256
|
+
observeDeliveries(listener: (event: {
|
|
2257
|
+
namespace: string;
|
|
2258
|
+
state: string;
|
|
2259
|
+
deliveryId: string;
|
|
2260
|
+
}) => void): () => void;
|
|
2261
|
+
setTransportPolicy(policy: OwnedTransport): void;
|
|
2262
|
+
pending(namespace: string): number;
|
|
2263
|
+
snapshot(namespace: string): unknown;
|
|
2264
|
+
exportSnapshot(handle: unknown): unknown;
|
|
2265
|
+
importSnapshot(namespace: string, document: unknown): unknown;
|
|
2266
|
+
restore(namespace: string, snapshot: unknown): void;
|
|
2267
|
+
publish(input: PublishInput): WebhookMessage;
|
|
2268
|
+
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
2269
|
+
setEndpoints(namespace: string, endpoints: WebhookEndpoint[]): WebhookEndpoint[];
|
|
2270
|
+
/** The endpoints a namespace delivers to: its own, plus the global ones. */
|
|
2271
|
+
endpoints(namespace: string): WebhookEndpoint[];
|
|
2272
|
+
messages(namespace?: string): WebhookMessage[];
|
|
2273
|
+
deliveries(namespace?: string): WebhookDelivery[];
|
|
2274
|
+
replay(deliveryId: string): Promise<WebhookDelivery | undefined>;
|
|
2275
|
+
/** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
|
|
2276
|
+
flush(): Promise<void>;
|
|
2277
|
+
/** Resolve once nothing is in flight. */
|
|
2278
|
+
idle(namespace?: string): Promise<void>;
|
|
2279
|
+
fault(namespace: string, fault: WebhookFault): void;
|
|
2280
|
+
clear(namespace?: string): void;
|
|
2281
|
+
};
|
|
2282
|
+
declare const createWebhookHub: (options: WebhookHubOptions) => WebhookHub;
|
|
2283
|
+
/**
|
|
2284
|
+
* The standard webhook admin routes, for a runtime that owns `hub`:
|
|
2285
|
+
*
|
|
2286
|
+
* - `GET /webhooks` deliveries, `GET /webhooks/events` published messages (with bodies:
|
|
2287
|
+
* they are the mock's own output), `POST /webhooks/:id/replay`, `POST /webhooks/flush`
|
|
2288
|
+
* - `GET|PUT|DELETE /webhook-endpoints` per namespace
|
|
2289
|
+
* - `POST /webhooks/faults {mode: duplicate|reorder|drop, count?}`
|
|
2290
|
+
*/
|
|
2291
|
+
declare const webhookAdminRoutes: (hub: WebhookHub) => AdminRoutes;
|
|
2292
|
+
|
|
2293
|
+
/**
|
|
2294
|
+
* Stamped on every response the runtime returns — vendor, fault, admin and health alike —
|
|
2295
|
+
* as `<service>@<version>; ns=<namespace>`, so a consumer can tell the mock from the vendor.
|
|
2296
|
+
*/
|
|
2297
|
+
declare const MOCKINGBIRD_HEADER = "x-mockingbird";
|
|
2298
|
+
/** Selects a named copy-on-write history branch. `main` is the compatibility default. */
|
|
2299
|
+
declare const BRANCH_HEADER: "x-mockingbird-branch";
|
|
2300
|
+
/** Reads a historical checkpoint. With a branch header, initializes that branch from it. */
|
|
2301
|
+
declare const AT_HEADER: "x-mockingbird-at";
|
|
2302
|
+
/** Identifies the resulting checkpoint on successful mutations. */
|
|
2303
|
+
declare const CHECKPOINT_HEADER = "x-mockingbird-checkpoint";
|
|
2304
|
+
/** On a response a fault rule answered or shaped: that rule's id. */
|
|
2305
|
+
declare const FAULT_HEADER: "x-mockingbird-fault";
|
|
2306
|
+
/** On a response to a request that asked for a snapshot: the snapshot's id. */
|
|
2307
|
+
declare const SNAPSHOT_HEADER: "x-mockingbird-snapshot";
|
|
2308
|
+
/** What the runtime needs from a service: a Fetch handler it can reset. */
|
|
2309
|
+
type ServiceInstance = FetchAPI & {
|
|
2310
|
+
reset(): Promise<void>;
|
|
2311
|
+
};
|
|
2312
|
+
/** Everything an instance shares with the runtime that owns it. */
|
|
2313
|
+
type InstanceContext = {
|
|
2314
|
+
/** Storage namespace for this instance's records. */
|
|
2315
|
+
namespace: string;
|
|
2316
|
+
/** The public namespace name a request selects it by. */
|
|
2317
|
+
adminPrefix: string;
|
|
2318
|
+
publicNamespace: string;
|
|
2319
|
+
sqlite: SqliteClient;
|
|
2320
|
+
clock: Clock;
|
|
2321
|
+
rng: Rng;
|
|
2322
|
+
};
|
|
2323
|
+
type RuntimeOptions<T extends ServiceInstance> = {
|
|
2324
|
+
upstream?: UpstreamOptions;
|
|
2325
|
+
/** Capture an accepted real send without invoking an emulated handler. */
|
|
2326
|
+
onForwardResponse?: (instance: T, request: Request, response: Response) => Promise<void>;
|
|
2327
|
+
/** Reject unsupported forwarding surfaces before any network request. */
|
|
2328
|
+
canForward?: (request: Request) => boolean;
|
|
2329
|
+
/** Service name, e.g. `"junction"`. Also the default namespace's storage key. */
|
|
2330
|
+
name: string;
|
|
2331
|
+
/** Build the instance backing one namespace. Called once per namespace, on first use. */
|
|
2332
|
+
create: (context: InstanceContext) => T;
|
|
2333
|
+
/** The vendor contract, used to name each request's operation in logs, metrics and faults. */
|
|
2334
|
+
document?: OpenAPIDocument;
|
|
2335
|
+
/** Shared by every namespace. Defaults to a fresh `@crvouga/mockingbird-service-sqlite`. */
|
|
2336
|
+
sqlite?: SqliteClient;
|
|
2337
|
+
/** Defaults to a live clock over `Date.now`. */
|
|
2338
|
+
clock?: Clock;
|
|
2339
|
+
/**
|
|
2340
|
+
* Where this boot's generated ids start. Counters restart at 0 on every boot, so by default
|
|
2341
|
+
* every boot reissues the previous boot's ids; a different base gives an unrelated run of ids
|
|
2342
|
+
* (a reset returns to the same run). A non-negative integer, or `"boot"` for one derived from
|
|
2343
|
+
* the wall clock when the runtime is created. Default `0`: the same ids on every boot.
|
|
2344
|
+
*/
|
|
2345
|
+
idBase?: IdBase;
|
|
2346
|
+
/** Seeds every random choice the runtime makes (fault rates, jitter). Default `0`. */
|
|
2347
|
+
seed?: number | string;
|
|
2348
|
+
/**
|
|
2349
|
+
* Admin controls to boot with: clock, latency, faults, a starting snapshot, credential
|
|
2350
|
+
* mappings and per-namespace overrides. `reset` returns a namespace to exactly this.
|
|
2351
|
+
*/
|
|
2352
|
+
adminControls?: MockControls;
|
|
2353
|
+
/** Extra `GET /__admin/health` fields, such as the loaded corpus version. */
|
|
2354
|
+
describe?: () => Record<string, unknown>;
|
|
2355
|
+
/** Service-specific admin routes, given the runtime so they can reach any namespace. */
|
|
2356
|
+
admin?: (runtime: ServiceRuntime<T>) => AdminRoutes;
|
|
2357
|
+
/** Reserved tree for every internal HTTP endpoint, UI and namespace carrier. Default /__admin. */
|
|
2358
|
+
adminPrefix?: string;
|
|
2359
|
+
/**
|
|
2360
|
+
* A namespace's clock was moved — by `/__admin/clock`, a header, a restore, a reset, or a
|
|
2361
|
+
* wait spent in virtual time. Run whatever the service schedules by that clock, so what
|
|
2362
|
+
* came due is seen at once and not on the next request. Moving the base clock in process
|
|
2363
|
+
* (`runtime.clock`) is the caller's own doing and is not reported here.
|
|
2364
|
+
*/
|
|
2365
|
+
onClockChange?: (namespace: string) => void;
|
|
2366
|
+
/** Invalidate transient handles before replacing an existing instance's stored state. */
|
|
2367
|
+
beforeRestore?: (instance: T) => void;
|
|
2368
|
+
/** Require this value in `x-mockingbird-admin-key` on admin data routes. */
|
|
2369
|
+
adminKey?: string;
|
|
2370
|
+
/** Structured request log sink, called once per request. */
|
|
2371
|
+
onLog?: (entry: RequestLog) => void;
|
|
2372
|
+
/** Requests each namespace's journal keeps (`GET /__admin/requests`). Default 1000; 0 turns it off. */
|
|
2373
|
+
journalSize?: number;
|
|
2374
|
+
/** Reported in the `x-mockingbird` header. Default: the bundled package's version. */
|
|
2375
|
+
version?: string;
|
|
2376
|
+
/**
|
|
2377
|
+
* The vendor chip `GET <adminPrefix>/ui/brands.json` serves. Default: the brand bundled into
|
|
2378
|
+
* this build; a service built on the shared `@crvouga/mockingbird-runtime` passes its own
|
|
2379
|
+
* (scripts/bundle-service-runtime.ts).
|
|
2380
|
+
*/
|
|
2381
|
+
adminBrand?: AdminBrand;
|
|
2382
|
+
/**
|
|
2383
|
+
* The vendor credential a request carries (API key, token, account SID, AWS access key
|
|
2384
|
+
* id), for SDKs that cannot send `x-mockingbird-namespace`: a suite maps credentials to
|
|
2385
|
+
* namespaces with `PUT /__admin/credentials`. See `bearerToken`, `basicAuth`,
|
|
2386
|
+
* `sigV4AccessKeyId`.
|
|
2387
|
+
*/
|
|
2388
|
+
credential?: (request: Request) => string | undefined;
|
|
2389
|
+
/**
|
|
2390
|
+
* Named faults, switched on with `POST /__admin/faults {"preset": "<name>"}`. They join the
|
|
2391
|
+
* standard presets every mock has; one with a standard name replaces it.
|
|
2392
|
+
*/
|
|
2393
|
+
presets?: Record<string, FaultPreset>;
|
|
2394
|
+
/** Outbound webhooks; adds the `/__admin/webhooks*` routes and clears on reset. */
|
|
2395
|
+
webhooks?: WebhookHub;
|
|
2396
|
+
/** Retained time-travel checkpoints per namespace. Default 1,000. */
|
|
2397
|
+
maxCheckpoints?: number;
|
|
2398
|
+
/** Injectable process IO used for observability and delays; logical service time uses `clock`. */
|
|
2399
|
+
io?: Partial<RuntimeIO>;
|
|
2400
|
+
/**
|
|
2401
|
+
* Collections the admin UI should show even before any row exists.
|
|
2402
|
+
* Stored collections are always included, whether or not they are declared.
|
|
2403
|
+
* A function may describe collections that depend on the live instance.
|
|
2404
|
+
*/
|
|
2405
|
+
state?: readonly StateDeclaration[] | ((api: T, namespace: string) => readonly StateDeclaration[]);
|
|
2406
|
+
/**
|
|
2407
|
+
* Panels added to the default admin UI, or a function that replaces the document.
|
|
2408
|
+
* `render` receives `defaultHtml()` so a bespoke UI can wrap the shared shell.
|
|
2409
|
+
*/
|
|
2410
|
+
adminUi?: AdminUi;
|
|
2411
|
+
};
|
|
2412
|
+
type RuntimeIO = {
|
|
2413
|
+
wallNow(): number;
|
|
2414
|
+
monotonicNow(): number;
|
|
2415
|
+
/** Wait on the wall clock. Rejects with the signal's reason when it aborts first. */
|
|
2416
|
+
sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
2417
|
+
};
|
|
2418
|
+
type ServiceTimelineState = Readonly<{
|
|
2419
|
+
snapshot: NamespaceSnapshot;
|
|
2420
|
+
/** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
|
|
2421
|
+
clock: Readonly<ClockState & {
|
|
2422
|
+
pinned?: boolean;
|
|
2423
|
+
}>;
|
|
2424
|
+
rngState: number;
|
|
2425
|
+
}>;
|
|
2426
|
+
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
2427
|
+
type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
|
|
2428
|
+
/** Stop package-owned lifecycle timers when a supervisor closes the runtime. */
|
|
2429
|
+
stop?(): void;
|
|
2430
|
+
readonly name: string;
|
|
2431
|
+
/** Same-origin Fetch handler and App Router method exports, sharing this runtime. */
|
|
2432
|
+
mount(prefix: string): MountedAPI;
|
|
2433
|
+
readonly sqlite: SqliteClient;
|
|
2434
|
+
readonly clock: Clock;
|
|
2435
|
+
/**
|
|
2436
|
+
* Change the id base ({@link RuntimeOptions.idBase}) for ids generated from now on, and
|
|
2437
|
+
* return the number it resolved to. Set it before the first request so a boot's ids stay
|
|
2438
|
+
* one run.
|
|
2439
|
+
*/
|
|
2440
|
+
setIdBase(base: IdBase): number;
|
|
2441
|
+
readonly faults: FaultRegistry;
|
|
2442
|
+
/** Baseline response time per namespace. */
|
|
2443
|
+
readonly latency: LatencyRegistry;
|
|
2444
|
+
readonly metrics: Metrics;
|
|
2445
|
+
readonly journal: Journal;
|
|
2446
|
+
readonly rng: Rng;
|
|
2447
|
+
readonly credentials: CredentialRegistry;
|
|
2448
|
+
/** The webhook hub, when the service has outbound webhooks. */
|
|
2449
|
+
readonly webhooks: WebhookHub | undefined;
|
|
2450
|
+
/**
|
|
2451
|
+
* A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
|
|
2452
|
+
* offset and a freeze of its own, so moving one never moves another.
|
|
2453
|
+
*/
|
|
2454
|
+
namespaceClock(namespace?: string): Clock;
|
|
2455
|
+
namespaceOf(request: Request): string;
|
|
2456
|
+
/** Opaque in-process checkpoint including records, controls and webhook history. */
|
|
2457
|
+
fleetSnapshot(namespace: string): object;
|
|
2458
|
+
fleetRestore(snapshot: unknown, namespace: string): void;
|
|
2459
|
+
fleetExport(snapshot: unknown): SnapshotDocument;
|
|
2460
|
+
fleetImport(namespace: string, document: unknown): object;
|
|
2461
|
+
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
2462
|
+
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
|
|
2463
|
+
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
2464
|
+
instance(namespace?: string): T;
|
|
2465
|
+
/** Public names of every namespace created so far. */
|
|
2466
|
+
namespaces(): string[];
|
|
2467
|
+
/** Return one namespace to its boot state, or every namespace with `"*"`. */
|
|
2468
|
+
reset(namespace?: string): Promise<void>;
|
|
2469
|
+
/** Reset a namespace and forget it, releasing everything it held. */
|
|
2470
|
+
dropNamespace(namespace: string): Promise<void>;
|
|
2471
|
+
/** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
|
|
2472
|
+
controls(namespace?: string): ControlState & {
|
|
2473
|
+
snapshots: SnapshotInfo[];
|
|
2474
|
+
};
|
|
2475
|
+
/** Apply a control document to a namespace. A given key replaces that control. */
|
|
2476
|
+
configure(controls: NamespaceControls$1, namespace?: string): ControlState & {
|
|
2477
|
+
snapshots: SnapshotInfo[];
|
|
2478
|
+
};
|
|
2479
|
+
/** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
|
|
2480
|
+
readonly snapshots: {
|
|
2481
|
+
/** Capture the namespace now. Reusing a name replaces that snapshot. */
|
|
2482
|
+
create(options?: {
|
|
2483
|
+
name?: string;
|
|
2484
|
+
namespace?: string;
|
|
2485
|
+
}): SnapshotInfo;
|
|
2486
|
+
list(namespace?: string): SnapshotInfo[];
|
|
2487
|
+
/** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
|
|
2488
|
+
export(id: string, namespace?: string): SnapshotDocument;
|
|
2489
|
+
/** Store a document as a snapshot without restoring it. */
|
|
2490
|
+
import(document: SnapshotDocument, options?: {
|
|
2491
|
+
name?: string;
|
|
2492
|
+
namespace?: string;
|
|
2493
|
+
}): SnapshotInfo;
|
|
2494
|
+
restore(id: string, namespace?: string): void;
|
|
2495
|
+
delete(id: string, namespace?: string): boolean;
|
|
2496
|
+
};
|
|
2497
|
+
/** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
|
|
2498
|
+
headers(input: ControlHeaderInput): Record<string, string>;
|
|
2499
|
+
snapshot(namespace?: string): NamespaceSnapshot;
|
|
2500
|
+
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
2501
|
+
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
2502
|
+
checkpoint(namespace?: string, branch?: string): ServiceCheckpoint;
|
|
2503
|
+
/** Create an isolated branch, optionally from a historical checkpoint. */
|
|
2504
|
+
branch(name: string, options?: {
|
|
2505
|
+
namespace?: string;
|
|
2506
|
+
at?: string;
|
|
2507
|
+
}): ServiceCheckpoint;
|
|
2508
|
+
/** Restore a branch, clock, and PRNG to a checkpoint. */
|
|
2509
|
+
checkout(checkpoint: string, options?: {
|
|
2510
|
+
namespace?: string;
|
|
2511
|
+
branch?: string;
|
|
2512
|
+
}): void;
|
|
2513
|
+
/** Inspect the retained history for a namespace. */
|
|
2514
|
+
timeline(namespace?: string): Timeline<ServiceTimelineState>;
|
|
2515
|
+
/** Collections in one namespace: declared shape plus what the rows actually hold. */
|
|
2516
|
+
state(namespace?: string): StateView;
|
|
2517
|
+
};
|
|
2518
|
+
/** The namespace used when a request names none. */
|
|
2519
|
+
declare const DEFAULT_NAMESPACE = "default";
|
|
2520
|
+
/** Notify only after a durable mutation commits. No-op outside an active runtime request. */
|
|
2521
|
+
declare const markMutationAccepted: (request: Request, notes?: ResponseNotes) => void;
|
|
2522
|
+
/** Preserve request-bound effects and acceptance when a provider must rebuild a Request. */
|
|
2523
|
+
declare const forwardRequestContext: (source: Request, target: Request) => Request;
|
|
2524
|
+
/**
|
|
2525
|
+
* The fault effects (`{"effect": "created_but_500"}` rules) that fired for this request, in
|
|
2526
|
+
* rule order. Handlers read this to switch on a named vendor misbehaviour.
|
|
2527
|
+
*/
|
|
2528
|
+
declare const faultEffects: (request: Request) => {
|
|
2529
|
+
name: string;
|
|
2530
|
+
params: Record<string, unknown>;
|
|
2531
|
+
}[];
|
|
2532
|
+
/** Whether the named effect fired for this request; its params when it did. */
|
|
2533
|
+
declare const faultEffect: (request: Request, name: string) => Record<string, unknown> | undefined;
|
|
2534
|
+
/**
|
|
2535
|
+
* Thrown by an in-process `runtime.fetch` when a `drop` fault fires: the same thing a real
|
|
2536
|
+
* `fetch` does when the connection dies mid-request. A served mock destroys the socket.
|
|
2537
|
+
*/
|
|
2538
|
+
declare class DroppedConnectionError extends TypeError {
|
|
2539
|
+
readonly code = "MOCKINGBIRD_DROP";
|
|
2540
|
+
/** Why the connection died: a `drop`, a `timeout` that ran out, or a `truncate`d body. */
|
|
2541
|
+
readonly reason: "drop" | "timeout" | "truncate";
|
|
2542
|
+
constructor(reason?: "drop" | "timeout" | "truncate");
|
|
2543
|
+
}
|
|
2544
|
+
/**
|
|
2545
|
+
* Wrap a service in the shared Mockingbird contract: an unauthenticated `/__admin/health`,
|
|
2546
|
+
* the `/__admin/*` control plane, per-request namespaces, a controllable clock,
|
|
2547
|
+
* fault injection, and request metrics.
|
|
2548
|
+
*
|
|
2549
|
+
* Namespaces isolate parallel workers inside one process: each gets its own
|
|
2550
|
+
* instance over the same SQLite database, so rows are partitioned by namespace
|
|
2551
|
+
* and a worker's reset or restore never touches another worker's data.
|
|
2552
|
+
*/
|
|
2553
|
+
declare const createRuntime: <T extends ServiceInstance>(options: RuntimeOptions<T>) => ServiceRuntime<T>;
|
|
2554
|
+
|
|
2555
|
+
/**
|
|
2556
|
+
* What a comms vendor "sent" (an SMS, an email, a Slack post), kept per namespace so a
|
|
2557
|
+
* suite can assert on it and read codes and links out of it. Stored in SQLite beside the
|
|
2558
|
+
* service's other records, so reset, snapshot and restore cover it.
|
|
2559
|
+
*/
|
|
2560
|
+
type OutboxItem = {
|
|
2561
|
+
id: string;
|
|
2562
|
+
/** Recipient(s): a phone number, email address, channel or webhook path. */
|
|
2563
|
+
to: string | string[];
|
|
2564
|
+
/** ISO-8601 on the mock clock. */
|
|
2565
|
+
createdAt: string;
|
|
2566
|
+
};
|
|
2567
|
+
type OutboxQuery = {
|
|
2568
|
+
/** Case-insensitive match on any recipient. */
|
|
2569
|
+
to?: string;
|
|
2570
|
+
/** Only items at or after this instant (epoch ms). */
|
|
2571
|
+
since?: number;
|
|
2572
|
+
/** Extra filter over the item. */
|
|
2573
|
+
where?: (item: Record<string, unknown>) => boolean;
|
|
2574
|
+
limit?: number;
|
|
2575
|
+
};
|
|
2576
|
+
declare class OutboxStore<T extends OutboxItem = OutboxItem> {
|
|
2577
|
+
private readonly items;
|
|
2578
|
+
constructor(sqlite: SqliteClient, namespace: string, name?: string);
|
|
2579
|
+
record(item: T): T;
|
|
2580
|
+
get(id: string): T | undefined;
|
|
2581
|
+
update(id: string, item: T): void;
|
|
2582
|
+
/** Oldest first, so a suite reads messages in the order they were sent. */
|
|
2583
|
+
list(query?: OutboxQuery): T[];
|
|
2584
|
+
}
|
|
2585
|
+
/** Epoch ms from `since=` given as epoch ms or ISO-8601; `null` when malformed. */
|
|
2586
|
+
declare const parseSince: (value: string | null) => number | undefined | null;
|
|
2587
|
+
/**
|
|
2588
|
+
* `GET /__admin/outbox?to=&since=&limit=` (plus `GET /__admin/outbox/:id`) over the outbox
|
|
2589
|
+
* of the calling namespace. `filter` adds service-specific query parameters (e.g. Resend's
|
|
2590
|
+
* `tag=`, Slack's `channel=`).
|
|
2591
|
+
*/
|
|
2592
|
+
declare const outboxAdminRoutes: <S extends ServiceInstance>(runtime: ServiceRuntime<S>, pick: (instance: S) => OutboxStore<OutboxItem>, filter?: (params: URLSearchParams) => ((item: Record<string, unknown>) => boolean) | undefined) => AdminRoutes;
|
|
2593
|
+
/** Every `href` in an HTML document, in order, entity-decoded, without duplicates. */
|
|
2594
|
+
declare const extractLinks: (html: string) => string[];
|
|
2595
|
+
/** Numeric codes of `length` digits (default 4–8) in text, as Mailosaur's `codes[]` reports them. */
|
|
2596
|
+
declare const extractCodes: (text: string, length?: number) => string[];
|
|
2597
|
+
|
|
2598
|
+
/**
|
|
2599
|
+
* A minimal S3 `PutObject` for mocks whose vendor hands the app an `s3://` object (GxG
|
|
2600
|
+
* results, Daily transcripts): the mock writes the object into the stack's local S3
|
|
2601
|
+
* (s3rver, MinIO) so the app's own `GetObject` / `CopyObject` finds it. Path-style
|
|
2602
|
+
* addressing, SigV4-signed, no SDK.
|
|
2603
|
+
*/
|
|
2604
|
+
type S3Target = {
|
|
2605
|
+
/** e.g. `http://127.0.0.1:4569`. */
|
|
2606
|
+
endpoint: string;
|
|
2607
|
+
bucket: string;
|
|
2608
|
+
region?: string;
|
|
2609
|
+
accessKeyId?: string;
|
|
2610
|
+
secretAccessKey?: string;
|
|
2611
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
2612
|
+
};
|
|
2613
|
+
/** SigV4 headers for one request (service `s3`, unsigned query, signed payload hash). */
|
|
2614
|
+
declare const signV4: (input: {
|
|
2615
|
+
method: string;
|
|
2616
|
+
url: URL;
|
|
2617
|
+
body: Uint8Array;
|
|
2618
|
+
region: string;
|
|
2619
|
+
service: string;
|
|
2620
|
+
accessKeyId: string;
|
|
2621
|
+
secretAccessKey: string;
|
|
2622
|
+
headers?: Record<string, string>;
|
|
2623
|
+
now?: Date;
|
|
2624
|
+
}) => Promise<Record<string, string>>;
|
|
2625
|
+
/** Write one object; resolves to its `s3://bucket/key` URI. Throws on a non-2xx answer. */
|
|
2626
|
+
declare const putObject: (target: S3Target, key: string, body: Uint8Array | string, contentType?: string) => Promise<string>;
|
|
2627
|
+
|
|
2628
|
+
/**
|
|
2629
|
+
* HMAC and encoding primitives for vendor webhook signatures, over WebCrypto so they run
|
|
2630
|
+
* wherever the mocks do (Node, Bun, workers).
|
|
2631
|
+
*/
|
|
2632
|
+
type HmacAlgorithm = "SHA-1" | "SHA-256" | "SHA-512";
|
|
2633
|
+
type ByteEncoding = "hex" | "base64";
|
|
2634
|
+
declare const toBase64: (bytes: ArrayBuffer | Uint8Array) => string;
|
|
2635
|
+
declare const fromBase64: (value: string) => Uint8Array;
|
|
2636
|
+
declare const toHex: (bytes: ArrayBuffer | Uint8Array) => string;
|
|
2637
|
+
/** HMAC of `message` under `key` (a UTF-8 string or raw bytes), encoded as hex or base64. */
|
|
2638
|
+
declare const hmac: (algorithm: HmacAlgorithm, key: string | Uint8Array, message: string | Uint8Array, encoding?: ByteEncoding) => Promise<string>;
|
|
2639
|
+
/** SHA digest of `input`, hex-encoded. */
|
|
2640
|
+
declare const sha: (algorithm: "SHA-1" | "SHA-256" | "SHA-512", input: string | Uint8Array) => Promise<string>;
|
|
2641
|
+
/**
|
|
2642
|
+
* The raw key of a Svix-style secret: `whsec_<base64>` (Svix, Resend, Junction) or
|
|
2643
|
+
* `fwhsec_<base64>` (Flex). A bare base64 secret is accepted too.
|
|
2644
|
+
*/
|
|
2645
|
+
declare const svixSecretBytes: (secret: string) => Uint8Array;
|
|
2646
|
+
/** `v1,<base64 HMAC-SHA256>` over `"<id>.<timestamp>.<body>"`, as Svix signs. */
|
|
2647
|
+
declare const signSvix: (secret: string, messageId: string, timestampSeconds: number, body: string) => Promise<string>;
|
|
2648
|
+
/** `t=<unix>,v1=<hex HMAC-SHA256(secret, "<t>.<body>")>`, as Stripe (and Persona) sign. */
|
|
2649
|
+
declare const signTimestamped: (secret: string, timestampSeconds: number, body: string) => Promise<string>;
|
|
2650
|
+
/**
|
|
2651
|
+
* Twilio's `X-Twilio-Signature`: base64 HMAC-SHA1 of the full URL followed by every form
|
|
2652
|
+
* parameter as `key + value`, keys sorted. Sign against the public URL the app is
|
|
2653
|
+
* configured with, not the address the request is actually posted to.
|
|
2654
|
+
*/
|
|
2655
|
+
declare const signTwilio: (authToken: string, url: string, params: Record<string, string>) => Promise<string>;
|
|
2656
|
+
/** Constant-time string comparison, so a verifier in a test double does not leak timing. */
|
|
2657
|
+
declare const timingSafeEqual: (a: string, b: string) => boolean;
|
|
2658
|
+
|
|
2659
|
+
/** Placeholder a bundle carries until `release:publish` writes the real version over it. */
|
|
2660
|
+
declare const UNRELEASED_VERSION = "0.0.0-development";
|
|
2661
|
+
/** The published version of the service package this code is bundled into. */
|
|
2662
|
+
declare const PACKAGE_VERSION: string;
|
|
2663
|
+
|
|
2664
|
+
export { ADMIN_CONTROL_PAGES, ADMIN_DOCS_URL, ADMIN_KEY_HEADER, ADMIN_PREFIX, ADMIN_ROUTE_REFERENCE, AT_HEADER, AwsError, AwsProtocolAPI, BRANCH_HEADER, CHECKPOINT_HEADER, CONTROL_HEADERS, CONTROL_HEADER_REFERENCE, Collection, ControlError, DEFAULT_JOURNAL_SIZE, DEFAULT_NAMESPACE, DroppedConnectionError, FAULT_HEADER, HEALTH_PATH, HttpError, IdSequence, IdempotencyStore, MOCKINGBIRD_HEADER, NAMESPACE_HEADER, OperationRegistryError, OutboxStore, PACKAGE_VERSION, SNAPSHOT_FORMAT, SNAPSHOT_HEADER, STANDARD_ADMIN_ROUTES, STANDARD_FAULT_PRESETS, STATE_FIELD_KINDS, UNRELEASED_VERSION, annotateResponse, anyCredential, assertAdminPrefixAvailable, assertAdminUi, assertSnapshotDocument, awsList, awsMd5, awsPage, awsParseXml, awsRecord, awsRequired, awsXml, awsXmlEscape, basicAuth, bearerToken, bodyIssues, bootSqlite, codePointLength, coerce, controlHeaders, createClock, createControlPlane, createCredentialRegistry, createFaultRegistry, createJournal, createLatencyRegistry, createMetrics, createNamespaceControls, createRng, createRuntime, createService, createWebhookHub, decodeAwsQuery, defineMock, defineOperations, deriveClock, extractCodes, extractLinks, faultEffect, faultEffects, forwardRequestContext, fromBase64, hmac, idBaseOf, inspectState, isAdminPath, issuesByField, jsonRes, jsonRes as jsonResponse, markMutationAccepted, maskCredential, matchNamespacePath, normalizeBootControls, normalizeControls, normalizeFault, normalizeIdBase, opaqueToken, outboxAdminRoutes, ownedTransport, parseControlHeaders, parseDuration, parseFaultHeader, parseForm, parseInstant, parseSince, putObject, recordedIssues, requestBaseUrl, requestFingerprint, resolveAdminPrefix, responseNotes, restoreClock, restoreNamespace, seedFrom, setIdBase, sha, sigV4AccessKeyId, signSvix, signTimestamped, signTwilio, signV4, signers, snapshotNamespace, sortIssues, stableStringify, svixSecretBytes, timingSafeEqual, toBase64, toHex, unsupportedMediaType, verifyOperations, webhookAdminRoutes, withNamespaceRollback };
|
|
2665
|
+
export type { APIOptions, AdminBrand, AdminExtension, AdminPanel, AdminRequest, AdminRoute, AdminRoutes, AdminUi, AwsInput, AwsOperation, AwsProtocolOptions, BasicCredentials, BodyIssue, ByteEncoding, Clock, ClockControls, ClockState, ControlClock, ControlContext, ControlHeaderInput, ControlHeaderName, ControlPlane, ControlSettings, ControlState, CredentialRegistry, DerivedClock, Duration, FaultCandidate, FaultHit, FaultInput, FaultListing, FaultPreset, FaultPresetList, FaultRegistry, FaultRule, FaultSnapshot, FieldResult, FormIssue, HmacAlgorithm, IdBase, IdempotencyErrors, IdempotencyStoreOptions, InstanceContext, Instant, Journal, JournalEntry, JournalQuery, LatencyControls, LatencyRegistry, LatencyRule, LatencyRuleInput, LatencyState, ListRecordsOptions, Metrics, MetricsReport, MockCheckpoint, MockControlState, MockControls, MockCreateOptions, MockInstanceSurface, MockSnapshotInfo, MockSurface, MountedAPI, NamespaceControls, NamespaceControls$1 as NamespaceControlsInput, NamespaceSnapshot, OperationContext, OperationHandler, OperationHandlers, OutboundPolicy, OutboxItem, OutboxQuery, OwnedTransport, ParsedForm, PublishInput, RejectedRequest, RequestBodyValue, RequestControls, RequestLog, ResponseNotes, Rng, RuntimeIO, RuntimeOptions, S3Target, Service, ServiceCheckpoint, ServiceInstance, ServiceOptions, ServiceRuntime, ServiceTimelineState, SignInput, SnapshotDocument, SnapshotInfo, SnapshotStore, StandardAdminRoute, StateCollectionView, StateDeclaration, StateField, StateFieldKind, StateMeta, StatePage, StateRecord, StateView, Stored, UnsupportedMediaType, UpstreamOptions, UpstreamState, WebhookAttempt, WebhookDelivery, WebhookEndpoint, WebhookFault, WebhookHub, WebhookHubOptions, WebhookMessage, WebhookSigner };
|