From 672491db34b5575d2abb1c3f91382bc1f45ae7b7 Mon Sep 17 00:00:00 2001 From: Shiwaangee Date: Sat, 14 Feb 2026 21:12:56 +0530 Subject: [PATCH] fix: safe FormData detection for WeChat Mini Program (#7306) (#7324) Co-authored-by: Jay --- lib/utils.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 239a3d74..0ef9c0b5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -220,17 +220,27 @@ const isStream = (val) => isObject(val) && isFunction(val.pipe); * * @returns {boolean} True if value is an FormData, otherwise false */ +function getGlobal() { + if (typeof globalThis !== 'undefined') return globalThis; + if (typeof self !== 'undefined') return self; + if (typeof window !== 'undefined') return window; + if (typeof global !== 'undefined') return global; + return {}; +} + +const G = getGlobal(); +const FormDataCtor = typeof G.FormData !== 'undefined' ? G.FormData : undefined; + const isFormData = (thing) => { let kind; - return ( - thing && - ((typeof FormData === 'function' && thing instanceof FormData) || - (isFunction(thing.append) && - ((kind = kindOf(thing)) === 'formdata' || - // detect form-data instance - (kind === 'object' && - isFunction(thing.toString) && - thing.toString() === '[object FormData]')))) + return thing && ( + (FormDataCtor && thing instanceof FormDataCtor) || ( + isFunction(thing.append) && ( + (kind = kindOf(thing)) === 'formdata' || + // detect form-data instance + (kind === 'object' && isFunction(thing.toString) && thing.toString() === '[object FormData]') + ) + ) ); };