Support ArrayBuffer as body (#408)

This commit is contained in:
Gregor Martynus 2018-03-05 00:40:39 +00:00 committed by Timothy Gu
parent feae6d6ec8
commit fc539951ca
3 changed files with 34 additions and 0 deletions

View File

@ -54,6 +54,7 @@
"resumer": "0.0.0",
"rollup": "^0.55.1",
"rollup-plugin-babel": "^3.0.3",
"string-to-arraybuffer": "^1.0.0",
"url-search-params": "^0.10.0",
"whatwg-url": "^5.0.0"
},

View File

@ -40,6 +40,8 @@ export default function Body(body, {
// body is blob
} else if (Buffer.isBuffer(body)) {
// body is buffer
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
// body is array buffer
} else if (body instanceof Stream) {
// body is stream
} else {
@ -202,6 +204,11 @@ function consumeBody() {
return Body.Promise.resolve(this.body);
}
// body is buffer
if (Object.prototype.toString.call(this.body) === '[object ArrayBuffer]') {
return Body.Promise.resolve(this.body);
}
// istanbul ignore if: should never happen
if (!(this.body instanceof Stream)) {
return Body.Promise.resolve(Buffer.alloc(0));
@ -403,6 +410,9 @@ export function extractContentType(instance) {
} else if (Buffer.isBuffer(body)) {
// body is buffer
return null;
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
// body is array buffer
return null;
} else if (typeof body.getBoundary === 'function') {
// detect form data input from form-data module
return `multipart/form-data;boundary=${body.getBoundary()}`;
@ -441,6 +451,9 @@ export function getTotalBytes(instance) {
} else if (Buffer.isBuffer(body)) {
// body is buffer
return body.length;
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
// body is array buffer
return body.byteLength;
} else if (body && typeof body.getLengthSync === 'function') {
// detect form data input from form-data module
if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
@ -483,6 +496,10 @@ export function writeToStream(dest, instance) {
// body is buffer
dest.write(body);
dest.end()
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
// body is array buffer
dest.write(Buffer.from(body));
dest.end()
} else {
// body is stream
body.pipe(dest);

View File

@ -7,6 +7,7 @@ import chaiString from 'chai-string';
import then from 'promise';
import resumer from 'resumer';
import FormData from 'form-data';
import stringToArrayBuffer from 'string-to-arraybuffer';
import URLSearchParams_Polyfill from 'url-search-params';
import { URL } from 'whatwg-url';
@ -771,6 +772,21 @@ describe('node-fetch', () => {
});
});
it('should allow POST request with ArrayBuffer body', function() {
url = `${base}inspect`;
opts = {
method: 'POST'
, body: stringToArrayBuffer('Hello, world!\n')
};
return fetch(url, opts).then(res => res.json()).then(res => {
expect(res.method).to.equal('POST');
expect(res.body).to.equal('Hello, world!\n');
expect(res.headers['transfer-encoding']).to.be.undefined;
expect(res.headers['content-type']).to.be.undefined;
expect(res.headers['content-length']).to.equal('14');
});
});
it('should allow POST request with blob body without type', function() {
url = `${base}inspect`;
opts = {