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
3 changes: 3 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ res.send = function send(body) {
if (chunk === null) {
chunk = '';
} else if (ArrayBuffer.isView(chunk)) {
if (typeof DataView === 'function' && chunk instanceof DataView) {
chunk = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength)
}
if (!this.get('Content-Type')) {
this.type('bin');
}
Expand Down
47 changes: 47 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,53 @@ describe('res', function(){
.expect(200, "hey", done);
})

it('should accept DataView', function (done) {
var app = express()
var ab = new ArrayBuffer(4)
new Uint8Array(ab).set([1, 2, 3, 4])

app.use(function (req, res) {
res.send(new DataView(ab))
})

request(app)
.get('/')
.expect('Content-Type', 'application/octet-stream')
.expect(utils.shouldHaveBody(Buffer.from([1, 2, 3, 4])))
.end(done)
})

it('should honor DataView byteOffset and byteLength', function (done) {
var app = express()
var ab = new ArrayBuffer(6)
new Uint8Array(ab).set([9, 1, 2, 3, 4, 8])

app.use(function (req, res) {
res.send(new DataView(ab, 1, 4))
})

request(app)
.get('/')
.expect('Content-Type', 'application/octet-stream')
.expect(utils.shouldHaveBody(Buffer.from([1, 2, 3, 4])))
.end(done)
})

it('should keep existing non-byte typed array payload', function (done) {
var app = express()

app.use(function (req, res) {
res.send(new Uint16Array([0x0102, 0x0304]))
})

request(app)
.get('/')
.expect('Content-Type', 'application/octet-stream')
.expect('Content-Length', '2')
.expect(utils.shouldHaveBody(Buffer.from([0x02, 0x04])))
.end(done)
})

it('should not override ETag', function (done) {
var app = express()

Expand Down