mockaton 8.23.3 → 8.24.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 CHANGED
@@ -118,18 +118,13 @@ They will be saved in your `config.mocksDir` following the filename convention.
118
118
 
119
119
 
120
120
  ## Basic Usage
121
- Mockaton is a Node.js program with no dependencies.
122
-
123
- ### Create a Sample Mock
124
- The default `--mocks-dir` is **mockaton-mocks** in the current working directory.
121
+ Mockaton is a Node.js program available as an NPM module. After you have
122
+ Node.js, you can create a sample mock in the default mocks directory, which
123
+ is `./mockaton-mocks`. Then run it with `npx`, which installs it if needed.
125
124
  ```sh
126
125
  mkdir -p mockaton-mocks/api/
127
126
  echo "[1,2,3]" > mockaton-mocks/api/foo.GET.200.json
128
- ```
129
127
 
130
- ### Install and Run
131
- ```sh
132
- npm install mockaton
133
128
  npx mockaton --port 2345
134
129
  ```
135
130
 
@@ -139,34 +134,70 @@ CLI options override their counterparts in `mockaton.config.js`
139
134
  ```txt
140
135
  -c, --config <file> (default: ./mockaton.config.js)
141
136
 
142
- -m, --mocks-dir <dir> (default: ./mockaton-mocks/)
143
- -s, --static-dir <dir> (default: ./mockaton-static-mocks/)
144
-
145
137
  -H, --host <host> (default: 127.0.0.1)
146
138
  -p, --port <port> (default: 0) which means auto-assigned
139
+
140
+ -m, --mocks-dir <dir> (default: ./mockaton-mocks/)
141
+ -s, --static-dir <dir> (default: ./mockaton-static-mocks/)
147
142
  ```
148
143
 
149
144
 
150
- ## mockaton.config.js
151
- Optionally, use a `mockaton.config.js` file:
145
+ ## mockaton.config.js (Optional)
146
+ As an overview, these are the default options:
152
147
  ```js
153
- import { defineConfig } from 'mockaton'
148
+ import {
149
+ defineConfig,
150
+ jsToJsonPlugin,
151
+ openInBrowser,
152
+ SUPPORTED_METHODS
153
+ } from 'mockaton'
154
+
154
155
 
155
156
  export default defineConfig({
156
- port: 2345,
157
- mocksDir: 'my-mocks-dir',
158
- delayJitter: 0.5,
159
- // …
160
- })
157
+ mocksDir: 'mockaton-mocks',
158
+ staticDir: 'mockaton-static-mocks',
159
+ ignore: /(\.DS_Store|~)$/,
160
+
161
+ host: '127.0.0.1',
162
+ port: 0,
163
+
164
+ proxyFallback: '',
165
+ collectProxied: false,
166
+ formatCollectedJSON: true,
167
+
168
+ delay: 1200,
169
+ delayJitter: 0,
170
+
171
+ cookies: {},
172
+
173
+ extraHeaders: [],
174
+
175
+ extraMimes: {},
176
+
177
+ plugins: [
178
+ [/\.(js|ts)$/, jsToJsonPlugin]
179
+ ],
180
+
181
+ corsAllowed: true,
182
+ corsOrigins: ['*'],
183
+ corsMethods: SUPPORTED_METHODS,
184
+ corsHeaders: ['content-type', 'authorization'],
185
+ corsExposedHeaders: [],
186
+ corsCredentials: true,
187
+ corsMaxAge: 0,
188
+
189
+ onReady: await openInBrowser
190
+ })
161
191
  ```
162
192
 
163
193
  <details>
164
- <summary><b>See all config options</b></summary>
194
+ <summary><b>See Config Documentation</b></summary>
165
195
 
166
- ### `mocksDir: string`
167
- This is the only required field. The directory must exist.
196
+ ### `mocksDir?: string`
197
+ Defaults to `'mockaton-mocks'`.
168
198
 
169
199
  ### `staticDir?: string`
200
+ Defaults to `'mockaton-static-mocks'`.
170
201
  This option is not needed besides serving partial content (e.g., videos). But
171
202
  it’s convenient for serving 200 GET requests without having to add the filename
172
203
  extension convention. For example, for using Mockaton as a standalone demo server,
@@ -192,7 +223,7 @@ tested against the basename (filename without directory path).
192
223
 
193
224
 
194
225
  ### `host?: string`
195
- Defaults to `'localhost'`
226
+ Defaults to `'127.0.0.1'`
196
227
 
197
228
  ### `port?: number`
198
229
  Defaults to `0`, which means auto-assigned
@@ -622,7 +653,7 @@ api/foo/bar.GET.200.json
622
653
  <br/>
623
654
 
624
655
  ## Commander API
625
- `Commander` is a client for Mockaton’s HTTP API.
656
+ `Commander` is a JavaScript client for Mockaton’s HTTP API.
626
657
  All of its methods return their `fetch` response promise.
627
658
  ```js
628
659
  import { Commander } from 'mockaton'
@@ -632,7 +663,8 @@ const myMockatonAddr = 'http://localhost:2345'
632
663
  const mockaton = new Commander(myMockatonAddr)
633
664
  ```
634
665
 
635
- <br/>
666
+ <details>
667
+ <summary><b>See Commander Documentation</b></summary>
636
668
 
637
669
  ### Select a mock file for a route
638
670
  ```js
@@ -688,6 +720,7 @@ default, but the `proxyFallback`, `colledProxied`, and `corsAllowed` are not aff
688
720
  ```js
689
721
  await mockaton.reset()
690
722
  ```
723
+ </details>
691
724
 
692
725
 
693
726
  <br/>
@@ -704,7 +737,7 @@ example, if you are polling, and you want to test the state change.
704
737
 
705
738
  ### Client Side
706
739
  In contrast to Mockaton, which is an HTTP Server, these programs
707
- hijack the client (e.g., `fetch`) in Node.js and browsers.
740
+ hijack the HTTP client in Node.js and browsers.
708
741
 
709
742
  - [Mock Server Worker (MSW)](https://mswjs.io)
710
743
  - [Nock](https://github.com/nock/nock)
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "HTTP Mock Server",
4
4
  "type": "module",
5
- "version": "8.23.3",
5
+ "version": "8.24.0",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
@@ -25,7 +25,7 @@
25
25
  "outdated": "npm outdated --parseable | awk -F: '{ printf \"npm i %-30s ;# %s\\n\", $4, $2 }'"
26
26
  },
27
27
  "devDependencies": {
28
- "pixaton": "1.1.2",
28
+ "pixaton": "1.1.3",
29
29
  "puppeteer": "24.19.0"
30
30
  },
31
31
  "optionalDependencies": {
package/src/Api.js CHANGED
@@ -14,18 +14,17 @@ import { DF, API, LONG_POLL_SERVER_TIMEOUT } from './ApiConstants.js'
14
14
  import { sendOK, sendJSON, sendUnprocessableContent, sendFile } from './utils/http-response.js'
15
15
 
16
16
 
17
- const dashboardAssets = [
18
- '/ApiConstants.js',
19
- '/ApiCommander.js',
20
- '/Dashboard.css',
21
- '/Dashboard.js',
22
- '/Filename.js',
23
- '/Logo.svg'
24
- ]
25
-
26
17
  export const apiGetRequests = new Map([
27
- [API.dashboard, serveDashboard],
28
- ...dashboardAssets.map(f => [API.dashboard + f, serveDashboardAsset(f)]),
18
+ [API.dashboard, serveDashboardAsset('Dashboard.html')],
19
+ ...[
20
+ '/ApiConstants.js',
21
+ '/ApiCommander.js',
22
+ '/Dashboard.css',
23
+ '/Dashboard.js',
24
+ '/Filename.js',
25
+ '/Logo.svg'
26
+ ].map(f => [API.dashboard + f, serveDashboardAsset(f)]),
27
+
29
28
  [API.cors, getIsCorsAllowed],
30
29
  [API.static, listStaticFiles],
31
30
  [API.mocks, listMockBrokers],
@@ -55,13 +54,8 @@ export const apiPatchRequests = new Map([
55
54
 
56
55
  /** # GET */
57
56
 
58
- function serveDashboard(_, response) {
59
- sendFile(response, join(import.meta.dirname, 'Dashboard.html'))
60
- }
61
-
62
57
  function serveDashboardAsset(f) {
63
- return (_, response) =>
64
- sendFile(response, join(import.meta.dirname, f))
58
+ return (_, response) => sendFile(response, join(import.meta.dirname, f))
65
59
  }
66
60
 
67
61
  function listCookies(_, response) { sendJSON(response, cookie.list()) }
@@ -81,12 +75,10 @@ function longPollClientSyncVersion(req, response) {
81
75
  sendJSON(response, uiSyncVersion.version)
82
76
  return
83
77
  }
84
-
85
78
  function onAddOrRemoveMock() {
86
79
  uiSyncVersion.unsubscribe(onAddOrRemoveMock)
87
80
  sendJSON(response, uiSyncVersion.version)
88
81
  }
89
-
90
82
  response.setTimeout(LONG_POLL_SERVER_TIMEOUT, onAddOrRemoveMock)
91
83
  req.on('error', () => {
92
84
  uiSyncVersion.unsubscribe(onAddOrRemoveMock)
@@ -60,7 +60,7 @@ export class Commander {
60
60
 
61
61
  /** @type {JsonPromise<number>} */
62
62
  getSyncVersion(currentSyncVersion, abortSignal) {
63
- return fetch(API.syncVersion, {
63
+ return fetch(this.#addr + API.syncVersion, {
64
64
  signal: AbortSignal.any([abortSignal, AbortSignal.timeout(LONG_POLL_SERVER_TIMEOUT + 1000)]),
65
65
  headers: {
66
66
  [DF.syncVersion]: currentSyncVersion
package/src/Dashboard.css CHANGED
@@ -80,14 +80,15 @@ body {
80
80
  grid-template-rows: auto 1fr;
81
81
  background: var(--colorBackground);
82
82
  color: var(--colorText);
83
+ font-family: system-ui, sans-serif;
83
84
  }
84
85
 
85
86
  * {
86
87
  box-sizing: border-box;
87
88
  padding: 0;
88
89
  border: 0;
90
+ font-family: inherit;
89
91
  margin: 0;
90
- font-family: system-ui, sans-serif;
91
92
  font-size: 100%;
92
93
  outline: 0;
93
94
  scrollbar-width: thin;
@@ -530,15 +531,11 @@ table {
530
531
  .PayloadViewer {
531
532
  pre {
532
533
  padding-top: 12px;
534
+ font-family: monospace;
533
535
 
534
536
  code {
535
537
  white-space: pre;
536
- tab-size: 4;
537
- font-family: monospace;
538
-
539
- * {
540
- font-family: monospace;
541
- }
538
+ tab-size: 2;
542
539
 
543
540
  .json {
544
541
  color: var(--colorSecondaryAction);
@@ -22,7 +22,7 @@ export async function dispatchMock(req, response) {
22
22
  return
23
23
  }
24
24
 
25
- console.log('%s → %s', decodeURIComponent(req.url), broker.file)
25
+ console.log('%s %s → %s', new Date().toISOString(), decodeURIComponent(req.url), broker.file)
26
26
  response.statusCode = broker.status
27
27
 
28
28
  if (cookie.getCurrent())
@@ -15,6 +15,9 @@ export async function dispatchStatic(req, response) {
15
15
  sendNotFound(response)
16
16
  return
17
17
  }
18
+
19
+ console.log('%s %s (static)', new Date().toISOString(), decodeURIComponent(req.url))
20
+
18
21
  const file = join(config.staticDir, broker.route)
19
22
  if (req.headers.range)
20
23
  await sendPartialContent(response, req.headers.range, file)
package/src/cli.js CHANGED
@@ -45,7 +45,7 @@ Options:
45
45
 
46
46
  Notes:
47
47
  * mockaton.config.js supports more options, see:
48
- https://github.com/ericfortis/mockaton?tab=readme-ov-file#config
48
+ https://github.com/ericfortis/mockaton?tab=readme-ov-file#mockatonconfigjs-optional
49
49
  * CLI options override their mockaton.config.js counterparts`)
50
50
 
51
51
  else if (args.config && !isFile(args.config)) {