@fastify/reply-from 9.3.0 → 9.4.0
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/README.md +4 -0
- package/index.js +5 -4
- package/package.json +1 -1
- package/test/method.test.js +70 -0
- package/types/index.d.ts +1 -0
- package/types/index.test-d.ts +1 -0
package/README.md
CHANGED
|
@@ -388,6 +388,10 @@ through `JSON.stringify()`.
|
|
|
388
388
|
Setting this option for GET, HEAD requests will throw an error "Rewriting the body when doing a {GET|HEAD} is not allowed".
|
|
389
389
|
Setting this option to `null` will strip the body (and `content-type` header) entirely from the proxied request.
|
|
390
390
|
|
|
391
|
+
#### `method`
|
|
392
|
+
|
|
393
|
+
Replaces the original request method with what is specified.
|
|
394
|
+
|
|
391
395
|
#### `retriesCount`
|
|
392
396
|
|
|
393
397
|
How many times it will try to pick another connection on socket hangup (`ECONNRESET` error).
|
package/index.js
CHANGED
|
@@ -52,6 +52,7 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
52
52
|
fastify.decorateReply('from', function (source, opts) {
|
|
53
53
|
opts = opts || {}
|
|
54
54
|
const req = this.request.raw
|
|
55
|
+
const method = opts.method || req.method
|
|
55
56
|
const onResponse = opts.onResponse
|
|
56
57
|
const rewriteHeaders = opts.rewriteHeaders || headersNoOp
|
|
57
58
|
const rewriteRequestHeaders = opts.rewriteRequestHeaders || requestHeadersNoOp
|
|
@@ -127,12 +128,12 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
127
128
|
// fastify ignore message body when it's a GET or HEAD request
|
|
128
129
|
// when proxy this request, we should reset the content-length to make it a valid http request
|
|
129
130
|
// discussion: https://github.com/fastify/fastify/issues/953
|
|
130
|
-
if (
|
|
131
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
131
132
|
// body will be populated here only if opts.body is passed.
|
|
132
133
|
// if we are doing that with a GET or HEAD request is a programmer error
|
|
133
134
|
// and as such we can throw immediately.
|
|
134
135
|
if (body) {
|
|
135
|
-
throw new Error(`Rewriting the body when doing a ${
|
|
136
|
+
throw new Error(`Rewriting the body when doing a ${method} is not allowed`)
|
|
136
137
|
}
|
|
137
138
|
}
|
|
138
139
|
|
|
@@ -141,13 +142,13 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
141
142
|
const requestHeaders = rewriteRequestHeaders(this.request, headers)
|
|
142
143
|
const contentLength = requestHeaders['content-length']
|
|
143
144
|
let requestImpl
|
|
144
|
-
if (retryMethods.has(
|
|
145
|
+
if (retryMethods.has(method) && !contentLength) {
|
|
145
146
|
requestImpl = createRequestRetry(request, this, retriesCount, retryOnError, maxRetriesOn503)
|
|
146
147
|
} else {
|
|
147
148
|
requestImpl = request
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
requestImpl({ method
|
|
151
|
+
requestImpl({ method, url, qs, headers: requestHeaders, body }, (err, res) => {
|
|
151
152
|
if (err) {
|
|
152
153
|
this.request.log.warn(err, 'response errored')
|
|
153
154
|
if (!this.sent) {
|
package/package.json
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
|
|
11
|
+
t.plan(9)
|
|
12
|
+
t.teardown(instance.close.bind(instance))
|
|
13
|
+
|
|
14
|
+
const bodyString = JSON.stringify({ hello: 'world' })
|
|
15
|
+
|
|
16
|
+
const parsedLength = Buffer.byteLength(bodyString)
|
|
17
|
+
|
|
18
|
+
const target = http.createServer((req, res) => {
|
|
19
|
+
t.pass('request proxied')
|
|
20
|
+
t.equal(req.method, 'POST')
|
|
21
|
+
t.equal(req.headers['content-type'], 'application/json')
|
|
22
|
+
t.same(req.headers['content-length'], parsedLength)
|
|
23
|
+
let data = ''
|
|
24
|
+
req.setEncoding('utf8')
|
|
25
|
+
req.on('data', (d) => {
|
|
26
|
+
data += d
|
|
27
|
+
})
|
|
28
|
+
req.on('end', () => {
|
|
29
|
+
t.same(JSON.parse(data), { hello: 'world' })
|
|
30
|
+
res.statusCode = 200
|
|
31
|
+
res.setHeader('content-type', 'application/json')
|
|
32
|
+
res.end(JSON.stringify({ something: 'else' }))
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
instance.patch('/', (request, reply) => {
|
|
37
|
+
reply.from(`http://localhost:${target.address().port}`, { method: 'POST' })
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
t.teardown(target.close.bind(target))
|
|
41
|
+
|
|
42
|
+
target.listen({ port: 0 }, (err) => {
|
|
43
|
+
t.error(err)
|
|
44
|
+
|
|
45
|
+
instance.addContentTypeParser('application/json', function (req, payload, done) {
|
|
46
|
+
done(null, payload)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
instance.register(From, {
|
|
50
|
+
base: `http://localhost:${target.address().port}`,
|
|
51
|
+
undici: true
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
instance.listen({ port: 0 }, (err) => {
|
|
55
|
+
t.error(err)
|
|
56
|
+
|
|
57
|
+
get({
|
|
58
|
+
url: `http://localhost:${instance.server.address().port}`,
|
|
59
|
+
method: 'PATCH',
|
|
60
|
+
headers: {
|
|
61
|
+
'content-type': 'application/json'
|
|
62
|
+
},
|
|
63
|
+
body: bodyString
|
|
64
|
+
}, (err, res, data) => {
|
|
65
|
+
t.error(err)
|
|
66
|
+
const parsed = JSON.parse(data)
|
|
67
|
+
t.same(parsed, { something: 'else' })
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
})
|
package/types/index.d.ts
CHANGED