From 9d49e4a87064c73d8fe430055a1f17203a93c51e Mon Sep 17 00:00:00 2001 From: Prince Jain Date: Thu, 22 Oct 2026 21:20:40 +0530 Subject: [PATCH] fix(response): send raw bytes of non-Uint8Array typed arrays in res.send() A non-Buffer ArrayBuffer view reached res.end() unconverted, which 500s on small payloads, or passed through Buffer.from(view), which writes one byte per element instead of the view's bytes, silently truncating values like 0x0102 to 0x02. Wrap any non-Buffer view in a Buffer sharing its memory, so Content-Length, ETag and the body all agree on the raw bytes. Fixes #7538 --- lib/response.js | 6 ++++ test/res.send.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/lib/response.js b/lib/response.js index 0d5a4ae4d4d..bd293717bb2 100644 --- a/lib/response.js +++ b/lib/response.js @@ -167,6 +167,12 @@ res.send = function send(body) { if (Buffer.isBuffer(chunk)) { // get length of Buffer len = chunk.length + } else if (ArrayBuffer.isView(chunk)) { + // wrap non-Buffer views (TypedArrays, DataView) in a Buffer + // sharing their memory so the raw bytes are sent as-is + chunk = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength) + encoding = undefined; + len = chunk.length } else if (!generateETag && chunk.length < 1000) { // just calculate length when no ETag + small chunk len = Buffer.byteLength(chunk, encoding) diff --git a/test/res.send.js b/test/res.send.js index e765c8c60c1..8f53531c89d 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -217,6 +217,91 @@ describe('res', function(){ .expect(200, "hey", done); }) + it('should send the bytes of other typed arrays', function(done){ + var app = express(); + + app.use(function(req, res){ + res.send(new Uint16Array(new Uint8Array([0x02, 0x01, 0x04, 0x03]).buffer)); + }) + + request(app) + .get("/") + .expect("Content-Type", "application/octet-stream") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([0x02, 0x01, 0x04, 0x03])); + }) + .end(done); + }) + + it('should send the bytes of other typed arrays when etag is disabled', function(done){ + var app = express(); + app.set('etag', false); + + app.use(function(req, res){ + res.send(new Uint16Array(new Uint8Array([0x02, 0x01, 0x04, 0x03]).buffer)); + }) + + request(app) + .get("/") + .expect("Content-Type", "application/octet-stream") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([0x02, 0x01, 0x04, 0x03])); + }) + .end(done); + }) + + it('should respect byteOffset and byteLength on typed array and DataView slices', function(done){ + var app = express(); + var raw = new Uint8Array([0xaa, 0xbb, 0x02, 0x01, 0x04, 0x03, 0xcc, 0xdd]).buffer; + + app.get("/typed", function(req, res){ + res.send(new Uint16Array(raw, 2, 2)); + }) + + app.get("/dataview", function(req, res){ + res.send(new DataView(raw, 1, 5)); + }) + + request(app) + .get("/typed") + .expect("Content-Type", "application/octet-stream") + .expect("Content-Length", "4") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([0x02, 0x01, 0x04, 0x03])); + }) + .end(function(err){ + if (err) return done(err); + request(app) + .get("/dataview") + .expect("Content-Type", "application/octet-stream") + .expect("Content-Length", "5") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([0xbb, 0x02, 0x01, 0x04, 0x03])); + }) + .end(done); + }); + }) + + it('should send the bytes of a DataView', function(done){ + var app = express(); + + app.use(function(req, res){ + res.send(new DataView(new Uint8Array([1, 2, 3]).buffer)); + }) + + request(app) + .get("/") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([1, 2, 3])); + }) + .end(done); + }) + it('should not override ETag', function (done) { var app = express()