Skip to content

Commit

Permalink
fix(Body): Discurage form-data and buffer() (#1212)
Browse files Browse the repository at this point in the history
warn about using form-data
  • Loading branch information
jimmywarting committed Sep 4, 2021
1 parent 2603c67 commit 471f08c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/body.js
Expand Up @@ -6,7 +6,7 @@
*/

import Stream, {PassThrough} from 'stream';
import {types} from 'util';
import {types, deprecate} from 'util';

import Blob from 'fetch-blob';

Expand Down Expand Up @@ -140,6 +140,8 @@ export default class Body {
}
}

Body.prototype.buffer = deprecate(Body.prototype.buffer, 'Please use \'response.arrayBuffer()\' instead of \'response.buffer()\'', 'node-fetch#buffer');

// In browsers, all properties are enumerable.
Object.defineProperties(Body.prototype, {
body: {enumerable: true},
Expand Down Expand Up @@ -259,6 +261,12 @@ export const clone = (instance, highWaterMark) => {
return body;
};

const getNonSpecFormDataBoundary = deprecate(
body => body.getBoundary(),
'form-data doesn\'t follow the spec and requires special treatment. Use alternative package',
'https://github.com/node-fetch/node-fetch/issues/1167'
);

/**
* Performs the operation "extract a `Content-Type` value from |object|" as
* specified in the specification:
Expand Down Expand Up @@ -295,15 +303,15 @@ export const extractContentType = (body, request) => {
return null;
}

// Detect form data input from form-data module
if (body && typeof body.getBoundary === 'function') {
return `multipart/form-data;boundary=${body.getBoundary()}`;
}

if (isFormData(body)) {
return `multipart/form-data; boundary=${request[INTERNALS].boundary}`;
}

// Detect form data input from form-data module
if (body && typeof body.getBoundary === 'function') {
return `multipart/form-data;boundary=${getNonSpecFormDataBoundary(body)}`;
}

// Body is stream - can't really do much about this
if (body instanceof Stream) {
return null;
Expand Down Expand Up @@ -345,7 +353,7 @@ export const getTotalBytes = request => {
return body.hasKnownLength && body.hasKnownLength() ? body.getLengthSync() : null;
}

// Body is a spec-compliant form-data
// Body is a spec-compliant FormData
if (isFormData(body)) {
return getFormDataLength(request[INTERNALS].boundary);
}
Expand Down

0 comments on commit 471f08c

Please sign in to comment.