Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Remove circular refs from Code and push msg #5741

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/src/push/abstract.push.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoggerProxy as Logger } from 'n8n-workflow';
import { jsonStringify, LoggerProxy as Logger } from 'n8n-workflow';
import type { IPushDataType } from '@/Interfaces';
import { eventBus } from '../eventbus';

Expand Down Expand Up @@ -38,7 +38,7 @@ export abstract class AbstractPush<T> {

Logger.debug(`Send data of type "${type}" to editor-UI`, { dataType: type, sessionId });

const sendData = JSON.stringify({ type, data });
const sendData = jsonStringify({ type, data }, { replaceCircularRefs: true });

if (sessionId === undefined) {
// Send to all connected clients
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/sso/saml/routes/saml.controller.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export class SamlController {
private async handleInitSSO(res: express.Response) {
const result = this.samlService.getLoginRequestUrl();
if (result?.binding === 'redirect') {
// Return the redirect URL directly
return res.send(result.context.context);
} else if (result?.binding === 'post') {
return res.send(getInitSSOFormView(result.context as PostBindingContext));
Expand Down
28 changes: 21 additions & 7 deletions packages/nodes-base/nodes/Code/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IDataObject } from 'n8n-workflow';
import { jsonStringify } from 'n8n-workflow';
flipswitchingmonkey marked this conversation as resolved.
Show resolved Hide resolved

export function isObject(maybe: unknown): maybe is { [key: string]: unknown } {
return typeof maybe === 'object' && maybe !== null && !Array.isArray(maybe);
Expand All @@ -12,15 +13,28 @@ function isTraversable(maybe: unknown): maybe is IDataObject {
* Stringify any non-standard JS objects (e.g. `Date`, `RegExp`) inside output items at any depth.
*/
export function standardizeOutput(output: IDataObject) {
for (const [key, value] of Object.entries(output)) {
if (!isTraversable(value)) continue;
const knownObjects = new WeakSet();

output[key] =
value.constructor.name !== 'Object'
? JSON.stringify(value) // Date, RegExp, etc.
: standardizeOutput(value);
}
function standardizeOutputRecursive(obj: IDataObject): IDataObject {
for (const [key, value] of Object.entries(obj)) {
if (!isTraversable(value)) continue;

if (typeof value === 'object' && value !== null) {
if (knownObjects.has(value)) {
// Found circular reference
continue;
}
knownObjects.add(value);
}

obj[key] =
value.constructor.name !== 'Object'
? JSON.stringify(value) // Date, RegExp, etc.
: standardizeOutputRecursive(value);
}
return obj;
}
standardizeOutputRecursive(output);
return output;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions packages/workflow/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ 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 => {
try {
if (options?.replaceCircularRefs === true) {
return JSON.stringify(obj, getReplaceCircularReferencesFn(options));
}
return JSON.stringify(obj);
} catch (error) {
flipswitchingmonkey marked this conversation as resolved.
Show resolved Hide resolved
// TOOD: handle stringify errors?
throw error;
}
};

export const sleep = async (ms: number): Promise<void> =>
new Promise((resolve) => {
setTimeout(resolve, ms);
Expand Down