This commit is contained in:
2026-07-26 21:23:30 +00:00
parent 6fa0e1b9a6
commit 7c262aeb8b
60 changed files with 2359 additions and 582 deletions
+47 -8
View File
@@ -2,6 +2,7 @@
var formats = require('./formats');
var getSideChannel = require('side-channel');
var defineProperty = require('es-define-property');
var has = Object.prototype.hasOwnProperty;
var isArray = Array.isArray;
@@ -66,6 +67,19 @@ var arrayToObject = function arrayToObject(source, options) {
return obj;
};
var setProperty = function setProperty(obj, key, value) {
if (key === '__proto__' && defineProperty) {
defineProperty(obj, key, {
configurable: true,
enumerable: true,
value: value,
writable: true
});
} else {
obj[key] = value;
}
};
var merge = function merge(target, source, options) {
/* eslint no-param-reassign: 0 */
if (!source) {
@@ -75,7 +89,10 @@ var merge = function merge(target, source, options) {
if (typeof source !== 'object' && typeof source !== 'function') {
if (isArray(target)) {
var nextIndex = target.length;
if (options && typeof options.arrayLimit === 'number' && nextIndex > options.arrayLimit) {
if (options && typeof options.arrayLimit === 'number' && nextIndex >= options.arrayLimit) {
if (options.throwOnLimitExceeded) {
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
}
return markOverflow(arrayToObject(target.concat(source), options), nextIndex);
}
target[nextIndex] = source;
@@ -115,6 +132,9 @@ var merge = function merge(target, source, options) {
}
var combined = [target].concat(source);
if (options && typeof options.arrayLimit === 'number' && combined.length > options.arrayLimit) {
if (options.throwOnLimitExceeded) {
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
}
return markOverflow(arrayToObject(combined, options), combined.length - 1);
}
return combined;
@@ -138,6 +158,12 @@ var merge = function merge(target, source, options) {
target[i] = item;
}
});
if (options && typeof options.arrayLimit === 'number' && target.length > options.arrayLimit) {
if (options.throwOnLimitExceeded) {
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
}
return markOverflow(arrayToObject(target, options), target.length - 1);
}
return target;
}
@@ -145,9 +171,9 @@ var merge = function merge(target, source, options) {
var value = source[key];
if (has.call(acc, key)) {
acc[key] = merge(acc[key], value, options);
setProperty(acc, key, merge(acc[key], value, options));
} else {
acc[key] = value;
setProperty(acc, key, value);
}
if (isOverflow(source) && !isOverflow(acc)) {
@@ -166,7 +192,7 @@ var merge = function merge(target, source, options) {
var assign = function assignSingleSource(target, source) {
return Object.keys(source).reduce(function (acc, key) {
acc[key] = source[key];
setProperty(acc, key, source[key]);
return acc;
}, target);
};
@@ -212,6 +238,13 @@ var encode = function encode(str, defaultEncoder, charset, kind, format) {
var out = '';
for (var j = 0; j < string.length; j += limit) {
var segment = string.length >= limit ? string.slice(j, j + limit) : string;
if (j + limit < string.length) {
var last = segment.charCodeAt(segment.length - 1);
if (last >= 0xD800 && last <= 0xDBFF) {
segment = segment.slice(0, -1);
j -= 1;
}
}
var arr = [];
for (var i = 0; i < segment.length; ++i) {
@@ -265,7 +298,7 @@ var encode = function encode(str, defaultEncoder, charset, kind, format) {
var compact = function compact(value) {
var queue = [{ obj: { o: value }, prop: 'o' }];
var refs = [];
var refs = getSideChannel();
for (var i = 0; i < queue.length; ++i) {
var item = queue[i];
@@ -275,9 +308,9 @@ var compact = function compact(value) {
for (var j = 0; j < keys.length; ++j) {
var key = keys[j];
var val = obj[key];
if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
if (typeof val === 'object' && val !== null && !refs.has(val)) {
queue[queue.length] = { obj: obj, prop: key };
refs[refs.length] = val;
refs.set(val, true);
}
}
}
@@ -299,9 +332,12 @@ var isBuffer = function isBuffer(obj) {
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
};
var combine = function combine(a, b, arrayLimit, plainObjects) {
var combine = function combine(a, b, arrayLimit, plainObjects, throwOnLimitExceeded) {
// If 'a' is already an overflow object, add to it
if (isOverflow(a)) {
if (throwOnLimitExceeded) {
throw new RangeError('Array limit exceeded. Only ' + arrayLimit + ' element' + (arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
}
var newIndex = getMaxIndex(a) + 1;
a[newIndex] = b;
setMaxIndex(a, newIndex);
@@ -310,6 +346,9 @@ var combine = function combine(a, b, arrayLimit, plainObjects) {
var result = [].concat(a, b);
if (result.length > arrayLimit) {
if (throwOnLimitExceeded) {
throw new RangeError('Array limit exceeded. Only ' + arrayLimit + ' element' + (arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
}
return markOverflow(arrayToObject(result, { plainObjects: plainObjects }), result.length - 1);
}
return result;