mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
fix: prevent RangeError when using large Buffers (#6961)
This commit is contained in:
@@ -136,6 +136,27 @@ const isPlainObject = (val) => {
|
||||
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(toStringTag in val) && !(iterator in val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is an empty object (safely handles Buffers)
|
||||
*
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is an empty object, otherwise false
|
||||
*/
|
||||
const isEmptyObject = (val) => {
|
||||
// Early return for non-objects or Buffers to prevent RangeError
|
||||
if (!isObject(val) || isBuffer(val)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return Object.keys(val).length === 0 && Object.getPrototypeOf(val) === Object.prototype;
|
||||
} catch (e) {
|
||||
// Fallback for any other objects that might cause RangeError with Object.keys()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a Date
|
||||
*
|
||||
@@ -258,6 +279,11 @@ function forEach(obj, fn, {allOwnKeys = false} = {}) {
|
||||
fn.call(null, obj[i], i, obj);
|
||||
}
|
||||
} else {
|
||||
// Buffer check
|
||||
if (isBuffer(obj)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Iterate over object keys
|
||||
const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
|
||||
const len = keys.length;
|
||||
@@ -271,6 +297,10 @@ function forEach(obj, fn, {allOwnKeys = false} = {}) {
|
||||
}
|
||||
|
||||
function findKey(obj, key) {
|
||||
if (isBuffer(obj)){
|
||||
return null;
|
||||
}
|
||||
|
||||
key = key.toLowerCase();
|
||||
const keys = Object.keys(obj);
|
||||
let i = keys.length;
|
||||
@@ -624,6 +654,11 @@ const toJSONObject = (obj) => {
|
||||
return;
|
||||
}
|
||||
|
||||
//Buffer check
|
||||
if (isBuffer(source)) {
|
||||
return source;
|
||||
}
|
||||
|
||||
if(!('toJSON' in source)) {
|
||||
stack[i] = source;
|
||||
const target = isArray(source) ? [] : {};
|
||||
@@ -695,6 +730,7 @@ export default {
|
||||
isBoolean,
|
||||
isObject,
|
||||
isPlainObject,
|
||||
isEmptyObject,
|
||||
isReadableStream,
|
||||
isRequest,
|
||||
isResponse,
|
||||
|
||||
Reference in New Issue
Block a user