mockaton 9.2.0 → 9.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "HTTP Mock Server",
4
4
  "type": "module",
5
- "version": "9.2.0",
5
+ "version": "9.3.0",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
@@ -13,7 +13,10 @@ import { sendInternalServerError, sendNotFound } from './utils/http-response.js'
13
13
 
14
14
  export async function dispatchMock(req, response) {
15
15
  try {
16
- const broker = mockBrokerCollection.brokerByRoute(req.method, req.url)
16
+ let broker = mockBrokerCollection.brokerByRoute(req.method, req.url)
17
+ const isHead = req.method === 'HEAD'
18
+ if (!broker && isHead)
19
+ broker = mockBrokerCollection.brokerByRoute('GET', req.url)
17
20
  if (!broker || broker.proxied) {
18
21
  if (config.proxyFallback)
19
22
  await proxy(req, response, Number(broker?.delayed && calcDelay()))
@@ -36,7 +39,9 @@ export async function dispatchMock(req, response) {
36
39
  : await applyPlugins(join(config.mocksDir, broker.file), req, response)
37
40
 
38
41
  response.setHeader('Content-Type', mime)
39
- setTimeout(() => response.end(body), Number(broker.delayed && calcDelay()))
42
+ response.setHeader('Content-Length', length(body))
43
+ setTimeout(() => response.end(isHead ? null : body),
44
+ Number(broker.delayed && calcDelay()))
40
45
  }
41
46
  catch (error) {
42
47
  if (error?.code === 'ENOENT') // mock-file has been deleted
@@ -51,7 +56,6 @@ export async function dispatchMock(req, response) {
51
56
  }
52
57
  }
53
58
 
54
-
55
59
  async function applyPlugins(filePath, req, response) {
56
60
  for (const [regex, plugin] of config.plugins)
57
61
  if (regex.test(filePath))
@@ -62,7 +66,6 @@ async function applyPlugins(filePath, req, response) {
62
66
  }
63
67
  }
64
68
 
65
-
66
69
  export async function jsToJsonPlugin(filePath, req, response) {
67
70
  const jsExport = (await import(pathToFileURL(filePath) + '?' + Date.now())).default // date for cache busting
68
71
  const body = typeof jsExport === 'function'
@@ -73,3 +76,10 @@ export async function jsToJsonPlugin(filePath, req, response) {
73
76
  body
74
77
  }
75
78
  }
79
+
80
+ function length(body) {
81
+ if (typeof body === 'string') return Buffer.byteLength(body)
82
+ if (Buffer.isBuffer(body)) return body.length
83
+ if (body instanceof Uint8Array) return body.byteLength
84
+ return 0
85
+ }