Skip to content

Commit

Permalink
Support ArrayBuffer in BodyInit
Browse files Browse the repository at this point in the history
Fixes #350.
  • Loading branch information
tamird committed Nov 11, 2016
1 parent 87fd1cd commit 211d5ca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
25 changes: 17 additions & 8 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@
} else if (!body) {
this._bodyText = ''
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
// Only support ArrayBuffers for POST method.
// Receiving ArrayBuffers happens via Blobs, instead.
if (body.slice) {
this._bodyArrayBuffer = new Uint8Array(body.slice(0))
} else {
this._bodyArrayBuffer = new Uint8Array(body.byteLength)
this._bodyArrayBuffer.set(new Uint8Array(body))
}
} else {
throw new Error('unsupported BodyInit type')
}
Expand Down Expand Up @@ -211,10 +215,6 @@
}
}

this.arrayBuffer = function() {
return this.blob().then(readBlobAsArrayBuffer)
}

this.text = function() {
var rejected = consumed(this)
if (rejected) {
Expand All @@ -231,8 +231,17 @@
}
} else {
this.text = function() {
var rejected = consumed(this)
return rejected ? rejected : Promise.resolve(this._bodyText)
return consumed(this) || Promise.resolve(this._bodyText)
}
}

if (support.arrayBuffer) {
this.arrayBuffer = function() {
if (this._bodyArrayBuffer) {
return consumed(this) || Promise.resolve(this._bodyArrayBuffer.buffer)
} else {
return this.blob().then(readBlobAsArrayBuffer)
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,28 @@ suite('Request', function() {
})
})

featureDependent(suite, support.arrayBuffer, 'type ArrayBuffer', function() {
test('consume as array buffer', function() {
var text = 'name=Hubot'

var buf = new ArrayBuffer(text.length)
var view = new Uint8Array(buf)

for(var i = 0; i < text.length; i++) {
view[i] = text.charCodeAt(i)
}

var request = new Request('', {method: 'POST', body: buf})
return request.arrayBuffer().then(function(buffer) {
var bufView = new Uint8Array(buffer)
assert.equal(bufView.byteLength, view.length)
for(var i = 0; i < bufView.length; i++) {
assert.equal(bufView[i], view[i])
}
})
})
})

suite('type USVString', function() {
test('consume as text', function() {
var request = new Request('', {method: 'POST', body: 'hello'})
Expand Down Expand Up @@ -482,6 +504,28 @@ suite('Response', function() {
})
})

featureDependent(suite, support.arrayBuffer, 'type ArrayBuffer', function() {
test('consume as array buffer', function() {
var text = 'name=Hubot'

var buf = new ArrayBuffer(text.length)
var view = new Uint8Array(buf)

for(var i = 0; i < text.length; i++) {
view[i] = text.charCodeAt(i)
}

var response = new Response(buf)
return response.arrayBuffer().then(function(buffer) {
var bufView = new Uint8Array(buffer)
assert.equal(bufView.byteLength, view.byteLength)
for(var i = 0; i < bufView.byteLength; i++) {
assert.equal(bufView[i], view[i])
}
})
})
})

suite('type USVString', function() {
test('consume as text', function() {
var response = new Response('hello')
Expand Down

0 comments on commit 211d5ca

Please sign in to comment.