Unify internal body as buffer (#552)
This commit is contained in:
parent
35a4abe825
commit
7d3293200a
72
src/body.js
72
src/body.js
|
@ -34,25 +34,26 @@ export default function Body(body, {
|
||||||
if (body == null) {
|
if (body == null) {
|
||||||
// body is undefined or null
|
// body is undefined or null
|
||||||
body = null;
|
body = null;
|
||||||
} else if (typeof body === 'string') {
|
|
||||||
// body is string
|
|
||||||
} else if (isURLSearchParams(body)) {
|
} else if (isURLSearchParams(body)) {
|
||||||
body = Buffer.from(body.toString());
|
|
||||||
// body is a URLSearchParams
|
// body is a URLSearchParams
|
||||||
|
body = Buffer.from(body.toString());
|
||||||
} else if (body instanceof Blob) {
|
} else if (body instanceof Blob) {
|
||||||
// body is blob
|
// body is blob
|
||||||
|
body = body[BUFFER];
|
||||||
} else if (Buffer.isBuffer(body)) {
|
} else if (Buffer.isBuffer(body)) {
|
||||||
// body is Buffer
|
// body is Buffer
|
||||||
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
|
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
|
||||||
// body is ArrayBuffer
|
// body is ArrayBuffer
|
||||||
|
body = Buffer.from(body);
|
||||||
} else if (ArrayBuffer.isView(body)) {
|
} else if (ArrayBuffer.isView(body)) {
|
||||||
// body is ArrayBufferView
|
// body is ArrayBufferView
|
||||||
|
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
||||||
} else if (body instanceof Stream) {
|
} else if (body instanceof Stream) {
|
||||||
// body is stream
|
// body is stream
|
||||||
} else {
|
} else {
|
||||||
// none of the above
|
// none of the above
|
||||||
// coerce to string
|
// coerce to string then buffer
|
||||||
body = String(body);
|
body = Buffer.from(String(body));
|
||||||
}
|
}
|
||||||
this[INTERNALS] = {
|
this[INTERNALS] = {
|
||||||
body,
|
body,
|
||||||
|
@ -149,9 +150,7 @@ Body.prototype = {
|
||||||
*/
|
*/
|
||||||
textConverted() {
|
textConverted() {
|
||||||
return consumeBody.call(this).then(buffer => convertBody(buffer, this.headers));
|
return consumeBody.call(this).then(buffer => convertBody(buffer, this.headers));
|
||||||
},
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// In browsers, all properties are enumerable.
|
// In browsers, all properties are enumerable.
|
||||||
|
@ -197,31 +196,11 @@ function consumeBody() {
|
||||||
return Body.Promise.resolve(Buffer.alloc(0));
|
return Body.Promise.resolve(Buffer.alloc(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// body is string
|
|
||||||
if (typeof this.body === 'string') {
|
|
||||||
return Body.Promise.resolve(Buffer.from(this.body));
|
|
||||||
}
|
|
||||||
|
|
||||||
// body is blob
|
|
||||||
if (this.body instanceof Blob) {
|
|
||||||
return Body.Promise.resolve(this.body[BUFFER]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// body is buffer
|
// body is buffer
|
||||||
if (Buffer.isBuffer(this.body)) {
|
if (Buffer.isBuffer(this.body)) {
|
||||||
return Body.Promise.resolve(this.body);
|
return Body.Promise.resolve(this.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
// body is ArrayBuffer
|
|
||||||
if (Object.prototype.toString.call(this.body) === '[object ArrayBuffer]') {
|
|
||||||
return Body.Promise.resolve(Buffer.from(this.body));
|
|
||||||
}
|
|
||||||
|
|
||||||
// body is ArrayBufferView
|
|
||||||
if (ArrayBuffer.isView(this.body)) {
|
|
||||||
return Body.Promise.resolve(Buffer.from(this.body.buffer, this.body.byteOffset, this.body.byteLength));
|
|
||||||
}
|
|
||||||
|
|
||||||
// istanbul ignore if: should never happen
|
// istanbul ignore if: should never happen
|
||||||
if (!(this.body instanceof Stream)) {
|
if (!(this.body instanceof Stream)) {
|
||||||
return Body.Promise.resolve(Buffer.alloc(0));
|
return Body.Promise.resolve(Buffer.alloc(0));
|
||||||
|
@ -426,7 +405,7 @@ export function extractContentType(body) {
|
||||||
// body is string
|
// body is string
|
||||||
return 'text/plain;charset=UTF-8';
|
return 'text/plain;charset=UTF-8';
|
||||||
} else if (isURLSearchParams(body)) {
|
} else if (isURLSearchParams(body)) {
|
||||||
// body is a URLSearchParams
|
// body is a URLSearchParams
|
||||||
return 'application/x-www-form-urlencoded;charset=UTF-8';
|
return 'application/x-www-form-urlencoded;charset=UTF-8';
|
||||||
} else if (body instanceof Blob) {
|
} else if (body instanceof Blob) {
|
||||||
// body is blob
|
// body is blob
|
||||||
|
@ -469,24 +448,9 @@ export function getTotalBytes(instance) {
|
||||||
if (body === null) {
|
if (body === null) {
|
||||||
// body is null
|
// body is null
|
||||||
return 0;
|
return 0;
|
||||||
} else if (typeof body === 'string') {
|
|
||||||
// body is string
|
|
||||||
return Buffer.byteLength(body);
|
|
||||||
} else if (isURLSearchParams(body)) {
|
|
||||||
// body is URLSearchParams
|
|
||||||
return Buffer.byteLength(String(body));
|
|
||||||
} else if (body instanceof Blob) {
|
|
||||||
// body is blob
|
|
||||||
return body.size;
|
|
||||||
} else if (Buffer.isBuffer(body)) {
|
} else if (Buffer.isBuffer(body)) {
|
||||||
// body is buffer
|
// body is buffer
|
||||||
return body.length;
|
return body.length;
|
||||||
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
|
|
||||||
// body is ArrayBuffer
|
|
||||||
return body.byteLength;
|
|
||||||
} else if (ArrayBuffer.isView(body)) {
|
|
||||||
// body is ArrayBufferView
|
|
||||||
return body.byteLength;
|
|
||||||
} else if (body && typeof body.getLengthSync === 'function') {
|
} else if (body && typeof body.getLengthSync === 'function') {
|
||||||
// detect form data input from form-data module
|
// detect form data input from form-data module
|
||||||
if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
|
if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
|
||||||
|
@ -513,30 +477,10 @@ export function writeToStream(dest, instance) {
|
||||||
if (body === null) {
|
if (body === null) {
|
||||||
// body is null
|
// body is null
|
||||||
dest.end();
|
dest.end();
|
||||||
} else if (typeof body === 'string') {
|
|
||||||
// body is string
|
|
||||||
dest.write(body);
|
|
||||||
dest.end();
|
|
||||||
} else if (isURLSearchParams(body)) {
|
|
||||||
// body is URLSearchParams
|
|
||||||
dest.write(Buffer.from(String(body)));
|
|
||||||
dest.end();
|
|
||||||
} else if (body instanceof Blob) {
|
|
||||||
// body is blob
|
|
||||||
dest.write(body[BUFFER]);
|
|
||||||
dest.end();
|
|
||||||
} else if (Buffer.isBuffer(body)) {
|
} else if (Buffer.isBuffer(body)) {
|
||||||
// body is buffer
|
// body is buffer
|
||||||
dest.write(body);
|
dest.write(body);
|
||||||
dest.end()
|
dest.end()
|
||||||
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
|
|
||||||
// body is ArrayBuffer
|
|
||||||
dest.write(Buffer.from(body));
|
|
||||||
dest.end()
|
|
||||||
} else if (ArrayBuffer.isView(body)) {
|
|
||||||
// body is ArrayBufferView
|
|
||||||
dest.write(Buffer.from(body.buffer, body.byteOffset, body.byteLength));
|
|
||||||
dest.end()
|
|
||||||
} else {
|
} else {
|
||||||
// body is stream
|
// body is stream
|
||||||
body.pipe(dest);
|
body.pipe(dest);
|
||||||
|
|
Loading…
Reference in New Issue