Skip to content

Commit

Permalink
Merge 17980a5 into a2d934d
Browse files Browse the repository at this point in the history
  • Loading branch information
penx committed Feb 10, 2021
2 parents a2d934d + 17980a5 commit 8d9bd24
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -106,7 +106,7 @@ module.exports = fp(function from (fastify, opts, next) {
if (!this.sent) {
if (err.code === 'ERR_HTTP2_STREAM_CANCEL' || err.code === 'ENOTFOUND') {
onError(this, { error: new createError.ServiceUnavailable() })
} else if (err instanceof TimeoutError || err.code === 'UND_ERR_REQUEST_TIMEOUT') {
} else if (err instanceof TimeoutError || err.code === 'UND_ERR_HEADERS_TIMEOUT' || err.code === 'UND_ERR_BODY_TIMEOUT') {
onError(this, { error: new createError.GatewayTimeout() })
} else {
onError(this, { error: createError(500, err) })
Expand Down
6 changes: 5 additions & 1 deletion lib/request.js
Expand Up @@ -98,7 +98,8 @@ function buildRequest (opts) {
method: opts.method,
headers: Object.assign({}, opts.headers),
body: opts.body,
requestTimeout: undiciOpts.requestTimeout
headersTimeout: undiciOpts.headersTimeout,
bodyTimeout: undiciOpts.bodyTimeout
}

// remove forbidden headers
Expand All @@ -111,6 +112,9 @@ function buildRequest (opts) {
return
}

// using delete, otherwise it will render as an empty string
delete res.headers['transfer-encoding']

done(null, { statusCode: res.statusCode, headers: res.headers, stream: res.body })
})
}
Expand Down
63 changes: 63 additions & 0 deletions test/undici-timeout-body.js
@@ -0,0 +1,63 @@
'use strict'

const t = require('tap')
const http = require('http')
const Fastify = require('fastify')
const From = require('..')
const got = require('got')
const FakeTimers = require('@sinonjs/fake-timers')

const clock = FakeTimers.createClock()

t.autoend(false)

const target = http.createServer((req, res) => {
t.pass('request proxied')
req.on('data', () => undefined)
req.on('end', () => {
res.flushHeaders()
clock.setTimeout(() => {
res.end()
t.end()
}, 1000)
})
})

async function main () {
await target.listen(0)

const instance = Fastify()
t.tearDown(instance.close.bind(instance))
t.tearDown(target.close.bind(target))

instance.register(From, {
base: `http://localhost:${target.address().port}`,
undici: {
bodyTimeout: 100
}
})

instance.get('/', (request, reply) => {
reply.from()
})

await instance.listen(0)

try {
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
} catch (err) {
t.equal(err.response.statusCode, 500)
t.deepEqual(JSON.parse(err.response.body), {
statusCode: 500,
code: 'UND_ERR_BODY_TIMEOUT',
error: 'Internal Server Error',
message: 'Body Timeout Error'
})
clock.tick(1000)
return
}

t.fail()
}

main()
2 changes: 1 addition & 1 deletion test/undici-timeout.js
Expand Up @@ -31,7 +31,7 @@ async function main () {
instance.register(From, {
base: `http://localhost:${target.server.address().port}`,
undici: {
requestTimeout: 100
headersTimeout: 100
}
})

Expand Down

0 comments on commit 8d9bd24

Please sign in to comment.