mockaton 10.5.3 → 10.5.4

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": "10.5.3",
5
+ "version": "10.5.4",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
@@ -8,7 +8,7 @@ import { cookie } from './cookie.js'
8
8
  import { mimeFor } from './utils/mime.js'
9
9
  import { config, calcDelay } from './config.js'
10
10
  import * as mockBrokerCollection from './mockBrokersCollection.js'
11
- import { sendInternalServerError, sendNotFound } from './utils/http-response.js'
11
+ import { sendInternalServerError, sendMockNotFound } from './utils/http-response.js'
12
12
 
13
13
 
14
14
  export async function dispatchMock(req, response) {
@@ -21,23 +21,22 @@ export async function dispatchMock(req, response) {
21
21
  if (config.proxyFallback)
22
22
  await proxy(req, response, broker?.delayed ? calcDelay() : 0)
23
23
  else
24
- sendNotFound(response)
24
+ sendMockNotFound(response)
25
25
  return
26
26
  }
27
27
 
28
- logger.accessMock(req.url, broker.file)
29
- response.statusCode = broker.status
30
-
31
28
  if (cookie.getCurrent())
32
29
  response.setHeader('Set-Cookie', cookie.getCurrent())
33
30
 
34
31
  for (let i = 0; i < config.extraHeaders.length; i += 2)
35
32
  response.setHeader(config.extraHeaders[i], config.extraHeaders[i + 1])
36
33
 
34
+ response.statusCode = broker.status // TESTME plugins can change it
37
35
  const { mime, body } = broker.temp500IsSelected
38
36
  ? { mime: '', body: '' }
39
37
  : await applyPlugins(join(config.mocksDir, broker.file), req, response)
40
38
 
39
+ logger.accessMock(req.url, broker.file)
41
40
  response.setHeader('Content-Type', mime)
42
41
  response.setHeader('Content-Length', length(body))
43
42
  setTimeout(() => response.end(isHead ? null : body),
@@ -45,7 +44,7 @@ export async function dispatchMock(req, response) {
45
44
  }
46
45
  catch (error) {
47
46
  if (error?.code === 'ENOENT') // mock-file has been deleted
48
- sendNotFound(response)
47
+ sendMockNotFound(response)
49
48
  else
50
49
  sendInternalServerError(response, error)
51
50
  }
@@ -5,7 +5,9 @@ import { logger } from './utils/logger.js'
5
5
  import { mimeFor } from './utils/mime.js'
6
6
  import { brokerByRoute } from './staticCollection.js'
7
7
  import { config, calcDelay } from './config.js'
8
- import { sendNotFound, sendPartialContent } from './utils/http-response.js'
8
+ import { sendMockNotFound, sendPartialContent } from './utils/http-response.js'
9
+ import { execFileSync } from 'node:child_process'
10
+ import { isFile } from './utils/fs.js'
9
11
 
10
12
 
11
13
  export async function dispatchStatic(req, response) {
@@ -13,14 +15,17 @@ export async function dispatchStatic(req, response) {
13
15
 
14
16
  setTimeout(async () => {
15
17
  if (!broker || broker.status === 404) { // TESTME
16
- logger.accessMock(req.url, 'static404')
17
- sendNotFound(response)
18
+ sendMockNotFound(response)
18
19
  return
19
20
  }
20
21
 
21
- logger.accessMock(req.url, 'static200')
22
-
23
22
  const file = join(config.staticDir, broker.route)
23
+ if (!isFile(file)) {
24
+ sendMockNotFound(response) // TESTME
25
+ return
26
+ }
27
+
28
+ logger.accessMock(req.url, 'static200')
24
29
  if (req.headers.range)
25
30
  await sendPartialContent(response, req.headers.range, file)
26
31
  else {
package/src/Watcher.js CHANGED
@@ -3,24 +3,24 @@ import { watch } from 'node:fs'
3
3
  import { EventEmitter } from 'node:events'
4
4
 
5
5
  import { config } from './config.js'
6
- import { isFile } from './utils/fs.js'
6
+ import { isFile, isDirectory } from './utils/fs.js'
7
+ import * as staticCollection from './staticCollection.js'
7
8
  import * as mockBrokerCollection from './mockBrokersCollection.js'
8
- import { registerStaticMock, unregisterStaticMock } from './staticCollection.js'
9
9
 
10
10
 
11
- /** # AR = Add or Remove Mock Event */
11
+ /** # ARR = Add, Remove, or Rename Mock Event */
12
12
  export const uiSyncVersion = new class extends EventEmitter {
13
13
  version = 0
14
14
 
15
15
  increment() {
16
16
  this.version++
17
- super.emit('AR')
17
+ super.emit('ARR')
18
18
  }
19
19
  subscribe(listener) {
20
- this.once('AR', listener)
20
+ this.once('ARR', listener)
21
21
  }
22
22
  unsubscribe(listener) {
23
- this.removeListener('AR', listener)
23
+ this.removeListener('ARR', listener)
24
24
  }
25
25
  }
26
26
 
@@ -29,7 +29,16 @@ export function watchMocksDir() {
29
29
  watch(dir, { recursive: true, persistent: false }, (_, file) => {
30
30
  if (!file)
31
31
  return
32
- if (isFile(join(dir, file))) {
32
+
33
+ const path = join(dir, file)
34
+
35
+ if (isDirectory(path)) {
36
+ mockBrokerCollection.init()
37
+ uiSyncVersion.increment()
38
+ return
39
+ }
40
+
41
+ if (isFile(path)) {
33
42
  if (mockBrokerCollection.registerMock(file, Boolean('isFromWatcher')))
34
43
  uiSyncVersion.increment()
35
44
  }
@@ -47,12 +56,21 @@ export function watchStaticDir() {
47
56
  watch(dir, { recursive: true, persistent: false }, (_, file) => {
48
57
  if (!file)
49
58
  return
50
- if (isFile(join(dir, file))) {
51
- if (registerStaticMock(file))
59
+
60
+ const path = join(dir, file)
61
+
62
+ if (isDirectory(path)) {
63
+ staticCollection.init()
64
+ uiSyncVersion.increment()
65
+ return
66
+ }
67
+
68
+ if (isFile(path)) {
69
+ if (staticCollection.registerMock(file))
52
70
  uiSyncVersion.increment()
53
71
  }
54
72
  else {
55
- unregisterStaticMock(file)
73
+ staticCollection.unregisterMock(file)
56
74
  uiSyncVersion.increment()
57
75
  }
58
76
  })
@@ -23,12 +23,12 @@ export function init() {
23
23
  collection = {}
24
24
  listFilesRecursively(config.staticDir)
25
25
  .sort()
26
- .forEach(registerStaticMock)
26
+ .forEach(registerMock)
27
27
  }
28
28
 
29
29
 
30
30
  /** @returns {boolean} registered */
31
- export function registerStaticMock(relativeFile) {
31
+ export function registerMock(relativeFile) {
32
32
  if (!isFileAllowed(basename(relativeFile)))
33
33
  return false
34
34
 
@@ -41,7 +41,7 @@ export function registerStaticMock(relativeFile) {
41
41
  }
42
42
 
43
43
 
44
- export function unregisterStaticMock(relativeFile) {
44
+ export function unregisterMock(relativeFile) {
45
45
  delete collection['/' + relativeFile]
46
46
  }
47
47
 
@@ -41,9 +41,9 @@ export function sendBadRequest(response) {
41
41
  response.end()
42
42
  }
43
43
 
44
- export function sendNotFound(response) {
44
+ export function sendMockNotFound(response) {
45
45
  response.statusCode = 404
46
- logger.access(response)
46
+ logger.accessMock(response.req.url, '404')
47
47
  response.end()
48
48
  }
49
49