From ea69047503bda3a8e4a42a406fabfd1c03b1a182 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 21 Feb 2023 16:41:11 +0100 Subject: [PATCH] fix: improve isFormDataLike compat Fixes: https://github.com/nodejs/undici/issues/1952 --- lib/core/util.js | 25 +++++++++++-------------- test/fetch/formdata.js | 4 ++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/core/util.js b/lib/core/util.js index ef9b4570dcd..334bc26c776 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -375,20 +375,17 @@ function ReadableStreamFrom (iterable) { // The chunk should be a FormData instance and contains // all the required methods. -function isFormDataLike (chunk) { - return (chunk && - chunk.constructor && chunk.constructor.name === 'FormData' && - typeof chunk === 'object' && - (typeof chunk.append === 'function' && - typeof chunk.delete === 'function' && - typeof chunk.get === 'function' && - typeof chunk.getAll === 'function' && - typeof chunk.has === 'function' && - typeof chunk.set === 'function' && - typeof chunk.entries === 'function' && - typeof chunk.keys === 'function' && - typeof chunk.values === 'function' && - typeof chunk.forEach === 'function') +function isFormDataLike (object) { + return ( + object && + typeof object === 'object' && + typeof object.append === 'function' && + typeof object.delete === 'function' && + typeof object.get === 'function' && + typeof object.getAll === 'function' && + typeof object.has === 'function' && + typeof object.set === 'function' && + object[Symbol.toStringTag] === 'FormData' ) } diff --git a/test/fetch/formdata.js b/test/fetch/formdata.js index 156c11fb77d..fed95bfb3da 100644 --- a/test/fetch/formdata.js +++ b/test/fetch/formdata.js @@ -349,6 +349,10 @@ test('FormData should be compatible with third-party libraries', (t) => { this.data = [] } + get [Symbol.toStringTag] () { + return 'FormData' + } + append () {} delete () {} get () {}