Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

fix: check for flushed response headers during proxy request errors #88

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ function fastProxy (opts = {}) {
}
request(reqParams, (err, response) => {
if (err) {
if (!res.sent) {
// check if response has already been sent and all data has been flushed
// before configuring error response headers
if (res.sent === false || res.writableFinished === false) {
if (err.code === 'ECONNREFUSED' || err.code === 'ERR_HTTP2_STREAM_CANCEL') {
res.statusCode = 503
res.end('Service Unavailable')
Expand Down
11 changes: 11 additions & 0 deletions test/1.smoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ describe('fast-proxy smoke', () => {
gateway.use(bodyParser.urlencoded({ extended: true }))
gateway.use(bodyParser.text())

gateway.get('/service/flushed', function (req, res) {
res.end()
proxy(req, res, req.url, {})
})

gateway.all('/service/*', function (req, res) {
proxy(req, res, req.url, {})
})
Expand All @@ -44,6 +49,12 @@ describe('fast-proxy smoke', () => {
.expect(503)
})

it('should 200 when proxy errors after response has been sent', async () => {
await request(gHttpServer)
.get('/service/flushed')
.expect(200)
})

it('init & start remote service', async () => {
// init remote service
service = require('restana')()
Expand Down