Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Body): Discourage form-data and buffer() #1212

Merged
merged 4 commits into from Sep 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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