@fastify/reply-from 8.4.0 → 8.4.1
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 +2 -2
- package/lib/utils.js +26 -0
- package/package.json +1 -1
- package/test/undici-chaining.js +57 -0
package/lib/request.js
CHANGED
|
@@ -5,7 +5,7 @@ const querystring = require('querystring')
|
|
|
5
5
|
const eos = require('end-of-stream')
|
|
6
6
|
const pump = require('pump')
|
|
7
7
|
const undici = require('undici')
|
|
8
|
-
const { stripHttp1ConnectionHeaders } = require('./utils')
|
|
8
|
+
const { patchUndiciHeaders, stripHttp1ConnectionHeaders } = require('./utils')
|
|
9
9
|
const http2 = require('http2')
|
|
10
10
|
|
|
11
11
|
const {
|
|
@@ -158,7 +158,7 @@ function buildRequest (opts) {
|
|
|
158
158
|
// using delete, otherwise it will render as an empty string
|
|
159
159
|
delete res.headers['transfer-encoding']
|
|
160
160
|
|
|
161
|
-
done(null, { statusCode: res.statusCode, headers: res.headers, stream: res.body })
|
|
161
|
+
done(null, { statusCode: res.statusCode, headers: patchUndiciHeaders(res.headers), stream: res.body })
|
|
162
162
|
})
|
|
163
163
|
}
|
|
164
164
|
|
package/lib/utils.js
CHANGED
|
@@ -12,6 +12,31 @@ function filterPseudoHeaders (headers) {
|
|
|
12
12
|
return dest
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
// http requires the header to be encoded with latin1
|
|
16
|
+
// undici will convert the latin1 buffer to utf8
|
|
17
|
+
// https://github.com/nodejs/undici/blob/2b260c997ad4efe4ed2064b264b4b546a59e7a67/lib/core/util.js#L216-L229
|
|
18
|
+
// after chaining, the header will be serialised using wrong encoding
|
|
19
|
+
// Buffer.from('', 'latin1').toString('utf8') applied
|
|
20
|
+
//
|
|
21
|
+
// in order to persist the encoding, always encode it
|
|
22
|
+
// back to latin1
|
|
23
|
+
function patchUndiciHeaders (headers) {
|
|
24
|
+
const headersKeys = Object.keys(headers)
|
|
25
|
+
const dist = {}
|
|
26
|
+
|
|
27
|
+
let header
|
|
28
|
+
let i
|
|
29
|
+
|
|
30
|
+
for (i = 0; i < headersKeys.length; i++) {
|
|
31
|
+
header = headersKeys[i]
|
|
32
|
+
if (header.charCodeAt(0) !== 58) { // fast path for indexOf(':') === 0
|
|
33
|
+
dist[header] = Buffer.from(headers[header]).toString('latin1')
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return dist
|
|
38
|
+
}
|
|
39
|
+
|
|
15
40
|
function copyHeaders (headers, reply) {
|
|
16
41
|
const headersKeys = Object.keys(headers)
|
|
17
42
|
|
|
@@ -74,6 +99,7 @@ function buildURL (source, reqBase) {
|
|
|
74
99
|
}
|
|
75
100
|
|
|
76
101
|
module.exports = {
|
|
102
|
+
patchUndiciHeaders,
|
|
77
103
|
copyHeaders,
|
|
78
104
|
stripHttp1ConnectionHeaders,
|
|
79
105
|
filterPseudoHeaders,
|
package/package.json
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const get = require('simple-get').concat
|
|
6
|
+
const From = require('..')
|
|
7
|
+
|
|
8
|
+
const header = 'attachment; filename="år.pdf"'
|
|
9
|
+
|
|
10
|
+
t.plan(6)
|
|
11
|
+
|
|
12
|
+
const instance = Fastify()
|
|
13
|
+
t.teardown(instance.close.bind(instance))
|
|
14
|
+
const proxy1 = Fastify()
|
|
15
|
+
t.teardown(proxy1.close.bind(proxy1))
|
|
16
|
+
const proxy2 = Fastify()
|
|
17
|
+
t.teardown(proxy2.close.bind(proxy2))
|
|
18
|
+
|
|
19
|
+
instance.get('/', (request, reply) => {
|
|
20
|
+
reply.header('content-disposition', header).send('OK')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
proxy1.register(From, {
|
|
24
|
+
undici: {
|
|
25
|
+
keepAliveMaxTimeout: 10
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
proxy1.get('/', (request, reply) => {
|
|
29
|
+
return reply.from(`http://localhost:${instance.server.address().port}`)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
proxy2.register(From, {
|
|
33
|
+
undici: {
|
|
34
|
+
keepAliveMaxTimeout: 10
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
proxy2.get('/', (request, reply) => {
|
|
38
|
+
return reply.from(`http://localhost:${proxy1.server.address().port}`)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
instance.listen({ port: 0 }, err => {
|
|
42
|
+
t.error(err)
|
|
43
|
+
|
|
44
|
+
proxy1.listen({ port: 0 }, err => {
|
|
45
|
+
t.error(err)
|
|
46
|
+
|
|
47
|
+
proxy2.listen({ port: 0 }, err => {
|
|
48
|
+
t.error(err)
|
|
49
|
+
|
|
50
|
+
get(`http://localhost:${proxy2.server.address().port}`, (err, res, data) => {
|
|
51
|
+
t.error(err)
|
|
52
|
+
t.equal(res.statusCode, 200)
|
|
53
|
+
t.equal(data.toString(), 'OK')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
})
|