Skip to content
Open
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
6 changes: 6 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
85 changes: 85 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down