mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(core): updating deepCopy to avoid max callstack with circular deps (#4468)
* fix(core): updating deepCopy to avoid max callstack in case of circular dep * fix(core): show warning with path added to circular reference
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument */
|
||||
export const deepCopy = <T>(source: T): T => {
|
||||
export const deepCopy = <T>(source: T, hash = new WeakMap(), path = ''): T => {
|
||||
let clone: any;
|
||||
let i: any;
|
||||
const hasOwnProp = Object.prototype.hasOwnProperty.bind(source);
|
||||
// Primitives & Null
|
||||
if (typeof source !== 'object' || source === null) {
|
||||
// Primitives & Null & Function
|
||||
if (typeof source !== 'object' || source === null || source instanceof Function) {
|
||||
return source;
|
||||
}
|
||||
if (hash.has(source)) {
|
||||
console.warn(`Circular reference detected at "source${path}"`);
|
||||
return hash.get(source);
|
||||
}
|
||||
// Date
|
||||
if (source instanceof Date) {
|
||||
return new Date(source.getTime()) as T;
|
||||
@@ -16,15 +20,16 @@ export const deepCopy = <T>(source: T): T => {
|
||||
clone = [];
|
||||
const len = source.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
clone[i] = deepCopy(source[i]);
|
||||
clone[i] = deepCopy(source[i], hash, path + `[${i as string}]`);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
// Object
|
||||
clone = {};
|
||||
hash.set(source, clone);
|
||||
for (i in source) {
|
||||
if (hasOwnProp(i)) {
|
||||
clone[i] = deepCopy((source as any)[i]);
|
||||
clone[i] = deepCopy((source as any)[i], hash, path + `.${i as string}`);
|
||||
}
|
||||
}
|
||||
return clone;
|
||||
|
||||
Reference in New Issue
Block a user