Skip to content

Commit fc53995

Browse files
gr2mTimothyGu
authored andcommitted
Support ArrayBuffer as body (node-fetch#408)
1 parent feae6d6 commit fc53995

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"resumer": "0.0.0",
5555
"rollup": "^0.55.1",
5656
"rollup-plugin-babel": "^3.0.3",
57+
"string-to-arraybuffer": "^1.0.0",
5758
"url-search-params": "^0.10.0",
5859
"whatwg-url": "^5.0.0"
5960
},

src/body.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export default function Body(body, {
4040
// body is blob
4141
} else if (Buffer.isBuffer(body)) {
4242
// body is buffer
43+
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
44+
// body is array buffer
4345
} else if (body instanceof Stream) {
4446
// body is stream
4547
} else {
@@ -202,6 +204,11 @@ function consumeBody() {
202204
return Body.Promise.resolve(this.body);
203205
}
204206

207+
// body is buffer
208+
if (Object.prototype.toString.call(this.body) === '[object ArrayBuffer]') {
209+
return Body.Promise.resolve(this.body);
210+
}
211+
205212
// istanbul ignore if: should never happen
206213
if (!(this.body instanceof Stream)) {
207214
return Body.Promise.resolve(Buffer.alloc(0));
@@ -403,6 +410,9 @@ export function extractContentType(instance) {
403410
} else if (Buffer.isBuffer(body)) {
404411
// body is buffer
405412
return null;
413+
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
414+
// body is array buffer
415+
return null;
406416
} else if (typeof body.getBoundary === 'function') {
407417
// detect form data input from form-data module
408418
return `multipart/form-data;boundary=${body.getBoundary()}`;
@@ -441,6 +451,9 @@ export function getTotalBytes(instance) {
441451
} else if (Buffer.isBuffer(body)) {
442452
// body is buffer
443453
return body.length;
454+
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
455+
// body is array buffer
456+
return body.byteLength;
444457
} else if (body && typeof body.getLengthSync === 'function') {
445458
// detect form data input from form-data module
446459
if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
@@ -483,6 +496,10 @@ export function writeToStream(dest, instance) {
483496
// body is buffer
484497
dest.write(body);
485498
dest.end()
499+
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
500+
// body is array buffer
501+
dest.write(Buffer.from(body));
502+
dest.end()
486503
} else {
487504
// body is stream
488505
body.pipe(dest);

test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import chaiString from 'chai-string';
77
import then from 'promise';
88
import resumer from 'resumer';
99
import FormData from 'form-data';
10+
import stringToArrayBuffer from 'string-to-arraybuffer';
1011
import URLSearchParams_Polyfill from 'url-search-params';
1112
import { URL } from 'whatwg-url';
1213

@@ -771,6 +772,21 @@ describe('node-fetch', () => {
771772
});
772773
});
773774

775+
it('should allow POST request with ArrayBuffer body', function() {
776+
url = `${base}inspect`;
777+
opts = {
778+
method: 'POST'
779+
, body: stringToArrayBuffer('Hello, world!\n')
780+
};
781+
return fetch(url, opts).then(res => res.json()).then(res => {
782+
expect(res.method).to.equal('POST');
783+
expect(res.body).to.equal('Hello, world!\n');
784+
expect(res.headers['transfer-encoding']).to.be.undefined;
785+
expect(res.headers['content-type']).to.be.undefined;
786+
expect(res.headers['content-length']).to.equal('14');
787+
});
788+
});
789+
774790
it('should allow POST request with blob body without type', function() {
775791
url = `${base}inspect`;
776792
opts = {

0 commit comments

Comments
 (0)