diff --git a/CHANGELOG.md b/CHANGELOG.md index d41a024..2fea383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fix +- `res.write()` now returns the underlying write's boolean result, restoring backpressure signalling for streaming/SSE responses [@IanMcNelly](https://github.com/IanMcNelly). + + ## [4.0.3] - 2025-04-14 ### Fix @@ -55,4 +61,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed support for objects configuration in array fields from [@ugolas](https://github.com/ugolas). - Fixed vulnerabilities from [@ugolas](https://github.com/ugolas). - Replaced coverage reporter from istanbul to nyc from [@ugolas](https://github.com/ugolas). -- Removed support for node 6 from [@ugolas](https://github.com/ugolas). \ No newline at end of file +- Removed support for node 6 from [@ugolas](https://github.com/ugolas). diff --git a/lib/express-logger.js b/lib/express-logger.js index 193f04f..d832d71 100644 --- a/lib/express-logger.js +++ b/lib/express-logger.js @@ -21,7 +21,7 @@ var audit = function (req, res, next) { res.write = function (chunk) { chunks.push(Buffer.from(chunk)); - oldWrite.apply(res, arguments); + return oldWrite.apply(res, arguments); }; // decorate response#json method from express @@ -107,4 +107,4 @@ function setBodyLengthFields(options) { const isValid = field => field && !isNaN(field) && field > 0; options.request.maxBodyLength = !isValid(options.request.maxBodyLength) ? undefined : options.request.maxBodyLength; options.response.maxBodyLength = !isValid(options.response.maxBodyLength) ? undefined : options.response.maxBodyLength; -} \ No newline at end of file +} diff --git a/test/express-logger-test.js b/test/express-logger-test.js index a4ee0e9..66a89af 100644 --- a/test/express-logger-test.js +++ b/test/express-logger-test.js @@ -201,6 +201,22 @@ describe('express-logger tests', function(){ should(auditResponseStub.calledOnce).eql(true); should(res._bodyStr).eql('chunk'); }); + it('Should return the underlying write() return value to preserve backpressure', function(){ + var auditMethod = expressLogger(); + // Start request + auditMethod(req, res, next); + should(next.calledOnce).eql(true); + + // Underlying write() signals its buffer is full + resWriteStub.returns(false); + should(res.write('chunk')).eql(false); + + // ... and that it has drained + resWriteStub.returns(true); + should(res.write('chunk')).eql(true); + + should(resWriteStub.calledTwice).eql(true); + }); it('Should add body from end chunk to response', function(){ var auditMethod = expressLogger(); // Start request @@ -236,4 +252,4 @@ describe('express-logger tests', function(){ should(res).eql(false); }) }); -}); \ No newline at end of file +});