@fastify/reply-from 9.2.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/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/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",
|
|
@@ -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
|
+
})
|