Skip to content

Commit

Permalink
refactor(core): fixes n8n-local-rules/no-json-parse-json-stringify wa…
Browse files Browse the repository at this point in the history
…rnings (n8n-io#4407)

* 🔨 fixes

* 🔨 set rule to error
  • Loading branch information
michael-radency authored Oct 21, 2022
1 parent b661041 commit 46a43e4
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 50 deletions.
3 changes: 1 addition & 2 deletions packages/@n8n_io/eslint-config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ const config = (module.exports = {
// TODO: set to `error` and fix offenses
'n8n-local-rules/no-uncaught-json-parse': 'warn',

// TODO: set to `error` and fix offenses
'n8n-local-rules/no-json-parse-json-stringify': 'warn',
'n8n-local-rules/no-json-parse-json-stringify': 'error',

// ******************************************************************
// overrides to base ruleset
Expand Down
13 changes: 7 additions & 6 deletions packages/nodes-base/nodes/Amqp/AmqpTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ContainerOptions, create_container, EventContext, Message, ReceiverOpti

import { ITriggerFunctions } from 'n8n-core';
import {
deepCopy,
IDataObject,
INodeType,
INodeTypeDescription,
Expand Down Expand Up @@ -172,20 +173,20 @@ export class AmqpTrigger implements INodeType {

if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
// The buffer is not ready... Stringify and parse back to load it.
const cont = JSON.stringify(data.body.content);
data.body = String.fromCharCode.apply(null, JSON.parse(cont).data);
const cont = deepCopy(data.body.content);
data.body = String.fromCharCode.apply(null, cont.data);
}

if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
// The buffer is not ready... Stringify and parse back to load it.
const cont = JSON.stringify(data.body.content);
data.body = String.fromCharCode.apply(null, JSON.parse(cont).data);
const cont = deepCopy(data.body.content);
data.body = String.fromCharCode.apply(null, cont.data);
}

if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
// The buffer is not ready... Stringify and parse back to load it.
const content = JSON.stringify(data.body.content);
data.body = String.fromCharCode.apply(null, JSON.parse(content).data);
const content = deepCopy(data.body.content);
data.body = String.fromCharCode.apply(null, content.data);
}

if (options.jsonParseBody === true) {
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Aws/DynamoDB/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'n8n-core';

import {
deepCopy,
ICredentialDataDecryptedObject,
IDataObject,
IHttpRequestOptions,
Expand Down Expand Up @@ -93,7 +94,7 @@ export function copyInputItem(item: INodeExecutionData, properties: string[]): I
if (item.json[property] === undefined) {
newItem[property] = null;
} else {
newItem[property] = JSON.parse(JSON.stringify(item.json[property]));
newItem[property] = deepCopy(item.json[property]);
}
}
return newItem;
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Aws/DynamoDB/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDataObject, INodeExecutionData } from 'n8n-workflow';
import { deepCopy, IDataObject, INodeExecutionData } from 'n8n-workflow';

import {
AdjustedPutItem,
Expand Down Expand Up @@ -103,7 +103,7 @@ export function copyInputItem(item: INodeExecutionData, properties: string[]): I
if (item.json[property] === undefined) {
newItem[property] = null;
} else {
newItem[property] = JSON.parse(JSON.stringify(item.json[property]));
newItem[property] = deepCopy(item.json[property]);
}
}
return newItem;
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Crypto/Crypto.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { set } from 'lodash';
import { IExecuteFunctions } from 'n8n-core';

import {
deepCopy,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
Expand Down Expand Up @@ -483,7 +484,7 @@ export class Crypto implements INodeType {
if (dataPropertyName.includes('.')) {
// Uses dot notation so copy all data
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
json: deepCopy(item.json),
pairedItem: {
item: i,
},
Expand Down
5 changes: 3 additions & 2 deletions packages/nodes-base/nodes/DateTime/DateTime.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IExecuteFunctions } from 'n8n-core';

import {
deepCopy,
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
Expand Down Expand Up @@ -431,7 +432,7 @@ export class DateTime implements INodeType {
if (dataPropertyName.includes('.')) {
// Uses dot notation so copy all data
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
json: deepCopy(item.json),
pairedItem: {
item: i,
},
Expand Down Expand Up @@ -475,7 +476,7 @@ export class DateTime implements INodeType {
if (dataPropertyName.includes('.')) {
// Uses dot notation so copy all data
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
json: deepCopy(item.json),
pairedItem: {
item: i,
},
Expand Down
5 changes: 3 additions & 2 deletions packages/nodes-base/nodes/EditImage/EditImage.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BINARY_ENCODING, IExecuteFunctions } from 'n8n-core';
import {
deepCopy,
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
Expand Down Expand Up @@ -1269,8 +1270,8 @@ export class EditImage implements INodeType {
Object.assign(newItem.binary, item.binary);
// Make a deep copy of the binary data we change
if (newItem.binary![dataPropertyName as string]) {
newItem.binary![dataPropertyName as string] = JSON.parse(
JSON.stringify(newItem.binary![dataPropertyName as string]),
newItem.binary![dataPropertyName as string] = deepCopy(
newItem.binary![dataPropertyName as string],
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/nodes-base/nodes/Function/Function.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IExecuteFunctions } from 'n8n-core';
import {
deepCopy,
IBinaryKeyData,
IDataObject,
INodeExecutionData,
Expand Down Expand Up @@ -67,7 +68,7 @@ return items;`,
let items = this.getInputData();

// Copy the items as they may get changed in the functions
items = JSON.parse(JSON.stringify(items));
items = deepCopy(items);

// Assign item indexes
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
Expand All @@ -82,7 +83,7 @@ return items;`,
inputData[key] = cleanupData(inputData[key] as IDataObject);
} else {
// Is some special object like a Date so stringify
inputData[key] = JSON.parse(JSON.stringify(inputData[key]));
inputData[key] = deepCopy(inputData[key]);
}
}
});
Expand Down
5 changes: 3 additions & 2 deletions packages/nodes-base/nodes/FunctionItem/FunctionItem.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IExecuteFunctions } from 'n8n-core';
import {
deepCopy,
IBinaryKeyData,
IDataObject,
INodeExecutionData,
Expand Down Expand Up @@ -74,7 +75,7 @@ return item;`,
inputData[key] = cleanupData(inputData[key] as IDataObject);
} else {
// Is some special object like a Date so stringify
inputData[key] = JSON.parse(JSON.stringify(inputData[key]));
inputData[key] = deepCopy(inputData[key]);
}
}
});
Expand All @@ -89,7 +90,7 @@ return item;`,
item.index = itemIndex;

// Copy the items as they may get changed in the functions
item = JSON.parse(JSON.stringify(item));
item = deepCopy(item);

// Define the global objects for the custom function
const sandbox = {
Expand Down
5 changes: 3 additions & 2 deletions packages/nodes-base/nodes/Markdown/Markdown.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IExecuteFunctions } from 'n8n-core';

import {
deepCopy,
IDataObject,
INodeExecutionData,
INodeType,
Expand Down Expand Up @@ -590,7 +591,7 @@ export class Markdown implements INodeType {

const markdownFromHTML = NodeHtmlMarkdown.translate(html, markdownOptions);

const newItem = JSON.parse(JSON.stringify(items[i].json));
const newItem = deepCopy(items[i].json);
set(newItem, destinationKey, markdownFromHTML);
returnData.push(newItem);
}
Expand All @@ -605,7 +606,7 @@ export class Markdown implements INodeType {
Object.keys(options).forEach((key) => converter.setOption(key, options[key]));
const htmlFromMarkdown = converter.makeHtml(markdown);

const newItem = JSON.parse(JSON.stringify(items[i].json));
const newItem = deepCopy(items[i].json);
set(newItem, destinationKey, htmlFromMarkdown);

returnData.push(newItem);
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Merge/v1/MergeV1.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { get } from 'lodash';
import { IExecuteFunctions } from 'n8n-core';

import {
deepCopy,
GenericValue,
INodeExecutionData,
INodeType,
Expand Down Expand Up @@ -427,7 +428,7 @@ export class MergeV1 implements INodeType {
continue;
} else if (mode === 'mergeByKey') {
// Copy the entry as the data gets changed
entry = JSON.parse(JSON.stringify(entry));
entry = deepCopy(entry);

for (key of Object.keys(copyData[referenceValue as string].json)) {
if (key === propertyName2) {
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Microsoft/Sql/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDataObject, INodeExecutionData } from 'n8n-workflow';
import { deepCopy, IDataObject, INodeExecutionData } from 'n8n-workflow';
import { ITables } from './TableInterface';

/**
Expand All @@ -15,7 +15,7 @@ export function copyInputItem(item: INodeExecutionData, properties: string[]): I
if (item.json[property] === undefined) {
newItem[property] = null;
} else {
newItem[property] = JSON.parse(JSON.stringify(item.json[property]));
newItem[property] = deepCopy(item.json[property]);
}
}
return newItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BINARY_ENCODING } from 'n8n-core';

import { IExecuteFunctions } from 'n8n-core';
import {
deepCopy,
IBinaryData,
IDataObject,
INodeExecutionData,
Expand Down Expand Up @@ -364,7 +365,7 @@ export class MoveBinaryData implements INodeType {
newItem.json = JSON.parse(convertedValue);
} else {
// Does get added to existing data so copy it first
newItem.json = JSON.parse(JSON.stringify(item.json));
newItem.json = deepCopy(item.json);

if (options.keepAsBase64 !== true) {
convertedValue = iconv.decode(buffer, encoding, {
Expand All @@ -387,7 +388,7 @@ export class MoveBinaryData implements INodeType {
newItem.binary = item.binary;
} else {
// Binary data will change so copy it
newItem.binary = JSON.parse(JSON.stringify(item.binary));
newItem.binary = deepCopy(item.binary);
unset(newItem.binary, sourceKey);
}
} else if (mode === 'jsonToBinary') {
Expand All @@ -408,7 +409,7 @@ export class MoveBinaryData implements INodeType {

if (item.binary !== undefined) {
// Item already has binary data so copy it
newItem.binary = JSON.parse(JSON.stringify(item.binary));
newItem.binary = deepCopy(item.binary);
} else {
// Item does not have binary data yet so initialize empty
newItem.binary = {};
Expand Down Expand Up @@ -447,7 +448,7 @@ export class MoveBinaryData implements INodeType {
} else {
// Data should not be kept and only one key has to get removed. So copy all
// data and then remove the not needed one
newItem.json = JSON.parse(JSON.stringify(item.json));
newItem.json = deepCopy(item.json);
const sourceKey = this.getNodeParameter('sourceKey', itemIndex) as string;

unset(newItem.json, sourceKey);
Expand Down
17 changes: 13 additions & 4 deletions packages/nodes-base/nodes/MySql/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ICredentialDataDecryptedObject, IDataObject, ILoadOptionsFunctions, INodeExecutionData, INodeListSearchResult } from 'n8n-workflow';
import {
deepCopy,
ICredentialDataDecryptedObject,
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodeListSearchResult,
} from 'n8n-workflow';
import mysql2 from 'mysql2/promise';

/**
Expand All @@ -17,14 +24,16 @@ export function copyInputItems(items: INodeExecutionData[], properties: string[]
if (item.json[property] === undefined) {
newItem[property] = null;
} else {
newItem[property] = JSON.parse(JSON.stringify(item.json[property]));
newItem[property] = deepCopy(item.json[property]);
}
}
return newItem;
});
}

export function createConnection(credentials: ICredentialDataDecryptedObject): Promise<mysql2.Connection> {
export function createConnection(
credentials: ICredentialDataDecryptedObject,
): Promise<mysql2.Connection> {
const { ssl, caCertificate, clientCertificate, clientPrivateKey, ...baseCredentials } =
credentials;

Expand Down Expand Up @@ -57,7 +66,7 @@ export async function searchTables(
ORDER BY table_name
`;
const [rows] = await connection.query(sql);
const results = (rows as IDataObject[]).map(r => ({
const results = (rows as IDataObject[]).map((r) => ({
name: r.TABLE_NAME as string,
value: r.TABLE_NAME as string,
}));
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Odoo/Odoo.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IExecuteFunctions } from 'n8n-core';
import { OptionsWithUri } from 'request';

import {
deepCopy,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
Expand Down Expand Up @@ -297,7 +298,7 @@ export class Odoo implements INodeType {

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
let items = this.getInputData();
items = JSON.parse(JSON.stringify(items));
items = deepCopy(items);
const returnData: IDataObject[] = [];
let responseData;

Expand Down
10 changes: 8 additions & 2 deletions packages/nodes-base/nodes/RenameKeys/RenameKeys.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { IExecuteFunctions } from 'n8n-core';
import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
import {
deepCopy,
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';

import { get, set, unset } from 'lodash';
import { options } from 'rhea';
Expand Down Expand Up @@ -165,7 +171,7 @@ export class RenameKeys implements INodeType {

// Copy the whole JSON data as data on any level can be renamed
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
json: deepCopy(item.json),
pairedItem: {
item: itemIndex,
},
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Set/Set.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IExecuteFunctions } from 'n8n-core';
import {
deepCopy,
IDataObject,
INodeExecutionData,
INodeParameters,
Expand Down Expand Up @@ -162,7 +163,7 @@ export class Set implements INodeType {
Object.assign(newItem.binary, item.binary);
}

newItem.json = JSON.parse(JSON.stringify(item.json));
newItem.json = deepCopy(item.json);
}

// Add boolean values
Expand Down
Loading

0 comments on commit 46a43e4

Please sign in to comment.