forked from popomore/koa-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
189 lines (167 loc) · 5.03 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
const join = require('url').resolve
const iconv = require('iconv-lite')
const LRU = require('lru-cache')
const requestPromise = require('request-promise-native')
module.exports = function(options = {}) {
if (!(options.host || options.map || options.url)) {
throw new Error('miss options')
}
const lruCache = LRU(options.lruOptions)
return async function proxy(ctx, next) {
const url = resolve(ctx.path, options)
if (typeof options.suppressRequestHeaders === 'object') {
options.suppressRequestHeaders.forEach(function(h, i) {
options.suppressRequestHeaders[i] = h.toLowerCase()
})
}
const suppressResponseHeaders = [] // We should not be overwriting the options object!
if (typeof options.suppressResponseHeaders === 'object') {
options.suppressResponseHeaders.forEach(function(h, i) {
suppressResponseHeaders.push(h.toLowerCase())
})
}
// don't match
if (!url) {
return next()
}
// if match option supplied, restrict proxy to that match
if (options.match) {
if (!ctx.path.match(options.match)) {
return next()
}
}
ctx.proxyLruCache = lruCache
// if match hook path
if (options.hooks) {
for (const { path, handle } of options.hooks) {
if (ctx.path === path) {
if (typeof handle === 'function') {
return handle(ctx)
}
}
}
}
if (options.cache) {
let isCacheable = options.cache
if (isCacheable instanceof RegExp) {
isCacheable = isCacheable.test(ctx.path)
} else if (isCacheable instanceof Function) {
isCacheable = !!isCacheable(ctx.path)
}
if (isCacheable) {
ctx.proxyIsCacheable = true
const cacheResponse = lruCache.get(ctx.path)
if (cacheResponse) {
ctx.proxyCacheResponse = cacheResponse
}
}
}
const parsedBody = getParsedBody(ctx)
let opt = {
uri: url + (ctx.querystring ? '?' + ctx.querystring : ''),
headers: ctx.header,
method: ctx.method,
encoding: null,
followRedirect: options.followRedirect !== false,
body: parsedBody,
resolveWithFullResponse: true
}
// set 'Host' header to options.host (without protocol prefix), strip trailing slash
if (options.host) {
opt.headers.host = options.host
.slice(options.host.indexOf('://') + 3)
.replace(/\/$/, '')
}
if (options.requestOptions) {
if (typeof options.requestOptions === 'function') {
opt = options.requestOptions(ctx.request, opt)
} else {
Object.keys(options.requestOptions).forEach(function(option) {
opt[option] = options.requestOptions[option]
})
}
}
for (const name in opt.headers) {
if (
options.suppressRequestHeaders &&
options.suppressRequestHeaders.indexOf(name.toLowerCase()) >= 0
) {
delete opt.headers[name]
}
}
const res = ctx.proxyCacheResponse
? ctx.proxyCacheResponse
: await requestPromise(opt).catch(error => {
const { statusCode } = error
if (statusCode === 304 || statusCode === 404) {
return Promise.resolve({ statusCode, headers: {} })
}
return Promise.reject(error)
})
ctx.status = res.statusCode
if (ctx.proxyIsCacheable && !ctx.proxyCacheResponse) {
if (res.body && res.statusCode === 200) {
lruCache.set(ctx.path, {
body: res.body,
statusCode: res.statusCode,
headers: res.headers
})
}
}
for (const name in res.headers) {
// http://stackoverflow.com/questions/35525715/http-get-parse-error-code-hpe-unexpected-content-length
if (suppressResponseHeaders.indexOf(name.toLowerCase()) >= 0) {
continue
}
if (name === 'transfer-encoding') {
continue
}
ctx.set(name, res.headers[name])
}
if (options.encoding === 'gbk') {
ctx.body = iconv.decode(res.body, 'gbk')
return
}
if (res.body) {
ctx.body = res.body
}
if (options.yieldNext) {
return next()
}
}
}
function resolve(path, options) {
let url = options.url
if (url) {
if (!/^http/.test(url)) {
url = options.host ? join(options.host, url) : null
}
return ignoreQuery(url)
}
if (typeof options.map === 'object') {
if (options.map && options.map[path]) {
path = ignoreQuery(options.map[path])
}
} else if (typeof options.map === 'function') {
path = options.map(path)
}
return options.host ? join(options.host, path) : null
}
function ignoreQuery(url) {
return url ? url.split('?')[0] : null
}
function getParsedBody(ctx) {
let body = ctx.request.body
if (body === undefined || body === null) {
return undefined
}
const contentType = ctx.request.header['content-type']
if (!Buffer.isBuffer(body) && typeof body !== 'string') {
if (contentType && contentType.indexOf('json') !== -1) {
body = JSON.stringify(body)
} else {
body = body + ''
}
}
return body
}