mockaton 8.27.0 → 9.1.2

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/src/config.js CHANGED
@@ -18,29 +18,24 @@ import { validateCorsAllowedMethods, validateCorsAllowedOrigins } from './utils/
18
18
  const schema = {
19
19
  mocksDir: [join(process.cwd(), 'mockaton-mocks'), isDirectory],
20
20
  staticDir: [join(process.cwd(), 'mockaton-static-mocks'), optional(isDirectory)],
21
- ignore: [/(\.DS_Store|~)$/, is(RegExp)],
21
+ ignore: [/(\.DS_Store|~)$/, is(RegExp)], // TODO think about .well-known/appspecific/com.chrome.devtools
22
22
 
23
23
  host: ['127.0.0.1', is(String)],
24
24
  port: [0, port => Number.isInteger(port) && port >= 0 && port < 2 ** 16], // 0 means auto-assigned
25
25
 
26
- proxyFallback: ['', optional(URL.canParse)], // e.g. http://localhost:9999
27
- collectProxied: [false, is(Boolean)],
28
- formatCollectedJSON: [true, is(Boolean)],
26
+ logLevel: ['normal', val => ['normal', 'quiet'].includes(val)],
29
27
 
30
28
  delay: [1200, ms => Number.isInteger(ms) && ms >= 0],
31
29
  delayJitter: [0, percent => percent >= 0 && percent <= 3],
32
30
 
33
- cookies: [{}, is(Object)], // defaults to the first kv
31
+ proxyFallback: ['', optional(URL.canParse)], // e.g. http://localhost:9999
32
+ collectProxied: [false, is(Boolean)],
33
+ formatCollectedJSON: [true, is(Boolean)],
34
34
 
35
+ cookies: [{}, is(Object)], // defaults to the first kv
35
36
  extraHeaders: [[], val => Array.isArray(val) && val.length % 2 === 0],
36
-
37
37
  extraMimes: [{}, is(Object)],
38
38
 
39
- plugins: [
40
- [
41
- [/\.(js|ts)$/, jsToJsonPlugin]
42
- ], Array.isArray],
43
-
44
39
  corsAllowed: [true, is(Boolean)],
45
40
  corsOrigins: [['*'], validateCorsAllowedOrigins],
46
41
  corsMethods: [SUPPORTED_METHODS, validateCorsAllowedMethods],
@@ -49,9 +44,12 @@ const schema = {
49
44
  corsCredentials: [true, is(Boolean)],
50
45
  corsMaxAge: [0, is(Number)],
51
46
 
52
- onReady: [await openInBrowser, is(Function)],
47
+ plugins: [
48
+ [
49
+ [/\.(js|ts)$/, jsToJsonPlugin]
50
+ ], Array.isArray],
53
51
 
54
- logLevel: ['normal', val => ['normal', 'quiet'].includes(val)]
52
+ onReady: [await openInBrowser, is(Function)]
55
53
  }
56
54
 
57
55
 
@@ -80,14 +78,9 @@ export function setup(options) {
80
78
  if (!options.staticDir && !isDirectory(defaults.staticDir))
81
79
  options.staticDir = ''
82
80
 
83
- try {
84
- Object.assign(config, options)
85
- validate(config, ConfigValidator)
86
- log.setLevel(config.logLevel)
87
- }
88
- catch (err) {
89
- return err.message
90
- }
81
+ Object.assign(config, options)
82
+ validate(config, ConfigValidator)
83
+ log.setLevel(config.logLevel)
91
84
  }
92
85
 
93
86
 
@@ -35,7 +35,7 @@ export function sendUnprocessableContent(response, error) {
35
35
  }
36
36
 
37
37
  export function sendInternalServerError(response, error) {
38
- log.error(error)
38
+ log.error(error?.message || error, error?.stack || undefined)
39
39
  response.statusCode = 500
40
40
  response.end()
41
41
  }
package/src/utils/mime.js CHANGED
@@ -1,11 +1,14 @@
1
1
  import { config } from '../config.js'
2
2
  import { EXT_FOR_UNKNOWN_MIME } from '../ApiConstants.js'
3
3
 
4
+
5
+ // Generated with:
4
6
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
5
- // m = {};
7
+ // m = {}
6
8
  // for (const row of tbody.children)
7
- // m[row.children[0].querySelector('code').innerText] = row.children[2].querySelector('code').innerText
8
- const mimes = {
9
+ // m[row.children[0].querySelector('code').innerText] = row.children[2].querySelector('code').innerText
10
+
11
+ const extToMime = {
9
12
  '3g2': 'video/3gpp2',
10
13
  '3gp': 'video/3gpp',
11
14
  '7z': 'application/x-7z-compressed',
@@ -38,7 +41,7 @@ const mimes = {
38
41
  jar: 'application/java-archive',
39
42
  jpeg: 'image/jpeg',
40
43
  jpg: 'image/jpeg',
41
- js: 'text/javascript',
44
+ js: 'application/javascript',
42
45
  json: 'application/json',
43
46
  jsonld: 'application/ld+json',
44
47
  mid: 'audio/midi',
@@ -87,9 +90,18 @@ const mimes = {
87
90
  zip: 'application/zip'
88
91
  }
89
92
 
93
+ const mimeToExt = mapMimeToExt(extToMime)
94
+
95
+ function mapMimeToExt(e2m) {
96
+ const m = {}
97
+ for (const [ext, mime] of Object.entries(e2m))
98
+ m[mime] = ext
99
+ return m
100
+ }
101
+
90
102
  export function mimeFor(filename) {
91
103
  const ext = extname(filename).toLowerCase()
92
- return config.extraMimes[ext] || mimes[ext] || ''
104
+ return config.extraMimes[ext] || extToMime[ext] || ''
93
105
  }
94
106
  function extname(filename) {
95
107
  const i = filename.lastIndexOf('.')
@@ -104,12 +116,13 @@ export function extFor(mime) {
104
116
  ? findExt(mime)
105
117
  : 'empty'
106
118
  }
107
- function findExt(targetMime) {
108
- for (const [ext, mime] of Object.entries(config.extraMimes))
109
- if (targetMime === mime)
110
- return ext
111
- for (const [ext, mime] of Object.entries(mimes))
112
- if (targetMime === mime)
113
- return ext
114
- return EXT_FOR_UNKNOWN_MIME
119
+ function findExt(rawMime) {
120
+ const m = parseMime(rawMime)
121
+ const extraMimeToExt = mapMimeToExt(config.extraMimes)
122
+ return extraMimeToExt[m] || mimeToExt[m] || EXT_FOR_UNKNOWN_MIME
123
+ }
124
+
125
+ export function parseMime(mime) {
126
+ return mime.split(';')[0].toLowerCase()
127
+ // RFC 9110 §8.3.1
115
128
  }