@jaredwray/mockhttp 2.0.0 → 3.0.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/README.md +68 -68
- package/dist/index.d.mts +130 -159
- package/dist/index.mjs +200 -231
- package/package.json +21 -18
- package/site/dist/api/index.html +5 -5
- package/site/dist/api/swagger.json +10 -10
- package/site/dist/changelog/index.html +49 -33
- package/site/dist/changelog/page/2/index.html +33 -32
- package/site/dist/changelog/{v0-2-0 → v2-1-0}/index.html +47 -8
- package/site/dist/changelog/{v0-1-0 → v3-0-0}/index.html +71 -22
- package/site/dist/changelog-latest.json +26 -26
- package/site/dist/changelog.json +26 -26
- package/site/dist/docs/bins/index.html +78 -53
- package/site/dist/docs/configuration/index.html +42 -4
- package/site/dist/docs/hosted/index.html +47 -8
- package/site/dist/docs/how-to-deploy/cloudflare/index.html +650 -0
- package/site/dist/docs/how-to-deploy/docker/index.html +664 -0
- package/site/dist/docs/how-to-deploy/wasmer/index.html +625 -0
- package/site/dist/docs/https/index.html +42 -4
- package/site/dist/docs/index.html +44 -5
- package/site/dist/docs/library/index.html +58 -24
- package/site/dist/docs/taps/index.html +42 -4
- package/site/dist/feed.xml +1 -1
- package/site/dist/index.html +44 -5
- package/site/dist/llms-full.txt +390 -115
- package/site/dist/llms.txt +6 -3
- package/site/dist/search-index.json +1 -1
- package/site/dist/sitemap.xml +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
import { RateLimitPluginOptions } from "@fastify/rate-limit";
|
|
2
2
|
import { FastifyInstance, FastifyRequest } from "fastify";
|
|
3
3
|
import { Hookified, HookifiedOptions } from "hookified";
|
|
4
|
+
import Keyv from "keyv";
|
|
5
|
+
//#region src/certificate.d.ts
|
|
6
|
+
type CertificateOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* Common Name for the certificate subject. Defaults to 'localhost'.
|
|
9
|
+
*/
|
|
10
|
+
commonName?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Subject Alternative Names. Defaults to [dns:localhost, ip:127.0.0.1, ip:::1].
|
|
13
|
+
*/
|
|
14
|
+
altNames?: Array<{
|
|
15
|
+
type: "dns";
|
|
16
|
+
value: string;
|
|
17
|
+
} | {
|
|
18
|
+
type: "ip";
|
|
19
|
+
value: string;
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Number of days the certificate is valid. Defaults to 365.
|
|
23
|
+
*/
|
|
24
|
+
validityDays?: number;
|
|
25
|
+
/**
|
|
26
|
+
* RSA key size in bits. Defaults to 2048.
|
|
27
|
+
*/
|
|
28
|
+
keySize?: number;
|
|
29
|
+
};
|
|
30
|
+
type CertificateResult = {
|
|
31
|
+
/**
|
|
32
|
+
* PEM-encoded X.509 certificate.
|
|
33
|
+
*/
|
|
34
|
+
cert: string;
|
|
35
|
+
/**
|
|
36
|
+
* PEM-encoded PKCS#8 private key.
|
|
37
|
+
*/
|
|
38
|
+
key: string;
|
|
39
|
+
};
|
|
40
|
+
type CertificateFileOptions = CertificateOptions & {
|
|
41
|
+
/**
|
|
42
|
+
* File path to write the certificate PEM.
|
|
43
|
+
*/
|
|
44
|
+
certPath: string;
|
|
45
|
+
/**
|
|
46
|
+
* File path to write the private key PEM.
|
|
47
|
+
*/
|
|
48
|
+
keyPath: string;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Generate a self-signed certificate using only Node.js built-in crypto.
|
|
52
|
+
* Returns PEM-encoded certificate and private key strings.
|
|
53
|
+
*/
|
|
54
|
+
export declare function generateCertificate(options?: CertificateOptions): CertificateResult;
|
|
55
|
+
/**
|
|
56
|
+
* Generate a self-signed certificate and write PEM files to disk.
|
|
57
|
+
* Returns the same CertificateResult as generateCertificate().
|
|
58
|
+
*/
|
|
59
|
+
export declare function generateCertificateFiles(options: CertificateFileOptions): Promise<CertificateResult>;
|
|
60
|
+
//#endregion
|
|
4
61
|
//#region src/bin-store.d.ts
|
|
5
62
|
/**
|
|
6
63
|
* A request captured by a bin, including method, headers, body and metadata.
|
|
@@ -48,179 +105,110 @@ type Bin = {
|
|
|
48
105
|
/** Number of requests currently stored in the bin */
|
|
49
106
|
requestCount: number;
|
|
50
107
|
};
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/mock-http-bins.d.ts
|
|
51
110
|
/**
|
|
52
|
-
*
|
|
53
|
-
* implementation is in-memory; alternative backends (Redis, SQLite, etc.) can
|
|
54
|
-
* implement this interface without changes to the rest of the codebase.
|
|
55
|
-
*/
|
|
56
|
-
interface BinStore {
|
|
57
|
-
/** Create a new bin record. */
|
|
58
|
-
createBin(bin: Bin): void;
|
|
59
|
-
/** Look up a bin by id. */
|
|
60
|
-
getBin(id: string): Bin | undefined;
|
|
61
|
-
/** Return all bins. */
|
|
62
|
-
listBins(): Bin[];
|
|
63
|
-
/** Remove a bin and all its captured requests. Returns true if removed. */
|
|
64
|
-
deleteBin(id: string): boolean;
|
|
65
|
-
/**
|
|
66
|
-
* Append a captured request to a bin, dropping the oldest if the bin has
|
|
67
|
-
* already reached `max` requests (FIFO).
|
|
68
|
-
*/
|
|
69
|
-
addRequest(binId: string, req: CapturedRequest, max: number): void;
|
|
70
|
-
/** List captured requests for a bin (newest first). */
|
|
71
|
-
getRequests(binId: string): CapturedRequest[];
|
|
72
|
-
/** Look up a single captured request by id. */
|
|
73
|
-
getRequest(binId: string, reqId: string): CapturedRequest | undefined;
|
|
74
|
-
/** Clear all captured requests in a bin (the bin itself is preserved). */
|
|
75
|
-
clearRequests(binId: string): void;
|
|
76
|
-
/**
|
|
77
|
-
* Remove bins whose `expiresAt` is in the past relative to `now`.
|
|
78
|
-
* Returns the ids of bins that were removed.
|
|
79
|
-
*/
|
|
80
|
-
cleanupExpired(now: number): string[];
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* In-memory implementation of {@link BinStore} backed by two Maps. State is
|
|
84
|
-
* lost when the process exits.
|
|
111
|
+
* One bin and its captured requests, stored as a single Keyv value.
|
|
85
112
|
*/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
getBin(id: string): Bin | undefined;
|
|
91
|
-
listBins(): Bin[];
|
|
92
|
-
deleteBin(id: string): boolean;
|
|
93
|
-
addRequest(binId: string, req: CapturedRequest, max: number): void;
|
|
94
|
-
getRequests(binId: string): CapturedRequest[];
|
|
95
|
-
getRequest(binId: string, reqId: string): CapturedRequest | undefined;
|
|
96
|
-
clearRequests(binId: string): void;
|
|
97
|
-
cleanupExpired(now: number): string[];
|
|
98
|
-
}
|
|
99
|
-
//#endregion
|
|
100
|
-
//#region src/bin-manager.d.ts
|
|
113
|
+
type BinDocument = {
|
|
114
|
+
bin: Bin;
|
|
115
|
+
requests: CapturedRequest[];
|
|
116
|
+
};
|
|
101
117
|
/**
|
|
102
|
-
*
|
|
118
|
+
* Options for a {@link MockHttpBins} instance.
|
|
103
119
|
*/
|
|
104
|
-
type
|
|
105
|
-
/**
|
|
106
|
-
|
|
107
|
-
|
|
120
|
+
type MockHttpBinsOptions = {
|
|
121
|
+
/**
|
|
122
|
+
* Keyv store for bins. The default is `new Keyv()`, which keeps data in memory.
|
|
123
|
+
* Pass an instance when you want a different Keyv adapter.
|
|
124
|
+
*/
|
|
125
|
+
keyv?: Keyv<BinDocument>;
|
|
126
|
+
/** Bin lifetime in milliseconds. The default is 24 hours. */
|
|
108
127
|
defaultTtlMs?: number;
|
|
109
|
-
/** Maximum number of requests
|
|
128
|
+
/** Maximum number of requests in one bin. The default is 100. */
|
|
110
129
|
maxRequestsPerBin?: number;
|
|
111
|
-
/** Maximum captured body size in bytes.
|
|
130
|
+
/** Maximum captured body size in bytes. The default is 128 KiB. */
|
|
112
131
|
maxBodySize?: number;
|
|
113
|
-
/** Length of generated bin
|
|
132
|
+
/** Length of a generated bin id or request id. The default is 12. */
|
|
114
133
|
idLength?: number;
|
|
115
|
-
/** Interval between cleanup sweeps in milliseconds. Defaults to 1 minute. */
|
|
116
|
-
cleanupIntervalMs?: number;
|
|
117
134
|
};
|
|
118
135
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
136
|
+
* Stores request bins in Keyv.
|
|
137
|
+
* The default store is `new Keyv()`, which keeps data in memory.
|
|
121
138
|
*/
|
|
122
|
-
export declare class
|
|
123
|
-
private readonly
|
|
139
|
+
export declare class MockHttpBins {
|
|
140
|
+
private readonly _keyv;
|
|
124
141
|
private readonly _defaultTtlMs;
|
|
125
142
|
private readonly _maxRequestsPerBin;
|
|
126
143
|
private readonly _maxBodySize;
|
|
127
144
|
private readonly _idLength;
|
|
128
|
-
private readonly
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
get store(): BinStore;
|
|
145
|
+
private readonly _locks;
|
|
146
|
+
constructor(options?: MockHttpBinsOptions);
|
|
147
|
+
/** Keyv instance that stores the bins. */
|
|
148
|
+
get keyv(): Keyv<BinDocument>;
|
|
133
149
|
/** Maximum captured body size in bytes. */
|
|
134
150
|
get maxBodySize(): number;
|
|
135
|
-
/** Maximum number of requests
|
|
151
|
+
/** Maximum number of requests in one bin. */
|
|
136
152
|
get maxRequestsPerBin(): number;
|
|
137
153
|
/** Default bin lifetime in milliseconds. */
|
|
138
154
|
get defaultTtlMs(): number;
|
|
139
|
-
/** Create a new bin with the configured TTL. */
|
|
140
|
-
createBin(): Bin;
|
|
141
|
-
/**
|
|
142
|
-
* Look up a bin by id. Returns `undefined` if the bin does not exist or
|
|
143
|
-
* has expired (in which case it is also lazily removed).
|
|
144
|
-
*/
|
|
145
|
-
getBin(id: string): Bin | undefined;
|
|
146
|
-
/** List all non-expired bins. */
|
|
147
|
-
listBins(): Bin[];
|
|
148
|
-
/** Delete a bin. Returns true if the bin existed. */
|
|
149
|
-
deleteBin(id: string): boolean;
|
|
150
|
-
/**
|
|
151
|
-
* Record an incoming request against a bin. Returns the stored
|
|
152
|
-
* {@link CapturedRequest}, or `undefined` if the bin does not exist or is
|
|
153
|
-
* expired.
|
|
154
|
-
*/
|
|
155
|
-
recordRequest(binId: string, raw: Omit<CapturedRequest, "id" | "binId" | "capturedAt">): CapturedRequest | undefined;
|
|
156
|
-
/** List captured requests for a bin (newest first). */
|
|
157
|
-
getRequests(binId: string): CapturedRequest[];
|
|
158
|
-
/** Look up a single captured request. */
|
|
159
|
-
getRequest(binId: string, reqId: string): CapturedRequest | undefined;
|
|
160
|
-
/** Clear all captured requests in a bin. */
|
|
161
|
-
clearRequests(binId: string): void;
|
|
162
|
-
/** Start the periodic cleanup of expired bins. */
|
|
163
|
-
start(): void;
|
|
164
|
-
/** Stop the periodic cleanup. Idempotent. */
|
|
165
|
-
stop(): void;
|
|
166
|
-
private generateId;
|
|
167
|
-
}
|
|
168
|
-
//#endregion
|
|
169
|
-
//#region src/certificate.d.ts
|
|
170
|
-
type CertificateOptions = {
|
|
171
155
|
/**
|
|
172
|
-
*
|
|
156
|
+
* Create a new bin with the configured lifetime.
|
|
157
|
+
* Throws when Keyv does not store the bin.
|
|
173
158
|
*/
|
|
174
|
-
|
|
159
|
+
createBin(): Promise<Bin>;
|
|
175
160
|
/**
|
|
176
|
-
*
|
|
161
|
+
* Look up a bin by id.
|
|
162
|
+
* Returns `undefined` when the bin does not exist or Keyv has expired it.
|
|
163
|
+
* A failed read also returns `undefined` when the Keyv instance has an `error` listener.
|
|
177
164
|
*/
|
|
178
|
-
|
|
179
|
-
type: "dns";
|
|
180
|
-
value: string;
|
|
181
|
-
} | {
|
|
182
|
-
type: "ip";
|
|
183
|
-
value: string;
|
|
184
|
-
}>;
|
|
165
|
+
getBin(id: string): Promise<Bin | undefined>;
|
|
185
166
|
/**
|
|
186
|
-
*
|
|
167
|
+
* List the bins that Keyv still holds.
|
|
168
|
+
* Throws when the Keyv store has no iterator.
|
|
169
|
+
* When the Keyv instance has an `error` listener, a failed walk returns the bins read before the error.
|
|
187
170
|
*/
|
|
188
|
-
|
|
171
|
+
listBins(): Promise<Bin[]>;
|
|
189
172
|
/**
|
|
190
|
-
*
|
|
173
|
+
* Delete a bin. Returns true when the bin existed.
|
|
174
|
+
* The delete waits for captures already queued for that bin, so a capture cannot write the bin back.
|
|
191
175
|
*/
|
|
192
|
-
|
|
193
|
-
};
|
|
194
|
-
type CertificateResult = {
|
|
176
|
+
deleteBin(id: string): Promise<boolean>;
|
|
195
177
|
/**
|
|
196
|
-
*
|
|
178
|
+
* Record a request for a bin.
|
|
179
|
+
* Returns `undefined` when the bin does not exist.
|
|
180
|
+
* Throws when Keyv does not store the capture.
|
|
197
181
|
*/
|
|
198
|
-
|
|
182
|
+
recordRequest(binId: string, raw: Omit<CapturedRequest, "id" | "binId" | "capturedAt">): Promise<CapturedRequest | undefined>;
|
|
183
|
+
/** List captured requests for a bin, newest first. */
|
|
184
|
+
getRequests(binId: string): Promise<CapturedRequest[]>;
|
|
185
|
+
/** Look up one captured request. */
|
|
186
|
+
getRequest(binId: string, reqId: string): Promise<CapturedRequest | undefined>;
|
|
199
187
|
/**
|
|
200
|
-
*
|
|
188
|
+
* Remove captured requests and keep the bin.
|
|
189
|
+
* Returns false when the bin does not exist.
|
|
190
|
+
* Throws when Keyv does not store the change.
|
|
201
191
|
*/
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
192
|
+
clearRequests(binId: string): Promise<boolean>;
|
|
193
|
+
/** Keyv 6 yields nothing when the store has no iterator. Listing must fail instead. */
|
|
194
|
+
private canIterate;
|
|
195
|
+
/** Returns false when the bin does not exist. */
|
|
196
|
+
private appendRequest;
|
|
205
197
|
/**
|
|
206
|
-
*
|
|
198
|
+
* Write a bin document with a TTL from its lifetime.
|
|
199
|
+
* Keyv returns false or throws when the write fails. Both cases throw here.
|
|
207
200
|
*/
|
|
208
|
-
|
|
201
|
+
private save;
|
|
209
202
|
/**
|
|
210
|
-
*
|
|
203
|
+
* Run writes for one bin in order.
|
|
204
|
+
* A failed write does not block the next write for that bin.
|
|
211
205
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
export declare function generateCertificate(options?: CertificateOptions): CertificateResult;
|
|
219
|
-
/**
|
|
220
|
-
* Generate a self-signed certificate and write PEM files to disk.
|
|
221
|
-
* Returns the same CertificateResult as generateCertificate().
|
|
222
|
-
*/
|
|
223
|
-
export declare function generateCertificateFiles(options: CertificateFileOptions): Promise<CertificateResult>;
|
|
206
|
+
private enqueue;
|
|
207
|
+
/** Keyv keeps a key without a TTL forever, so the TTL never drops below 1 ms. */
|
|
208
|
+
private ttlFor;
|
|
209
|
+
private binKey;
|
|
210
|
+
private generateId;
|
|
211
|
+
}
|
|
224
212
|
//#endregion
|
|
225
213
|
//#region src/tap-manager.d.ts
|
|
226
214
|
/**
|
|
@@ -430,12 +418,6 @@ type MockHttpOptions = {
|
|
|
430
418
|
* timeout (needed on Cloudflare Workers, where avvio timers are unreliable).
|
|
431
419
|
*/
|
|
432
420
|
pluginTimeout?: number;
|
|
433
|
-
/**
|
|
434
|
-
* Whether to start the request-bin cleanup timer during `initialize()`.
|
|
435
|
-
* Defaults to true. Disable this on Cloudflare Workers, where `setInterval`
|
|
436
|
-
* during startup can hang the isolate.
|
|
437
|
-
*/
|
|
438
|
-
startBins?: boolean;
|
|
439
421
|
/**
|
|
440
422
|
* HTTPS configuration. Set to true to auto-generate a self-signed certificate,
|
|
441
423
|
* or provide an object with cert/key strings/paths or auto-generation options.
|
|
@@ -464,7 +446,6 @@ declare class MockHttp extends Hookified {
|
|
|
464
446
|
private _siteDistPath;
|
|
465
447
|
private _logging;
|
|
466
448
|
private _pluginTimeout?;
|
|
467
|
-
private _startBins;
|
|
468
449
|
private _httpBin;
|
|
469
450
|
private _rateLimit?;
|
|
470
451
|
private _http2;
|
|
@@ -563,16 +544,6 @@ declare class MockHttp extends Hookified {
|
|
|
563
544
|
* Fastify plugin boot timeout in milliseconds. `undefined` uses Fastify's default.
|
|
564
545
|
*/
|
|
565
546
|
set pluginTimeout(pluginTimeout: number | undefined);
|
|
566
|
-
/**
|
|
567
|
-
* Whether to start the request-bin cleanup timer during `initialize()`.
|
|
568
|
-
* @default true
|
|
569
|
-
*/
|
|
570
|
-
get startBins(): boolean;
|
|
571
|
-
/**
|
|
572
|
-
* Whether to start the request-bin cleanup timer during `initialize()`.
|
|
573
|
-
* @default true
|
|
574
|
-
*/
|
|
575
|
-
set startBins(startBins: boolean);
|
|
576
547
|
/**
|
|
577
548
|
* HTTPS configuration. Set to true to auto-generate a self-signed certificate,
|
|
578
549
|
* or provide an HttpsOptions object with cert/key or auto-generation options.
|
|
@@ -646,13 +617,13 @@ declare class MockHttp extends Hookified {
|
|
|
646
617
|
*/
|
|
647
618
|
set taps(taps: TapManager);
|
|
648
619
|
/**
|
|
649
|
-
* The
|
|
620
|
+
* The MockHttpBins instance for request bins.
|
|
650
621
|
*/
|
|
651
|
-
get bins():
|
|
622
|
+
get bins(): MockHttpBins;
|
|
652
623
|
/**
|
|
653
|
-
* The
|
|
624
|
+
* The MockHttpBins instance for request bins.
|
|
654
625
|
*/
|
|
655
|
-
set bins(bins:
|
|
626
|
+
set bins(bins: MockHttpBins);
|
|
656
627
|
/**
|
|
657
628
|
* Register plugins and routes without listening. Use `inject()` against
|
|
658
629
|
* `server`, or call `start()` to bind a port.
|
|
@@ -749,4 +720,4 @@ declare class MockHttp extends Hookified {
|
|
|
749
720
|
//#region src/index.d.ts
|
|
750
721
|
export declare const start: () => Promise<MockHttp>;
|
|
751
722
|
//#endregion
|
|
752
|
-
export { type Bin, type
|
|
723
|
+
export { type Bin, type CapturedRequest, type CertificateFileOptions, type CertificateOptions, type CertificateResult, type HttpsOptions, type InjectionMatcher, type InjectionResponse, type InjectionTap, type MockHttpBinsOptions, MockHttp as default, MockHttp as mockhttp };
|