@fastify/reply-from 9.0.1 → 9.1.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/workflows/ci.yml +5 -0
- package/README.md +59 -0
- package/index.js +5 -1
- package/lib/errors.js +1 -0
- package/lib/request.js +2 -1
- package/package.json +5 -4
- package/test/core-with-path-in-base.test.js +4 -1
- package/test/disable-request-logging.test.js +1 -3
- package/test/head-with-body.test.js +6 -0
- package/test/undici-connect-timeout.test.js +58 -0
- package/test/undici-no-destroy.test.js +29 -0
- package/types/index.d.ts +1 -0
- package/types/index.test-d.ts +1 -0
package/.github/workflows/ci.yml
CHANGED
package/README.md
CHANGED
|
@@ -246,6 +246,15 @@ fastify.register(FastifyReplyFrom, {
|
|
|
246
246
|
|
|
247
247
|
---
|
|
248
248
|
|
|
249
|
+
#### `destoryAgent`
|
|
250
|
+
|
|
251
|
+
If set to `true`, it will destory all agents when the Fastify is closed.
|
|
252
|
+
If set to `false`, it will not destroy the agents.
|
|
253
|
+
|
|
254
|
+
By Default: `false`
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
249
258
|
#### `maxRetriesOn503`
|
|
250
259
|
|
|
251
260
|
This plugin will always retry on `GET` requests that returns 503 errors, _unless_ `retryMethods` does not contain `GET`.
|
|
@@ -312,6 +321,56 @@ Helpful for a gradual rollout of new services.
|
|
|
312
321
|
Parameters are the Fastify request and the base string from the plugin options.
|
|
313
322
|
It must return the upstream destination.
|
|
314
323
|
|
|
324
|
+
Only http1! As http2 uses one connection for the whole session only the base upstream is used. If you want to
|
|
325
|
+
have different upstreams based on the request you can add multiple Fastify.register's with different
|
|
326
|
+
ContraintStrategies.
|
|
327
|
+
|
|
328
|
+
e.g.:
|
|
329
|
+
|
|
330
|
+
Route grpc-web/http1 and grpc/http2 to different routes with a ContentType-ConstraintStrategy:
|
|
331
|
+
```
|
|
332
|
+
const contentTypeMatchContraintStrategy = {
|
|
333
|
+
// strategy name for referencing in the route handler `constraints` options
|
|
334
|
+
name: 'contentType',
|
|
335
|
+
// storage factory for storing routes in the find-my-way route tree
|
|
336
|
+
storage: function () {
|
|
337
|
+
let handlers = {}
|
|
338
|
+
return {
|
|
339
|
+
get: (type: any) => { return handlers[type] || null },
|
|
340
|
+
set: (type: any, store: any) => { handlers[type] = store }
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
// function to get the value of the constraint from each incoming request
|
|
344
|
+
deriveConstraint: (req: any, ctx: any) => {
|
|
345
|
+
return req.headers['content-type']
|
|
346
|
+
},
|
|
347
|
+
// optional flag marking if handlers without constraints can match requests that have a value for this constraint
|
|
348
|
+
mustMatchWhenDerived: true
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
server.addConstraintStrategy(contentTypeMatchContraintStrategy);
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
and then 2 different upstreams with different register's:
|
|
355
|
+
```
|
|
356
|
+
// grpc-web / http1
|
|
357
|
+
server.register(fastifyHttpProxy, {
|
|
358
|
+
// Although most browsers send with http2, nodejs cannot handle this http2 request
|
|
359
|
+
// therefore we have to transport to the grpc-web-proxy via http1
|
|
360
|
+
http2: false,
|
|
361
|
+
upstream: 'http://grpc-web-proxy',
|
|
362
|
+
constraints: { "contentType": "application/grpc-web+proto" }
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
// grpc / http2
|
|
366
|
+
server.register(fastifyHttpProxy, {
|
|
367
|
+
http2: true,
|
|
368
|
+
upstream: 'http://grpc.server',
|
|
369
|
+
constraints: { "contentType": "application/grpc+proto" }
|
|
370
|
+
});
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
|
|
315
374
|
#### `queryString` or `queryString(search, reqUrl)`
|
|
316
375
|
|
|
317
376
|
Replaces the original querystring of the request with what is specified.
|
package/index.js
CHANGED
|
@@ -17,6 +17,7 @@ const {
|
|
|
17
17
|
ServiceUnavailableError,
|
|
18
18
|
GatewayTimeoutError,
|
|
19
19
|
ConnectionResetError,
|
|
20
|
+
ConnectTimeoutError,
|
|
20
21
|
UndiciSocketError,
|
|
21
22
|
InternalServerError
|
|
22
23
|
} = require('./lib/errors')
|
|
@@ -38,7 +39,8 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
38
39
|
http2: opts.http2,
|
|
39
40
|
base,
|
|
40
41
|
undici: opts.undici,
|
|
41
|
-
globalAgent: opts.globalAgent
|
|
42
|
+
globalAgent: opts.globalAgent,
|
|
43
|
+
destroyAgent: opts.destroyAgent
|
|
42
44
|
})
|
|
43
45
|
if (requestBuilt instanceof Error) {
|
|
44
46
|
next(requestBuilt)
|
|
@@ -157,6 +159,8 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
157
159
|
onError(this, { error: new ConnectionResetError() })
|
|
158
160
|
} else if (err.code === 'UND_ERR_SOCKET') {
|
|
159
161
|
onError(this, { error: new UndiciSocketError() })
|
|
162
|
+
} else if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
|
|
163
|
+
onError(this, { error: new ConnectTimeoutError() })
|
|
160
164
|
} else {
|
|
161
165
|
onError(this, { error: new InternalServerError(err.message) })
|
|
162
166
|
}
|
package/lib/errors.js
CHANGED
|
@@ -9,5 +9,6 @@ module.exports.Http2SessionTimeoutError = createError('FST_REPLY_FROM_HTTP2_SESS
|
|
|
9
9
|
module.exports.ServiceUnavailableError = createError('FST_REPLY_FROM_SERVICE_UNAVAILABLE', 'Service Unavailable', 503)
|
|
10
10
|
module.exports.GatewayTimeoutError = createError('FST_REPLY_FROM_GATEWAY_TIMEOUT', 'Gateway Timeout', 504)
|
|
11
11
|
module.exports.ConnectionResetError = createError('ECONNRESET', 'Connection Reset', 500)
|
|
12
|
+
module.exports.ConnectTimeoutError = createError('UND_ERR_CONNECT_TIMEOUT', 'Connect Timeout Error', 500)
|
|
12
13
|
module.exports.UndiciSocketError = createError('UND_ERR_SOCKET', 'Undici Socket Error', 500)
|
|
13
14
|
module.exports.InternalServerError = createError('FST_REPLY_FROM_INTERNAL_SERVER_ERROR', '%s', 500)
|
package/lib/request.js
CHANGED
|
@@ -42,6 +42,7 @@ function buildRequest (opts) {
|
|
|
42
42
|
const baseUrl = opts.base && new URL(opts.base).origin
|
|
43
43
|
const undiciOpts = opts.undici || {}
|
|
44
44
|
const globalAgent = opts.globalAgent
|
|
45
|
+
const destroyAgent = opts.destroyAgent
|
|
45
46
|
let http2Client
|
|
46
47
|
let undiciAgent
|
|
47
48
|
let undiciInstance
|
|
@@ -85,7 +86,7 @@ function buildRequest (opts) {
|
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
function close () {
|
|
88
|
-
if (globalAgent) {
|
|
89
|
+
if (globalAgent || destroyAgent === false) {
|
|
89
90
|
return
|
|
90
91
|
}
|
|
91
92
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/reply-from",
|
|
3
|
-
"version": "9.0
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"description": "forward your HTTP request to another server, for fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"@fastify/multipart": "^7.4.0",
|
|
33
33
|
"@fastify/pre-commit": "^2.0.2",
|
|
34
34
|
"@sinonjs/fake-timers": "^10.0.0",
|
|
35
|
-
"@types/node": "^
|
|
35
|
+
"@types/node": "^20.1.4",
|
|
36
36
|
"@types/tap": "^15.0.7",
|
|
37
37
|
"fastify": "^4.0.2",
|
|
38
38
|
"form-data": "^4.0.0",
|
|
@@ -41,12 +41,13 @@
|
|
|
41
41
|
"msgpack5": "^6.0.1",
|
|
42
42
|
"nock": "^13.2.6",
|
|
43
43
|
"proxyquire": "^2.1.3",
|
|
44
|
+
"semver": "^7.5.1",
|
|
44
45
|
"simple-get": "^4.0.1",
|
|
45
46
|
"snazzy": "^9.0.0",
|
|
46
47
|
"split2": "^4.1.0",
|
|
47
48
|
"standard": "^17.0.0",
|
|
48
49
|
"tap": "^16.2.0",
|
|
49
|
-
"tsd": "^0.
|
|
50
|
+
"tsd": "^0.28.0"
|
|
50
51
|
},
|
|
51
52
|
"dependencies": {
|
|
52
53
|
"@fastify/error": "^3.0.0",
|
|
@@ -54,7 +55,7 @@
|
|
|
54
55
|
"fast-querystring": "^1.0.0",
|
|
55
56
|
"fastify-plugin": "^4.0.0",
|
|
56
57
|
"pump": "^3.0.0",
|
|
57
|
-
"tiny-lru": "^
|
|
58
|
+
"tiny-lru": "^11.0.0",
|
|
58
59
|
"undici": "^5.19.1"
|
|
59
60
|
},
|
|
60
61
|
"pre-commit": [
|
|
@@ -39,7 +39,10 @@ target.listen({ port: 0 }, (err) => {
|
|
|
39
39
|
instance.listen({ port: 0 }, (err) => {
|
|
40
40
|
t.error(err)
|
|
41
41
|
|
|
42
|
-
get(
|
|
42
|
+
get({
|
|
43
|
+
url: `http://localhost:${instance.server.address().port}`,
|
|
44
|
+
agent: false
|
|
45
|
+
}, (err, res, data) => {
|
|
43
46
|
t.error(err)
|
|
44
47
|
t.equal(res.headers['content-type'], 'text/plain')
|
|
45
48
|
t.equal(res.headers['x-my-header'], 'hello!')
|
|
@@ -19,12 +19,12 @@ const target = http.createServer((req, res) => {
|
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
t.test('use a custom instance of \'undici\'', async t => {
|
|
22
|
+
t.plan(3)
|
|
22
23
|
t.teardown(target.close.bind(target))
|
|
23
24
|
|
|
24
25
|
await new Promise((resolve, reject) => target.listen({ port: 0 }, err => err ? reject(err) : resolve()))
|
|
25
26
|
|
|
26
27
|
t.test('disableRequestLogging is set to true', t => {
|
|
27
|
-
t.plan(10)
|
|
28
28
|
const logStream = split(JSON.parse)
|
|
29
29
|
const instance = Fastify({
|
|
30
30
|
logger: {
|
|
@@ -69,7 +69,6 @@ t.test('use a custom instance of \'undici\'', async t => {
|
|
|
69
69
|
})
|
|
70
70
|
|
|
71
71
|
t.test('disableRequestLogging is set to false', t => {
|
|
72
|
-
t.plan(8)
|
|
73
72
|
const logStream = split(JSON.parse)
|
|
74
73
|
const instance = Fastify({
|
|
75
74
|
logger: {
|
|
@@ -114,7 +113,6 @@ t.test('use a custom instance of \'undici\'', async t => {
|
|
|
114
113
|
})
|
|
115
114
|
|
|
116
115
|
t.test('disableRequestLogging is not defined', t => {
|
|
117
|
-
t.plan(8)
|
|
118
116
|
const logStream = split(JSON.parse)
|
|
119
117
|
const instance = Fastify({
|
|
120
118
|
logger: {
|
|
@@ -5,6 +5,12 @@ const Fastify = require('fastify')
|
|
|
5
5
|
const From = require('..')
|
|
6
6
|
const http = require('http')
|
|
7
7
|
const get = require('simple-get').concat
|
|
8
|
+
const semver = require('semver')
|
|
9
|
+
|
|
10
|
+
if (semver.gte(process.version, '20.2.0')) {
|
|
11
|
+
t.comment('skip this test on node >= 20.2.0 as it is current broken')
|
|
12
|
+
process.exit(0)
|
|
13
|
+
}
|
|
8
14
|
|
|
9
15
|
const instance = Fastify()
|
|
10
16
|
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const http = require('http')
|
|
5
|
+
const net = require('net')
|
|
6
|
+
const Fastify = require('fastify')
|
|
7
|
+
const From = require('..')
|
|
8
|
+
const got = require('got')
|
|
9
|
+
|
|
10
|
+
t.autoend(false)
|
|
11
|
+
|
|
12
|
+
// never connect
|
|
13
|
+
net.connect = function (options) {
|
|
14
|
+
return new net.Socket(options)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const target = http.createServer((req, res) => {
|
|
18
|
+
t.fail('target never called')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
async function main () {
|
|
22
|
+
t.plan(2)
|
|
23
|
+
await target.listen({ port: 0 })
|
|
24
|
+
|
|
25
|
+
const instance = Fastify()
|
|
26
|
+
t.teardown(instance.close.bind(instance))
|
|
27
|
+
t.teardown(target.close.bind(target))
|
|
28
|
+
|
|
29
|
+
instance.register(From, {
|
|
30
|
+
base: `http://localhost:${target.address().port}`,
|
|
31
|
+
undici: {
|
|
32
|
+
connectTimeout: 50
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
instance.get('/', (request, reply) => {
|
|
37
|
+
reply.from()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
await instance.listen({ port: 0 })
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
|
|
44
|
+
} catch (err) {
|
|
45
|
+
t.equal(err.response.statusCode, 500)
|
|
46
|
+
t.same(JSON.parse(err.response.body), {
|
|
47
|
+
statusCode: 500,
|
|
48
|
+
code: 'UND_ERR_CONNECT_TIMEOUT',
|
|
49
|
+
error: 'Internal Server Error',
|
|
50
|
+
message: 'Connect Timeout Error'
|
|
51
|
+
})
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
t.fail()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
main()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const undici = require('undici')
|
|
6
|
+
const From = require('..')
|
|
7
|
+
|
|
8
|
+
test('destroyAgent false', async (t) => {
|
|
9
|
+
const mockAgent = new undici.Agent()
|
|
10
|
+
mockAgent.destroy = () => {
|
|
11
|
+
t.fail()
|
|
12
|
+
}
|
|
13
|
+
const instance = Fastify()
|
|
14
|
+
|
|
15
|
+
t.teardown(instance.close.bind(instance))
|
|
16
|
+
|
|
17
|
+
instance.get('/', (request, reply) => {
|
|
18
|
+
reply.from()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
instance.register(From, {
|
|
22
|
+
base: 'http://localhost:4242',
|
|
23
|
+
undici: mockAgent,
|
|
24
|
+
destroyAgent: false
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
await instance.ready()
|
|
28
|
+
await instance.close()
|
|
29
|
+
})
|
package/types/index.d.ts
CHANGED