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.
@@ -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
- import { setCorsHeaders, isPreflight } from './utils/http-cors.js'
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
- setHeaders(response, ['Server', `Mockaton ${pkgJSON.version}`])
53
- setHeaders(response, config.extraHeaders)
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
- sendTooLongURI(response)
61
+ response.uriTooLong()
59
62
  return
60
63
  }
61
64
  if (hasControlChars(url)) {
62
- sendBadRequest(response)
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
- sendNoContent(response)
77
+ response.noContent()
75
78
 
76
- else if (method === 'PATCH' && apiPatchRequests.has(pathname))
77
- await apiPatchRequests.get(pathname)(req, response)
79
+ else if (method === 'PATCH' && apiPatchReqs.has(pathname))
80
+ await apiPatchReqs.get(pathname)(req, response)
78
81
 
79
- else if (method === 'GET' && apiGetRequests.has(pathname))
80
- apiGetRequests.get(pathname)(req, response)
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
- sendUnprocessable(response, `${error.name}: ${error.message}`)
93
+ response.unprocessable(`${error.name}: ${error.message}`)
91
94
  else
92
- sendInternalServerError(response, error)
95
+ response.internalServerError(error)
93
96
  }
94
97
  }