-
Notifications
You must be signed in to change notification settings - Fork 2
/
hiddie.js
169 lines (145 loc) · 3.39 KB
/
hiddie.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
'use strict'
const reusify = require('reusify')
const { pathToRegexp } = require('path-to-regexp')
function hiddie (complete) {
var middlewares = []
var pool = reusify(Holder)
return {
use,
run
}
function use (url, f) {
if (f === undefined) {
f = url
url = null
}
var regexp
var keys = []
if (url) {
regexp = pathToRegexp(sanitizePrefixUrl(url), keys, {
end: false,
strict: false
})
}
if (Array.isArray(f)) {
for (var val of f) {
middlewares.push({
regexp,
keys,
fn: val
})
}
} else {
middlewares.push({
regexp,
keys,
fn: f
})
}
return this
}
function run (req, res, ctx) {
if (!middlewares.length) {
complete(null, req, res, ctx)
return
}
req.originalUrl = req.url
var holder = pool.get()
holder.req = req
holder.res = res
holder.url = sanitizeUrl(req.url)
holder.context = ctx
holder.done()
}
function Holder () {
this.next = null
this.req = null
this.res = null
this.url = null
this.context = null
this.i = 0
var that = this
this.done = function (err) {
var req = that.req
var res = that.res
var url = that.url
var context = that.context
var i = that.i++
req.url = req.originalUrl
if (res.finished === true) {
that.req = null
that.res = null
that.context = null
that.i = 0
pool.release(that)
return
}
if (err || middlewares.length === i) {
complete(err, req, res, context)
that.req = null
that.res = null
that.context = null
that.i = 0
pool.release(that)
} else {
var middleware = middlewares[i]
var fn = middleware.fn
var regexp = middleware.regexp
var keys = middleware.keys
if (regexp) {
var result = regexp.exec(url)
if (result) {
var params = {}
for (var j = 1; j < result.length; j++) {
var prop = keys[j - 1].name
var val = decodeParam(result[j])
if (!!val || !Object.prototype.hasOwnProperty.call(params, prop)) {
params[prop] = val
}
}
req.url = req.url.replace(result[0], '')
req.params = params
if (req.url.startsWith('/') === false) {
req.url = '/' + req.url
}
fn(req, res, that.done)
} else {
that.done()
}
} else {
fn(req, res, that.done)
}
}
}
}
}
function decodeParam (val) {
if (typeof val !== 'string' || val.length === 0) {
return val
}
try {
return decodeURIComponent(val)
} catch (err) {
if (err instanceof URIError) {
err.message = 'Failed to decode param \'' + val + '\''
err.status = err.statusCode = 400
}
throw err
}
}
function sanitizeUrl (url) {
for (var i = 0, len = url.length; i < len; i++) {
var charCode = url.charCodeAt(i)
if (charCode === 63 || charCode === 35) {
return url.slice(0, i)
}
}
return url
}
function sanitizePrefixUrl (url) {
if (url === '') return url
if (url === '/') return ''
if (url[url.length - 1] === '/') return url.slice(0, -1)
return url
}
module.exports = hiddie