From 471f08c15c66c52944b883b9ddb539d25fff1fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Sun, 5 Sep 2021 01:14:38 +0200 Subject: [PATCH] fix(Body): Discurage form-data and buffer() (#1212) warn about using form-data --- src/body.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/body.js b/src/body.js index c923a8ce5..ecc50ed5f 100644 --- a/src/body.js +++ b/src/body.js @@ -6,7 +6,7 @@ */ import Stream, {PassThrough} from 'stream'; -import {types} from 'util'; +import {types, deprecate} from 'util'; import Blob from 'fetch-blob'; @@ -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}, @@ -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: @@ -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; @@ -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); }