forked from fastify/fast-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (157 loc) · 4.82 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
'use strict'
const pump = require('pump')
const lru = require('tiny-lru').lru
const Stream = require('stream')
const buildRequest = require('./lib/request')
const {
filterPseudoHeaders,
copyHeaders,
stripHttp1ConnectionHeaders,
filterHeaders,
buildURL
} = require('./lib/utils')
function populateHeaders (headers, body, contentType) {
headers['content-length'] = Buffer.byteLength(body)
// only populate content-type if not present
if (!headers['content-type']) {
headers['content-type'] = contentType
}
}
function fastProxy (opts = {}) {
const { request, close } = buildRequest({
...opts
})
const cache = getCacheStorage(opts.cacheURLs)
const base = opts.base
const qsModule = opts.queryString || require('fast-querystring')
return {
close,
proxy (req, res, source, opts) {
opts = opts || {}
const reqOpts = opts.request || {}
const onResponse = opts.onResponse
const rewriteHeaders = opts.rewriteHeaders || rewriteHeadersNoOp
const rewriteRequestHeaders = opts.rewriteRequestHeaders || rewriteRequestHeadersNoOp
const url = getReqUrl(source || req.url, cache, base, opts)
const sourceHttp2 = req.httpVersionMajor === 2
let headers = { ...sourceHttp2 ? filterPseudoHeaders(req.headers) : req.headers }
headers['x-forwarded-host'] = headers.host
headers.host = url.hostname
if (url.port) {
headers.host += `:${url.port}`
}
const qs = getQueryString(qsModule, url.search, req.url, opts)
let body = null
// according to https://tools.ietf.org/html/rfc2616#section-4.3
// proxy should ignore message body when it's a GET or HEAD request
// when proxy this request, we should reset the content-length to make it a valid http request
if (req.method === 'GET' || req.method === 'HEAD') {
if (headers['content-length']) {
headers = filterHeaders(headers, 'content-length')
}
} else {
if (req.body) {
if (req.body instanceof Stream) {
body = req.body
} else if (typeof req.body === 'string') {
body = req.body
populateHeaders(headers, body, 'text/plain')
} else if (headers['content-type'] === 'application/x-www-form-urlencoded') {
body = qsModule.stringify(req.body)
populateHeaders(headers, body, 'application/x-www-form-urlencoded')
} else {
body = JSON.stringify(req.body)
populateHeaders(headers, body, 'application/json')
}
} else {
body = req
}
}
const reqParams = {
method: req.method,
url,
qs,
headers: rewriteRequestHeaders(req, headers),
body,
request: reqOpts
}
request(reqParams, (err, response) => {
if (err) {
if (!res.sent) {
if (err.code === 'ECONNREFUSED' || err.code === 'ERR_HTTP2_STREAM_CANCEL') {
res.statusCode = 503
res.end('Service Unavailable')
} else if (err.code === 'ECONNRESET' || err.code === 'UND_ERR_HEADERS_TIMEOUT' || err.code === 'UND_ERR_BODY_TIMEOUT') {
res.statusCode = 504
res.end(err.message)
} else {
res.statusCode = 500
res.end(err.message)
}
}
return
}
// destructing response from remote
const { headers, statusCode, stream } = response
if (sourceHttp2) {
copyHeaders(
rewriteHeaders(stripHttp1ConnectionHeaders(headers)),
res
)
} else {
copyHeaders(rewriteHeaders(headers), res)
}
// set origin response code
res.statusCode = statusCode
if (onResponse) {
onResponse(req, res, stream)
} else {
pump(stream, res)
}
})
}
}
}
function getQueryString (qs, search, reqUrl, opts) {
if (opts.queryString) {
return '?' + qs.stringify(opts.queryString)
}
if (search.length > 0) {
return search
}
const queryIndex = reqUrl.indexOf('?')
if (queryIndex > 0) {
return reqUrl.slice(queryIndex)
}
return ''
}
function rewriteHeadersNoOp (headers) {
return headers
}
function rewriteRequestHeadersNoOp (req, headers) {
return headers
}
function getCacheStorage (size) {
if (size === 0) {
return null
}
return lru(size || 100)
}
function getReqUrl (source, cache, base, opts) {
const reqBase = opts.base || base
let url
if (cache) {
const cacheKey = reqBase + source
url = cache.get(cacheKey)
if (!url) {
url = buildURL(source, reqBase)
cache.set(cacheKey, url)
}
} else {
url = buildURL(source, reqBase)
}
return url
}
module.exports = fastProxy
module.exports.default = fastProxy
module.exports.fastProxy = fastProxy