@fastify/reply-from 9.1.0 → 9.3.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 +2 -2
- package/lib/request.js +8 -1
- package/lib/utils.js +6 -0
- package/package.json +2 -2
- package/test/build-url.test.js +9 -2
- package/test/undici-custom-dispatcher.test.js +73 -0
package/README.md
CHANGED
|
@@ -246,9 +246,9 @@ fastify.register(FastifyReplyFrom, {
|
|
|
246
246
|
|
|
247
247
|
---
|
|
248
248
|
|
|
249
|
-
#### `
|
|
249
|
+
#### `destroyAgent`
|
|
250
250
|
|
|
251
|
-
If set to `true`, it will
|
|
251
|
+
If set to `true`, it will destroy all agents when the Fastify is closed.
|
|
252
252
|
If set to `false`, it will not destroy the agents.
|
|
253
253
|
|
|
254
254
|
By Default: `false`
|
package/lib/request.js
CHANGED
|
@@ -22,10 +22,17 @@ function shouldUseUndici (opts) {
|
|
|
22
22
|
return true
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
function isRequestable (obj) {
|
|
26
|
+
return obj !== null &&
|
|
27
|
+
typeof obj === 'object' &&
|
|
28
|
+
typeof obj.request === 'function'
|
|
29
|
+
}
|
|
30
|
+
|
|
25
31
|
function isUndiciInstance (obj) {
|
|
26
32
|
return obj instanceof undici.Pool ||
|
|
27
33
|
obj instanceof undici.Client ||
|
|
28
|
-
obj instanceof undici.Dispatcher
|
|
34
|
+
obj instanceof undici.Dispatcher ||
|
|
35
|
+
isRequestable(obj)
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
function buildRequest (opts) {
|
package/lib/utils.js
CHANGED
|
@@ -57,6 +57,12 @@ function stripHttp1ConnectionHeaders (headers) {
|
|
|
57
57
|
// issue ref: https://github.com/fastify/fast-proxy/issues/42
|
|
58
58
|
function buildURL (source, reqBase) {
|
|
59
59
|
let baseOrigin = reqBase ? new URL(reqBase).href : undefined
|
|
60
|
+
|
|
61
|
+
// To make sure we don't accidentally override the base path
|
|
62
|
+
if (baseOrigin && source.length > 1 && source[0] === '/' && source[1] === '/') {
|
|
63
|
+
source = '.' + source
|
|
64
|
+
}
|
|
65
|
+
|
|
60
66
|
const dest = new URL(source, reqBase)
|
|
61
67
|
|
|
62
68
|
// if base is specified, source url should not override it
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/reply-from",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.3.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",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@fastify/formbody": "^7.4.0",
|
|
32
32
|
"@fastify/multipart": "^7.4.0",
|
|
33
33
|
"@fastify/pre-commit": "^2.0.2",
|
|
34
|
-
"@sinonjs/fake-timers": "^
|
|
34
|
+
"@sinonjs/fake-timers": "^11.0.0",
|
|
35
35
|
"@types/node": "^20.1.4",
|
|
36
36
|
"@types/tap": "^15.0.7",
|
|
37
37
|
"fastify": "^4.0.2",
|
package/test/build-url.test.js
CHANGED
|
@@ -42,12 +42,19 @@ test('should handle default port in base', (t) => {
|
|
|
42
42
|
t.equal(url.href, 'https://localhost/hi')
|
|
43
43
|
})
|
|
44
44
|
|
|
45
|
+
test('should append instead of override base', (t) => {
|
|
46
|
+
t.plan(2)
|
|
47
|
+
let url = buildURL('//10.0.0.10/hi', 'http://localhost')
|
|
48
|
+
t.equal(url.href, 'http://localhost//10.0.0.10/hi')
|
|
49
|
+
|
|
50
|
+
url = buildURL('//httpbin.org/hi', 'http://localhost')
|
|
51
|
+
t.equal(url.href, 'http://localhost//httpbin.org/hi')
|
|
52
|
+
})
|
|
53
|
+
|
|
45
54
|
const errorInputs = [
|
|
46
|
-
{ source: '//10.0.0.10/hi', base: 'http://localhost' },
|
|
47
55
|
{ source: 'http://10.0.0.10/hi', base: 'http://localhost' },
|
|
48
56
|
{ source: 'https://10.0.0.10/hi', base: 'http://localhost' },
|
|
49
57
|
{ source: 'blah://10.0.0.10/hi', base: 'http://localhost' },
|
|
50
|
-
{ source: '//httpbin.org/hi', base: 'http://localhost' },
|
|
51
58
|
{ source: 'urn:foo:bar', base: 'http://localhost' },
|
|
52
59
|
{ source: 'http://localhost/private', base: 'http://localhost/exposed/' },
|
|
53
60
|
{ source: 'http://localhost/exposed-extra', base: 'http://localhost/exposed' },
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const undici = require('undici')
|
|
5
|
+
const Fastify = require('fastify')
|
|
6
|
+
const From = require('..')
|
|
7
|
+
|
|
8
|
+
class CustomDispatcher {
|
|
9
|
+
constructor (...args) {
|
|
10
|
+
this._dispatcher = new undici.Pool(...args)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
request (...args) {
|
|
14
|
+
return this._dispatcher.request(...args)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
close (...args) {
|
|
18
|
+
return this._dispatcher.close(...args)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
destroy (...args) {
|
|
22
|
+
return this._dispatcher.destroy(...args)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('use a custom instance of \'undici\'', async t => {
|
|
27
|
+
const target = Fastify({
|
|
28
|
+
keepAliveTimeout: 1
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
target.get('/', (req, reply) => {
|
|
32
|
+
t.pass('request proxied')
|
|
33
|
+
|
|
34
|
+
reply.headers({
|
|
35
|
+
'Content-Type': 'text/plain',
|
|
36
|
+
'x-my-header': 'hello!'
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
reply.statusCode = 205
|
|
40
|
+
reply.send('hello world')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
await target.listen({ port: 3001 })
|
|
44
|
+
t.teardown(async () => {
|
|
45
|
+
await target.close()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const instance = Fastify({
|
|
49
|
+
keepAliveTimeout: 1
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
instance.register(From, {
|
|
53
|
+
undici: new CustomDispatcher('http://localhost:3001')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
instance.get('/', (request, reply) => {
|
|
57
|
+
reply.from('http://myserver.local')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
await instance.listen({ port: 0 })
|
|
61
|
+
t.teardown(async () => {
|
|
62
|
+
await instance.close()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const res = await undici.request(`http://localhost:${instance.server.address().port}`)
|
|
66
|
+
|
|
67
|
+
t.equal(res.headers['content-type'], 'text/plain')
|
|
68
|
+
t.equal(res.headers['x-my-header'], 'hello!')
|
|
69
|
+
t.equal(res.statusCode, 205)
|
|
70
|
+
|
|
71
|
+
const data = await res.body.text()
|
|
72
|
+
t.equal(data, 'hello world')
|
|
73
|
+
})
|