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()