From ec4ee9e316f289050cde452d7e118ccd6746c240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Sun, 18 Jul 2021 23:21:47 +0200 Subject: [PATCH 1/4] warn about using form-data --- src/body.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/body.js b/src/body.js index c923a8ce5..76a7b5ebd 100644 --- a/src/body.js +++ b/src/body.js @@ -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: @@ -297,6 +299,15 @@ export const extractContentType = (body, request) => { // Detect form data input from form-data module if (body && typeof body.getBoundary === 'function') { + if (!warnedUsingOldFormData) { + console.warn('\x1b[33m%s\x1b[0m', `[WARN]`, ` + 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; + } return `multipart/form-data;boundary=${body.getBoundary()}`; } @@ -345,7 +356,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); } From 6e135d35b49c628618ff99c48f8a0569e10ebed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Sun, 18 Jul 2021 23:22:24 +0200 Subject: [PATCH 2/4] check for spec compatible first --- src/body.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/body.js b/src/body.js index 76a7b5ebd..a652f540e 100644 --- a/src/body.js +++ b/src/body.js @@ -297,6 +297,11 @@ 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') { if (!warnedUsingOldFormData) { @@ -311,10 +316,6 @@ export const extractContentType = (body, request) => { return `multipart/form-data;boundary=${body.getBoundary()}`; } - if (isFormData(body)) { - return `multipart/form-data; boundary=${request[INTERNALS].boundary}`; - } - // Body is stream - can't really do much about this if (body instanceof Stream) { return null; From 0e465d6b2012ee3ac1a6e32fd25b8647235b6081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Sun, 18 Jul 2021 23:27:57 +0200 Subject: [PATCH 3/4] xo --fix --- src/body.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/body.js b/src/body.js index a652f540e..1679bc4e2 100644 --- a/src/body.js +++ b/src/body.js @@ -297,7 +297,6 @@ export const extractContentType = (body, request) => { return null; } - if (isFormData(body)) { return `multipart/form-data; boundary=${request[INTERNALS].boundary}`; } @@ -305,14 +304,15 @@ export const extractContentType = (body, request) => { // Detect form data input from form-data module if (body && typeof body.getBoundary === 'function') { if (!warnedUsingOldFormData) { - console.warn('\x1b[33m%s\x1b[0m', `[WARN]`, ` + console.warn('\u001B[33m%s\u001B[0m', '[WARN]', ` 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; } + return `multipart/form-data;boundary=${body.getBoundary()}`; } From 1c506c384c47fe0ce7bf71263ce01dffd5d12b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Mon, 19 Jul 2021 00:34:58 +0200 Subject: [PATCH 4/4] use util instead --- src/body.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/body.js b/src/body.js index 1679bc4e2..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,7 +261,11 @@ export const clone = (instance, highWaterMark) => { return body; }; -let warnedUsingOldFormData = false; +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 @@ -303,17 +309,7 @@ export const extractContentType = (body, request) => { // Detect form data input from form-data module if (body && typeof body.getBoundary === 'function') { - if (!warnedUsingOldFormData) { - console.warn('\u001B[33m%s\u001B[0m', '[WARN]', ` - 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; - } - - return `multipart/form-data;boundary=${body.getBoundary()}`; + return `multipart/form-data;boundary=${getNonSpecFormDataBoundary(body)}`; } // Body is stream - can't really do much about this