@crvouga/mockingbird-service-flex 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +213 -0
- package/dist/chunk-Q6AKODYZ.js +380 -0
- package/dist/chunk-Q6AKODYZ.js.map +7 -0
- package/dist/chunk-RB3RIE5G.js +4546 -0
- package/dist/chunk-RB3RIE5G.js.map +7 -0
- package/dist/cli.js +19 -0
- package/dist/cli.js.map +7 -0
- package/dist/index.d.ts +1112 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +7 -0
- package/dist/server.d.ts +1414 -0
- package/dist/server.js +12 -0
- package/dist/server.js.map +7 -0
- package/package.json +97 -0
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,1414 @@
|
|
|
1
|
+
import { Server } from 'node:http';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The single source of time for a service.
|
|
6
|
+
*
|
|
7
|
+
* Every timestamp a mock writes reads from here, so a suite moves time instead of
|
|
8
|
+
* sleeping: appointment windows, result delays and expiries become reachable in
|
|
9
|
+
* milliseconds. A frozen clock also makes timestamps reproducible from a seed.
|
|
10
|
+
*/
|
|
11
|
+
type ClockState$1 = {
|
|
12
|
+
/** Current epoch milliseconds. */
|
|
13
|
+
now: number;
|
|
14
|
+
/** True while time does not advance on its own. */
|
|
15
|
+
frozen: boolean;
|
|
16
|
+
/** Milliseconds this clock adds to its underlying source. */
|
|
17
|
+
offsetMs: number;
|
|
18
|
+
};
|
|
19
|
+
type Clock$1 = {
|
|
20
|
+
now(): number;
|
|
21
|
+
/** Pin the clock to an exact instant, keeping it frozen if it already was. */
|
|
22
|
+
set(epochMs: number): void;
|
|
23
|
+
/** Move the clock forward, or back with a negative delta. */
|
|
24
|
+
advance(deltaMs: number): void;
|
|
25
|
+
/** Stop time at the current instant. */
|
|
26
|
+
freeze(): void;
|
|
27
|
+
/** Resume from the current instant. */
|
|
28
|
+
unfreeze(): void;
|
|
29
|
+
/** Drop back to the underlying source, live. */
|
|
30
|
+
reset(): void;
|
|
31
|
+
state(): ClockState$1;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
35
|
+
type SqliteValue$2 = null | number | bigint | string | Uint8Array | boolean;
|
|
36
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
37
|
+
type SqliteRunResult$2 = {
|
|
38
|
+
changes: number;
|
|
39
|
+
lastInsertRowid: number | bigint;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
43
|
+
*
|
|
44
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
45
|
+
*/
|
|
46
|
+
interface SqliteStatement$2 {
|
|
47
|
+
run(...params: SqliteValue$2[]): SqliteRunResult$2;
|
|
48
|
+
all<T = Record<string, unknown>>(...params: SqliteValue$2[]): T[];
|
|
49
|
+
get<T = Record<string, unknown>>(...params: SqliteValue$2[]): T | undefined;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
53
|
+
*
|
|
54
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
55
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
56
|
+
*/
|
|
57
|
+
interface SqliteClient$2 {
|
|
58
|
+
exec(sql: string): void;
|
|
59
|
+
prepare(sql: string): SqliteStatement$2;
|
|
60
|
+
transaction<T>(fn: () => T): T;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
|
|
65
|
+
* request a percentage fault hits — is reproducible from a seed.
|
|
66
|
+
*
|
|
67
|
+
* mulberry32: small, fast, and stable across runtimes, which matters more here
|
|
68
|
+
* than statistical quality.
|
|
69
|
+
*/
|
|
70
|
+
type Rng$1 = {
|
|
71
|
+
/** Next value in `[0, 1)`. */
|
|
72
|
+
next(): number;
|
|
73
|
+
/** Next integer in `[min, max]`. */
|
|
74
|
+
int(min: number, max: number): number;
|
|
75
|
+
/** Restart the stream from its seed. */
|
|
76
|
+
reset(): void;
|
|
77
|
+
/** Serializable engine state used by deterministic checkpoints. */
|
|
78
|
+
state(): number;
|
|
79
|
+
/** Restore a state previously returned by {@link state}. */
|
|
80
|
+
setState(state: number): void;
|
|
81
|
+
seed: number;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A deliberate failure injected in front of an operation.
|
|
86
|
+
*
|
|
87
|
+
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
88
|
+
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
89
|
+
* appears under load, the 5xx that proves a retry path works.
|
|
90
|
+
*/
|
|
91
|
+
type FaultRule$1 = {
|
|
92
|
+
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
93
|
+
id: string;
|
|
94
|
+
/** Fault only this operation. Omit to match every operation. */
|
|
95
|
+
operationId?: string;
|
|
96
|
+
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
97
|
+
method?: string;
|
|
98
|
+
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
99
|
+
pathPrefix?: string;
|
|
100
|
+
/**
|
|
101
|
+
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
102
|
+
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
103
|
+
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
104
|
+
*/
|
|
105
|
+
namespace?: string;
|
|
106
|
+
/**
|
|
107
|
+
* Status of the injected response. Omit for a rule that only delays (`delayMs` /
|
|
108
|
+
* `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
|
|
109
|
+
* the request then still reaches the service.
|
|
110
|
+
*/
|
|
111
|
+
status?: number;
|
|
112
|
+
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
113
|
+
body?: unknown;
|
|
114
|
+
headers?: Record<string, string>;
|
|
115
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
116
|
+
count?: number;
|
|
117
|
+
/** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
|
|
118
|
+
rate?: number;
|
|
119
|
+
/** Hold the response back this long, to exercise timeouts. */
|
|
120
|
+
delayMs?: number;
|
|
121
|
+
/** Alias of `delayMs`. */
|
|
122
|
+
latencyMs?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
125
|
+
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
126
|
+
*/
|
|
127
|
+
drop?: boolean;
|
|
128
|
+
/**
|
|
129
|
+
* A named service behaviour to switch on for the matching request instead of (or
|
|
130
|
+
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
131
|
+
* read it with `faultEffects(request)`.
|
|
132
|
+
*/
|
|
133
|
+
effect?: string;
|
|
134
|
+
/** Parameters for `effect`. */
|
|
135
|
+
params?: Record<string, unknown>;
|
|
136
|
+
/** From the preset this rule was expanded from, if any. */
|
|
137
|
+
preset?: string;
|
|
138
|
+
};
|
|
139
|
+
/** A fault that fired for one request. */
|
|
140
|
+
type FaultHit$1 = {
|
|
141
|
+
id: string;
|
|
142
|
+
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
143
|
+
response?: Response;
|
|
144
|
+
drop?: boolean;
|
|
145
|
+
effect?: {
|
|
146
|
+
name: string;
|
|
147
|
+
params: Record<string, unknown>;
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
/** What a request looks like to the fault matcher. */
|
|
151
|
+
type FaultCandidate$1 = {
|
|
152
|
+
operationId: string | undefined;
|
|
153
|
+
method: string;
|
|
154
|
+
path: string;
|
|
155
|
+
namespace: string;
|
|
156
|
+
};
|
|
157
|
+
type FaultRegistry$1 = {
|
|
158
|
+
add(rule: FaultRule$1): FaultRule$1;
|
|
159
|
+
list(): (FaultRule$1 & {
|
|
160
|
+
remaining: number | null;
|
|
161
|
+
hits: number;
|
|
162
|
+
})[];
|
|
163
|
+
remove(id: string): boolean;
|
|
164
|
+
clear(): void;
|
|
165
|
+
/**
|
|
166
|
+
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
167
|
+
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
168
|
+
* each matching rule's remaining uses.
|
|
169
|
+
*/
|
|
170
|
+
take(candidate: FaultCandidate$1): Promise<FaultHit$1[]>;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/** One handled request, as the structured log sees it. */
|
|
174
|
+
type RequestLog$1 = {
|
|
175
|
+
service: string;
|
|
176
|
+
namespace: string;
|
|
177
|
+
operationId: string | undefined;
|
|
178
|
+
method: string;
|
|
179
|
+
path: string;
|
|
180
|
+
status: number;
|
|
181
|
+
durationMs: number;
|
|
182
|
+
/** True when the path matched no operation in the contract. */
|
|
183
|
+
unmatched: boolean;
|
|
184
|
+
/** Set when a fault rule produced the response. */
|
|
185
|
+
faultId?: string;
|
|
186
|
+
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
187
|
+
ids?: Record<string, string>;
|
|
188
|
+
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
189
|
+
adopted?: boolean;
|
|
190
|
+
};
|
|
191
|
+
type MetricsReport$1 = {
|
|
192
|
+
requests: number;
|
|
193
|
+
/** Counts keyed `<operationId> <status>`. */
|
|
194
|
+
byOperation: Record<string, number>;
|
|
195
|
+
/**
|
|
196
|
+
* Paths that matched no operation, most frequent first.
|
|
197
|
+
*
|
|
198
|
+
* This is the early-warning signal: a consumer calling something the mock does
|
|
199
|
+
* not implement shows up here as a count, before it fails a suite as a 404.
|
|
200
|
+
*/
|
|
201
|
+
unmatched: {
|
|
202
|
+
method: string;
|
|
203
|
+
path: string;
|
|
204
|
+
count: number;
|
|
205
|
+
}[];
|
|
206
|
+
faults: number;
|
|
207
|
+
totalDurationMs: number;
|
|
208
|
+
};
|
|
209
|
+
type Metrics$1 = {
|
|
210
|
+
record(entry: RequestLog$1): void;
|
|
211
|
+
report(): MetricsReport$1;
|
|
212
|
+
reset(): void;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/** One journal entry: a request log stamped with when (on the mock clock) it was handled. */
|
|
216
|
+
type JournalEntry$1 = RequestLog$1 & {
|
|
217
|
+
at: string;
|
|
218
|
+
};
|
|
219
|
+
type JournalQuery$1 = {
|
|
220
|
+
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
221
|
+
namespace?: string;
|
|
222
|
+
operationId?: string;
|
|
223
|
+
status?: number;
|
|
224
|
+
/** Only entries at or after this instant (epoch ms). */
|
|
225
|
+
since?: number;
|
|
226
|
+
/** At most this many, the most recent kept. */
|
|
227
|
+
limit?: number;
|
|
228
|
+
};
|
|
229
|
+
type Journal$1 = {
|
|
230
|
+
readonly size: number;
|
|
231
|
+
record(entry: JournalEntry$1): void;
|
|
232
|
+
list(query?: JournalQuery$1): JournalEntry$1[];
|
|
233
|
+
/** Forget one namespace's entries, or every namespace's. */
|
|
234
|
+
clear(namespace?: string): void;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
238
|
+
type CredentialRegistry$1 = {
|
|
239
|
+
set(credential: string, namespace: string): void;
|
|
240
|
+
get(credential: string): string | undefined;
|
|
241
|
+
remove(credential: string): boolean;
|
|
242
|
+
clear(): void;
|
|
243
|
+
entries(): {
|
|
244
|
+
credential: string;
|
|
245
|
+
namespace: string;
|
|
246
|
+
}[];
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/** A stable identifier for a point in a {@link Timeline}. */
|
|
250
|
+
type CheckpointId$1 = string;
|
|
251
|
+
/** An immutable node in a timeline's checkpoint DAG. */
|
|
252
|
+
type Checkpoint$1<T> = Readonly<{
|
|
253
|
+
id: CheckpointId$1;
|
|
254
|
+
branch: string;
|
|
255
|
+
parent: CheckpointId$1 | null;
|
|
256
|
+
/** Logical time supplied by the timeline's injected clock. */
|
|
257
|
+
at: number;
|
|
258
|
+
value: T;
|
|
259
|
+
}>;
|
|
260
|
+
type TimelineOptions$1 = {
|
|
261
|
+
/** Logical clock used to stamp checkpoints. Defaults to a deterministic counter. */
|
|
262
|
+
now?: () => number;
|
|
263
|
+
/** Maximum retained checkpoints. Branch heads are never collected. Default 1,000. */
|
|
264
|
+
maxCheckpoints?: number;
|
|
265
|
+
/** Customize deterministic checkpoint IDs. */
|
|
266
|
+
id?: (sequence: number) => CheckpointId$1;
|
|
267
|
+
};
|
|
268
|
+
type CommitOptions$1 = {
|
|
269
|
+
branch?: string;
|
|
270
|
+
/** Parent checkpoint. Defaults to the selected branch's current head. */
|
|
271
|
+
parent?: CheckpointId$1 | null;
|
|
272
|
+
};
|
|
273
|
+
type ForkOptions$1 = {
|
|
274
|
+
/** Checkpoint to fork from. Defaults to the main branch's head. */
|
|
275
|
+
from?: CheckpointId$1;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Small, storage-agnostic checkpoint DAG shared by service runtimes. Its values may be immutable
|
|
279
|
+
* records, namespace images, or copy-on-write SQL engine snapshots.
|
|
280
|
+
*
|
|
281
|
+
* Values are retained by reference. Engines can therefore use persistent/COW snapshots while
|
|
282
|
+
* simpler services can use immutable values. IDs and GC order are deterministic, and all IO
|
|
283
|
+
* (the logical clock) is injected.
|
|
284
|
+
*/
|
|
285
|
+
declare class Timeline$1<T> {
|
|
286
|
+
readonly maxCheckpoints: number;
|
|
287
|
+
private readonly now;
|
|
288
|
+
private readonly makeId;
|
|
289
|
+
private readonly nodes;
|
|
290
|
+
private readonly heads;
|
|
291
|
+
/** Unreferenced nodes in the exact order they became collectible. */
|
|
292
|
+
private readonly evictable;
|
|
293
|
+
/** Branch heads plus explicit retainers. Absent means zero. */
|
|
294
|
+
private readonly references;
|
|
295
|
+
private readonly explicitPins;
|
|
296
|
+
private sequence;
|
|
297
|
+
constructor(options?: TimelineOptions$1);
|
|
298
|
+
/** Capture a new immutable value and move `branch` to it. */
|
|
299
|
+
commit(value: T, options?: CommitOptions$1): Checkpoint$1<T>;
|
|
300
|
+
/** Create a branch pointer without copying its checkpoint value. */
|
|
301
|
+
fork(branch: string, options?: ForkOptions$1): Checkpoint$1<T> | undefined;
|
|
302
|
+
/** Move a branch pointer to an existing checkpoint. */
|
|
303
|
+
checkout(branch: string, id: CheckpointId$1): Checkpoint$1<T>;
|
|
304
|
+
get(id: CheckpointId$1): Checkpoint$1<T>;
|
|
305
|
+
head(branch?: string): Checkpoint$1<T> | undefined;
|
|
306
|
+
hasBranch(branch: string): boolean;
|
|
307
|
+
branches(): Readonly<Record<string, CheckpointId$1>>;
|
|
308
|
+
checkpoints(): readonly Checkpoint$1<T>[];
|
|
309
|
+
/** Number of retained checkpoints without allocating an array. */
|
|
310
|
+
get size(): number;
|
|
311
|
+
/** Pin a checkpoint independently of branch heads (used by compatibility snapshot handles). */
|
|
312
|
+
retain(id: CheckpointId$1): Checkpoint$1<T>;
|
|
313
|
+
/** Release one explicit pin. Branch heads remain pinned until moved or deleted. */
|
|
314
|
+
release(id: CheckpointId$1): boolean;
|
|
315
|
+
deleteBranch(branch: string): boolean;
|
|
316
|
+
/**
|
|
317
|
+
* Deterministically discard oldest unpinned checkpoints. Collection is O(number removed):
|
|
318
|
+
* commits never scan pinned nodes or the retained history. Parents are metadata rather than a
|
|
319
|
+
* storage dependency, so a retained node remains usable after pruning.
|
|
320
|
+
*/
|
|
321
|
+
gc(max?: number): CheckpointId$1[];
|
|
322
|
+
private collect;
|
|
323
|
+
private moveHead;
|
|
324
|
+
private addReference;
|
|
325
|
+
private removeReference;
|
|
326
|
+
private assertBranch;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
331
|
+
*
|
|
332
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
333
|
+
* It is the only contract shared across the whole graph.
|
|
334
|
+
*/
|
|
335
|
+
interface FetchAPI$2 {
|
|
336
|
+
fetch(request: Request): Promise<Response>;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* A point-in-time copy of everything a service namespace holds.
|
|
341
|
+
*
|
|
342
|
+
* All service state lives in the two core tables keyed by namespace, so a snapshot
|
|
343
|
+
* is generic: any service gets per-test rollback without knowing its own schema.
|
|
344
|
+
* Restoring is much cheaper than rebuilding a namespace from a corpus.
|
|
345
|
+
*/
|
|
346
|
+
type NamespaceSnapshot$1 = {
|
|
347
|
+
namespace: string;
|
|
348
|
+
records: {
|
|
349
|
+
collection: string;
|
|
350
|
+
id: string;
|
|
351
|
+
seq: number;
|
|
352
|
+
value: string;
|
|
353
|
+
}[];
|
|
354
|
+
sequences: {
|
|
355
|
+
name: string;
|
|
356
|
+
kind: string;
|
|
357
|
+
value: number;
|
|
358
|
+
}[];
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
type WebhookEndpoint$1 = {
|
|
362
|
+
/** Stable id; generated when omitted. */
|
|
363
|
+
id?: string;
|
|
364
|
+
url: string;
|
|
365
|
+
secret?: string;
|
|
366
|
+
/** Event types to deliver; omit or include `"*"` for every type. */
|
|
367
|
+
events?: string[];
|
|
368
|
+
/** Deliver only messages whose tags include all of these (e.g. `{ account: "mso" }`). */
|
|
369
|
+
tags?: Record<string, string>;
|
|
370
|
+
/** The public URL the receiver verifies signatures against (Twilio), when it differs. */
|
|
371
|
+
signUrl?: string;
|
|
372
|
+
headers?: Record<string, string>;
|
|
373
|
+
};
|
|
374
|
+
type WebhookMessage$1 = {
|
|
375
|
+
id: string;
|
|
376
|
+
namespace: string;
|
|
377
|
+
type: string;
|
|
378
|
+
body: string;
|
|
379
|
+
contentType: string;
|
|
380
|
+
tags: Record<string, string>;
|
|
381
|
+
headers?: Record<string, string>;
|
|
382
|
+
/** Wall-clock ISO-8601 time of publication. */
|
|
383
|
+
publishedAt: string;
|
|
384
|
+
};
|
|
385
|
+
type WebhookAttempt$1 = {
|
|
386
|
+
attempt: number;
|
|
387
|
+
at: string;
|
|
388
|
+
status: number | null;
|
|
389
|
+
error: string | null;
|
|
390
|
+
durationMs: number;
|
|
391
|
+
/** Exact receiver response body, when one was returned. */
|
|
392
|
+
responseBody?: string | null;
|
|
393
|
+
};
|
|
394
|
+
type WebhookDelivery$1 = {
|
|
395
|
+
id: string;
|
|
396
|
+
messageId: string;
|
|
397
|
+
namespace: string;
|
|
398
|
+
type: string;
|
|
399
|
+
endpointId: string;
|
|
400
|
+
url: string;
|
|
401
|
+
state: "pending" | "delivered" | "failed" | "dropped";
|
|
402
|
+
attempts: WebhookAttempt$1[];
|
|
403
|
+
};
|
|
404
|
+
/** A delivery-level fault: what happens to the next `count` messages in a namespace. */
|
|
405
|
+
type WebhookFault$1 = {
|
|
406
|
+
mode: "duplicate" | "reorder" | "drop";
|
|
407
|
+
/** Messages affected; default 1. */
|
|
408
|
+
count?: number;
|
|
409
|
+
};
|
|
410
|
+
type PublishInput$1 = {
|
|
411
|
+
namespace: string;
|
|
412
|
+
type: string;
|
|
413
|
+
/** Exact body; objects are JSON-encoded. */
|
|
414
|
+
body: string | Record<string, unknown> | unknown[];
|
|
415
|
+
/** Default `application/json`, or form-encoded when `form` is given. */
|
|
416
|
+
contentType?: string;
|
|
417
|
+
/** Form parameters, when the vendor posts `application/x-www-form-urlencoded`. */
|
|
418
|
+
form?: Record<string, string>;
|
|
419
|
+
tags?: Record<string, string>;
|
|
420
|
+
/** Message-specific delivery headers, captured as part of durable message state. */
|
|
421
|
+
headers?: Record<string, string>;
|
|
422
|
+
/** Message id; generated when omitted. */
|
|
423
|
+
id?: string;
|
|
424
|
+
};
|
|
425
|
+
type WebhookHub$1 = {
|
|
426
|
+
publish(input: PublishInput$1): WebhookMessage$1;
|
|
427
|
+
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
428
|
+
setEndpoints(namespace: string, endpoints: WebhookEndpoint$1[]): WebhookEndpoint$1[];
|
|
429
|
+
/** The endpoints a namespace delivers to: its own, plus the global ones. */
|
|
430
|
+
endpoints(namespace: string): WebhookEndpoint$1[];
|
|
431
|
+
messages(namespace?: string): WebhookMessage$1[];
|
|
432
|
+
deliveries(namespace?: string): WebhookDelivery$1[];
|
|
433
|
+
replay(deliveryId: string): Promise<WebhookDelivery$1 | undefined>;
|
|
434
|
+
/** Run every pending retry (and release held reordered messages) now. */
|
|
435
|
+
flush(): Promise<void>;
|
|
436
|
+
/** Resolve once nothing is in flight. */
|
|
437
|
+
idle(): Promise<void>;
|
|
438
|
+
fault(namespace: string, fault: WebhookFault$1): void;
|
|
439
|
+
clear(namespace?: string): void;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
/** What the runtime needs from a service: a Fetch handler it can reset. */
|
|
443
|
+
type ServiceInstance$1 = FetchAPI$2 & {
|
|
444
|
+
reset(): Promise<void>;
|
|
445
|
+
};
|
|
446
|
+
type ServiceTimelineState$1 = Readonly<{
|
|
447
|
+
snapshot: NamespaceSnapshot$1;
|
|
448
|
+
clock: Readonly<ReturnType<Clock$1["state"]>>;
|
|
449
|
+
rngState: number;
|
|
450
|
+
}>;
|
|
451
|
+
type ServiceCheckpoint$1 = Checkpoint$1<ServiceTimelineState$1>;
|
|
452
|
+
type ServiceRuntime$1<T extends ServiceInstance$1> = FetchAPI$2 & {
|
|
453
|
+
readonly name: string;
|
|
454
|
+
readonly sqlite: SqliteClient$2;
|
|
455
|
+
readonly clock: Clock$1;
|
|
456
|
+
readonly faults: FaultRegistry$1;
|
|
457
|
+
readonly metrics: Metrics$1;
|
|
458
|
+
readonly journal: Journal$1;
|
|
459
|
+
readonly rng: Rng$1;
|
|
460
|
+
readonly credentials: CredentialRegistry$1;
|
|
461
|
+
/** The webhook hub, when the service has outbound webhooks. */
|
|
462
|
+
readonly webhooks: WebhookHub$1 | undefined;
|
|
463
|
+
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
464
|
+
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule$1>): FaultRule$1[];
|
|
465
|
+
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
466
|
+
instance(namespace?: string): T;
|
|
467
|
+
/** Public names of every namespace created so far. */
|
|
468
|
+
namespaces(): string[];
|
|
469
|
+
/** Reset one namespace, or every namespace with `"*"`. */
|
|
470
|
+
reset(namespace?: string): Promise<void>;
|
|
471
|
+
snapshot(namespace?: string): NamespaceSnapshot$1;
|
|
472
|
+
restore(snapshot: NamespaceSnapshot$1, namespace?: string): void;
|
|
473
|
+
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
474
|
+
checkpoint(namespace?: string, branch?: string): ServiceCheckpoint$1;
|
|
475
|
+
/** Create an isolated branch, optionally from a historical checkpoint. */
|
|
476
|
+
branch(name: string, options?: {
|
|
477
|
+
namespace?: string;
|
|
478
|
+
at?: string;
|
|
479
|
+
}): ServiceCheckpoint$1;
|
|
480
|
+
/** Restore a branch, clock, and PRNG to a checkpoint. */
|
|
481
|
+
checkout(checkpoint: string, options?: {
|
|
482
|
+
namespace?: string;
|
|
483
|
+
branch?: string;
|
|
484
|
+
}): void;
|
|
485
|
+
/** Inspect the retained history for a namespace. */
|
|
486
|
+
timeline(namespace?: string): Timeline$1<ServiceTimelineState$1>;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
type CliOption = {
|
|
490
|
+
type: "string" | "boolean";
|
|
491
|
+
description: string;
|
|
492
|
+
/** Shown in help; the value placeholder, e.g. `<port>`. */
|
|
493
|
+
value?: string;
|
|
494
|
+
default?: string | boolean;
|
|
495
|
+
};
|
|
496
|
+
type CliValues = Record<string, string | boolean | undefined>;
|
|
497
|
+
type CommonServeOptions = {
|
|
498
|
+
adminKey: string | undefined;
|
|
499
|
+
seed: string | undefined;
|
|
500
|
+
onLog: ((entry: RequestLog$1) => void) | undefined;
|
|
501
|
+
};
|
|
502
|
+
/**
|
|
503
|
+
* What a service contributes to `serve`: how to build its runtime from CLI flags,
|
|
504
|
+
* and what to say at startup. Every service's `./server` entry exports one as
|
|
505
|
+
* `serveTarget`, which is also how `serve --config` finds services by name.
|
|
506
|
+
*/
|
|
507
|
+
type ServeTarget = {
|
|
508
|
+
name: string;
|
|
509
|
+
defaultPort: number;
|
|
510
|
+
/** Serve flags beyond the common ones. */
|
|
511
|
+
options?: Record<string, CliOption>;
|
|
512
|
+
create(values: CliValues, common: CommonServeOptions): Promise<ServiceRuntime$1<ServiceInstance$1>> | ServiceRuntime$1<ServiceInstance$1>;
|
|
513
|
+
/** Startup lines after the listen address, e.g. the loaded corpus version. */
|
|
514
|
+
banner?(runtime: ServiceRuntime$1<ServiceInstance$1>): string[];
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
/** A running server, with the address it actually bound. */
|
|
518
|
+
type Listening = {
|
|
519
|
+
url: string;
|
|
520
|
+
port: number;
|
|
521
|
+
host: string;
|
|
522
|
+
server: Server;
|
|
523
|
+
close(): Promise<void>;
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* The single source of time for a service.
|
|
528
|
+
*
|
|
529
|
+
* Every timestamp a mock writes reads from here, so a suite moves time instead of
|
|
530
|
+
* sleeping: appointment windows, result delays and expiries become reachable in
|
|
531
|
+
* milliseconds. A frozen clock also makes timestamps reproducible from a seed.
|
|
532
|
+
*/
|
|
533
|
+
type ClockState = {
|
|
534
|
+
/** Current epoch milliseconds. */
|
|
535
|
+
now: number;
|
|
536
|
+
/** True while time does not advance on its own. */
|
|
537
|
+
frozen: boolean;
|
|
538
|
+
/** Milliseconds this clock adds to its underlying source. */
|
|
539
|
+
offsetMs: number;
|
|
540
|
+
};
|
|
541
|
+
type Clock = {
|
|
542
|
+
now(): number;
|
|
543
|
+
/** Pin the clock to an exact instant, keeping it frozen if it already was. */
|
|
544
|
+
set(epochMs: number): void;
|
|
545
|
+
/** Move the clock forward, or back with a negative delta. */
|
|
546
|
+
advance(deltaMs: number): void;
|
|
547
|
+
/** Stop time at the current instant. */
|
|
548
|
+
freeze(): void;
|
|
549
|
+
/** Resume from the current instant. */
|
|
550
|
+
unfreeze(): void;
|
|
551
|
+
/** Drop back to the underlying source, live. */
|
|
552
|
+
reset(): void;
|
|
553
|
+
state(): ClockState;
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
557
|
+
type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
|
|
558
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
559
|
+
type SqliteRunResult$1 = {
|
|
560
|
+
changes: number;
|
|
561
|
+
lastInsertRowid: number | bigint;
|
|
562
|
+
};
|
|
563
|
+
/**
|
|
564
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
565
|
+
*
|
|
566
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
567
|
+
*/
|
|
568
|
+
interface SqliteStatement$1 {
|
|
569
|
+
run(...params: SqliteValue$1[]): SqliteRunResult$1;
|
|
570
|
+
all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
|
|
571
|
+
get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
575
|
+
*
|
|
576
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
577
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
578
|
+
*/
|
|
579
|
+
interface SqliteClient$1 {
|
|
580
|
+
exec(sql: string): void;
|
|
581
|
+
prepare(sql: string): SqliteStatement$1;
|
|
582
|
+
transaction<T>(fn: () => T): T;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** Every stored record carries a monotonically increasing sequence for stable ordering. */
|
|
586
|
+
type Stored<T> = {
|
|
587
|
+
seq: number;
|
|
588
|
+
value: T;
|
|
589
|
+
};
|
|
590
|
+
type ListRecordsOptions<T> = {
|
|
591
|
+
/** Keep only records passing the predicate. */
|
|
592
|
+
where?: (value: T, seq: number) => boolean;
|
|
593
|
+
/** Sort order; default newest first. */
|
|
594
|
+
order?: "newest" | "oldest";
|
|
595
|
+
};
|
|
596
|
+
/**
|
|
597
|
+
* A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
|
|
598
|
+
* sequence, never by id lexicographic order, so list semantics stay stable.
|
|
599
|
+
*/
|
|
600
|
+
declare class Collection<T> {
|
|
601
|
+
private readonly sqlite;
|
|
602
|
+
private readonly namespace;
|
|
603
|
+
private readonly name;
|
|
604
|
+
constructor(sqlite: SqliteClient$1, namespace: string, name: string);
|
|
605
|
+
private bumpCollectionSeq;
|
|
606
|
+
nextSequence(): number;
|
|
607
|
+
get(id: string): T | undefined;
|
|
608
|
+
has(id: string): boolean;
|
|
609
|
+
/** Insert a new record, assigning it the next sequence number. */
|
|
610
|
+
insert(id: string, value: T): Stored<T>;
|
|
611
|
+
/** Replace an existing record's value, keeping its position. */
|
|
612
|
+
update(id: string, value: T): Stored<T> | undefined;
|
|
613
|
+
delete(id: string): boolean;
|
|
614
|
+
/** How many records the collection holds, without reading them. */
|
|
615
|
+
count(): number;
|
|
616
|
+
list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
|
|
617
|
+
id: string;
|
|
618
|
+
}>;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
|
|
623
|
+
* request a percentage fault hits — is reproducible from a seed.
|
|
624
|
+
*
|
|
625
|
+
* mulberry32: small, fast, and stable across runtimes, which matters more here
|
|
626
|
+
* than statistical quality.
|
|
627
|
+
*/
|
|
628
|
+
type Rng = {
|
|
629
|
+
/** Next value in `[0, 1)`. */
|
|
630
|
+
next(): number;
|
|
631
|
+
/** Next integer in `[min, max]`. */
|
|
632
|
+
int(min: number, max: number): number;
|
|
633
|
+
/** Restart the stream from its seed. */
|
|
634
|
+
reset(): void;
|
|
635
|
+
/** Serializable engine state used by deterministic checkpoints. */
|
|
636
|
+
state(): number;
|
|
637
|
+
/** Restore a state previously returned by {@link state}. */
|
|
638
|
+
setState(state: number): void;
|
|
639
|
+
seed: number;
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* A deliberate failure injected in front of an operation.
|
|
644
|
+
*
|
|
645
|
+
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
646
|
+
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
647
|
+
* appears under load, the 5xx that proves a retry path works.
|
|
648
|
+
*/
|
|
649
|
+
type FaultRule = {
|
|
650
|
+
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
651
|
+
id: string;
|
|
652
|
+
/** Fault only this operation. Omit to match every operation. */
|
|
653
|
+
operationId?: string;
|
|
654
|
+
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
655
|
+
method?: string;
|
|
656
|
+
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
657
|
+
pathPrefix?: string;
|
|
658
|
+
/**
|
|
659
|
+
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
660
|
+
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
661
|
+
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
662
|
+
*/
|
|
663
|
+
namespace?: string;
|
|
664
|
+
/**
|
|
665
|
+
* Status of the injected response. Omit for a rule that only delays (`delayMs` /
|
|
666
|
+
* `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
|
|
667
|
+
* the request then still reaches the service.
|
|
668
|
+
*/
|
|
669
|
+
status?: number;
|
|
670
|
+
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
671
|
+
body?: unknown;
|
|
672
|
+
headers?: Record<string, string>;
|
|
673
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
674
|
+
count?: number;
|
|
675
|
+
/** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
|
|
676
|
+
rate?: number;
|
|
677
|
+
/** Hold the response back this long, to exercise timeouts. */
|
|
678
|
+
delayMs?: number;
|
|
679
|
+
/** Alias of `delayMs`. */
|
|
680
|
+
latencyMs?: number;
|
|
681
|
+
/**
|
|
682
|
+
* Drop the connection instead of answering: an in-process `fetch` rejects with a
|
|
683
|
+
* `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
|
|
684
|
+
*/
|
|
685
|
+
drop?: boolean;
|
|
686
|
+
/**
|
|
687
|
+
* A named service behaviour to switch on for the matching request instead of (or
|
|
688
|
+
* before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
|
|
689
|
+
* read it with `faultEffects(request)`.
|
|
690
|
+
*/
|
|
691
|
+
effect?: string;
|
|
692
|
+
/** Parameters for `effect`. */
|
|
693
|
+
params?: Record<string, unknown>;
|
|
694
|
+
/** From the preset this rule was expanded from, if any. */
|
|
695
|
+
preset?: string;
|
|
696
|
+
};
|
|
697
|
+
/** A fault that fired for one request. */
|
|
698
|
+
type FaultHit = {
|
|
699
|
+
id: string;
|
|
700
|
+
/** The injected response; absent when the rule only delays, drops, or sets an effect. */
|
|
701
|
+
response?: Response;
|
|
702
|
+
drop?: boolean;
|
|
703
|
+
effect?: {
|
|
704
|
+
name: string;
|
|
705
|
+
params: Record<string, unknown>;
|
|
706
|
+
};
|
|
707
|
+
};
|
|
708
|
+
/** What a request looks like to the fault matcher. */
|
|
709
|
+
type FaultCandidate = {
|
|
710
|
+
operationId: string | undefined;
|
|
711
|
+
method: string;
|
|
712
|
+
path: string;
|
|
713
|
+
namespace: string;
|
|
714
|
+
};
|
|
715
|
+
type FaultRegistry = {
|
|
716
|
+
add(rule: FaultRule): FaultRule;
|
|
717
|
+
list(): (FaultRule & {
|
|
718
|
+
remaining: number | null;
|
|
719
|
+
hits: number;
|
|
720
|
+
})[];
|
|
721
|
+
remove(id: string): boolean;
|
|
722
|
+
clear(): void;
|
|
723
|
+
/**
|
|
724
|
+
* Every fault this request should get, in rule order, stopping at the first that answers
|
|
725
|
+
* or drops (effect-only and delay-only rules let later rules match too). Consumes one of
|
|
726
|
+
* each matching rule's remaining uses.
|
|
727
|
+
*/
|
|
728
|
+
take(candidate: FaultCandidate): Promise<FaultHit[]>;
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
/** One handled request, as the structured log sees it. */
|
|
732
|
+
type RequestLog = {
|
|
733
|
+
service: string;
|
|
734
|
+
namespace: string;
|
|
735
|
+
operationId: string | undefined;
|
|
736
|
+
method: string;
|
|
737
|
+
path: string;
|
|
738
|
+
status: number;
|
|
739
|
+
durationMs: number;
|
|
740
|
+
/** True when the path matched no operation in the contract. */
|
|
741
|
+
unmatched: boolean;
|
|
742
|
+
/** Set when a fault rule produced the response. */
|
|
743
|
+
faultId?: string;
|
|
744
|
+
/** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
|
|
745
|
+
ids?: Record<string, string>;
|
|
746
|
+
/** Set when the service created a resource the request referred to but that did not exist. */
|
|
747
|
+
adopted?: boolean;
|
|
748
|
+
};
|
|
749
|
+
type MetricsReport = {
|
|
750
|
+
requests: number;
|
|
751
|
+
/** Counts keyed `<operationId> <status>`. */
|
|
752
|
+
byOperation: Record<string, number>;
|
|
753
|
+
/**
|
|
754
|
+
* Paths that matched no operation, most frequent first.
|
|
755
|
+
*
|
|
756
|
+
* This is the early-warning signal: a consumer calling something the mock does
|
|
757
|
+
* not implement shows up here as a count, before it fails a suite as a 404.
|
|
758
|
+
*/
|
|
759
|
+
unmatched: {
|
|
760
|
+
method: string;
|
|
761
|
+
path: string;
|
|
762
|
+
count: number;
|
|
763
|
+
}[];
|
|
764
|
+
faults: number;
|
|
765
|
+
totalDurationMs: number;
|
|
766
|
+
};
|
|
767
|
+
type Metrics = {
|
|
768
|
+
record(entry: RequestLog): void;
|
|
769
|
+
report(): MetricsReport;
|
|
770
|
+
reset(): void;
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
/** One journal entry: a request log stamped with when (on the mock clock) it was handled. */
|
|
774
|
+
type JournalEntry = RequestLog & {
|
|
775
|
+
at: string;
|
|
776
|
+
};
|
|
777
|
+
type JournalQuery = {
|
|
778
|
+
/** Only this namespace. Omit for every namespace, oldest first across all of them. */
|
|
779
|
+
namespace?: string;
|
|
780
|
+
operationId?: string;
|
|
781
|
+
status?: number;
|
|
782
|
+
/** Only entries at or after this instant (epoch ms). */
|
|
783
|
+
since?: number;
|
|
784
|
+
/** At most this many, the most recent kept. */
|
|
785
|
+
limit?: number;
|
|
786
|
+
};
|
|
787
|
+
type Journal = {
|
|
788
|
+
readonly size: number;
|
|
789
|
+
record(entry: JournalEntry): void;
|
|
790
|
+
list(query?: JournalQuery): JournalEntry[];
|
|
791
|
+
/** Forget one namespace's entries, or every namespace's. */
|
|
792
|
+
clear(namespace?: string): void;
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
/** Credential → namespace mapping behind `PUT /__admin/credentials`. */
|
|
796
|
+
type CredentialRegistry = {
|
|
797
|
+
set(credential: string, namespace: string): void;
|
|
798
|
+
get(credential: string): string | undefined;
|
|
799
|
+
remove(credential: string): boolean;
|
|
800
|
+
clear(): void;
|
|
801
|
+
entries(): {
|
|
802
|
+
credential: string;
|
|
803
|
+
namespace: string;
|
|
804
|
+
}[];
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Sequential id source persisted in SQLite. Ids are deterministic for a given
|
|
809
|
+
* sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
|
|
810
|
+
*/
|
|
811
|
+
declare class IdSequence {
|
|
812
|
+
private readonly sqlite;
|
|
813
|
+
private readonly namespace;
|
|
814
|
+
private readonly salt;
|
|
815
|
+
constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
|
|
816
|
+
next(prefix: string, length?: number): string;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/** A stable identifier for a point in a {@link Timeline}. */
|
|
820
|
+
type CheckpointId = string;
|
|
821
|
+
/** An immutable node in a timeline's checkpoint DAG. */
|
|
822
|
+
type Checkpoint<T> = Readonly<{
|
|
823
|
+
id: CheckpointId;
|
|
824
|
+
branch: string;
|
|
825
|
+
parent: CheckpointId | null;
|
|
826
|
+
/** Logical time supplied by the timeline's injected clock. */
|
|
827
|
+
at: number;
|
|
828
|
+
value: T;
|
|
829
|
+
}>;
|
|
830
|
+
type TimelineOptions = {
|
|
831
|
+
/** Logical clock used to stamp checkpoints. Defaults to a deterministic counter. */
|
|
832
|
+
now?: () => number;
|
|
833
|
+
/** Maximum retained checkpoints. Branch heads are never collected. Default 1,000. */
|
|
834
|
+
maxCheckpoints?: number;
|
|
835
|
+
/** Customize deterministic checkpoint IDs. */
|
|
836
|
+
id?: (sequence: number) => CheckpointId;
|
|
837
|
+
};
|
|
838
|
+
type CommitOptions = {
|
|
839
|
+
branch?: string;
|
|
840
|
+
/** Parent checkpoint. Defaults to the selected branch's current head. */
|
|
841
|
+
parent?: CheckpointId | null;
|
|
842
|
+
};
|
|
843
|
+
type ForkOptions = {
|
|
844
|
+
/** Checkpoint to fork from. Defaults to the main branch's head. */
|
|
845
|
+
from?: CheckpointId;
|
|
846
|
+
};
|
|
847
|
+
/**
|
|
848
|
+
* Small, storage-agnostic checkpoint DAG shared by service runtimes. Its values may be immutable
|
|
849
|
+
* records, namespace images, or copy-on-write SQL engine snapshots.
|
|
850
|
+
*
|
|
851
|
+
* Values are retained by reference. Engines can therefore use persistent/COW snapshots while
|
|
852
|
+
* simpler services can use immutable values. IDs and GC order are deterministic, and all IO
|
|
853
|
+
* (the logical clock) is injected.
|
|
854
|
+
*/
|
|
855
|
+
declare class Timeline<T> {
|
|
856
|
+
readonly maxCheckpoints: number;
|
|
857
|
+
private readonly now;
|
|
858
|
+
private readonly makeId;
|
|
859
|
+
private readonly nodes;
|
|
860
|
+
private readonly heads;
|
|
861
|
+
/** Unreferenced nodes in the exact order they became collectible. */
|
|
862
|
+
private readonly evictable;
|
|
863
|
+
/** Branch heads plus explicit retainers. Absent means zero. */
|
|
864
|
+
private readonly references;
|
|
865
|
+
private readonly explicitPins;
|
|
866
|
+
private sequence;
|
|
867
|
+
constructor(options?: TimelineOptions);
|
|
868
|
+
/** Capture a new immutable value and move `branch` to it. */
|
|
869
|
+
commit(value: T, options?: CommitOptions): Checkpoint<T>;
|
|
870
|
+
/** Create a branch pointer without copying its checkpoint value. */
|
|
871
|
+
fork(branch: string, options?: ForkOptions): Checkpoint<T> | undefined;
|
|
872
|
+
/** Move a branch pointer to an existing checkpoint. */
|
|
873
|
+
checkout(branch: string, id: CheckpointId): Checkpoint<T>;
|
|
874
|
+
get(id: CheckpointId): Checkpoint<T>;
|
|
875
|
+
head(branch?: string): Checkpoint<T> | undefined;
|
|
876
|
+
hasBranch(branch: string): boolean;
|
|
877
|
+
branches(): Readonly<Record<string, CheckpointId>>;
|
|
878
|
+
checkpoints(): readonly Checkpoint<T>[];
|
|
879
|
+
/** Number of retained checkpoints without allocating an array. */
|
|
880
|
+
get size(): number;
|
|
881
|
+
/** Pin a checkpoint independently of branch heads (used by compatibility snapshot handles). */
|
|
882
|
+
retain(id: CheckpointId): Checkpoint<T>;
|
|
883
|
+
/** Release one explicit pin. Branch heads remain pinned until moved or deleted. */
|
|
884
|
+
release(id: CheckpointId): boolean;
|
|
885
|
+
deleteBranch(branch: string): boolean;
|
|
886
|
+
/**
|
|
887
|
+
* Deterministically discard oldest unpinned checkpoints. Collection is O(number removed):
|
|
888
|
+
* commits never scan pinned nodes or the retained history. Parents are metadata rather than a
|
|
889
|
+
* storage dependency, so a retained node remains usable after pruning.
|
|
890
|
+
*/
|
|
891
|
+
gc(max?: number): CheckpointId[];
|
|
892
|
+
private collect;
|
|
893
|
+
private moveHead;
|
|
894
|
+
private addReference;
|
|
895
|
+
private removeReference;
|
|
896
|
+
private assertBranch;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
901
|
+
*
|
|
902
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
903
|
+
* It is the only contract shared across the whole graph.
|
|
904
|
+
*/
|
|
905
|
+
interface FetchAPI$1 {
|
|
906
|
+
fetch(request: Request): Promise<Response>;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* A point-in-time copy of everything a service namespace holds.
|
|
911
|
+
*
|
|
912
|
+
* All service state lives in the two core tables keyed by namespace, so a snapshot
|
|
913
|
+
* is generic: any service gets per-test rollback without knowing its own schema.
|
|
914
|
+
* Restoring is much cheaper than rebuilding a namespace from a corpus.
|
|
915
|
+
*/
|
|
916
|
+
type NamespaceSnapshot = {
|
|
917
|
+
namespace: string;
|
|
918
|
+
records: {
|
|
919
|
+
collection: string;
|
|
920
|
+
id: string;
|
|
921
|
+
seq: number;
|
|
922
|
+
value: string;
|
|
923
|
+
}[];
|
|
924
|
+
sequences: {
|
|
925
|
+
name: string;
|
|
926
|
+
kind: string;
|
|
927
|
+
value: number;
|
|
928
|
+
}[];
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
type WebhookEndpoint = {
|
|
932
|
+
/** Stable id; generated when omitted. */
|
|
933
|
+
id?: string;
|
|
934
|
+
url: string;
|
|
935
|
+
secret?: string;
|
|
936
|
+
/** Event types to deliver; omit or include `"*"` for every type. */
|
|
937
|
+
events?: string[];
|
|
938
|
+
/** Deliver only messages whose tags include all of these (e.g. `{ account: "mso" }`). */
|
|
939
|
+
tags?: Record<string, string>;
|
|
940
|
+
/** The public URL the receiver verifies signatures against (Twilio), when it differs. */
|
|
941
|
+
signUrl?: string;
|
|
942
|
+
headers?: Record<string, string>;
|
|
943
|
+
};
|
|
944
|
+
type WebhookMessage = {
|
|
945
|
+
id: string;
|
|
946
|
+
namespace: string;
|
|
947
|
+
type: string;
|
|
948
|
+
body: string;
|
|
949
|
+
contentType: string;
|
|
950
|
+
tags: Record<string, string>;
|
|
951
|
+
headers?: Record<string, string>;
|
|
952
|
+
/** Wall-clock ISO-8601 time of publication. */
|
|
953
|
+
publishedAt: string;
|
|
954
|
+
};
|
|
955
|
+
type WebhookAttempt = {
|
|
956
|
+
attempt: number;
|
|
957
|
+
at: string;
|
|
958
|
+
status: number | null;
|
|
959
|
+
error: string | null;
|
|
960
|
+
durationMs: number;
|
|
961
|
+
/** Exact receiver response body, when one was returned. */
|
|
962
|
+
responseBody?: string | null;
|
|
963
|
+
};
|
|
964
|
+
type WebhookDelivery = {
|
|
965
|
+
id: string;
|
|
966
|
+
messageId: string;
|
|
967
|
+
namespace: string;
|
|
968
|
+
type: string;
|
|
969
|
+
endpointId: string;
|
|
970
|
+
url: string;
|
|
971
|
+
state: "pending" | "delivered" | "failed" | "dropped";
|
|
972
|
+
attempts: WebhookAttempt[];
|
|
973
|
+
};
|
|
974
|
+
/** A delivery-level fault: what happens to the next `count` messages in a namespace. */
|
|
975
|
+
type WebhookFault = {
|
|
976
|
+
mode: "duplicate" | "reorder" | "drop";
|
|
977
|
+
/** Messages affected; default 1. */
|
|
978
|
+
count?: number;
|
|
979
|
+
};
|
|
980
|
+
type PublishInput = {
|
|
981
|
+
namespace: string;
|
|
982
|
+
type: string;
|
|
983
|
+
/** Exact body; objects are JSON-encoded. */
|
|
984
|
+
body: string | Record<string, unknown> | unknown[];
|
|
985
|
+
/** Default `application/json`, or form-encoded when `form` is given. */
|
|
986
|
+
contentType?: string;
|
|
987
|
+
/** Form parameters, when the vendor posts `application/x-www-form-urlencoded`. */
|
|
988
|
+
form?: Record<string, string>;
|
|
989
|
+
tags?: Record<string, string>;
|
|
990
|
+
/** Message-specific delivery headers, captured as part of durable message state. */
|
|
991
|
+
headers?: Record<string, string>;
|
|
992
|
+
/** Message id; generated when omitted. */
|
|
993
|
+
id?: string;
|
|
994
|
+
};
|
|
995
|
+
type WebhookHub = {
|
|
996
|
+
publish(input: PublishInput): WebhookMessage;
|
|
997
|
+
/** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
|
|
998
|
+
setEndpoints(namespace: string, endpoints: WebhookEndpoint[]): WebhookEndpoint[];
|
|
999
|
+
/** The endpoints a namespace delivers to: its own, plus the global ones. */
|
|
1000
|
+
endpoints(namespace: string): WebhookEndpoint[];
|
|
1001
|
+
messages(namespace?: string): WebhookMessage[];
|
|
1002
|
+
deliveries(namespace?: string): WebhookDelivery[];
|
|
1003
|
+
replay(deliveryId: string): Promise<WebhookDelivery | undefined>;
|
|
1004
|
+
/** Run every pending retry (and release held reordered messages) now. */
|
|
1005
|
+
flush(): Promise<void>;
|
|
1006
|
+
/** Resolve once nothing is in flight. */
|
|
1007
|
+
idle(): Promise<void>;
|
|
1008
|
+
fault(namespace: string, fault: WebhookFault): void;
|
|
1009
|
+
clear(namespace?: string): void;
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
/** What the runtime needs from a service: a Fetch handler it can reset. */
|
|
1013
|
+
type ServiceInstance = FetchAPI$1 & {
|
|
1014
|
+
reset(): Promise<void>;
|
|
1015
|
+
};
|
|
1016
|
+
type ServiceTimelineState = Readonly<{
|
|
1017
|
+
snapshot: NamespaceSnapshot;
|
|
1018
|
+
clock: Readonly<ReturnType<Clock["state"]>>;
|
|
1019
|
+
rngState: number;
|
|
1020
|
+
}>;
|
|
1021
|
+
type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
|
|
1022
|
+
type ServiceRuntime<T extends ServiceInstance> = FetchAPI$1 & {
|
|
1023
|
+
readonly name: string;
|
|
1024
|
+
readonly sqlite: SqliteClient$1;
|
|
1025
|
+
readonly clock: Clock;
|
|
1026
|
+
readonly faults: FaultRegistry;
|
|
1027
|
+
readonly metrics: Metrics;
|
|
1028
|
+
readonly journal: Journal;
|
|
1029
|
+
readonly rng: Rng;
|
|
1030
|
+
readonly credentials: CredentialRegistry;
|
|
1031
|
+
/** The webhook hub, when the service has outbound webhooks. */
|
|
1032
|
+
readonly webhooks: WebhookHub | undefined;
|
|
1033
|
+
/** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
|
|
1034
|
+
applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
|
|
1035
|
+
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
1036
|
+
instance(namespace?: string): T;
|
|
1037
|
+
/** Public names of every namespace created so far. */
|
|
1038
|
+
namespaces(): string[];
|
|
1039
|
+
/** Reset one namespace, or every namespace with `"*"`. */
|
|
1040
|
+
reset(namespace?: string): Promise<void>;
|
|
1041
|
+
snapshot(namespace?: string): NamespaceSnapshot;
|
|
1042
|
+
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
1043
|
+
/** Capture the current branch. Mutating HTTP calls do this automatically. */
|
|
1044
|
+
checkpoint(namespace?: string, branch?: string): ServiceCheckpoint;
|
|
1045
|
+
/** Create an isolated branch, optionally from a historical checkpoint. */
|
|
1046
|
+
branch(name: string, options?: {
|
|
1047
|
+
namespace?: string;
|
|
1048
|
+
at?: string;
|
|
1049
|
+
}): ServiceCheckpoint;
|
|
1050
|
+
/** Restore a branch, clock, and PRNG to a checkpoint. */
|
|
1051
|
+
checkout(checkpoint: string, options?: {
|
|
1052
|
+
namespace?: string;
|
|
1053
|
+
branch?: string;
|
|
1054
|
+
}): void;
|
|
1055
|
+
/** Inspect the retained history for a namespace. */
|
|
1056
|
+
timeline(namespace?: string): Timeline<ServiceTimelineState>;
|
|
1057
|
+
};
|
|
1058
|
+
|
|
1059
|
+
/** Options every provider constructor accepts. */
|
|
1060
|
+
type APIOptions = {
|
|
1061
|
+
/** Sync SQLite client. Defaults to `@crvouga/mockingbird-service-sqlite`. */
|
|
1062
|
+
sqlite?: SqliteClient$1;
|
|
1063
|
+
/** Clock used for `created`-style fields. Default `Date.now`. */
|
|
1064
|
+
now?: () => number;
|
|
1065
|
+
/**
|
|
1066
|
+
* Storage namespace for this instance's records. Instances sharing one SQLite
|
|
1067
|
+
* client stay isolated when their namespaces differ. Defaults to the service name.
|
|
1068
|
+
*/
|
|
1069
|
+
namespace?: string;
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
1073
|
+
type SqliteValue = null | number | bigint | string | Uint8Array | boolean;
|
|
1074
|
+
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
1075
|
+
type SqliteRunResult = {
|
|
1076
|
+
changes: number;
|
|
1077
|
+
lastInsertRowid: number | bigint;
|
|
1078
|
+
};
|
|
1079
|
+
/**
|
|
1080
|
+
* Prepared statement bound to a {@link SqliteClient}.
|
|
1081
|
+
*
|
|
1082
|
+
* Pass bind values as rest arguments on each call (no sticky `bind()`).
|
|
1083
|
+
*/
|
|
1084
|
+
interface SqliteStatement {
|
|
1085
|
+
run(...params: SqliteValue[]): SqliteRunResult;
|
|
1086
|
+
all<T = Record<string, unknown>>(...params: SqliteValue[]): T[];
|
|
1087
|
+
get<T = Record<string, unknown>>(...params: SqliteValue[]): T | undefined;
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Sync SQLite client port owned by Mockingbird.
|
|
1091
|
+
*
|
|
1092
|
+
* Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
|
|
1093
|
+
* `bun:sqlite` instances all work when they expose this surface.
|
|
1094
|
+
*/
|
|
1095
|
+
interface SqliteClient {
|
|
1096
|
+
exec(sql: string): void;
|
|
1097
|
+
prepare(sql: string): SqliteStatement;
|
|
1098
|
+
transaction<T>(fn: () => T): T;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
1103
|
+
*
|
|
1104
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
1105
|
+
* It is the only contract shared across the whole graph.
|
|
1106
|
+
*/
|
|
1107
|
+
interface FetchAPI {
|
|
1108
|
+
fetch(request: Request): Promise<Response>;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
declare const NEXT_ACTION_TYPES: readonly ["collect_letter_of_medical_necessity", "provide_second_payment_method", "provide_alternative_payment_method", "payment_failed"];
|
|
1112
|
+
type NextActionType = (typeof NEXT_ACTION_TYPES)[number];
|
|
1113
|
+
declare const PAYMENT_INTENT_STATUSES: readonly ["requires_payment_method", "requires_action", "processing", "succeeded", "canceled"];
|
|
1114
|
+
type PaymentIntentStatus = (typeof PAYMENT_INTENT_STATUSES)[number];
|
|
1115
|
+
type ProductRecord = {
|
|
1116
|
+
product_id: string;
|
|
1117
|
+
name: string;
|
|
1118
|
+
description: string | null;
|
|
1119
|
+
url: string | null;
|
|
1120
|
+
client_reference_id: string | null;
|
|
1121
|
+
hsa_fsa_eligibility: string | null;
|
|
1122
|
+
visit_type: string | null;
|
|
1123
|
+
active: boolean;
|
|
1124
|
+
test_mode: boolean;
|
|
1125
|
+
metadata: Record<string, string> | null;
|
|
1126
|
+
created_at: string;
|
|
1127
|
+
};
|
|
1128
|
+
type CustomerRecord = {
|
|
1129
|
+
customer_id: string;
|
|
1130
|
+
first_name: string | null;
|
|
1131
|
+
last_name: string | null;
|
|
1132
|
+
email: string | null;
|
|
1133
|
+
phone: string | null;
|
|
1134
|
+
test_mode: boolean;
|
|
1135
|
+
created_at: string;
|
|
1136
|
+
};
|
|
1137
|
+
/** A saved card. Only whether it is an HSA/FSA card is kept: never the number. */
|
|
1138
|
+
type PaymentMethodRecord = {
|
|
1139
|
+
payment_method_id: string;
|
|
1140
|
+
type: "card";
|
|
1141
|
+
hsa_fsa: boolean;
|
|
1142
|
+
customer: string | null;
|
|
1143
|
+
};
|
|
1144
|
+
type PaymentIntentRecord = {
|
|
1145
|
+
payment_intent_id: string;
|
|
1146
|
+
amount: number;
|
|
1147
|
+
amount_received: number | null;
|
|
1148
|
+
customer: string | null;
|
|
1149
|
+
payment_method: string | null;
|
|
1150
|
+
status: PaymentIntentStatus;
|
|
1151
|
+
created_at: string;
|
|
1152
|
+
};
|
|
1153
|
+
type SetupIntentRecord = {
|
|
1154
|
+
setup_intent_id: string;
|
|
1155
|
+
status: PaymentIntentStatus;
|
|
1156
|
+
customer: string | null;
|
|
1157
|
+
payment_method: string | null;
|
|
1158
|
+
created_at: string;
|
|
1159
|
+
};
|
|
1160
|
+
type NextAction = {
|
|
1161
|
+
type: NextActionType;
|
|
1162
|
+
} & Record<string, unknown>;
|
|
1163
|
+
type LineItemRecord = {
|
|
1164
|
+
price_data: {
|
|
1165
|
+
product: string;
|
|
1166
|
+
unit_amount: number;
|
|
1167
|
+
};
|
|
1168
|
+
quantity: number;
|
|
1169
|
+
amount_total: number;
|
|
1170
|
+
};
|
|
1171
|
+
type SessionMode = "payment" | "off_session" | "setup";
|
|
1172
|
+
type SessionStatus = "open" | "paid" | "complete" | "canceled" | "expired";
|
|
1173
|
+
type SessionRecord = {
|
|
1174
|
+
checkout_session_id: string;
|
|
1175
|
+
client_reference_id: string | null;
|
|
1176
|
+
amount_total: number;
|
|
1177
|
+
amount_received: number | null;
|
|
1178
|
+
amount_refunded: number;
|
|
1179
|
+
customer: string | null;
|
|
1180
|
+
payment_intent: string | null;
|
|
1181
|
+
setup_intent: string | null;
|
|
1182
|
+
mode: SessionMode;
|
|
1183
|
+
status: SessionStatus;
|
|
1184
|
+
/** The hosted page (`redirect_url` and `url`). */
|
|
1185
|
+
url: string;
|
|
1186
|
+
success_url: string;
|
|
1187
|
+
cancel_url: string | null;
|
|
1188
|
+
next_action: NextAction | null;
|
|
1189
|
+
visit_type: string | null;
|
|
1190
|
+
metadata: Record<string, string> | null;
|
|
1191
|
+
line_items: LineItemRecord[];
|
|
1192
|
+
allow_promotion_codes: boolean;
|
|
1193
|
+
capture_method: string;
|
|
1194
|
+
setup_future_use: string | null;
|
|
1195
|
+
test_mode: boolean;
|
|
1196
|
+
created_at: string;
|
|
1197
|
+
expires_at: string;
|
|
1198
|
+
/** Mock-clock epoch ms after which an open session expires. */
|
|
1199
|
+
expiresAtMs: number;
|
|
1200
|
+
};
|
|
1201
|
+
type RefundRecord = {
|
|
1202
|
+
refund_id: string;
|
|
1203
|
+
checkout_session: string;
|
|
1204
|
+
payment_intent: string | null;
|
|
1205
|
+
amount: number;
|
|
1206
|
+
status: "succeeded";
|
|
1207
|
+
created_at: string;
|
|
1208
|
+
};
|
|
1209
|
+
/** Per-namespace knobs, set through `PUT /__admin/settings`; cleared on reset. */
|
|
1210
|
+
type Settings = {
|
|
1211
|
+
/**
|
|
1212
|
+
* `dotted` sends `checkout.session.completed` / `checkout.session.expired`; `underscored`
|
|
1213
|
+
* sends the aliases `checkout_session.completed` / `checkout_session.expired`.
|
|
1214
|
+
*/
|
|
1215
|
+
eventNaming: "dotted" | "underscored";
|
|
1216
|
+
/** What an off-session charge does in the create response. */
|
|
1217
|
+
offSessionOutcome: "succeeded" | "declined" | "requires_action";
|
|
1218
|
+
/** Open sessions expire after this long on the mock clock. */
|
|
1219
|
+
sessionTtlSeconds: number;
|
|
1220
|
+
/** A non-HSA card on a letter-of-medical-necessity product asks for the letter. */
|
|
1221
|
+
lmnOnRegularCard: boolean;
|
|
1222
|
+
/** Base URL of the hosted page in session URLs; default: the request's own origin. */
|
|
1223
|
+
publicUrl: string | null;
|
|
1224
|
+
};
|
|
1225
|
+
/**
|
|
1226
|
+
* Products: the recorded corpus as an immutable base layer, with created and edited
|
|
1227
|
+
* products in a Collection on top (so reset and snapshots cover every change and a fresh
|
|
1228
|
+
* namespace costs nothing to seed).
|
|
1229
|
+
*/
|
|
1230
|
+
declare class ProductStore {
|
|
1231
|
+
private readonly base;
|
|
1232
|
+
private readonly overlay;
|
|
1233
|
+
constructor(sqlite: SqliteClient, namespace: string, base: () => Map<string, ProductRecord>);
|
|
1234
|
+
get(id: string): ProductRecord | undefined;
|
|
1235
|
+
put(product: ProductRecord): void;
|
|
1236
|
+
/** Corpus order (by product id), then created products oldest first. */
|
|
1237
|
+
list(): ProductRecord[];
|
|
1238
|
+
}
|
|
1239
|
+
declare class FlexState {
|
|
1240
|
+
private readonly seed;
|
|
1241
|
+
readonly products: ProductStore;
|
|
1242
|
+
readonly sessions: Collection<SessionRecord>;
|
|
1243
|
+
readonly customers: Collection<CustomerRecord>;
|
|
1244
|
+
readonly paymentMethods: Collection<PaymentMethodRecord>;
|
|
1245
|
+
readonly paymentIntents: Collection<PaymentIntentRecord>;
|
|
1246
|
+
readonly setupIntents: Collection<SetupIntentRecord>;
|
|
1247
|
+
readonly refunds: Collection<RefundRecord>;
|
|
1248
|
+
readonly settings: Collection<Settings>;
|
|
1249
|
+
readonly ids: IdSequence;
|
|
1250
|
+
constructor(sqlite: SqliteClient, namespace: string, seed: {
|
|
1251
|
+
products?: readonly ProductRecord[];
|
|
1252
|
+
settings: Partial<Settings>;
|
|
1253
|
+
});
|
|
1254
|
+
current(): Settings;
|
|
1255
|
+
update(patch: Partial<Settings>): Settings;
|
|
1256
|
+
/** ULID-looking lowercase ids (`fprod_01z…`) that sort after every recorded corpus id. */
|
|
1257
|
+
nextId(prefix: string): string;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
/** The event inside the `{event: {...}}` webhook envelope. */
|
|
1261
|
+
type FlexEvent = {
|
|
1262
|
+
event_id: string;
|
|
1263
|
+
event_type: string;
|
|
1264
|
+
object: Record<string, unknown>;
|
|
1265
|
+
/** Unix seconds (mock clock). */
|
|
1266
|
+
event_dt: number;
|
|
1267
|
+
test_mode: boolean;
|
|
1268
|
+
created_at: string;
|
|
1269
|
+
};
|
|
1270
|
+
type FlexAPIOptions = APIOptions & {
|
|
1271
|
+
/** Products every namespace starts with. Default: the recorded corpus. */
|
|
1272
|
+
products?: readonly ProductRecord[];
|
|
1273
|
+
/** Initial per-namespace settings. */
|
|
1274
|
+
settings?: Partial<Settings>;
|
|
1275
|
+
/** The public namespace name, so hosted-page URLs carry `/ns/<name>` (the browser has no headers). */
|
|
1276
|
+
publicNamespace?: string;
|
|
1277
|
+
/** Called for every webhook event; the runtime signs and delivers it. */
|
|
1278
|
+
onEvent?: (event: FlexEvent) => void;
|
|
1279
|
+
};
|
|
1280
|
+
type SessionView = {
|
|
1281
|
+
expandCustomer: boolean;
|
|
1282
|
+
expandPaymentIntent: boolean;
|
|
1283
|
+
};
|
|
1284
|
+
/**
|
|
1285
|
+
* Stateful mock of the Flex HSA/FSA payments API.
|
|
1286
|
+
*
|
|
1287
|
+
* Payment sessions open unpaid and settle only through the hosted page (`/pay/:id`), admin
|
|
1288
|
+
* transitions, or synchronously for off-session charges; every settlement emits the webhook
|
|
1289
|
+
* events Flex would. Products come from the recorded catalog corpus.
|
|
1290
|
+
*/
|
|
1291
|
+
declare class FlexAPI implements FetchAPI {
|
|
1292
|
+
readonly app: Hono;
|
|
1293
|
+
readonly sqlite: SqliteClient;
|
|
1294
|
+
readonly state: FlexState;
|
|
1295
|
+
private readonly service;
|
|
1296
|
+
private readonly idempotency;
|
|
1297
|
+
private readonly now;
|
|
1298
|
+
private readonly publicNamespace;
|
|
1299
|
+
private readonly onEvent;
|
|
1300
|
+
constructor(options?: FlexAPIOptions);
|
|
1301
|
+
fetch(request: Request): Promise<Response>;
|
|
1302
|
+
reset(): Promise<void>;
|
|
1303
|
+
private iso;
|
|
1304
|
+
private testMode;
|
|
1305
|
+
private idempotent;
|
|
1306
|
+
private validate;
|
|
1307
|
+
private listProducts;
|
|
1308
|
+
private createProduct;
|
|
1309
|
+
private getProduct;
|
|
1310
|
+
private updateProduct;
|
|
1311
|
+
/** Store a product change and emit `product.updated`. */
|
|
1312
|
+
putProduct(product: ProductRecord): ProductRecord;
|
|
1313
|
+
private pageBase;
|
|
1314
|
+
private createSession;
|
|
1315
|
+
private newSession;
|
|
1316
|
+
/** Off-session charges settle before the create response, per `offSessionOutcome`. */
|
|
1317
|
+
private chargeOffSession;
|
|
1318
|
+
private view;
|
|
1319
|
+
/** The wire shape of a session, with expansions and the response-shaping faults applied. */
|
|
1320
|
+
present(session: SessionRecord, view: SessionView, request?: Request): Record<string, unknown>;
|
|
1321
|
+
private sessionResponse;
|
|
1322
|
+
private getSession;
|
|
1323
|
+
private listSessions;
|
|
1324
|
+
private refund;
|
|
1325
|
+
/** Refund part or all of a paid session, emitting the refund events. */
|
|
1326
|
+
applyRefund(id: string, amount: number): SessionRecord | undefined;
|
|
1327
|
+
private createCustomer;
|
|
1328
|
+
private newCustomer;
|
|
1329
|
+
private getSetupIntent;
|
|
1330
|
+
private paymentIntentFor;
|
|
1331
|
+
/** Create or update the session's payment intent (`PUT /__admin/sessions/:id/payment-intent`). */
|
|
1332
|
+
setPaymentIntent(id: string, patch: Partial<Pick<PaymentIntentRecord, "status" | "amount_received" | "payment_method" | "customer">>): PaymentIntentRecord | undefined;
|
|
1333
|
+
private newPaymentMethod;
|
|
1334
|
+
/**
|
|
1335
|
+
* Complete a session: the payment intent succeeds (or, in setup mode, the setup intent
|
|
1336
|
+
* saves a card), `amount_received` is set and the completion events go out.
|
|
1337
|
+
*/
|
|
1338
|
+
settle(id: string, options?: {
|
|
1339
|
+
paymentMethod?: string;
|
|
1340
|
+
hsa?: boolean;
|
|
1341
|
+
customer?: CustomerRecord;
|
|
1342
|
+
}): SessionRecord | undefined;
|
|
1343
|
+
/** Decline: the payment intent falls back to requires_payment_method. */
|
|
1344
|
+
decline(id: string): SessionRecord | undefined;
|
|
1345
|
+
expire(id: string): SessionRecord | undefined;
|
|
1346
|
+
/** Put a next action on the session (its URL is the hosted page's step for it). */
|
|
1347
|
+
requireAction(id: string, type?: NextActionType): SessionRecord | undefined;
|
|
1348
|
+
private completedType;
|
|
1349
|
+
/** Expire open sessions whose `expires_at` has passed on the mock clock. */
|
|
1350
|
+
tick(): number;
|
|
1351
|
+
sessions(): SessionRecord[];
|
|
1352
|
+
/** Emit any event type for a session or product (`POST /__admin/events`). */
|
|
1353
|
+
emitFor(type: string, target: {
|
|
1354
|
+
sessionId?: string;
|
|
1355
|
+
productId?: string;
|
|
1356
|
+
}): FlexEvent | undefined;
|
|
1357
|
+
private emitSession;
|
|
1358
|
+
private emit;
|
|
1359
|
+
private pageInput;
|
|
1360
|
+
private render;
|
|
1361
|
+
private hostedPage;
|
|
1362
|
+
private redirect;
|
|
1363
|
+
private submitHosted;
|
|
1364
|
+
private cancelHosted;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
type WebhookHubOptionsSubset = {
|
|
1368
|
+
retryDelaysMs?: readonly number[];
|
|
1369
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
1370
|
+
};
|
|
1371
|
+
type FlexRuntimeOptions = {
|
|
1372
|
+
sqlite?: SqliteClient;
|
|
1373
|
+
clock?: Clock;
|
|
1374
|
+
seed?: number | string;
|
|
1375
|
+
adminKey?: string;
|
|
1376
|
+
onLog?: (entry: RequestLog) => void;
|
|
1377
|
+
/** Products every namespace starts with. Default: the recorded corpus. */
|
|
1378
|
+
products?: readonly ProductRecord[];
|
|
1379
|
+
settings?: Partial<Settings>;
|
|
1380
|
+
/**
|
|
1381
|
+
* Where webhooks go (`POST /billing/webhooks/flex`), Svix-signed with `secret`
|
|
1382
|
+
* (`fwhsec_<base64>` or `whsec_<base64>`, the app's `FLEX_WEBHOOK_SECRET`).
|
|
1383
|
+
*/
|
|
1384
|
+
webhooks?: Omit<WebhookEndpoint, "id"> & WebhookHubOptionsSubset;
|
|
1385
|
+
/**
|
|
1386
|
+
* Expire due sessions on this real-time interval (ms), so `expired` webhooks fire without
|
|
1387
|
+
* a request arriving. The served mock uses 100 ms; in-process runtimes default to off.
|
|
1388
|
+
*/
|
|
1389
|
+
tickMs?: number;
|
|
1390
|
+
};
|
|
1391
|
+
type FlexRuntime = ServiceRuntime<FlexAPI> & {
|
|
1392
|
+
readonly webhooks: WebhookHub;
|
|
1393
|
+
/** Stop the background ticker, if one runs. */
|
|
1394
|
+
stop(): void;
|
|
1395
|
+
};
|
|
1396
|
+
|
|
1397
|
+
/** Port `mockingbird-flex serve` listens on when none is given. */
|
|
1398
|
+
declare const DEFAULT_PORT = 8792;
|
|
1399
|
+
type FlexServerOptions = FlexRuntimeOptions & {
|
|
1400
|
+
/** Default `0`: the OS picks a free port. */
|
|
1401
|
+
port?: number;
|
|
1402
|
+
/** Default `127.0.0.1`. */
|
|
1403
|
+
host?: string;
|
|
1404
|
+
};
|
|
1405
|
+
type FlexServer = Listening & {
|
|
1406
|
+
runtime: FlexRuntime;
|
|
1407
|
+
};
|
|
1408
|
+
/** Serve the Flex mock over `node:http`, expiring due sessions every 100 ms. */
|
|
1409
|
+
declare const createServer: (options?: FlexServerOptions) => Promise<FlexServer>;
|
|
1410
|
+
/** How `serve` (and `serve --config`) builds the Flex mock from flags. */
|
|
1411
|
+
declare const serveTarget: ServeTarget;
|
|
1412
|
+
|
|
1413
|
+
export { DEFAULT_PORT, createServer, serveTarget };
|
|
1414
|
+
export type { FlexServer, FlexServerOptions };
|