@fastify/reply-from 8.4.3 → 9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.taprc +1 -1
- package/README.md +6 -3
- package/index.js +4 -4
- package/lib/request.js +11 -1
- package/package.json +2 -2
- package/test/get-upstream-type.test.js +46 -0
- package/test/http-timeout.test.js +82 -0
- package/test/{http2-target-multi-crash.js → http2-target-multi-crash.test.js} +3 -1
- package/test/{http2-timeout.js → http2-timeout.test.js} +32 -1
- package/test/rewrite-headers-type.test.js +45 -0
- package/test/rewrite-request-headers-type.test.js +45 -0
- package/types/index.d.ts +3 -5
- package/types/index.test-d.ts +3 -4
- package/test/http-timeout.js +0 -57
- /package/test/{async-route-handler.js → async-route-handler.test.js} +0 -0
- /package/test/{base-get.js → base-get.test.js} +0 -0
- /package/test/{base-path.js → base-path.test.js} +0 -0
- /package/test/{base-querystring.js → base-querystring.test.js} +0 -0
- /package/test/{build-url.js → build-url.test.js} +0 -0
- /package/test/{core-with-path-in-base.js → core-with-path-in-base.test.js} +0 -0
- /package/test/{custom-undici-instance.js → custom-undici-instance.test.js} +0 -0
- /package/test/{disable-request-logging.js → disable-request-logging.test.js} +0 -0
- /package/test/{fastify-multipart-incompatibility.js → fastify-multipart-incompatibility.test.js} +0 -0
- /package/test/{full-get.js → full-get-test.test.js} +0 -0
- /package/test/{full-https-get.js → full-https-get.test.js} +0 -0
- /package/test/{full-post-extended-content-type.js → full-post-extended-content-type.test.js} +0 -0
- /package/test/{full-post-http2.js → full-post-http2.test.js} +0 -0
- /package/test/{full-post-stream-core.js → full-post-stream-core.test.js} +0 -0
- /package/test/{full-post-stream.js → full-post-stream.test.js} +0 -0
- /package/test/{full-post.js → full-post.test.js} +0 -0
- /package/test/{full-querystring-rewrite-option-complex.js → full-querystring-rewrite-option-complex.test.js} +0 -0
- /package/test/{full-querystring-rewrite-option-function.js → full-querystring-rewrite-option-function.test.js} +0 -0
- /package/test/{full-querystring-rewrite-option.js → full-querystring-rewrite-option.test.js} +0 -0
- /package/test/{full-querystring-rewrite-string.js → full-querystring-rewrite-string.test.js} +0 -0
- /package/test/{full-querystring-rewrite.js → full-querystring-rewrite.test.js} +0 -0
- /package/test/{full-querystring.js → full-querystring.test.js} +0 -0
- /package/test/{full-rewrite-body-content-type.js → full-rewrite-body-content-type.test.js} +0 -0
- /package/test/{full-rewrite-body-http.js → full-rewrite-body-http.test.js} +0 -0
- /package/test/{full-rewrite-body-to-empty-string.js → full-rewrite-body-to-empty-string.test.js} +0 -0
- /package/test/{full-rewrite-body-to-null.js → full-rewrite-body-to-null.test.js} +0 -0
- /package/test/{full-rewrite-body.js → full-rewrite-body.test.js} +0 -0
- /package/test/{get-upstream-cache.js → get-upstream-cache.test.js} +0 -0
- /package/test/{get-upstream-http.js → get-upstream-http.test.js} +0 -0
- /package/test/{get-upstream-undici.js → get-upstream-undici.test.js} +0 -0
- /package/test/{get-with-body.js → get-with-body.test.js} +0 -0
- /package/test/{head-with-body.js → head-with-body.test.js} +0 -0
- /package/test/{host-header.js → host-header.test.js} +0 -0
- /package/test/{http-agents.js → http-agents.test.js} +0 -0
- /package/test/{http-global-agent.js → http-global-agent.test.js} +0 -0
- /package/test/{http-http2.js → http-http2.test.js} +0 -0
- /package/test/{http-invalid-target.js → http-invalid-target.test.js} +0 -0
- /package/test/{http-retry.js → http-retry.test.js} +0 -0
- /package/test/{http2-http2.js → http2-http2.test.js} +0 -0
- /package/test/{http2-https.js → http2-https.test.js} +0 -0
- /package/test/{http2-invalid-base.js → http2-invalid-base.test.js} +0 -0
- /package/test/{http2-invalid-target.js → http2-invalid-target.test.js} +0 -0
- /package/test/{http2-target-crash.js → http2-target-crash.test.js} +0 -0
- /package/test/{http2-unix-socket.js → http2-unix-socket.test.js} +0 -0
- /package/test/{https-agents.js → https-agents.test.js} +0 -0
- /package/test/{https-global-agent.js → https-global-agent.test.js} +0 -0
- /package/test/{modifyCoreObjects-false.js → modifyCoreObjects-false.test.js} +0 -0
- /package/test/{no-body-opts-with-get.js → no-body-opts-with-get.test.js} +0 -0
- /package/test/{no-body-opts-with-head.js → no-body-opts-with-head.test.js} +0 -0
- /package/test/{no-stream-body-option.js → no-stream-body-option.test.js} +0 -0
- /package/test/{on-error.js → on-error.test.js} +0 -0
- /package/test/{onResponse.js → onResponse.test.js} +0 -0
- /package/test/{padded-body.js → padded-body.test.js} +0 -0
- /package/test/{post-formbody.js → post-formbody.test.js} +0 -0
- /package/test/{post-plain-text.js → post-plain-text.test.js} +0 -0
- /package/test/{post-with-custom-encoded-contenttype.js → post-with-custom-encoded-contenttype.test.js} +0 -0
- /package/test/{post-with-octet-stream.js → post-with-octet-stream.test.js} +0 -0
- /package/test/{retry-on-503.js → retry-on-503.test.js} +0 -0
- /package/test/{rewrite-headers.js → rewrite-headers.test.js} +0 -0
- /package/test/{rewrite-request-headers.js → rewrite-request-headers.test.js} +0 -0
- /package/test/{transform-body.js → transform-body.test.js} +0 -0
- /package/test/{undici-agent.js → undici-agent.test.js} +0 -0
- /package/test/{undici-body.js → undici-body.test.js} +0 -0
- /package/test/{undici-chaining.js → undici-chaining.test.js} +0 -0
- /package/test/{undici-global-agent.js → undici-global-agent.test.js} +0 -0
- /package/test/{undici-options.js → undici-options.test.js} +0 -0
- /package/test/{undici-retry.js → undici-retry.test.js} +0 -0
- /package/test/{undici-timeout-body-partial.js → undici-timeout-body-partial.test.js} +0 -0
- /package/test/{undici-timeout-body.js → undici-timeout-body.test.js} +0 -0
- /package/test/{undici-timeout.js → undici-timeout.test.js} +0 -0
- /package/test/{undici-with-path-in-base.js → undici-with-path-in-base.test.js} +0 -0
- /package/test/{undici.js → undici.test.js} +0 -0
- /package/test/{unexpected-error.js → unexpected-error.test.js} +0 -0
- /package/test/{unix-http-undici-from.js → unix-http-undici-from.test.js} +0 -0
- /package/test/{unix-http-undici.js → unix-http-undici.test.js} +0 -0
- /package/test/{unix-http.js → unix-http.test.js} +0 -0
- /package/test/{unix-https-undici.js → unix-https-undici.test.js} +0 -0
- /package/test/{unix-https.js → unix-https.test.js} +0 -0
- /package/test/{utils-filter-pseudo-headers.js → utils-filter-pseudo-headers.test.js} +0 -0
package/.taprc
CHANGED
package/README.md
CHANGED
|
@@ -292,21 +292,24 @@ The default behavior is `reply.send(error)`, which will be disabled if the
|
|
|
292
292
|
option is specified.
|
|
293
293
|
It must reply the error.
|
|
294
294
|
|
|
295
|
-
#### `rewriteHeaders(headers,
|
|
295
|
+
#### `rewriteHeaders(headers, request)`
|
|
296
296
|
|
|
297
297
|
Called to rewrite the headers of the response, before them being copied
|
|
298
298
|
over to the outer response.
|
|
299
|
+
Parameters are the original headers and the Fastify request.
|
|
299
300
|
It must return the new headers object.
|
|
300
301
|
|
|
301
|
-
#### `rewriteRequestHeaders(
|
|
302
|
+
#### `rewriteRequestHeaders(request, headers)`
|
|
302
303
|
|
|
303
304
|
Called to rewrite the headers of the request, before them being sent to the other server.
|
|
305
|
+
Parameters are the Fastify request and the original request headers.
|
|
304
306
|
It must return the new headers object.
|
|
305
307
|
|
|
306
|
-
#### `getUpstream(
|
|
308
|
+
#### `getUpstream(request, base)`
|
|
307
309
|
|
|
308
310
|
Called to get upstream destination, before the request is being sent. Useful when you want to decide which target server to call based on the request data.
|
|
309
311
|
Helpful for a gradual rollout of new services.
|
|
312
|
+
Parameters are the Fastify request and the base string from the plugin options.
|
|
310
313
|
It must return the upstream destination.
|
|
311
314
|
|
|
312
315
|
#### `queryString` or `queryString(search, reqUrl)`
|
package/index.js
CHANGED
|
@@ -63,7 +63,7 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// we leverage caching to avoid parsing the destination URL
|
|
66
|
-
const dest = getUpstream(
|
|
66
|
+
const dest = getUpstream(this.request, base)
|
|
67
67
|
let url
|
|
68
68
|
if (cache) {
|
|
69
69
|
const cacheKey = dest + source
|
|
@@ -136,7 +136,7 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
136
136
|
|
|
137
137
|
!disableRequestLogging && this.request.log.info({ source }, 'fetching from remote server')
|
|
138
138
|
|
|
139
|
-
const requestHeaders = rewriteRequestHeaders(
|
|
139
|
+
const requestHeaders = rewriteRequestHeaders(this.request, headers)
|
|
140
140
|
const contentLength = requestHeaders['content-length']
|
|
141
141
|
let requestImpl
|
|
142
142
|
if (retryMethods.has(req.method) && !contentLength) {
|
|
@@ -166,11 +166,11 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
166
166
|
!disableRequestLogging && this.request.log.info('response received')
|
|
167
167
|
if (sourceHttp2) {
|
|
168
168
|
copyHeaders(
|
|
169
|
-
rewriteHeaders(stripHttp1ConnectionHeaders(res.headers),
|
|
169
|
+
rewriteHeaders(stripHttp1ConnectionHeaders(res.headers), this.request),
|
|
170
170
|
this
|
|
171
171
|
)
|
|
172
172
|
} else {
|
|
173
|
-
copyHeaders(rewriteHeaders(res.headers,
|
|
173
|
+
copyHeaders(rewriteHeaders(res.headers, this.request), this)
|
|
174
174
|
}
|
|
175
175
|
this.code(res.statusCode)
|
|
176
176
|
if (onResponse) {
|
package/lib/request.js
CHANGED
|
@@ -112,6 +112,10 @@ function buildRequest (opts) {
|
|
|
112
112
|
})
|
|
113
113
|
req.on('error', done)
|
|
114
114
|
req.on('response', res => {
|
|
115
|
+
// remove timeout for sse connections
|
|
116
|
+
if (res.headers['content-type'] === 'text/event-stream') {
|
|
117
|
+
req.setTimeout(0)
|
|
118
|
+
}
|
|
115
119
|
done(null, { statusCode: res.statusCode, headers: res.headers, stream: res })
|
|
116
120
|
})
|
|
117
121
|
req.once('timeout', () => {
|
|
@@ -210,6 +214,12 @@ function buildRequest (opts) {
|
|
|
210
214
|
if (err) done(err)
|
|
211
215
|
})
|
|
212
216
|
req.on('response', headers => {
|
|
217
|
+
// remove timeout for sse connections
|
|
218
|
+
if (headers['content-type'] === 'text/event-stream') {
|
|
219
|
+
req.setTimeout(0)
|
|
220
|
+
http2Client.setTimeout(0)
|
|
221
|
+
}
|
|
222
|
+
|
|
213
223
|
const statusCode = headers[':status']
|
|
214
224
|
done(null, { statusCode, headers, stream: req })
|
|
215
225
|
})
|
|
@@ -250,7 +260,7 @@ function getHttp2Opts (opts) {
|
|
|
250
260
|
http2Opts.sessionOptions = http2Opts.sessionOptions || {}
|
|
251
261
|
|
|
252
262
|
if (!http2Opts.sessionTimeout) {
|
|
253
|
-
http2Opts.sessionTimeout = opts.sessionTimeout ||
|
|
263
|
+
http2Opts.sessionTimeout = opts.sessionTimeout || 60000
|
|
254
264
|
}
|
|
255
265
|
if (!http2Opts.requestTimeout) {
|
|
256
266
|
http2Opts.requestTimeout = 10000
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/reply-from",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1",
|
|
4
4
|
"description": "forward your HTTP request to another server, for fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"split2": "^4.1.0",
|
|
47
47
|
"standard": "^17.0.0",
|
|
48
48
|
"tap": "^16.2.0",
|
|
49
|
-
"tsd": "^0.
|
|
49
|
+
"tsd": "^0.26.0"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@fastify/error": "^3.0.0",
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const From = require('..')
|
|
6
|
+
const http = require('http')
|
|
7
|
+
const get = require('simple-get').concat
|
|
8
|
+
|
|
9
|
+
const instance = Fastify()
|
|
10
|
+
instance.register(From, {
|
|
11
|
+
disableCache: true
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
t.plan(8)
|
|
15
|
+
t.teardown(instance.close.bind(instance))
|
|
16
|
+
|
|
17
|
+
const target = http.createServer((req, res) => {
|
|
18
|
+
t.pass('request proxied')
|
|
19
|
+
t.equal(req.method, 'GET')
|
|
20
|
+
res.end(req.headers.host)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
instance.get('/', (request, reply) => {
|
|
24
|
+
reply.from(`http://localhost:${target.address().port}`, {
|
|
25
|
+
getUpstream: (req) => {
|
|
26
|
+
t.pass('getUpstream called with correct request parameter')
|
|
27
|
+
t.equal(req, request)
|
|
28
|
+
return `http://localhost:${target.address().port}`
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
t.teardown(target.close.bind(target))
|
|
34
|
+
|
|
35
|
+
instance.listen({ port: 0 }, (err) => {
|
|
36
|
+
t.error(err)
|
|
37
|
+
|
|
38
|
+
target.listen({ port: 0 }, (err) => {
|
|
39
|
+
t.error(err)
|
|
40
|
+
|
|
41
|
+
get(`http://localhost:${instance.server.address().port}`, (err, res) => {
|
|
42
|
+
t.error(err)
|
|
43
|
+
t.equal(res.statusCode, 200)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
})
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const From = require('..')
|
|
6
|
+
const got = require('got')
|
|
7
|
+
const FakeTimers = require('@sinonjs/fake-timers')
|
|
8
|
+
|
|
9
|
+
const clock = FakeTimers.createClock()
|
|
10
|
+
|
|
11
|
+
test('http request timeout', async (t) => {
|
|
12
|
+
t.autoend(false)
|
|
13
|
+
|
|
14
|
+
const target = Fastify()
|
|
15
|
+
t.teardown(target.close.bind(target))
|
|
16
|
+
|
|
17
|
+
target.get('/', (request, reply) => {
|
|
18
|
+
t.pass('request arrives')
|
|
19
|
+
|
|
20
|
+
clock.setTimeout(() => {
|
|
21
|
+
reply.status(200).send('hello world')
|
|
22
|
+
t.end()
|
|
23
|
+
}, 200)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
await target.listen({ port: 0 })
|
|
27
|
+
|
|
28
|
+
const instance = Fastify()
|
|
29
|
+
t.teardown(instance.close.bind(instance))
|
|
30
|
+
|
|
31
|
+
instance.register(From, { http: { requestOptions: { timeout: 100 } } })
|
|
32
|
+
|
|
33
|
+
instance.get('/', (request, reply) => {
|
|
34
|
+
reply.from(`http://localhost:${target.server.address().port}/`)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
await instance.listen({ port: 0 })
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
|
|
41
|
+
} catch (err) {
|
|
42
|
+
t.equal(err.response.statusCode, 504)
|
|
43
|
+
t.match(err.response.headers['content-type'], /application\/json/)
|
|
44
|
+
t.same(JSON.parse(err.response.body), {
|
|
45
|
+
statusCode: 504,
|
|
46
|
+
code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
|
|
47
|
+
error: 'Gateway Timeout',
|
|
48
|
+
message: 'Gateway Timeout'
|
|
49
|
+
})
|
|
50
|
+
clock.tick(200)
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
t.fail()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('http sse removes timeout test', async (t) => {
|
|
58
|
+
const target = Fastify()
|
|
59
|
+
t.teardown(target.close.bind(target))
|
|
60
|
+
|
|
61
|
+
target.get('/', (request, reply) => {
|
|
62
|
+
t.pass('request arrives')
|
|
63
|
+
|
|
64
|
+
reply.header('content-type', 'text/event-stream').status(200).send('hello world')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
await target.listen({ port: 0 })
|
|
68
|
+
|
|
69
|
+
const instance = Fastify()
|
|
70
|
+
t.teardown(instance.close.bind(instance))
|
|
71
|
+
|
|
72
|
+
instance.register(From, { http: { requestOptions: { timeout: 100 } } })
|
|
73
|
+
|
|
74
|
+
instance.get('/', (request, reply) => {
|
|
75
|
+
reply.from(`http://localhost:${target.server.address().port}/`)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
await instance.listen({ port: 0 })
|
|
79
|
+
|
|
80
|
+
const { statusCode } = await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
|
|
81
|
+
t.equal(statusCode, 200)
|
|
82
|
+
})
|
|
@@ -20,7 +20,7 @@ test('http2 request timeout', async (t) => {
|
|
|
20
20
|
|
|
21
21
|
instance.register(From, {
|
|
22
22
|
base: `http://localhost:${target.server.address().port}`,
|
|
23
|
-
http2: { requestTimeout: 100 }
|
|
23
|
+
http2: { requestTimeout: 100, sessionTimeout: 6000 }
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
instance.get('/', (request, reply) => {
|
|
@@ -92,3 +92,34 @@ test('http2 session timeout', async (t) => {
|
|
|
92
92
|
|
|
93
93
|
t.fail()
|
|
94
94
|
})
|
|
95
|
+
|
|
96
|
+
test('http2 sse removes request and session timeout test', async (t) => {
|
|
97
|
+
const target = Fastify({ http2: true, sessionTimeout: 0 })
|
|
98
|
+
|
|
99
|
+
target.get('/', (request, reply) => {
|
|
100
|
+
t.pass('request arrives')
|
|
101
|
+
|
|
102
|
+
reply.status(200).header('content-type', 'text/event-stream').send('hello world')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
await target.listen({ port: 0 })
|
|
106
|
+
|
|
107
|
+
const instance = Fastify()
|
|
108
|
+
|
|
109
|
+
instance.register(From, {
|
|
110
|
+
base: `http://localhost:${target.server.address().port}`,
|
|
111
|
+
http2: { sessionTimeout: 100 }
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
instance.get('/', (request, reply) => {
|
|
115
|
+
reply.from(`http://localhost:${target.server.address().port}/`)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
await instance.listen({ port: 0 })
|
|
119
|
+
|
|
120
|
+
t.teardown(instance.close.bind(instance))
|
|
121
|
+
t.teardown(target.close.bind(target))
|
|
122
|
+
|
|
123
|
+
const { statusCode } = await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
|
|
124
|
+
t.equal(statusCode, 200)
|
|
125
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const From = require('..')
|
|
6
|
+
const http = require('http')
|
|
7
|
+
const get = require('simple-get').concat
|
|
8
|
+
|
|
9
|
+
const instance = Fastify()
|
|
10
|
+
instance.register(From)
|
|
11
|
+
|
|
12
|
+
t.plan(8)
|
|
13
|
+
t.teardown(instance.close.bind(instance))
|
|
14
|
+
|
|
15
|
+
const target = http.createServer((req, res) => {
|
|
16
|
+
t.pass('request proxied')
|
|
17
|
+
t.equal(req.method, 'GET')
|
|
18
|
+
res.statusCode = 205
|
|
19
|
+
res.end('hello world')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
instance.get('/', (request, reply) => {
|
|
23
|
+
reply.from(`http://localhost:${target.address().port}`, {
|
|
24
|
+
rewriteHeaders: (headers, req) => {
|
|
25
|
+
t.pass('rewriteHeaders called with correct request parameter')
|
|
26
|
+
t.equal(req, request)
|
|
27
|
+
return {}
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
t.teardown(target.close.bind(target))
|
|
33
|
+
|
|
34
|
+
instance.listen({ port: 0 }, (err) => {
|
|
35
|
+
t.error(err)
|
|
36
|
+
|
|
37
|
+
target.listen({ port: 0 }, (err) => {
|
|
38
|
+
t.error(err)
|
|
39
|
+
|
|
40
|
+
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
|
|
41
|
+
t.error(err)
|
|
42
|
+
t.equal(res.statusCode, 205)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const From = require('..')
|
|
6
|
+
const http = require('http')
|
|
7
|
+
const get = require('simple-get').concat
|
|
8
|
+
|
|
9
|
+
const instance = Fastify()
|
|
10
|
+
instance.register(From)
|
|
11
|
+
|
|
12
|
+
t.plan(8)
|
|
13
|
+
t.teardown(instance.close.bind(instance))
|
|
14
|
+
|
|
15
|
+
const target = http.createServer((req, res) => {
|
|
16
|
+
t.pass('request proxied')
|
|
17
|
+
t.equal(req.method, 'GET')
|
|
18
|
+
res.statusCode = 205
|
|
19
|
+
res.end(req.headers.host)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
instance.get('/', (request, reply) => {
|
|
23
|
+
reply.from(`http://localhost:${target.address().port}`, {
|
|
24
|
+
rewriteRequestHeaders: (originalReq, headers) => {
|
|
25
|
+
t.pass('rewriteRequestHeaders called with correct request parameter')
|
|
26
|
+
t.equal(originalReq, request)
|
|
27
|
+
return {}
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
t.teardown(target.close.bind(target))
|
|
33
|
+
|
|
34
|
+
instance.listen({ port: 0 }, (err) => {
|
|
35
|
+
t.error(err)
|
|
36
|
+
|
|
37
|
+
target.listen({ port: 0 }, (err) => {
|
|
38
|
+
t.error(err)
|
|
39
|
+
|
|
40
|
+
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
|
|
41
|
+
t.error(err)
|
|
42
|
+
t.equal(res.statusCode, 205)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
})
|
package/types/index.d.ts
CHANGED
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
} from 'fastify';
|
|
12
12
|
|
|
13
13
|
import {
|
|
14
|
-
IncomingMessage,
|
|
15
14
|
IncomingHttpHeaders,
|
|
16
15
|
RequestOptions,
|
|
17
16
|
AgentOptions,
|
|
@@ -23,7 +22,6 @@ import {
|
|
|
23
22
|
Agent as SecureAgent
|
|
24
23
|
} from "https";
|
|
25
24
|
import {
|
|
26
|
-
Http2ServerRequest,
|
|
27
25
|
IncomingHttpHeaders as Http2IncomingHttpHeaders,
|
|
28
26
|
ClientSessionRequestOptions,
|
|
29
27
|
ClientSessionOptions,
|
|
@@ -59,14 +57,14 @@ declare namespace fastifyReplyFrom {
|
|
|
59
57
|
body?: unknown;
|
|
60
58
|
rewriteHeaders?: (
|
|
61
59
|
headers: Http2IncomingHttpHeaders | IncomingHttpHeaders,
|
|
62
|
-
|
|
60
|
+
request?: FastifyRequest<RequestGenericInterface, RawServerBase>
|
|
63
61
|
) => Http2IncomingHttpHeaders | IncomingHttpHeaders;
|
|
64
62
|
rewriteRequestHeaders?: (
|
|
65
|
-
|
|
63
|
+
request: FastifyRequest<RequestGenericInterface, RawServerBase>,
|
|
66
64
|
headers: Http2IncomingHttpHeaders | IncomingHttpHeaders
|
|
67
65
|
) => Http2IncomingHttpHeaders | IncomingHttpHeaders;
|
|
68
66
|
getUpstream?: (
|
|
69
|
-
|
|
67
|
+
request: FastifyRequest<RequestGenericInterface, RawServerBase>,
|
|
70
68
|
base: string
|
|
71
69
|
) => string;
|
|
72
70
|
}
|
package/types/index.test-d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import replyFrom, { FastifyReplyFromOptions } from "..";
|
|
2
|
-
import fastify, {FastifyReply, RawServerBase} from "fastify";
|
|
2
|
+
import fastify, {FastifyReply, FastifyRequest, RawServerBase, RequestGenericInterface} from "fastify";
|
|
3
3
|
import { AddressInfo } from "net";
|
|
4
4
|
import { IncomingHttpHeaders } from "http2";
|
|
5
5
|
import { expectType } from 'tsd';
|
|
6
6
|
import * as http from 'http';
|
|
7
7
|
import * as https from 'https';
|
|
8
|
-
import * as http2 from 'http2';
|
|
9
8
|
// @ts-ignore
|
|
10
9
|
import tap from 'tap'
|
|
11
10
|
|
|
@@ -67,11 +66,11 @@ async function main() {
|
|
|
67
66
|
reply.from("/v3", {
|
|
68
67
|
body: {hello: "world"},
|
|
69
68
|
rewriteRequestHeaders(req, headers) {
|
|
70
|
-
expectType<
|
|
69
|
+
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req);
|
|
71
70
|
return headers;
|
|
72
71
|
},
|
|
73
72
|
getUpstream(req, base) {
|
|
74
|
-
expectType<
|
|
73
|
+
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req);
|
|
75
74
|
return base;
|
|
76
75
|
}
|
|
77
76
|
});
|
package/test/http-timeout.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const t = require('tap')
|
|
4
|
-
const Fastify = require('fastify')
|
|
5
|
-
const From = require('..')
|
|
6
|
-
const got = require('got')
|
|
7
|
-
const FakeTimers = require('@sinonjs/fake-timers')
|
|
8
|
-
|
|
9
|
-
const clock = FakeTimers.createClock()
|
|
10
|
-
|
|
11
|
-
t.autoend(false)
|
|
12
|
-
|
|
13
|
-
const target = Fastify()
|
|
14
|
-
t.teardown(target.close.bind(target))
|
|
15
|
-
|
|
16
|
-
target.get('/', (request, reply) => {
|
|
17
|
-
t.pass('request arrives')
|
|
18
|
-
|
|
19
|
-
clock.setTimeout(() => {
|
|
20
|
-
reply.status(200).send('hello world')
|
|
21
|
-
t.end()
|
|
22
|
-
}, 200)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
async function main () {
|
|
26
|
-
await target.listen({ port: 0 })
|
|
27
|
-
|
|
28
|
-
const instance = Fastify()
|
|
29
|
-
t.teardown(instance.close.bind(instance))
|
|
30
|
-
|
|
31
|
-
instance.register(From, { http: { requestOptions: { timeout: 100 } } })
|
|
32
|
-
|
|
33
|
-
instance.get('/', (request, reply) => {
|
|
34
|
-
reply.from(`http://localhost:${target.server.address().port}/`)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
await instance.listen({ port: 0 })
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
|
|
41
|
-
} catch (err) {
|
|
42
|
-
t.equal(err.response.statusCode, 504)
|
|
43
|
-
t.match(err.response.headers['content-type'], /application\/json/)
|
|
44
|
-
t.same(JSON.parse(err.response.body), {
|
|
45
|
-
statusCode: 504,
|
|
46
|
-
code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
|
|
47
|
-
error: 'Gateway Timeout',
|
|
48
|
-
message: 'Gateway Timeout'
|
|
49
|
-
})
|
|
50
|
-
clock.tick(200)
|
|
51
|
-
return
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
t.fail()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/test/{fastify-multipart-incompatibility.js → fastify-multipart-incompatibility.test.js}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/test/{full-post-extended-content-type.js → full-post-extended-content-type.test.js}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/test/{full-querystring-rewrite-option.js → full-querystring-rewrite-option.test.js}
RENAMED
|
File without changes
|
/package/test/{full-querystring-rewrite-string.js → full-querystring-rewrite-string.test.js}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/test/{full-rewrite-body-to-empty-string.js → full-rewrite-body-to-empty-string.test.js}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|