@jaredwray/mockhttp 1.9.0 → 2.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.
Files changed (67) hide show
  1. package/README.md +40 -8
  2. package/dist/index.d.mts +90 -11
  3. package/dist/index.mjs +203 -240
  4. package/package.json +17 -13
  5. package/site/dist/api/index.html +17983 -0
  6. package/site/dist/api/swagger.json +5487 -0
  7. package/site/dist/changelog/index.html +784 -0
  8. package/site/dist/changelog/page/2/index.html +577 -0
  9. package/site/dist/changelog/v0-2-0/index.html +437 -0
  10. package/site/dist/changelog/v0-2-1/index.html +438 -0
  11. package/site/dist/changelog/v0-2-2/index.html +436 -0
  12. package/site/dist/changelog/v0-2-3/index.html +440 -0
  13. package/site/dist/changelog/v0-3-0/index.html +443 -0
  14. package/site/dist/changelog/v0-4-0/index.html +437 -0
  15. package/site/dist/changelog/v0-4-1/index.html +441 -0
  16. package/site/dist/changelog/v0-4-2/index.html +446 -0
  17. package/site/dist/changelog/v0-4-3/index.html +437 -0
  18. package/site/dist/changelog/v0-5-0/index.html +443 -0
  19. package/site/dist/changelog/v0-6-0/index.html +444 -0
  20. package/site/dist/changelog/v0-6-1/index.html +441 -0
  21. package/site/dist/changelog/v0-7-0/index.html +445 -0
  22. package/site/dist/changelog/v0-8-0/index.html +440 -0
  23. package/site/dist/changelog/v0-9-0/index.html +437 -0
  24. package/site/dist/changelog/v1-0-0/index.html +446 -0
  25. package/site/dist/changelog/v1-0-1/index.html +437 -0
  26. package/site/dist/changelog/v1-1-0/index.html +445 -0
  27. package/site/dist/changelog/v1-2-0/index.html +443 -0
  28. package/site/dist/changelog/v1-3-0/index.html +448 -0
  29. package/site/dist/changelog/v1-4-0/index.html +445 -0
  30. package/site/dist/changelog/v1-5-0/index.html +439 -0
  31. package/site/dist/changelog/v1-5-1/index.html +443 -0
  32. package/site/dist/changelog/v1-6-0/index.html +467 -0
  33. package/site/dist/changelog/v1-7-0/index.html +462 -0
  34. package/site/dist/changelog/v1-8-0/index.html +481 -0
  35. package/site/dist/changelog/v1-8-1/index.html +457 -0
  36. package/site/dist/changelog/v1-9-0/index.html +495 -0
  37. package/site/dist/changelog/v2-0-0/index.html +500 -0
  38. package/site/dist/changelog/v2-1-0/index.html +476 -0
  39. package/site/dist/changelog-latest.json +269 -0
  40. package/site/dist/changelog.json +399 -0
  41. package/site/dist/css/api.css +1113 -0
  42. package/site/dist/css/highlight/highlight.min.js +1433 -0
  43. package/site/dist/css/highlight/styles/base16/docula.css +123 -0
  44. package/site/dist/css/home.css +133 -0
  45. package/site/dist/css/styles.css +1227 -0
  46. package/site/dist/css/variables.css +32 -0
  47. package/site/dist/docs/bins/index.html +980 -0
  48. package/site/dist/docs/configuration/index.html +809 -0
  49. package/site/dist/docs/hosted/index.html +583 -0
  50. package/site/dist/docs/how-to-deploy/cloudflare/index.html +655 -0
  51. package/site/dist/docs/how-to-deploy/docker/index.html +664 -0
  52. package/site/dist/docs/how-to-deploy/wasmer/index.html +625 -0
  53. package/site/dist/docs/https/index.html +796 -0
  54. package/site/dist/docs/index.html +640 -0
  55. package/site/dist/docs/library/index.html +818 -0
  56. package/site/dist/docs/taps/index.html +746 -0
  57. package/site/dist/favicon.svg +14 -0
  58. package/site/dist/feed.xml +1 -0
  59. package/site/dist/index.html +640 -0
  60. package/site/dist/js/api.js +363 -0
  61. package/site/dist/js/search.js +436 -0
  62. package/site/dist/llms-full.txt +7640 -0
  63. package/site/dist/llms.txt +43 -0
  64. package/site/dist/logo.svg +21 -0
  65. package/site/dist/robots.txt +2 -0
  66. package/site/dist/search-index.json +1 -0
  67. package/site/dist/sitemap.xml +1 -0
package/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  [![npm](https://img.shields.io/npm/v/@jaredwray/mockhttp)](https://npmjs.com/package/@jaredwray/mockhttp)
8
8
  [![Docker Pulls](https://img.shields.io/docker/pulls/jaredwray/mockhttp)](https://hub.docker.com/r/jaredwray/mockhttp)
9
9
  [![mockhttp.org](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fjaredwray.com%2Fapi%2Fmockhttp-traffic&query=%24.message&label=mockhttp.org)](https://mockhttp.org)
10
+ [![Drydock review](https://img.shields.io/endpoint?url=https%3A%2F%2Fdrydock.org%2Fpublic%2Fbadge%2Fnpm%2F%40jaredwray%2Fmockhttp)](https://drydock.org/diff/@jaredwray/mockhttp)
10
11
 
11
12
  A simple HTTP server that can be used to mock HTTP responses for testing purposes. Inspired by [httpbin](https://httpbin.org/) and built using `nodejs` and `fastify` with the idea of running it via https://mockhttp.org, via docker `jaredwray/mockhttp`, or nodejs `npm install @jaredwray/mockhttp`.
12
13
 
@@ -16,9 +17,9 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
16
17
  * Bins - Capture and inspect incoming HTTP requests (great for webhook debugging)
17
18
  * `@fastify/helmet` built in by default
18
19
  * Built with `nodejs`, `typescript`, and `fastify`
19
- * Deploy via `docker` or `nodejs`
20
- * Global deployment via [mockhttp.org](https://mockhttp.org) (free service)
21
- * Better API documentation and examples
20
+ * Deploy via `docker`, `nodejs`, or Wasmer Edge
21
+ * Free hosted service at [mockhttp.org](https://mockhttp.org), running on a Cloudflare Worker
22
+ * Documentation site and interactive OpenAPI reference (Docula)
22
23
  * Auto detect the port that is not in use
23
24
  * Maintained and updated regularly!
24
25
 
@@ -26,6 +27,7 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
26
27
  - [Deploy via Docker](#deploy-via-docker)
27
28
  - [Deploy via Docker Compose](#deploy-via-docker-compose)
28
29
  - [Deploy via NodeJS](#deploy-via-nodejs)
30
+ - [Deploy via Wasmer](#deploy-via-wasmer)
29
31
  - [HTTPS Support](#https-support)
30
32
  - [HTTP/2 Support](#http2-support)
31
33
  - [Response Injection (Tap Feature)](#response-injection-tap-feature)
@@ -82,6 +84,12 @@ console.log(response);
82
84
  await mock.close(); // stop the server
83
85
  ```
84
86
 
87
+ # Deploy via Wasmer
88
+
89
+ Wasmer Edge runs the same Node server as Docker. [deploy-wasmer.yaml](.github/workflows/deploy-wasmer.yaml) publishes the source with `wasmer deploy --build-remote`, and the `Anybuild` file builds the server and the docs site. It runs when you start it by hand and when a GitHub Release is published. It does not deploy the Cloudflare Worker. The test app is [mockhttp.wasmer.app](https://mockhttp.wasmer.app).
90
+
91
+ Bins, taps, and `@fastify/rate-limit` are per instance on Wasmer. All [mockhttp.org](https://mockhttp.org) traffic stays on Cloudflare, where the rate limiter is shared across requests.
92
+
85
93
  # HTTPS Support
86
94
 
87
95
  MockHttp supports HTTPS with auto-generated self-signed certificates or your own custom certificates. No external dependencies are required — certificate generation uses only Node.js built-in `crypto`.
@@ -998,9 +1006,13 @@ new MockHttp(options?)
998
1006
  - `host?`: string - The host to listen on (default: '0.0.0.0')
999
1007
  - `autoDetectPort?`: boolean - Auto-detect next available port if in use (default: true)
1000
1008
  - `helmet?`: boolean - Use Helmet for security headers (default: true)
1001
- - `apiDocs?`: boolean - Enable Swagger API documentation (default: true)
1009
+ - `apiDocs?`: boolean - Serve the Docula documentation site and OpenAPI spec (default: true)
1010
+ - `siteDistPath?`: string - Path to the built Docula site (default: the package `site/dist` directory)
1002
1011
  - `rateLimit?`: RateLimitPluginOptions - Configure rate limiting (default: 1000 req/min, localhost excluded)
1003
1012
  - `logging?`: boolean - Enable logging (default: true)
1013
+ - `staticFiles?`: boolean - Serve files from the package `public/` directory (default: true)
1014
+ - `pluginTimeout?`: number - Fastify plugin boot timeout in milliseconds; `0` disables it (default: Fastify's timeout)
1015
+ - `startBins?`: boolean - Start the request-bin cleanup timer during `initialize()` (default: true)
1004
1016
  - `httpBin?`: HttpBinOptions - Configure which httpbin routes to enable
1005
1017
  - `httpMethods?`: boolean - Enable HTTP method routes (default: true)
1006
1018
  - `redirects?`: boolean - Enable redirect routes (default: true)
@@ -1024,8 +1036,12 @@ new MockHttp(options?)
1024
1036
  - `host`: string - Get/set the server host
1025
1037
  - `autoDetectPort`: boolean - Get/set auto-detect port behavior
1026
1038
  - `helmet`: boolean - Get/set Helmet security headers
1027
- - `apiDocs`: boolean - Get/set API documentation
1039
+ - `apiDocs`: boolean - Get/set whether the documentation site and OpenAPI spec are served
1040
+ - `siteDistPath`: string - Get/set the path to the built Docula site
1028
1041
  - `logging`: boolean - Get/set logging enabled state
1042
+ - `staticFiles`: boolean - Get/set whether files from `public/` are served
1043
+ - `pluginTimeout`: number | undefined - Get/set Fastify plugin boot timeout
1044
+ - `startBins`: boolean - Get/set whether bin cleanup starts during `initialize()`
1029
1045
  - `rateLimit`: RateLimitPluginOptions | undefined - Get/set rate limiting options
1030
1046
  - `httpBin`: HttpBinOptions - Get/set httpbin route options
1031
1047
  - `https`: HttpsOptions | undefined - Get/set HTTPS configuration
@@ -1038,6 +1054,10 @@ new MockHttp(options?)
1038
1054
 
1039
1055
  ### Methods
1040
1056
 
1057
+ #### `async initialize()`
1058
+
1059
+ Register plugins and routes without listening. Use `server.inject()` afterwards, or call `start()` to bind a port.
1060
+
1041
1061
  #### `async start()`
1042
1062
 
1043
1063
  Start the Fastify server. If already running, it will be closed and restarted.
@@ -1054,7 +1074,15 @@ Detect the next available port.
1054
1074
 
1055
1075
  #### `async registerApiDocs(fastifyInstance?)`
1056
1076
 
1057
- Register Swagger API documentation routes.
1077
+ Register OpenAPI generation and the Docula documentation site.
1078
+
1079
+ #### `async registerSwagger(fastifyInstance?)`
1080
+
1081
+ Register `@fastify/swagger` and the live `/openapi.json` spec route.
1082
+
1083
+ #### `async registerSite(fastifyInstance?)`
1084
+
1085
+ Serve the built Docula site from `siteDistPath` when the directory exists.
1058
1086
 
1059
1087
  #### `async registerHttpMethods(fastifyInstance?)`
1060
1088
 
@@ -1210,9 +1238,13 @@ A getter that returns whether there are any active injections.
1210
1238
 
1211
1239
  **Returns:** boolean - `true` if there are active injections, `false` otherwise
1212
1240
 
1213
- # About mockhttp.org
1241
+ # About mockhttp.org
1242
+
1243
+ [mockhttp.org](https://mockhttp.org) is a free hosted instance of this codebase for testing. It runs on a [Cloudflare Worker](https://developers.cloudflare.com/workers/) (no containers): documentation is served from Workers Assets, and mock APIs such as `/get` and `/post` run in the Worker. The service is globally available and rate-limited (1000 requests per minute per IP) to prevent abuse.
1244
+
1245
+ All [mockhttp.org](https://mockhttp.org) traffic stays on Cloudflare. A separate test app runs the Node server on Wasmer Edge at [mockhttp.wasmer.app](https://mockhttp.wasmer.app).
1214
1246
 
1215
- [mockhttp.org](https://mockhttp.org) is a free service that runs this codebase and allows you to use it for testing purposes. It is a simple way to mock HTTP responses for testing purposes. It is globally available has some limitations on it to prevent abuse such as requests per second. It is ran via [Cloudflare](https://cloudflare.com) and [Google Cloud Run](https://cloud.google.com/run/) across 7 regions globally and can do millions of requests per second.
1247
+ Getting Started is served at `/` and `/docs`, other guides at `/docs/...`, and the interactive HTTP API reference at `/api`. Mock endpoints such as `/get` and `/post` are unchanged.
1216
1248
 
1217
1249
  # Contributing
1218
1250
 
package/dist/index.d.mts CHANGED
@@ -83,7 +83,7 @@ interface BinStore {
83
83
  * In-memory implementation of {@link BinStore} backed by two Maps. State is
84
84
  * lost when the process exits.
85
85
  */
86
- declare class InMemoryBinStore implements BinStore {
86
+ export declare class InMemoryBinStore implements BinStore {
87
87
  private readonly _bins;
88
88
  private readonly _requests;
89
89
  createBin(bin: Bin): void;
@@ -119,7 +119,7 @@ type BinManagerOptions = {
119
119
  * Manages request bins: ephemeral URL endpoints that capture incoming HTTP
120
120
  * requests so they can be inspected later.
121
121
  */
122
- declare class BinManager {
122
+ export declare class BinManager {
123
123
  private readonly _store;
124
124
  private readonly _defaultTtlMs;
125
125
  private readonly _maxRequestsPerBin;
@@ -215,12 +215,12 @@ type CertificateFileOptions = CertificateOptions & {
215
215
  * Generate a self-signed certificate using only Node.js built-in crypto.
216
216
  * Returns PEM-encoded certificate and private key strings.
217
217
  */
218
- declare function generateCertificate(options?: CertificateOptions): CertificateResult;
218
+ export declare function generateCertificate(options?: CertificateOptions): CertificateResult;
219
219
  /**
220
220
  * Generate a self-signed certificate and write PEM files to disk.
221
221
  * Returns the same CertificateResult as generateCertificate().
222
222
  */
223
- declare function generateCertificateFiles(options: CertificateFileOptions): Promise<CertificateResult>;
223
+ export declare function generateCertificateFiles(options: CertificateFileOptions): Promise<CertificateResult>;
224
224
  //#endregion
225
225
  //#region src/tap-manager.d.ts
226
226
  /**
@@ -394,11 +394,22 @@ type MockHttpOptions = {
394
394
  */
395
395
  helmet?: boolean;
396
396
  /**
397
- * Whether to use Swagger for API documentation. Defaults to true.
397
+ * Whether to serve files from the package `public/` directory (favicon, logos).
398
+ * Defaults to true. Disable this when those files are served by another layer
399
+ * such as Cloudflare Workers Assets.
400
+ */
401
+ staticFiles?: boolean;
402
+ /**
403
+ * Whether to serve the Docula documentation site and OpenAPI spec. Defaults to true.
398
404
  */
399
405
  apiDocs?: boolean;
400
406
  /**
401
- * Whether to use Swagger UI. Defaults to true.
407
+ * Path to the built Docula site (`site/dist`). Defaults to the package's
408
+ * `site/dist` directory. Explicit paths are resolved from the current working directory.
409
+ */
410
+ siteDistPath?: string;
411
+ /**
412
+ * Configure which httpbin-style mock routes to enable. Defaults to all enabled.
402
413
  */
403
414
  httpBin?: HttpBinOptions;
404
415
  /**
@@ -414,6 +425,17 @@ type MockHttpOptions = {
414
425
  * Whether to enable logging. Defaults to true.
415
426
  */
416
427
  logging?: boolean;
428
+ /**
429
+ * Fastify plugin boot timeout in milliseconds. Set to `0` to disable the
430
+ * timeout (needed on Cloudflare Workers, where avvio timers are unreliable).
431
+ */
432
+ 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;
417
439
  /**
418
440
  * HTTPS configuration. Set to true to auto-generate a self-signed certificate,
419
441
  * or provide an object with cert/key strings/paths or auto-generation options.
@@ -437,8 +459,12 @@ declare class MockHttp extends Hookified {
437
459
  private _host;
438
460
  private _autoDetectPort;
439
461
  private _helmet;
462
+ private _staticFiles;
440
463
  private _apiDocs;
464
+ private _siteDistPath;
441
465
  private _logging;
466
+ private _pluginTimeout?;
467
+ private _startBins;
442
468
  private _httpBin;
443
469
  private _rateLimit?;
444
470
  private _http2;
@@ -490,15 +516,35 @@ declare class MockHttp extends Hookified {
490
516
  */
491
517
  set helmet(helmet: boolean);
492
518
  /**
493
- * Whether to use Swagger for API documentation. Defaults to true.
519
+ * Whether to serve files from the package `public/` directory. Defaults to true.
520
+ * @default true
521
+ */
522
+ get staticFiles(): boolean;
523
+ /**
524
+ * Whether to serve files from the package `public/` directory. Defaults to true.
525
+ * @default true
526
+ */
527
+ set staticFiles(staticFiles: boolean);
528
+ /**
529
+ * Whether to serve the Docula documentation site and OpenAPI spec. Defaults to true.
494
530
  * @default true
495
531
  */
496
532
  get apiDocs(): boolean;
497
533
  /**
498
- * Whether to use Swagger for API documentation. Defaults to true.
534
+ * Whether to serve the Docula documentation site and OpenAPI spec. Defaults to true.
499
535
  * @default true
500
536
  */
501
537
  set apiDocs(apiDocs: boolean);
538
+ /**
539
+ * Path to the built Docula site served when `apiDocs` is enabled.
540
+ * @default the package `site/dist` directory
541
+ */
542
+ get siteDistPath(): string;
543
+ /**
544
+ * Path to the built Docula site served when `apiDocs` is enabled.
545
+ * @default the package `site/dist` directory
546
+ */
547
+ set siteDistPath(siteDistPath: string);
502
548
  /**
503
549
  * Whether to enable logging. Defaults to true.
504
550
  * @default true
@@ -509,6 +555,24 @@ declare class MockHttp extends Hookified {
509
555
  * @default true
510
556
  */
511
557
  set logging(logging: boolean);
558
+ /**
559
+ * Fastify plugin boot timeout in milliseconds. `undefined` uses Fastify's default.
560
+ */
561
+ get pluginTimeout(): number | undefined;
562
+ /**
563
+ * Fastify plugin boot timeout in milliseconds. `undefined` uses Fastify's default.
564
+ */
565
+ 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);
512
576
  /**
513
577
  * HTTPS configuration. Set to true to auto-generate a self-signed certificate,
514
578
  * or provide an HttpsOptions object with cert/key or auto-generation options.
@@ -589,6 +653,11 @@ declare class MockHttp extends Hookified {
589
653
  * The BinManager instance for managing request bins.
590
654
  */
591
655
  set bins(bins: BinManager);
656
+ /**
657
+ * Register plugins and routes without listening. Use `inject()` against
658
+ * `server`, or call `start()` to bind a port.
659
+ */
660
+ initialize(): Promise<void>;
592
661
  /**
593
662
  * Start the Fastify server. If the server is already running, it will be closed and restarted.
594
663
  */
@@ -603,10 +672,20 @@ declare class MockHttp extends Hookified {
603
672
  */
604
673
  detectPort(): Promise<number>;
605
674
  /**
606
- * This will register the API documentation routes including openapi and swagger ui.
675
+ * Register OpenAPI generation and the Docula documentation site.
607
676
  * @param fastifyInstance - the server instance to register the routes on.
608
677
  */
609
678
  registerApiDocs(fastifyInstance?: FastifyInstance): Promise<void>;
679
+ /**
680
+ * Register `@fastify/swagger` and the live `/openapi.json` spec route.
681
+ * @param fastifyInstance - the server instance to register the routes on.
682
+ */
683
+ registerSwagger(fastifyInstance?: FastifyInstance): Promise<void>;
684
+ /**
685
+ * Serve the built Docula site from `siteDistPath` when the directory exists.
686
+ * @param fastifyInstance - the server instance to register the routes on.
687
+ */
688
+ registerSite(fastifyInstance?: FastifyInstance): Promise<void>;
610
689
  /**
611
690
  * Register the HTTP methods routes.
612
691
  * @param fastifyInstance - the server instance to register the routes on.
@@ -668,6 +747,6 @@ declare class MockHttp extends Hookified {
668
747
  }
669
748
  //#endregion
670
749
  //#region src/index.d.ts
671
- declare const start: () => Promise<MockHttp>;
750
+ export declare const start: () => Promise<MockHttp>;
672
751
  //#endregion
673
- export { type Bin, BinManager, type BinManagerOptions, type BinStore, type CapturedRequest, type CertificateFileOptions, type CertificateOptions, type CertificateResult, type HttpsOptions, InMemoryBinStore, type InjectionMatcher, type InjectionResponse, type InjectionTap, MockHttp as default, MockHttp as mockhttp, generateCertificate, generateCertificateFiles, start };
752
+ export { type Bin, type BinManagerOptions, type BinStore, type CapturedRequest, type CertificateFileOptions, type CertificateOptions, type CertificateResult, type HttpsOptions, type InjectionMatcher, type InjectionResponse, type InjectionTap, MockHttp as default, MockHttp as mockhttp };