mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(core): Remove circular refs from Code and push msg (#5741)
* remove circular refs from code items (and lint fixes) * cleanup --------- * add some tests Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
committed by
GitHub
parent
199a91b398
commit
b6d8a0f985
@@ -23,7 +23,7 @@ export * from './WorkflowErrors';
|
||||
export * from './WorkflowHooks';
|
||||
export * from './VersionedNodeType';
|
||||
export { LoggerProxy, NodeHelpers, ObservableObject, TelemetryHelpers };
|
||||
export { deepCopy, jsonParse, sleep, fileTypeFromMimeType, assert } from './utils';
|
||||
export { deepCopy, jsonParse, jsonStringify, sleep, fileTypeFromMimeType, assert } from './utils';
|
||||
export {
|
||||
isINodeProperties,
|
||||
isINodePropertyOptions,
|
||||
|
||||
@@ -62,6 +62,31 @@ export const jsonParse = <T>(jsonString: string, options?: JSONParseOptions<T>):
|
||||
}
|
||||
};
|
||||
|
||||
type JSONStringifyOptions = {
|
||||
replaceCircularRefs?: boolean;
|
||||
circularRefReplacement?: string;
|
||||
};
|
||||
|
||||
const getReplaceCircularReferencesFn = (options: JSONStringifyOptions) => {
|
||||
const knownObjects = new WeakSet();
|
||||
return (key: any, value: any) => {
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
if (knownObjects.has(value)) {
|
||||
return options?.circularRefReplacement ?? '[Circular Reference]';
|
||||
}
|
||||
knownObjects.add(value);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
};
|
||||
|
||||
export const jsonStringify = (obj: unknown, options: JSONStringifyOptions = {}): string => {
|
||||
const replacer = options?.replaceCircularRefs
|
||||
? getReplaceCircularReferencesFn(options)
|
||||
: undefined;
|
||||
return JSON.stringify(obj, replacer);
|
||||
};
|
||||
|
||||
export const sleep = async (ms: number): Promise<void> =>
|
||||
new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { jsonParse, deepCopy } from '@/utils';
|
||||
import { jsonParse, jsonStringify, deepCopy } from '@/utils';
|
||||
|
||||
describe('jsonParse', () => {
|
||||
it('parses JSON', () => {
|
||||
@@ -17,6 +17,21 @@ describe('jsonParse', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('jsonStringify', () => {
|
||||
const source: any = { a: 1, b: 2 };
|
||||
source.c = source;
|
||||
|
||||
it('should throw errors on circular references by default', () => {
|
||||
expect(() => jsonStringify(source)).toThrow('Converting circular structure to JSON');
|
||||
});
|
||||
|
||||
it('should break circular references when requested', () => {
|
||||
expect(jsonStringify(source, { replaceCircularRefs: true })).toEqual(
|
||||
'{"a":1,"b":2,"c":"[Circular Reference]"}',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deepCopy', () => {
|
||||
it('should deep copy an object', () => {
|
||||
const serializable = {
|
||||
|
||||
Reference in New Issue
Block a user