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

Commit 2aecb29

Browse files
jcbainJames Sansbury
andauthored
fix: check for flushed response headers during proxy request errors (#88)
* fix: prevent response from being sent if all res headers have been flushed (#86) * test: add test to assert that a response is not resent after res headers have been flushed (#86) * fix: remove whitespace * docs: add comment to err conditional in request callback * fix: perform explicit check on response attributes Co-authored-by: James Sansbury <james@lullabot.com> * fix linting errors --------- Co-authored-by: James Sansbury <james@lullabot.com>
1 parent 58cc5eb commit 2aecb29

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ function fastProxy (opts = {}) {
8888
}
8989
request(reqParams, (err, response) => {
9090
if (err) {
91-
if (!res.sent) {
91+
// check if response has already been sent and all data has been flushed
92+
// before configuring error response headers
93+
if (res.sent === false || res.writableFinished === false) {
9294
if (err.code === 'ECONNREFUSED' || err.code === 'ERR_HTTP2_STREAM_CANCEL') {
9395
res.statusCode = 503
9496
res.end('Service Unavailable')

test/1.smoke.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ describe('fast-proxy smoke', () => {
2525
gateway.use(bodyParser.urlencoded({ extended: true }))
2626
gateway.use(bodyParser.text())
2727

28+
gateway.get('/service/flushed', function (req, res) {
29+
res.end()
30+
proxy(req, res, req.url, {})
31+
})
32+
2833
gateway.all('/service/*', function (req, res) {
2934
proxy(req, res, req.url, {})
3035
})
@@ -44,6 +49,12 @@ describe('fast-proxy smoke', () => {
4449
.expect(503)
4550
})
4651

52+
it('should 200 when proxy errors after response has been sent', async () => {
53+
await request(gHttpServer)
54+
.get('/service/flushed')
55+
.expect(200)
56+
})
57+
4758
it('init & start remote service', async () => {
4859
// init remote service
4960
service = require('restana')()

0 commit comments

Comments
 (0)