-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.js
309 lines (270 loc) · 9.75 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
'use strict'
const fp = require('fastify-plugin')
const { lru } = require('tiny-lru')
const querystring = require('fast-querystring')
const Stream = require('node:stream')
const buildRequest = require('./lib/request')
const {
filterPseudoHeaders,
copyHeaders,
stripHttp1ConnectionHeaders,
buildURL
} = require('./lib/utils')
const {
TimeoutError,
ServiceUnavailableError,
GatewayTimeoutError,
ConnectionResetError,
ConnectTimeoutError,
UndiciSocketError,
InternalServerError
} = require('./lib/errors')
const fastifyReplyFrom = fp(function from (fastify, opts, next) {
const contentTypesToEncode = new Set([
'application/json',
...(opts.contentTypesToEncode || [])
])
const retryMethods = new Set(opts.retryMethods || [
'GET', 'HEAD', 'OPTIONS', 'TRACE'])
const cache = opts.disableCache ? undefined : lru(opts.cacheURLs || 100)
const base = opts.base
const requestBuilt = buildRequest({
http: opts.http,
http2: opts.http2,
base,
undici: opts.undici,
globalAgent: opts.globalAgent,
destroyAgent: opts.destroyAgent
})
if (requestBuilt instanceof Error) {
next(requestBuilt)
return
}
const { request, close, retryOnError } = requestBuilt
const disableRequestLogging = opts.disableRequestLogging || false
fastify.decorateReply('from', function (source, opts) {
opts = opts || {}
const req = this.request.raw
const method = opts.method || req.method
const onResponse = opts.onResponse
const rewriteHeaders = opts.rewriteHeaders || headersNoOp
const rewriteRequestHeaders = opts.rewriteRequestHeaders || requestHeadersNoOp
const getUpstream = opts.getUpstream || upstreamNoOp
const onError = opts.onError || onErrorDefault
const retriesCount = opts.retriesCount || 0
const maxRetriesOn503 = opts.maxRetriesOn503 || 10
const customRetry = opts.customRetry || undefined
if (!source) {
source = req.url
}
// we leverage caching to avoid parsing the destination URL
const dest = getUpstream(this.request, base)
let url
if (cache) {
const cacheKey = dest + source
url = cache.get(cacheKey) || buildURL(source, dest)
cache.set(cacheKey, url)
} else {
url = buildURL(source, dest)
}
const sourceHttp2 = req.httpVersionMajor === 2
const headers = sourceHttp2 ? filterPseudoHeaders(req.headers) : { ...req.headers }
headers.host = url.host
const qs = getQueryString(url.search, req.url, opts)
let body = ''
if (opts.body !== undefined) {
if (opts.body !== null) {
if (typeof opts.body.pipe === 'function') {
throw new Error('sending a new body as a stream is not supported yet')
}
if (opts.contentType) {
body = opts.body
} else {
body = JSON.stringify(opts.body)
opts.contentType = 'application/json'
}
headers['content-length'] = Buffer.byteLength(body)
headers['content-type'] = opts.contentType
} else {
body = undefined
headers['content-length'] = 0
delete headers['content-type']
}
} else if (this.request.body) {
if (this.request.body instanceof Stream) {
body = this.request.body
} else {
// Per RFC 7231 §3.1.1.5 if this header is not present we MAY assume application/octet-stream
const contentType = req.headers['content-type'] || 'application/octet-stream'
// detect if body should be encoded as JSON
// supporting extended content-type header formats:
// - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
const lowerCaseContentType = contentType.toLowerCase()
const plainContentType = lowerCaseContentType.indexOf(';') > -1
? lowerCaseContentType.slice(0, lowerCaseContentType.indexOf(';'))
: lowerCaseContentType
const shouldEncodeJSON = contentTypesToEncode.has(plainContentType)
// transparently support JSON encoding
body = shouldEncodeJSON ? JSON.stringify(this.request.body) : this.request.body
// update origin request headers after encoding
headers['content-length'] = Buffer.byteLength(body)
headers['content-type'] = contentType
}
}
// according to https://tools.ietf.org/html/rfc2616#section-4.3
// fastify 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
// discussion: https://github.com/fastify/fastify/issues/953
if (method === 'GET' || method === 'HEAD') {
// body will be populated here only if opts.body is passed.
// if we are doing that with a GET or HEAD request is a programmer error
// and as such we can throw immediately.
if (body) {
throw new Error(`Rewriting the body when doing a ${method} is not allowed`)
}
}
!disableRequestLogging && this.request.log.info({ source }, 'fetching from remote server')
const requestHeaders = rewriteRequestHeaders(this.request, headers)
const contentLength = requestHeaders['content-length']
let requestImpl
if (retryMethods.has(method) && !contentLength) {
const retryHandler = (req, res, err, retries) => {
const defaultDelay = () => {
// Magic number, so why not 42? We might want to make this configurable.
let retryAfter = 42 * Math.random() * (retries + 1)
if (res && res.headers['retry-after']) {
retryAfter = res.headers['retry-after']
}
if (res && res.statusCode === 503 && req.method === 'GET') {
if (retriesCount === 0 && retries < maxRetriesOn503) {
// we should stop at some point
return retryAfter
}
} else if (retriesCount > retries && err && err.code === retryOnError) {
return retryAfter
}
return null
}
if (customRetry && customRetry.handler) {
const customRetries = customRetry.retries || 1
if (++retries < customRetries) {
return customRetry.handler(req, res, defaultDelay)
}
}
return defaultDelay()
}
requestImpl = createRequestRetry(request, this, retryHandler)
} else {
requestImpl = request
}
requestImpl({ method, url, qs, headers: requestHeaders, body }, (err, res) => {
if (err) {
this.request.log.warn(err, 'response errored')
if (!this.sent) {
if (err.code === 'ERR_HTTP2_STREAM_CANCEL' || err.code === 'ENOTFOUND') {
onError(this, { error: ServiceUnavailableError() })
} else if (err instanceof TimeoutError || err.code === 'UND_ERR_HEADERS_TIMEOUT') {
onError(this, { error: new GatewayTimeoutError() })
} else if (err.code === 'ECONNRESET') {
onError(this, { error: new ConnectionResetError() })
} else if (err.code === 'UND_ERR_SOCKET') {
onError(this, { error: new UndiciSocketError() })
} else if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
onError(this, { error: new ConnectTimeoutError() })
} else {
onError(this, { error: new InternalServerError(err.message) })
}
}
return
}
!disableRequestLogging && this.request.log.info('response received')
if (sourceHttp2) {
copyHeaders(
rewriteHeaders(stripHttp1ConnectionHeaders(res.headers), this.request),
this
)
} else {
copyHeaders(rewriteHeaders(res.headers, this.request), this)
}
this.code(res.statusCode)
if (onResponse) {
onResponse(this.request, this, res.stream)
} else {
this.send(res.stream)
}
})
return this
})
fastify.addHook('onReady', (done) => {
if (isFastifyMultipartRegistered(fastify)) {
fastify.log.warn('@fastify/reply-from might not behave as expected when used with @fastify/multipart')
}
done()
})
fastify.onClose((fastify, next) => {
close()
// let the event loop do a full run so that it can
// actually destroy those sockets
setImmediate(next)
})
next()
}, {
fastify: '4.x',
name: '@fastify/reply-from'
})
function getQueryString (search, reqUrl, opts) {
if (typeof opts.queryString === 'function') {
return '?' + opts.queryString(search, reqUrl)
}
if (opts.queryString) {
return '?' + querystring.stringify(opts.queryString)
}
if (search.length > 0) {
return search
}
const queryIndex = reqUrl.indexOf('?')
if (queryIndex > 0) {
return reqUrl.slice(queryIndex)
}
return ''
}
function headersNoOp (headers, originalReq) {
return headers
}
function requestHeadersNoOp (originalReq, headers) {
return headers
}
function upstreamNoOp (req, base) {
return base
}
function onErrorDefault (reply, { error }) {
reply.send(error)
}
function isFastifyMultipartRegistered (fastify) {
// TODO: remove fastify.hasContentTypeParser('multipart') in next major
// It is used to be compatible with @fastify/multipart@<=7.3.0
return (fastify.hasContentTypeParser('multipart') || fastify.hasContentTypeParser('multipart/form-data')) && fastify.hasRequestDecorator('multipart')
}
function createRequestRetry (requestImpl, reply, retryHandler) {
function requestRetry (req, cb) {
let retries = 0
function run () {
requestImpl(req, function (err, res) {
const retryDelay = retryHandler(req, res, err, retries)
if (!reply.sent && retryDelay) {
return retry(retryDelay)
}
cb(err, res)
})
}
function retry (after) {
retries += 1
setTimeout(run, after)
}
run()
}
return requestRetry
}
module.exports = fastifyReplyFrom
module.exports.default = fastifyReplyFrom
module.exports.fastifyReplyFrom = fastifyReplyFrom