-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
339 lines (324 loc) · 9.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const vm = require('vm')
const { mock } = require('mockjs')
const { watch } = require('chokidar')
/**
* @typedef {import('express').Request} Req
*
* @typedef {import('express').Response} Res
*
* @typedef {<T>(req: Req, res: Res, args: { repeatTimes: number, render: render<T> }) => *} ResponseFunction
*
* @typedef {string|number|boolean|null|undefined} Primitive
*
* @typedef {(string|number|boolean|null|undefined|PlainObject)[]} PlainArray
*
* @typedef {{[k: string]: Primitive|PlainArray|PlainObject}} PlainObject
*/
/**
* @typedef ObjectRouteItem
*
* @property {string} [method=get]
*
* @property {string} url
*
* @property {Exclude<*, ResponseFunction>|ResponseFunction} [response]
* If response is `any type` except `function`, will be treated as a mockable response data,
* and emitted with `res.send`.
*
* @property {string} [range]
* Set the repeat times of a response mock,
* if set this argument, returned data will be an array of the response mock.
*
* Example:
*
* - `1..`, length is not less than 1.
* - `1..10`, length is between 1 and 10, including 1 and 10.
* - `..10`, the same as `0..10`
*
* @property {number|[number, number]} delay
* delay seconds(or array providing min and max delay seconds) to send response.
*
* @property {object} args
* Additional arguments `object` for parameters in interpolation.
*
* @property {(responseDataOrResponseFunctionReturn: *, req: Req, res: Res) => void} [processResponse]
* Post processing response.
*
* @typedef {Array<ObjectRouteItem[keyof ObjectRouteItem]>} ArrayRouteItem
*
* @typedef {ObjectRouteItem|ArrayRouteItem} RouteItem
*/
/**
* @param {object} options
*
* @param {string|string[]} options.routeFiles
* Specify mock routes definition file/files, regarded as absolute path/paths where mock routes definition files are;
*
* @param {RegExp} [options.ignore]
* ignore pattern.
*
* @param {boolean} [options.silent]
* ignore pattern.
*
* @return {function (import('express').Application): void}
*/
module.exports = options => {
const routeFiles = Array.isArray(options.routeFiles) ? options.routeFiles : [options.routeFiles]
const ignoreRegExp = options.ignore
return app => {
app.lazyrouter()
const watcher = watch([], { ignoreInitial: true })
const stack = app._router.stack
const addedLayers = []
let anchor = -1
loadRoutes()
watcher.on('all', (event, filepath) => {
unwatchRequireChain(filepath)
clearRequireCache(filepath)
loadRoutes()
})
function loadRoutes() {
try {
mountRoutes()
} catch (e) {
if (options.silent) {
app.emit('reloadRouteFailed', e)
} else {
throw e
}
return
}
app.emit('reloadRouteSuccess')
}
/**
* 1. remove previous added routes.
* 2. reset `anchor` and `routeCount`.
* 3. add new routes one by one with increasing `routeCount`.
* 4. rebase added routes to original position.
*/
function mountRoutes() {
// remove previous added routes.
for (let i = addedLayers.length; i--;) {
const layer = addedLayers[i]
const index = stack.indexOf(layer)
if (~index) {
stack.splice(index, 1)
anchor = index
}
}
// empty saved layers.
addedLayers.length = 0
// add new routes one by one with updating 'routeCount' in the meantime.
let err
try {
for (const file of routeFiles) {
const definitions = loadRouteModule(file)
for (const def of definitions) {
let method, url, response, range, delay, args, processResponse
if (Array.isArray(def)) {
let j
switch (def.length) {
case 0:
case 1:
continue
case 2:
[url, response] = def
break
case 3:
[method, url, response] = def
break
default:
[method, url, response] = def
processResponse = []
j = 3
while (j < def.length) {
switch (typeof def[j]) {
case 'function':
processResponse.push(def[j])
break
case 'object':
args = def[j]
break
case 'string':
range = def[j]
break
case 'number':
delay = def[j]
break
default:
}
j++
}
}
} else if (def) {
method = def.method
url = def.url
response = def.response
range = def.range
delay = def.delay
args = def.args
processResponse = Array.isArray(def.processResponse) ? def.processResponse : [def.processResponse]
} else {
continue
}
processResponse = (processResponse || []).filter(e => typeof e === 'function')
app[method || 'get'](url, function(req, res) {
const count = range ? random(range) : 1
let r
if (typeof response === 'function') {
r = response(req, res, { count, render, mock, delayCall })
} else if (range) {
r = new Array(count).fill(null).map((e, i) => mock(render(response, Object.assign({ req, i }, args))))
} else {
r = mock(render(response, Object.assign({ req }, args)))
}
processResponse.forEach(e => {
r = e(r, req, res, { count, render, mock, delayCall })
})
if (r === res) {
delayCall(delay, () => res.end())
} else if (r !== void 0) {
delayCall(delay, () => res.send(r))
}
})
// save new layers
addedLayers.push(stack.pop())
}
}
} catch (e) {
err = e
}
// add new layers
if (~anchor) {
stack.splice(anchor, 0, ...addedLayers)
} else {
stack.push(...addedLayers)
}
if (err) {
throw err
}
}
function loadRouteModule(file) {
try {
const definitions = require(file)
watchRequireChain(file)
return definitions
} catch (e) {
watchRequireChain(file)
throw e
}
}
function watchRequireChain(id) {
eachRequire(id, function(mod) {
if (ignoreRegExp && ignoreRegExp.test(mod.id)) {
return
}
watcher.add(mod.id)
})
}
function unwatchRequireChain(id) {
eachRequire(id, function(mod) {
if (mod.id !== id) {
watcher.unwatch(mod.id)
}
})
}
// delete cache from entry(route file) to changed file
function clearRequireCache(id) {
if (require.cache[id]) {
let parentId
if (require.cache[id].parent) {
parentId = require.cache[id].parent.id
removeEle(require.cache[id].parent.children, require.cache[id])
}
delete require.cache[id]
clearRequireCache(parentId)
}
}
}
}
/**
* Generate random integer.
*
* Examples:
*
* // integer not less than 1
* random('1..')
* // integer not more than 10 and not less than 1
* random('1..10')
* // same as `random('0..10')`
* random('..10')
*
* @param {string} str integer joined with `..`,
* the left one is minimum and the other is maximum.
* @return {number}
*/
function random(str) {
let [min, max] = str.split('..')
min |= 0
max |= 0
if (min >= max) return min
return Math.round(Math.random() * (max - min)) + min
}
function interpolate(str, ctx) {
let r = str.match(/^\$\{([^\}]+)\}$/)
if (r) {
return vm.runInNewContext(r[1], ctx)
} else {
return vm.runInNewContext('`' + str + '`', ctx)
}
}
/**
* Render data with arguments from template.
*
* @author lakca<912910011@qq.com>
* @template {string|object|array} T
* @param {T} tpl data template with using parameters.
* @param {object} ctx interpolation arguments.
* @returns {T}
*/
function render(tpl, ctx) {
let r, type = typeof tpl
if (tpl && type === 'object') {
type = toString.call(tpl).match(/\S+(?=])/)[0]
if ((type === 'Array' && (r = []))
|| (type === 'Object') && (r = {})) {
for (const k in tpl) {
r[k] = render(tpl[k], ctx)
}
} else {
r = tpl
}
} else if (type === 'string') {
r = interpolate(tpl, ctx)
} else {
r = tpl
}
return r
}
function delayCall(seconds, cb) {
if (Array.isArray(seconds)) {
seconds = Math.random() * (seconds[1] - seconds[0]) + seconds[0]
}
setTimeout(cb, seconds * 1000)
}
function eachRequire(id, handler, occurrence = []) {
const mod = require.cache[id]
if (mod && !occurrence.includes(id)) {
occurrence.push(mod.id)
const terminated = handler(mod)
if (!terminated) {
if (mod.children) {
mod.children.forEach(childMod => {
eachRequire(childMod.id, handler, occurrence)
})
}
}
}
}
function removeEle(arr, el) {
const index = arr.indexOf(el)
if (~index) {
arr.splice(index, 1)
}
}