@fastify/reply-from 7.0.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/.github/dependabot.yml +13 -0
- package/.github/stale.yml +21 -0
- package/.github/workflows/ci.yml +60 -0
- package/.taprc +5 -0
- package/LICENSE +21 -0
- package/README.md +335 -0
- package/example.js +36 -0
- package/index.d.ts +99 -0
- package/index.js +262 -0
- package/lib/request.js +288 -0
- package/lib/utils.js +81 -0
- package/package.json +70 -0
- package/test/async-route-handler.js +50 -0
- package/test/base-get.js +48 -0
- package/test/base-path.js +38 -0
- package/test/base-querystring.js +48 -0
- package/test/build-url.js +69 -0
- package/test/core-with-path-in-base.js +50 -0
- package/test/custom-undici-instance.js +77 -0
- package/test/fastify-multipart-incompatibility.js +90 -0
- package/test/fixtures/fastify.cert +19 -0
- package/test/fixtures/fastify.key +27 -0
- package/test/fixtures/file.txt +1 -0
- package/test/full-get.js +45 -0
- package/test/full-https-get.js +55 -0
- package/test/full-post-extended-content-type.js +59 -0
- package/test/full-post-http2.js +49 -0
- package/test/full-post-stream-core.js +66 -0
- package/test/full-post-stream.js +64 -0
- package/test/full-post.js +56 -0
- package/test/full-querystring-rewrite-option-complex.js +48 -0
- package/test/full-querystring-rewrite-option-function.js +51 -0
- package/test/full-querystring-rewrite-option.js +48 -0
- package/test/full-querystring-rewrite-string.js +46 -0
- package/test/full-querystring-rewrite.js +46 -0
- package/test/full-querystring.js +46 -0
- package/test/full-rewrite-body-content-type.js +59 -0
- package/test/full-rewrite-body-http.js +63 -0
- package/test/full-rewrite-body-to-empty-string.js +61 -0
- package/test/full-rewrite-body-to-null.js +61 -0
- package/test/full-rewrite-body.js +61 -0
- package/test/get-upstream-http.js +70 -0
- package/test/get-upstream-undici.js +45 -0
- package/test/get-with-body.js +55 -0
- package/test/head-with-body.js +57 -0
- package/test/host-header.js +67 -0
- package/test/http-agents.js +55 -0
- package/test/http-http2.js +50 -0
- package/test/http-invalid-target.js +35 -0
- package/test/http-retry.js +109 -0
- package/test/http-timeout.js +56 -0
- package/test/http2-http2.js +57 -0
- package/test/http2-https.js +80 -0
- package/test/http2-invalid-target.js +36 -0
- package/test/http2-target-crash.js +53 -0
- package/test/http2-target-multi-crash.js +60 -0
- package/test/http2-timeout.js +92 -0
- package/test/https-agents.js +67 -0
- package/test/index.test-d.ts +136 -0
- package/test/modifyCoreObjects-false.js +48 -0
- package/test/no-body-opts-with-get.js +49 -0
- package/test/no-body-opts-with-head.js +51 -0
- package/test/no-stream-body-option.js +58 -0
- package/test/on-error.js +62 -0
- package/test/onResponse.js +48 -0
- package/test/padded-body.js +64 -0
- package/test/post-formbody.js +59 -0
- package/test/post-plain-text.js +56 -0
- package/test/post-with-custom-encoded-contenttype.js +65 -0
- package/test/retry-on-503.js +101 -0
- package/test/rewrite-headers.js +52 -0
- package/test/rewrite-request-headers.js +47 -0
- package/test/transform-body.js +61 -0
- package/test/undici-agent.js +77 -0
- package/test/undici-body.js +70 -0
- package/test/undici-options.js +73 -0
- package/test/undici-retry.js +109 -0
- package/test/undici-timeout-body-partial.js +60 -0
- package/test/undici-timeout-body.js +63 -0
- package/test/undici-timeout.js +61 -0
- package/test/undici-with-path-in-base.js +50 -0
- package/test/undici.js +50 -0
- package/test/unexpected-error.js +46 -0
- package/test/unix-http-undici-from.js +51 -0
- package/test/unix-http-undici.js +62 -0
- package/test/unix-http.js +65 -0
- package/test/unix-https-undici.js +71 -0
- package/test/unix-https.js +71 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: "github-actions"
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: "monthly"
|
|
7
|
+
open-pull-requests-limit: 10
|
|
8
|
+
|
|
9
|
+
- package-ecosystem: "npm"
|
|
10
|
+
directory: "/"
|
|
11
|
+
schedule:
|
|
12
|
+
interval: "weekly"
|
|
13
|
+
open-pull-requests-limit: 10
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Number of days of inactivity before an issue becomes stale
|
|
2
|
+
daysUntilStale: 15
|
|
3
|
+
# Number of days of inactivity before a stale issue is closed
|
|
4
|
+
daysUntilClose: 7
|
|
5
|
+
# Issues with these labels will never be considered stale
|
|
6
|
+
exemptLabels:
|
|
7
|
+
- "discussion"
|
|
8
|
+
- "feature request"
|
|
9
|
+
- "bug"
|
|
10
|
+
- "help wanted"
|
|
11
|
+
- "plugin suggestion"
|
|
12
|
+
- "good first issue"
|
|
13
|
+
# Label to use when marking an issue as stale
|
|
14
|
+
staleLabel: stale
|
|
15
|
+
# Comment to post when marking an issue as stale. Set to `false` to disable
|
|
16
|
+
markComment: >
|
|
17
|
+
This issue has been automatically marked as stale because it has not had
|
|
18
|
+
recent activity. It will be closed if no further activity occurs. Thank you
|
|
19
|
+
for your contributions.
|
|
20
|
+
# Comment to post when closing a stale issue. Set to `false` to disable
|
|
21
|
+
closeComment: false
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
'on':
|
|
3
|
+
push:
|
|
4
|
+
paths-ignore:
|
|
5
|
+
- docs/**
|
|
6
|
+
- '*.md'
|
|
7
|
+
pull_request:
|
|
8
|
+
paths-ignore:
|
|
9
|
+
- docs/**
|
|
10
|
+
- '*.md'
|
|
11
|
+
jobs:
|
|
12
|
+
test:
|
|
13
|
+
runs-on: ${{ matrix.os }}
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
node-version:
|
|
17
|
+
- 12
|
|
18
|
+
- 14
|
|
19
|
+
- 16
|
|
20
|
+
os:
|
|
21
|
+
- macos-latest
|
|
22
|
+
- ubuntu-latest
|
|
23
|
+
- windows-latest
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v3
|
|
26
|
+
- name: Use Node.js
|
|
27
|
+
uses: actions/setup-node@v3
|
|
28
|
+
with:
|
|
29
|
+
node-version: ${{ matrix.node-version }}
|
|
30
|
+
- name: Install Dependencies
|
|
31
|
+
run: |
|
|
32
|
+
npm install --ignore-scripts
|
|
33
|
+
- name: Run Tests
|
|
34
|
+
run: |
|
|
35
|
+
npm run test:ci
|
|
36
|
+
- name: Coveralls Parallel
|
|
37
|
+
uses: coverallsapp/github-action@1.1.3
|
|
38
|
+
with:
|
|
39
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
40
|
+
parallel: true
|
|
41
|
+
flag-name: run-${{ matrix.node-version }}-${{ matrix.os }}
|
|
42
|
+
coverage:
|
|
43
|
+
needs: test
|
|
44
|
+
runs-on: ubuntu-latest
|
|
45
|
+
steps:
|
|
46
|
+
- name: Coveralls Finished
|
|
47
|
+
uses: coverallsapp/github-action@1.1.3
|
|
48
|
+
with:
|
|
49
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
50
|
+
parallel-finished: true
|
|
51
|
+
automerge:
|
|
52
|
+
needs: test
|
|
53
|
+
runs-on: ubuntu-latest
|
|
54
|
+
permissions:
|
|
55
|
+
pull-requests: write
|
|
56
|
+
contents: write
|
|
57
|
+
steps:
|
|
58
|
+
- uses: fastify/github-action-merge-dependabot@v3.0.2
|
|
59
|
+
with:
|
|
60
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
package/.taprc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Matteo Collina
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
# fastify-reply-from
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://www.npmjs.com/package/fastify-reply-from)
|
|
5
|
+
[](https://snyk.io/test/github/fastify/fastify-reply-from)
|
|
6
|
+
[](https://coveralls.io/github/fastify/fastify-reply-from?branch=master)
|
|
7
|
+
[](https://standardjs.com/)
|
|
8
|
+
|
|
9
|
+
Fastify plugin to forward the current HTTP request to another server.
|
|
10
|
+
HTTP2 to HTTP is supported too.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
npm i fastify-reply-from
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Compatibility with fastify-multipart
|
|
19
|
+
`fastify-reply-from` and [`fastify-multipart`](https://github.com/fastify/fastify-multipart) should not be registered as sibling plugins nor should they be registered in plugins which have a parent-child relationship.<br> The two plugins are incompatible, in the sense that the behavior of `fastify-reply-from` might not be the expected one when the above-mentioned conditions are not respected.<br> This is due to the fact that `fastify-multipart` consumes the multipart content by parsing it, hence this content is not forwarded to the target service by `fastify-reply-from`.<br>
|
|
20
|
+
However, the two plugins may be used within the same fastify instance, at the condition that they belong to disjoint branches of the fastify plugins hierarchy tree.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
The following example set up two Fastify servers and forward the request
|
|
25
|
+
from one to the other:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
'use strict'
|
|
29
|
+
|
|
30
|
+
const Fastify = require('fastify')
|
|
31
|
+
|
|
32
|
+
const target = Fastify({
|
|
33
|
+
logger: true
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
target.get('/', (request, reply) => {
|
|
37
|
+
reply.send('hello world')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const proxy = Fastify({
|
|
41
|
+
logger: true
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
proxy.register(require('fastify-reply-from'), {
|
|
45
|
+
base: 'http://localhost:3001/'
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
proxy.get('/', (request, reply) => {
|
|
49
|
+
reply.from('/')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
target.listen(3001, (err) => {
|
|
53
|
+
if (err) {
|
|
54
|
+
throw err
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
proxy.listen(3000, (err) => {
|
|
58
|
+
if (err) {
|
|
59
|
+
throw err
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### Plugin options
|
|
68
|
+
|
|
69
|
+
#### `base`
|
|
70
|
+
|
|
71
|
+
Set the base URL for all the forwarded requests. Will be required if `http2` is set to `true`
|
|
72
|
+
Note that _every path will be discarded_.
|
|
73
|
+
|
|
74
|
+
Custom URL protocols `unix+http:` and `unix+https:` can be used to forward requests to a unix
|
|
75
|
+
socket server by using `querystring.escape(socketPath)` as the hostname. This is not supported
|
|
76
|
+
for http2 nor undici. To illustrate:
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
const socketPath = require('querystring').escape('/run/http-daemon.socket')
|
|
80
|
+
proxy.register(require('fastify-reply-from'), {
|
|
81
|
+
base: 'unix+http://${socketPath}/'
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### `undici`
|
|
86
|
+
|
|
87
|
+
By default, [undici](https://github.com/mcollina/undici) will be used to perform the HTTP/1.1
|
|
88
|
+
requests. Enabling this flag should guarantee
|
|
89
|
+
20-50% more throughput.
|
|
90
|
+
|
|
91
|
+
This flag could controls the settings of the undici client, like so:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
proxy.register(require('fastify-reply-from'), {
|
|
95
|
+
base: 'http://localhost:3001/',
|
|
96
|
+
// default settings
|
|
97
|
+
undici: {
|
|
98
|
+
connections: 128,
|
|
99
|
+
pipelining: 1,
|
|
100
|
+
keepAliveTimeout: 60 * 1000,
|
|
101
|
+
tls: {
|
|
102
|
+
rejectUnauthorized: false
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
See undici own options for more configurations.
|
|
108
|
+
|
|
109
|
+
You can also pass the plugin a custom instance:
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
proxy.register(require('fastify-reply-from'), {
|
|
113
|
+
base: 'http://localhost:3001/',
|
|
114
|
+
undici: new undici.Pool('http://localhost:3001')
|
|
115
|
+
})
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### `http`
|
|
119
|
+
|
|
120
|
+
Set the `http` option to `true` or to an Object to use
|
|
121
|
+
Node's [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback)
|
|
122
|
+
will be used if you do not enable [`http2`](#http2). To customize the `request`,
|
|
123
|
+
you can pass in [`agentOptions`](https://nodejs.org/api/http.html#http_new_agent_options) and
|
|
124
|
+
[`requestOptions`](https://nodejs.org/api/http.html#http_http_request_options_callback). To illustrate:
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
proxy.register(require('fastify-reply-from'), {
|
|
128
|
+
base: 'http://localhost:3001/',
|
|
129
|
+
http: {
|
|
130
|
+
agentOptions: { // pass in any options from https://nodejs.org/api/http.html#http_new_agent_options
|
|
131
|
+
keepAliveMsecs: 10 * 60 * 1000
|
|
132
|
+
},
|
|
133
|
+
requestOptions: { // pass in any options from https://nodejs.org/api/http.html#http_http_request_options_callback
|
|
134
|
+
timeout: 5000 // timeout in msecs, defaults to 10000 (10 seconds)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
You can also pass custom HTTP agents. If you pass the agents, then the http.agentOptions will be ignored. To illustrate:
|
|
141
|
+
```js
|
|
142
|
+
proxy.register(require('fastify-reply-from'), {
|
|
143
|
+
base: 'http://localhost:3001/',
|
|
144
|
+
http: {
|
|
145
|
+
agents: {
|
|
146
|
+
'http:': new http.Agent({ keepAliveMsecs: 10 * 60 * 1000 }), // pass in any options from https://nodejs.org/api/http.html#http_new_agent_options
|
|
147
|
+
'https:': new https.Agent({ keepAliveMsecs: 10 * 60 * 1000 })
|
|
148
|
+
|
|
149
|
+
},
|
|
150
|
+
requestOptions: { // pass in any options from https://nodejs.org/api/http.html#http_http_request_options_callback
|
|
151
|
+
timeout: 5000 // timeout in msecs, defaults to 10000 (10 seconds)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
#### `http2`
|
|
158
|
+
|
|
159
|
+
You can either set `http2` to `true` or set the settings object to connect to a HTTP/2 server.
|
|
160
|
+
The `http2` settings object has the shape of:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
proxy.register(require('fastify-reply-from'), {
|
|
164
|
+
base: 'http://localhost:3001/',
|
|
165
|
+
http2: {
|
|
166
|
+
sessionTimeout: 10000, // HTTP/2 session timeout in msecs, defaults to 60000 (1 minute)
|
|
167
|
+
requestTimeout: 5000, // HTTP/2 request timeout in msecs, defaults to 10000 (10 seconds)
|
|
168
|
+
sessionOptions: { // HTTP/2 session connect options, pass in any options from https://nodejs.org/api/http2.html#http2_http2_connect_authority_options_listener
|
|
169
|
+
rejectUnauthorized: true
|
|
170
|
+
},
|
|
171
|
+
requestTimeout: { // HTTP/2 request options, pass in any options from https://nodejs.org/api/http2.html#http2_clienthttp2session_request_headers_options
|
|
172
|
+
endStream: true
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
#### `cacheURLs`
|
|
179
|
+
|
|
180
|
+
The number of parsed URLs that will be cached. Default: `100`.
|
|
181
|
+
|
|
182
|
+
#### `disableCache`
|
|
183
|
+
|
|
184
|
+
This option will disable the URL caching.
|
|
185
|
+
This cache is dedicated to reduce the amount of URL object generation.
|
|
186
|
+
Generating URLs is a main bottleneck of this module, please disable this cache with caution.
|
|
187
|
+
|
|
188
|
+
#### `contentTypesToEncode`
|
|
189
|
+
|
|
190
|
+
An array of content types whose response body will be passed through `JSON.stringify()`.
|
|
191
|
+
This only applies when a custom [`body`](#body) is not passed in. Defaults to:
|
|
192
|
+
|
|
193
|
+
```js
|
|
194
|
+
[
|
|
195
|
+
'application/json',
|
|
196
|
+
'application/x-www-form-urlencoded'
|
|
197
|
+
]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
#### `retryMethods`
|
|
201
|
+
|
|
202
|
+
On which methods should the connection be retried in case of socket hang up.
|
|
203
|
+
**Be aware** that setting here not idempotent method may lead to unexpected results on target.
|
|
204
|
+
|
|
205
|
+
By default: `['GET', 'HEAD', 'OPTIONS', 'TRACE' ]`
|
|
206
|
+
|
|
207
|
+
This plugin will always retry on 503 errors, _unless_ `retryMethods` does not contain `GET`.
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
#### `maxRetriesOn503`
|
|
212
|
+
|
|
213
|
+
This plugin will always retry on `GET` requests that returns 503 errors, _unless_ `retryMethods` does not contain `GET`.
|
|
214
|
+
|
|
215
|
+
This option set the limit on how many times the plugin should retry the request, specifically for 503 errors.
|
|
216
|
+
|
|
217
|
+
By Default: 10
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
### `reply.from(source, [opts])`
|
|
223
|
+
|
|
224
|
+
The plugin decorates the
|
|
225
|
+
[`Reply`](https://github.com/fastify/fastify/blob/master/docs/Reply.md)
|
|
226
|
+
instance with a `from` method, which will reply to the original request
|
|
227
|
+
__from the desired source__. The options allows to override any part of
|
|
228
|
+
the request or response being sent or received to/from the source.
|
|
229
|
+
|
|
230
|
+
**Note: If `base` is specified in plugin options, the `source` here should not override the host/origin.**
|
|
231
|
+
|
|
232
|
+
#### `onResponse(request, reply, res)`
|
|
233
|
+
|
|
234
|
+
Called when a HTTP response is received from the source.
|
|
235
|
+
The default behavior is `reply.send(res)`, which will be disabled if the
|
|
236
|
+
option is specified.
|
|
237
|
+
|
|
238
|
+
When replying with a body of a different length it is necessary to remove
|
|
239
|
+
the `content-length` header.
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
{
|
|
243
|
+
onResponse: (request, reply, res) => {
|
|
244
|
+
reply.removeHeader('content-length');
|
|
245
|
+
reply.send('New body of different length');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### `onError(reply, error)`
|
|
251
|
+
|
|
252
|
+
Called when a HTTP response is received with error from the source.
|
|
253
|
+
The default behavior is `reply.send(error)`, which will be disabled if the
|
|
254
|
+
option is specified.
|
|
255
|
+
It must reply the error.
|
|
256
|
+
|
|
257
|
+
#### `rewriteHeaders(headers, req)`
|
|
258
|
+
|
|
259
|
+
Called to rewrite the headers of the response, before them being copied
|
|
260
|
+
over to the outer response.
|
|
261
|
+
It must return the new headers object.
|
|
262
|
+
|
|
263
|
+
#### `rewriteRequestHeaders(originalReq, headers)`
|
|
264
|
+
|
|
265
|
+
Called to rewrite the headers of the request, before them being sent to the other server.
|
|
266
|
+
It must return the new headers object.
|
|
267
|
+
|
|
268
|
+
#### `getUpstream(originalReq, base)`
|
|
269
|
+
|
|
270
|
+
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.
|
|
271
|
+
Helpful for a gradual rollout of new services.
|
|
272
|
+
It must return the upstream destination.
|
|
273
|
+
|
|
274
|
+
#### `queryString` or `queryString(search, reqUrl)`
|
|
275
|
+
|
|
276
|
+
Replaces the original querystring of the request with what is specified.
|
|
277
|
+
This will be passed to
|
|
278
|
+
[`querystring.stringify`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options).
|
|
279
|
+
|
|
280
|
+
- `object`: accepts an object that will be passed to `querystring.stringify`
|
|
281
|
+
- `function`: function that will return a string with the query parameters e.g. `name=test&type=user`
|
|
282
|
+
|
|
283
|
+
#### `body`
|
|
284
|
+
|
|
285
|
+
Replaces the original request body with what is specified. Unless
|
|
286
|
+
[`contentType`](#contentType) is specified, the content will be passed
|
|
287
|
+
through `JSON.stringify()`.
|
|
288
|
+
Setting this option for GET, HEAD requests will throw an error "Rewriting the body when doing a {GET|HEAD} is not allowed".
|
|
289
|
+
Setting this option to `null` will strip the body (and `content-type` header) entirely from the proxied request.
|
|
290
|
+
|
|
291
|
+
#### `retriesCount`
|
|
292
|
+
|
|
293
|
+
How many times it will try to pick another connection on socket hangup (`ECONNRESET` error).
|
|
294
|
+
Useful when keeping the connection open (KeepAlive).
|
|
295
|
+
This number should be a function of the number of connections and the number of instances of a target.
|
|
296
|
+
|
|
297
|
+
By default: 0 (disabled)
|
|
298
|
+
|
|
299
|
+
#### `contentType`
|
|
300
|
+
|
|
301
|
+
Override the `'Content-Type'` header of the forwarded request, if we are
|
|
302
|
+
already overriding the [`body`](#body).
|
|
303
|
+
|
|
304
|
+
### Combining with [fastify-formbody](https://github.com/fastify/fastify-formbody)
|
|
305
|
+
|
|
306
|
+
`formbody` expects the body to be returned as a string and not an object.
|
|
307
|
+
Use the [`contentTypesToEncode`](#contentTypesToEncode) option to pass in `['application/x-www-form-urlencoded']`
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
### HTTP & HTTP2 timeouts
|
|
311
|
+
|
|
312
|
+
This library has:
|
|
313
|
+
- `timeout` for `http` set by default. The default value is 10 seconds (`10000`).
|
|
314
|
+
- `requestTimeout` & `sessionTimeout` for `http2` set by default.
|
|
315
|
+
- The default value for `requestTimeout` is 10 seconds (`10000`).
|
|
316
|
+
- The default value for `sessionTimeout` is 60 seconds (`60000`).
|
|
317
|
+
|
|
318
|
+
When a timeout happens, [`504 Gateway Timeout`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/504)
|
|
319
|
+
will be returned to the client.
|
|
320
|
+
|
|
321
|
+
## TODO
|
|
322
|
+
|
|
323
|
+
* [ ] support overriding the body with a stream
|
|
324
|
+
* [ ] forward the request id to the other peer might require some
|
|
325
|
+
refactoring because we have to make the `req.id` unique
|
|
326
|
+
(see [hyperid](https://npm.im/hyperid)).
|
|
327
|
+
* [ ] Support origin HTTP2 push
|
|
328
|
+
* [x] benchmarks
|
|
329
|
+
|
|
330
|
+
## License
|
|
331
|
+
|
|
332
|
+
MIT
|
|
333
|
+
|
|
334
|
+
[http-agent]: https://nodejs.org/api/http.html#http_new_agent_options
|
|
335
|
+
[https-agent]: https://nodejs.org/api/https.html#https_class_https_agent
|
package/example.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Fastify = require('fastify')
|
|
4
|
+
|
|
5
|
+
const target = Fastify({
|
|
6
|
+
logger: true
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
target.get('/', (request, reply) => {
|
|
10
|
+
reply.send('hello world')
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
const proxy = Fastify({
|
|
14
|
+
logger: true
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
// proxy.register(require('fastify-reply-from'), {
|
|
18
|
+
proxy.register(require('.'), {
|
|
19
|
+
base: 'http://localhost:3001/'
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
proxy.get('/', (request, reply) => {
|
|
23
|
+
reply.from('/')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
target.listen(3001, (err) => {
|
|
27
|
+
if (err) {
|
|
28
|
+
throw err
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
proxy.listen(3000, (err) => {
|
|
32
|
+
if (err) {
|
|
33
|
+
throw err
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
})
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
FastifyRequest,
|
|
5
|
+
FastifyReply,
|
|
6
|
+
RawReplyDefaultExpression,
|
|
7
|
+
RawServerBase,
|
|
8
|
+
RequestGenericInterface,
|
|
9
|
+
HTTPMethods,
|
|
10
|
+
FastifyPluginAsync,
|
|
11
|
+
FastifyPluginCallback,
|
|
12
|
+
} from 'fastify';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
IncomingMessage,
|
|
16
|
+
IncomingHttpHeaders,
|
|
17
|
+
RequestOptions,
|
|
18
|
+
AgentOptions,
|
|
19
|
+
Agent,
|
|
20
|
+
} from "http";
|
|
21
|
+
import {
|
|
22
|
+
RequestOptions as SecureRequestOptions,
|
|
23
|
+
AgentOptions as SecureAgentOptions,
|
|
24
|
+
Agent as SecureAgent
|
|
25
|
+
} from "https";
|
|
26
|
+
import {
|
|
27
|
+
Http2ServerRequest,
|
|
28
|
+
IncomingHttpHeaders as Http2IncomingHttpHeaders,
|
|
29
|
+
ClientSessionRequestOptions,
|
|
30
|
+
ClientSessionOptions,
|
|
31
|
+
SecureClientSessionOptions,
|
|
32
|
+
} from "http2";
|
|
33
|
+
import { Pool } from 'undici'
|
|
34
|
+
type QueryStringFunction = (search: string | undefined, reqUrl: string) => string;
|
|
35
|
+
export interface FastifyReplyFromHooks {
|
|
36
|
+
queryString?: { [key: string]: unknown } | QueryStringFunction;
|
|
37
|
+
contentType?: string;
|
|
38
|
+
onResponse?: (
|
|
39
|
+
request: FastifyRequest<RequestGenericInterface, RawServerBase>,
|
|
40
|
+
reply: FastifyReply<RawServerBase>,
|
|
41
|
+
res: RawReplyDefaultExpression<RawServerBase>
|
|
42
|
+
) => void;
|
|
43
|
+
onError?: (
|
|
44
|
+
reply: FastifyReply<RawServerBase>,
|
|
45
|
+
error: { error: Error }
|
|
46
|
+
) => void;
|
|
47
|
+
body?: unknown;
|
|
48
|
+
rewriteHeaders?: (
|
|
49
|
+
headers: Http2IncomingHttpHeaders | IncomingHttpHeaders,
|
|
50
|
+
req?: Http2ServerRequest | IncomingMessage
|
|
51
|
+
) => Http2IncomingHttpHeaders | IncomingHttpHeaders;
|
|
52
|
+
rewriteRequestHeaders?: (
|
|
53
|
+
req: Http2ServerRequest | IncomingMessage,
|
|
54
|
+
headers: Http2IncomingHttpHeaders | IncomingHttpHeaders
|
|
55
|
+
) => Http2IncomingHttpHeaders | IncomingHttpHeaders;
|
|
56
|
+
getUpstream?: (
|
|
57
|
+
req: Http2ServerRequest | IncomingMessage,
|
|
58
|
+
base: string
|
|
59
|
+
) => string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declare module "fastify" {
|
|
63
|
+
interface FastifyReply {
|
|
64
|
+
from(
|
|
65
|
+
source?: string,
|
|
66
|
+
opts?: FastifyReplyFromHooks
|
|
67
|
+
): void;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface Http2Options {
|
|
72
|
+
sessionTimeout?: number;
|
|
73
|
+
requestTimeout?: number;
|
|
74
|
+
sessionOptions?: ClientSessionOptions | SecureClientSessionOptions;
|
|
75
|
+
requestOptions?: ClientSessionRequestOptions;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface HttpOptions {
|
|
79
|
+
agentOptions?: AgentOptions | SecureAgentOptions;
|
|
80
|
+
requestOptions?: RequestOptions | SecureRequestOptions;
|
|
81
|
+
agents?: { 'http:': Agent, 'https:': SecureAgent }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface FastifyReplyFromOptions {
|
|
85
|
+
base?: string;
|
|
86
|
+
cacheURLs?: number;
|
|
87
|
+
disableCache?: boolean;
|
|
88
|
+
http?: HttpOptions;
|
|
89
|
+
http2?: Http2Options | boolean;
|
|
90
|
+
undici?: Pool.Options;
|
|
91
|
+
contentTypesToEncode?: string[];
|
|
92
|
+
retryMethods?: (HTTPMethods | 'TRACE')[];
|
|
93
|
+
maxRetriesOn503?: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
declare const fastifyReplyFrom:
|
|
97
|
+
| FastifyPluginCallback<FastifyReplyFromOptions>
|
|
98
|
+
| FastifyPluginAsync<FastifyReplyFromOptions>;
|
|
99
|
+
export default fastifyReplyFrom;
|