Skip to content

Commit

Permalink
Add support for ArrayBufferView as POST body
Browse files Browse the repository at this point in the history
Section 6.2 of the spec says that body can be BufferSource.
BufferSource is defined in https://heycam.github.io/webidl/#BufferSource:

> The BufferSource typedef is used to represent objects that are either
> themselves an ArrayBuffer or which provide a view on to an ArrayBuffer.

Furthermore, passing an ArrayBuffer directly to XMLHttpRequest is
considered deprecated and Safari throws a warning about it. This makes
it even more important to support ArrayBufferView in fetch.

The fallback for `ArrayBuffer.isView` is provided for IE 10, which
doesn't implement the method.
  • Loading branch information
mislav committed Nov 11, 2016
1 parent 87fd1cd commit c189b58
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
20 changes: 19 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
arrayBuffer: 'ArrayBuffer' in self
}

if (support.arrayBuffer) {
var viewClasses = [
'[object Int8Array]',
'[object Uint8Array]',
'[object Uint8ClampedArray]',
'[object Int16Array]',
'[object Uint16Array]',
'[object Int32Array]',
'[object Uint32Array]',
'[object Float32Array]',
'[object Float64Array]'
]

var isArrayBufferView = ArrayBuffer.isView || function(obj) {
return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
}
}

function normalizeName(name) {
if (typeof name !== 'string') {
name = String(name)
Expand Down Expand Up @@ -177,7 +195,7 @@
this._bodyText = body.toString()
} else if (!body) {
this._bodyText = ''
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
} else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
// Only support ArrayBuffers for POST method.
// Receiving ArrayBuffers happens via Blobs, instead.
} else {
Expand Down
32 changes: 23 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,29 @@ suite('fetch method', function() {
})
})

featureDependent(test, support.arrayBuffer, 'sends ArrayBuffer body', function() {
return fetch('/request', {
method: 'post',
body: arrayBufferFromText('name=Hubot')
}).then(function(response) {
return response.json()
}).then(function(request) {
assert.equal(request.method, 'POST')
assert.equal(request.data, 'name=Hubot')
featureDependent(suite, support.arrayBuffer, 'ArrayBuffer', function() {
test('ArrayBuffer body', function() {
return fetch('/request', {
method: 'post',
body: arrayBufferFromText('name=Hubot')
}).then(function(response) {
return response.json()
}).then(function(request) {
assert.equal(request.method, 'POST')
assert.equal(request.data, 'name=Hubot')
})
})

test('TypedArray body', function() {
return fetch('/request', {
method: 'post',
body: new Uint8Array(arrayBufferFromText('name=Hubot'))
}).then(function(response) {
return response.json()
}).then(function(request) {
assert.equal(request.method, 'POST')
assert.equal(request.data, 'name=Hubot')
})
})
})

Expand Down

0 comments on commit c189b58

Please sign in to comment.