mockaton 11.2.5 → 11.3.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 +15 -841
- package/index.js +1 -1
- package/package.json +7 -1
- package/src/client/ApiCommander.js +54 -39
- package/src/client/ApiConstants.js +1 -1
- package/src/client/Filename.js +3 -3
- package/src/client/app-store.test.js +81 -0
- package/src/client/app.js +83 -68
- package/src/client/dom-utils.js +5 -3
- package/src/client/styles.css +83 -48
- package/src/server/Api.js +112 -152
- package/src/server/Filename.js +8 -6
- package/src/server/MockBroker.js +1 -1
- package/src/server/MockDispatcher.js +5 -5
- package/src/server/Mockaton.js +23 -20
- package/src/server/Mockaton.test.js +1190 -0
- package/src/server/ProxyRelay.js +5 -5
- package/src/server/StaticDispatcher.js +4 -4
- package/src/server/Watcher.js +30 -3
- package/src/server/WatcherDevClient.js +37 -5
- package/src/server/cli.js +1 -0
- package/src/server/config.js +3 -2
- package/src/server/mockBrokersCollection.js +2 -1
- package/src/server/utils/{http-request.js → HttpIncomingMessage.js} +7 -1
- package/src/server/utils/HttpServerResponse.js +117 -0
- package/src/server/utils/fs.js +1 -0
- package/src/server/utils/http-cors.js +1 -1
- package/src/server/utils/http-cors.test.js +225 -0
- package/src/server/utils/logger.js +1 -1
- package/src/server/utils/mime.test.js +24 -0
- package/src/server/utils/validate.test.js +47 -0
- package/src/server/utils/http-response.js +0 -107
package/src/server/Mockaton.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import { createServer } from 'node:http'
|
|
2
|
+
|
|
2
3
|
import pkgJSON from '../../package.json' with { type: 'json' }
|
|
3
4
|
|
|
4
|
-
import { API } from './ApiConstants.js'
|
|
5
5
|
import { logger } from './utils/logger.js'
|
|
6
|
+
import { ServerResponse } from './utils/HttpServerResponse.js'
|
|
7
|
+
import { IncomingMessage } from './utils/HttpIncomingMessage.js'
|
|
8
|
+
import { setCorsHeaders, isPreflight } from './utils/http-cors.js'
|
|
9
|
+
import { BodyReaderError, hasControlChars } from './utils/HttpIncomingMessage.js'
|
|
10
|
+
|
|
11
|
+
import { API } from './ApiConstants.js'
|
|
6
12
|
import { config, setup } from './config.js'
|
|
13
|
+
import { apiPatchReqs, apiGetReqs } from './Api.js'
|
|
14
|
+
|
|
7
15
|
import { dispatchMock } from './MockDispatcher.js'
|
|
8
16
|
import { dispatchStatic } from './StaticDispatcher.js'
|
|
17
|
+
|
|
9
18
|
import * as staticCollection from './staticCollection.js'
|
|
10
19
|
import * as mockBrokerCollection from './mockBrokersCollection.js'
|
|
11
|
-
|
|
12
|
-
import { apiPatchRequests, apiGetRequests } from './Api.js'
|
|
13
|
-
import { BodyReaderError, hasControlChars } from './utils/http-request.js'
|
|
14
|
-
import {
|
|
15
|
-
setHeaders, sendNoContent, sendInternalServerError,
|
|
16
|
-
sendUnprocessable, sendTooLongURI, sendBadRequest
|
|
17
|
-
} from './utils/http-response.js'
|
|
20
|
+
|
|
18
21
|
import { watchDevSPA } from './WatcherDevClient.js'
|
|
19
22
|
import { watchMocksDir, watchStaticDir } from './Watcher.js'
|
|
20
23
|
|
|
@@ -33,7 +36,7 @@ export function Mockaton(options) {
|
|
|
33
36
|
if (config.hotReload)
|
|
34
37
|
watchDevSPA()
|
|
35
38
|
|
|
36
|
-
const server = createServer(onRequest)
|
|
39
|
+
const server = createServer({ IncomingMessage, ServerResponse }, onRequest)
|
|
37
40
|
server.on('error', reject)
|
|
38
41
|
server.listen(config.port, config.host, () => {
|
|
39
42
|
const url = `http://${server.address().address}:${server.address().port}`
|
|
@@ -49,17 +52,17 @@ export function Mockaton(options) {
|
|
|
49
52
|
async function onRequest(req, response) {
|
|
50
53
|
response.on('error', logger.warn)
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
response.setHeader('Server', `Mockaton ${pkgJSON.version}`)
|
|
56
|
+
response.setHeaderList(config.extraHeaders)
|
|
54
57
|
|
|
55
58
|
const url = req.url || ''
|
|
56
59
|
|
|
57
60
|
if (url.length > 2048) {
|
|
58
|
-
|
|
61
|
+
response.uriTooLong()
|
|
59
62
|
return
|
|
60
63
|
}
|
|
61
64
|
if (hasControlChars(url)) {
|
|
62
|
-
|
|
65
|
+
response.badRequest()
|
|
63
66
|
return
|
|
64
67
|
}
|
|
65
68
|
|
|
@@ -71,13 +74,13 @@ async function onRequest(req, response) {
|
|
|
71
74
|
const { pathname } = new URL(url, 'http://_')
|
|
72
75
|
|
|
73
76
|
if (isPreflight(req))
|
|
74
|
-
|
|
77
|
+
response.noContent()
|
|
75
78
|
|
|
76
|
-
else if (method === 'PATCH' &&
|
|
77
|
-
await
|
|
79
|
+
else if (method === 'PATCH' && apiPatchReqs.has(pathname))
|
|
80
|
+
await apiPatchReqs.get(pathname)(req, response)
|
|
78
81
|
|
|
79
|
-
else if (method === 'GET' &&
|
|
80
|
-
|
|
82
|
+
else if (method === 'GET' && apiGetReqs.has(pathname))
|
|
83
|
+
apiGetReqs.get(pathname)(req, response)
|
|
81
84
|
|
|
82
85
|
else if (method === 'GET' && staticCollection.brokerByRoute(pathname))
|
|
83
86
|
await dispatchStatic(req, response)
|
|
@@ -87,8 +90,8 @@ async function onRequest(req, response) {
|
|
|
87
90
|
}
|
|
88
91
|
catch (error) {
|
|
89
92
|
if (error instanceof BodyReaderError)
|
|
90
|
-
|
|
93
|
+
response.unprocessable(`${error.name}: ${error.message}`)
|
|
91
94
|
else
|
|
92
|
-
|
|
95
|
+
response.internalServerError(error)
|
|
93
96
|
}
|
|
94
97
|
}
|