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 3 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: 17 additions & 5 deletions src/body.js
Expand Up @@ -259,6 +259,8 @@ export const clone = (instance, highWaterMark) => {
return body;
};

let warnedUsingOldFormData = false;

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

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=${body.getBoundary()}`;
}
if (!warnedUsingOldFormData) {
console.warn('\u001B[33m%s\u001B[0m', '[WARN]', `
jimmywarting marked this conversation as resolved.
Show resolved Hide resolved
You are probably using form-data.
form-data doesn't follow the spec and requires special treatment.
We will eventually remove support for form-data.
See alternatives https://github.com/node-fetch/node-fetch/issues/1167
`);
warnedUsingOldFormData = true;
}

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

// Body is stream - can't really do much about this
Expand Down Expand Up @@ -345,7 +357,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