-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (30 loc) · 803 Bytes
/
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
const Layer = require('express/lib/router/layer')
const Router = require('express/lib/router')
Object.defineProperty(Layer.prototype, 'handle', {
enumerable: true,
get() {
return this.__handle
},
set(fn) {
this.__handle = wrapHandle(fn)
}
})
function isAsyncFunction(fn) {
return Object.prototype.toString.call(fn) === '[object AsyncFunction]'
}
function wrapHandle(fn) {
if (isAsyncFunction(fn)) {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next)
}
}
return fn
}
function patchRouterParam() {
const originalParam = Router.prototype.constructor.param;
Router.prototype.constructor.param = function param(name, fn) {
fn = wrapHandle(fn);
return originalParam.call(this, name, fn);
};
}
patchRouterParam();