@fastify/reply-from 9.0.1 → 10.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/workflows/ci.yml
CHANGED
package/README.md
CHANGED
|
@@ -312,6 +312,56 @@ Helpful for a gradual rollout of new services.
|
|
|
312
312
|
Parameters are the Fastify request and the base string from the plugin options.
|
|
313
313
|
It must return the upstream destination.
|
|
314
314
|
|
|
315
|
+
Only http1! As http2 uses one connection for the whole session only the base upstream is used. If you want to
|
|
316
|
+
have different upstreams based on the request you can add multiple Fastify.register's with different
|
|
317
|
+
ContraintStrategies.
|
|
318
|
+
|
|
319
|
+
e.g.:
|
|
320
|
+
|
|
321
|
+
Route grpc-web/http1 and grpc/http2 to different routes with a ContentType-ConstraintStrategy:
|
|
322
|
+
```
|
|
323
|
+
const contentTypeMatchContraintStrategy = {
|
|
324
|
+
// strategy name for referencing in the route handler `constraints` options
|
|
325
|
+
name: 'contentType',
|
|
326
|
+
// storage factory for storing routes in the find-my-way route tree
|
|
327
|
+
storage: function () {
|
|
328
|
+
let handlers = {}
|
|
329
|
+
return {
|
|
330
|
+
get: (type: any) => { return handlers[type] || null },
|
|
331
|
+
set: (type: any, store: any) => { handlers[type] = store }
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
// function to get the value of the constraint from each incoming request
|
|
335
|
+
deriveConstraint: (req: any, ctx: any) => {
|
|
336
|
+
return req.headers['content-type']
|
|
337
|
+
},
|
|
338
|
+
// optional flag marking if handlers without constraints can match requests that have a value for this constraint
|
|
339
|
+
mustMatchWhenDerived: true
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
server.addConstraintStrategy(contentTypeMatchContraintStrategy);
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
and then 2 different upstreams with different register's:
|
|
346
|
+
```
|
|
347
|
+
// grpc-web / http1
|
|
348
|
+
server.register(fastifyHttpProxy, {
|
|
349
|
+
// Although most browsers send with http2, nodejs cannot handle this http2 request
|
|
350
|
+
// therefore we have to transport to the grpc-web-proxy via http1
|
|
351
|
+
http2: false,
|
|
352
|
+
upstream: 'http://grpc-web-proxy',
|
|
353
|
+
constraints: { "contentType": "application/grpc-web+proto" }
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// grpc / http2
|
|
357
|
+
server.register(fastifyHttpProxy, {
|
|
358
|
+
http2: true,
|
|
359
|
+
upstream: 'http://grpc.server',
|
|
360
|
+
constraints: { "contentType": "application/grpc+proto" }
|
|
361
|
+
});
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
|
|
315
365
|
#### `queryString` or `queryString(search, reqUrl)`
|
|
316
366
|
|
|
317
367
|
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')
|
|
@@ -157,6 +158,8 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
|
|
|
157
158
|
onError(this, { error: new ConnectionResetError() })
|
|
158
159
|
} else if (err.code === 'UND_ERR_SOCKET') {
|
|
159
160
|
onError(this, { error: new UndiciSocketError() })
|
|
161
|
+
} else if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
|
|
162
|
+
onError(this, { error: new ConnectTimeoutError() })
|
|
160
163
|
} else {
|
|
161
164
|
onError(this, { error: new InternalServerError(err.message) })
|
|
162
165
|
}
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/reply-from",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.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",
|
|
@@ -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.28.0"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@fastify/error": "^3.0.0",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"fast-querystring": "^1.0.0",
|
|
55
55
|
"fastify-plugin": "^4.0.0",
|
|
56
56
|
"pump": "^3.0.0",
|
|
57
|
-
"tiny-lru": "^
|
|
57
|
+
"tiny-lru": "^11.0.0",
|
|
58
58
|
"undici": "^5.19.1"
|
|
59
59
|
},
|
|
60
60
|
"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: {
|
|
@@ -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()
|