feat(DebugHelper Node): Fix and include in main app (#6406)

* improve node a bit

* fixing continueOnFail() ton contain error in json

* improve pairedItem

* fix random data returning object results

* fix nanoId length typo

* update pnpm-lock file

---------

Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
Michael Auerswald 2023-06-20 10:47:15 +02:00 committed by GitHub
parent 6ccab3eaaa
commit 18f588444f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 565 additions and 6 deletions

View file

@ -0,0 +1,10 @@
{
"node": "n8n-nodes-base.debughelper",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Development"],
"resources": {
"credentialDocumentation": [],
"primaryDocumentation": []
}
}

View file

@ -0,0 +1,374 @@
import type {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
import {
generateCreditCard,
generateIPv4,
generateIPv6,
generateLocation,
generateMAC,
generateNanoid,
generateRandomAddress,
generateRandomEmail,
generateRandomUser,
generateURL,
generateUUID,
generateVersion,
} from './randomData';
import { setSeed, array as mfArray } from 'minifaker';
import { generateGarbageMemory, runGarbageCollector } from './functions';
export class DebugHelper implements INodeType {
description: INodeTypeDescription = {
displayName: 'DebugHelper',
name: 'debugHelper',
icon: 'file:DebugHelper.svg',
group: ['output'],
subtitle: '={{$parameter["category"]}}',
description: 'Causes problems intentionally and generates useful data for debugging',
version: 1,
defaults: {
name: 'DebugHelper',
},
inputs: ['main'],
outputs: ['main'],
credentials: [],
properties: [
{
displayName: 'Category',
name: 'category',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Do Nothing',
value: 'doNothing',
description: 'Does nothing',
},
{
name: 'Throw Error',
value: 'throwError',
description: 'Throws an error with the specified type and message',
},
{
name: 'Out Of Memory',
value: 'oom',
description: 'Generates a large amount of memory to cause an out of memory error',
},
{
name: 'Generate Random Data',
value: 'randomData',
description: 'Generates random data sets',
},
],
default: 'throwError',
},
{
displayName: 'Error Type',
name: 'throwErrorType',
type: 'options',
noDataExpression: true,
options: [
{
name: 'NodeApiError',
value: 'NodeApiError',
},
{
name: 'NodeOperationError',
value: 'NodeOperationError',
},
{
name: 'Error',
value: 'Error',
},
],
default: 'NodeApiError',
displayOptions: {
show: {
category: ['throwError'],
},
},
},
{
displayName: 'Error Message',
name: 'throwErrorMessage',
type: 'string',
default: 'Node has thrown an error',
description: 'The message to send as part of the error',
displayOptions: {
show: {
category: ['throwError'],
},
},
},
{
displayName: 'Memory Size to Generate',
name: 'memorySizeValue',
type: 'number',
default: 10,
description: 'The approximate amount of memory to generate. Be generous...',
displayOptions: {
show: {
category: ['oom'],
},
},
},
{
displayName: 'Data Type',
name: 'randomDataType',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Address',
value: 'address',
},
{
name: 'Coordinates',
value: 'latLong',
},
{
name: 'Credit Card',
value: 'creditCard',
},
{
name: 'Email',
value: 'email',
},
{
name: 'IPv4',
value: 'ipv4',
},
{
name: 'IPv6',
value: 'ipv6',
},
{
name: 'MAC',
value: 'macAddress',
},
{
name: 'NanoIds',
value: 'nanoid',
},
{
name: 'URL',
value: 'url',
},
{
name: 'User Data',
value: 'user',
},
{
name: 'UUID',
value: 'uuid',
},
{
name: 'Version',
value: 'semver',
},
],
default: 'user',
displayOptions: {
show: {
category: ['randomData'],
},
},
},
{
displayName: 'NanoId Alphabet',
name: 'nanoidAlphabet',
type: 'string',
default: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
description: 'The alphabet to use for generating the nanoIds',
displayOptions: {
show: {
category: ['randomData'],
randomDataType: ['nanoid'],
},
},
},
{
displayName: 'NanoId Length',
name: 'nanoidLength',
type: 'string',
default: '16',
description: 'The length of each nanoIds',
displayOptions: {
show: {
category: ['randomData'],
randomDataType: ['nanoid'],
},
},
},
{
displayName: 'Seed',
name: 'randomDataSeed',
type: 'string',
default: '',
placeholder: 'Leave empty for random seed',
description:
'If set, seed to use for generating the data (same seed will generate the same data)',
displayOptions: {
show: {
category: ['randomData'],
},
},
},
{
displayName: 'Number of Items to Generate',
name: 'randomDataCount',
type: 'number',
default: 10,
description: 'The number of random data items to generate into an array',
displayOptions: {
show: {
category: ['randomData'],
},
},
},
{
displayName: 'Output as Single Array',
name: 'randomDataSingleArray',
type: 'boolean',
default: false,
description: 'Whether to output a single array instead of multiple items',
displayOptions: {
show: {
category: ['randomData'],
},
},
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const category = this.getNodeParameter('category', 0) as string;
for (let i = 0; i < items.length; i++) {
try {
switch (category) {
case 'doNothing':
// as it says on the tin...
break;
case 'throwError':
const throwErrorType = this.getNodeParameter('throwErrorType', 0) as string;
const throwErrorMessage = this.getNodeParameter('throwErrorMessage', 0) as string;
switch (throwErrorType) {
case 'NodeApiError':
throw new NodeApiError(
this.getNode(),
{ message: throwErrorMessage },
{ message: throwErrorMessage },
);
case 'NodeOperationError':
throw new NodeOperationError(this.getNode(), throwErrorMessage, {
message: throwErrorMessage,
});
case 'Error':
// eslint-disable-next-line n8n-nodes-base/node-execute-block-wrong-error-thrown
throw new Error(throwErrorMessage);
default:
break;
}
case 'oom':
const memorySizeValue = this.getNodeParameter('memorySizeValue', 0) as number;
runGarbageCollector();
const memUsed = generateGarbageMemory(memorySizeValue);
items[i].json = memUsed;
returnData.push(items[i]);
break;
case 'randomData':
const randomDataType = this.getNodeParameter('randomDataType', 0) as string;
const randomDataCount = this.getNodeParameter('randomDataCount', 0) as number;
const randomDataSeed = this.getNodeParameter('randomDataSeed', 0) as string;
const randomDataSingleArray = this.getNodeParameter(
'randomDataSingleArray',
0,
) as boolean;
const newItem: INodeExecutionData = {
json: {},
pairedItem: { item: i },
};
if (randomDataSeed !== '') {
setSeed(randomDataSeed);
}
let randomFn: () => any = generateRandomUser;
switch (randomDataType) {
case 'user':
randomFn = generateRandomUser;
break;
case 'email':
randomFn = generateRandomEmail;
break;
case 'address':
randomFn = generateRandomAddress;
break;
case 'creditCard':
randomFn = generateCreditCard;
break;
case 'uuid':
randomFn = generateUUID;
break;
case 'macAddress':
randomFn = generateMAC;
break;
case 'ipv4':
randomFn = generateIPv4;
break;
case 'ipv6':
randomFn = generateIPv6;
break;
case 'latLong':
randomFn = generateLocation;
break;
case 'semver':
randomFn = generateVersion;
break;
case 'url':
randomFn = generateURL;
break;
case 'nanoid':
const nanoidAlphabet = this.getNodeParameter('nanoidAlphabet', 0) as string;
const nanoidLength = this.getNodeParameter('nanoidLength', 0) as string;
randomFn = () => generateNanoid(nanoidAlphabet, nanoidLength);
break;
}
const generatedItems = mfArray(randomDataCount, randomFn);
if (randomDataSingleArray) {
newItem.json = { generatedItems };
returnData.push(newItem);
} else {
for (const generatedItem of generatedItems) {
returnData.push({
json: generatedItem,
pairedItem: { item: i },
});
}
}
break;
default:
break;
}
} catch (error) {
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ error: error.message }),
{ itemData: { item: i } },
);
returnData.push(...executionErrorData);
continue;
}
throw error;
}
}
return this.prepareOutputData(returnData);
}
}

View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
<svg height="800px" width="800px" version="1.1" id="Layer_1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512" xml:space="preserve">
<path style="fill:#F4A026;" d="M345,167c32.953,0,46.333,40.047,46.333,73v100.333c0,74.375-60.292,134.667-134.667,134.667l0,0
C182.292,475,122,414.708,122,340.333V240c0-32.953,13.38-73,46.333-73H345z"/>
<path style="fill:#CA463D;" d="M309,200.333H204.333C185.372,200.333,170,184.961,170,166l0,0c0-39.948,32.385-72.333,72.333-72.333
H271c39.948,0,72.333,32.385,72.333,72.333l0,0C343.333,184.961,327.961,200.333,309,200.333z"/>
<path d="M498.667,290.667H404V240c0-1.016,0.313-2.036,0.291-3.055C452.4,231.927,480,200.272,480,148.333v-12
c0-7.364-5.971-13.333-13.333-13.333s-13.333,5.969-13.333,13.333v12c0,38.399-17.005,58.821-51.885,62.167
c-6.069-27.025-20.875-50.381-45.537-55.7c-3.745-28.54-21.413-52.689-46.115-65.227c10.321-10.501,16.871-24.887,16.871-40.74V37
c0-7.364-5.971-13.333-13.333-13.333S300,29.636,300,37v11.833C300,66.203,285.536,80,268.167,80h-23
c-17.369,0-31.833-13.797-31.833-31.167V37c0-7.364-5.971-13.333-13.333-13.333S186.667,29.636,186.667,37v11.833
c0,15.853,6.549,30.239,16.871,40.741c-24.701,12.537-42.453,36.687-46.199,65.227c-24.695,5.324-39.465,28.736-45.519,55.808
c-35.759-2.96-53.153-23.403-53.153-62.276v-12c0-7.364-5.971-13.333-13.333-13.333S32,128.969,32,136.333v12
c0,52.415,27.439,84.168,76.375,88.739C108.353,238.048,108,239.025,108,240v50.667H13.333C5.971,290.667,0,296.636,0,304
c0,7.364,5.971,13.333,13.333,13.333H108v23c0,10.628,1.469,20.993,3.608,30.992C60.659,374.777,32,406.773,32,460.333v12
c0,7.364,5.971,13.333,13.333,13.333s13.333-5.969,13.333-13.333v-12c0-41.795,20.151-62.291,61.565-62.649
c22.451,53.208,75.151,90.649,136.435,90.649c61.276,0,113.971-37.432,136.425-90.629c40.519,0.784,60.241,21.283,60.241,62.629v12
c0,7.364,5.971,13.333,13.333,13.333S480,479.697,480,472.333v-12c0-53.1-28.823-85.013-78.96-88.921
c2.151-10.025,2.96-20.421,2.96-31.079v-23h94.667c7.363,0,13.333-5.969,13.333-13.333C512,296.636,506.029,290.667,498.667,290.667
z M242.333,106.667h2.833h23H271c32.532,0,59,26.468,59,59c0,11.58-9.42,21-21,21H204.333c-11.58,0-21-9.42-21-21
C183.333,133.135,209.801,106.667,242.333,106.667z M134.667,340.333V240c0-20.793,6.948-50.531,24.483-58.035
c6.627,18.368,24.56,31.368,45.184,31.368h38.333v247.521C182.333,453.891,134.667,402.501,134.667,340.333z M269.333,461.007
V213.333H309c20.624,0,37.891-13,44.517-31.368c17.535,7.504,23.816,37.241,23.816,58.035v100.333
C377.333,402.96,330.307,454.653,269.333,461.007z"/>
</svg>

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

@ -0,0 +1,30 @@
import { setFlagsFromString } from 'v8';
import { runInNewContext } from 'vm';
export const runGarbageCollector = () => {
try {
setFlagsFromString('--expose_gc');
const gc = runInNewContext('gc'); // nocommit
gc();
} catch (error) {
console.log(error);
}
};
export const generateGarbageMemory = (sizeInMB: number, onHeap = true) => {
const divider = onHeap ? 8 : 1;
const size = Math.max(1, Math.floor((sizeInMB * 1024 * 1024) / divider));
if (onHeap) {
// arrays are allocated on the heap
// size in this case is only an approximation...
const array = Array(size);
array.fill(0);
} else {
const array = new Uint8Array(size);
array.fill(0);
}
// const used = process.memoryUsage().heapUsed / 1024 / 1024;
// const external = process.memoryUsage().external / 1024 / 1024;
// console.log(`heap: ${used} MB / external: ${external} MB`);
return { ...process.memoryUsage() };
};

View file

@ -0,0 +1,101 @@
import {
firstName,
lastName,
streetAddress,
cityName,
zipCode,
state,
country,
password,
creditCardNumber,
creditCardCVV,
email,
boolean,
uuid,
nanoId,
domainUrl,
semver,
latLong,
macAddress,
ip,
ipv6,
number,
} from 'minifaker';
import 'minifaker/locales/en';
export function generateRandomUser() {
return {
uid: uuid.v4(),
email: email(),
firstname: firstName(),
lastname: lastName(),
password: password(),
};
}
export function generateRandomAddress() {
return {
firstname: firstName(),
lastname: lastName(),
street: streetAddress(),
city: cityName(),
zip: zipCode({ format: '#####' }),
state: state(),
country: country(),
};
}
export function generateRandomEmail() {
return {
email: email(),
confirmed: boolean(),
};
}
export function generateUUID() {
return { uuid: uuid.v4() };
}
export function generateNanoid(customAlphabet: string, length: string) {
return { nanoId: nanoId.customAlphabet(customAlphabet, parseInt(length, 10))().toString() };
}
export function generateCreditCard() {
return {
type: boolean() ? 'MasterCard' : 'Visa',
number: creditCardNumber(),
ccv: creditCardCVV(),
exp: `${number({ min: 1, max: 12, float: false }).toString().padStart(2, '0')}/${number({
min: 1,
max: 40,
float: false,
})
.toString()
.padStart(2, '0')}`,
holder_name: `${firstName()} ${lastName()}`,
};
}
export function generateURL() {
return { url: domainUrl() };
}
export function generateIPv4() {
return { ip: ip() };
}
export function generateIPv6() {
return { ipv6: ipv6() };
}
export function generateMAC() {
return { mac: macAddress() };
}
export function generateLocation() {
return { location: latLong() };
}
export function generateVersion() {
return { version: semver() };
}

View file

@ -424,6 +424,7 @@
"dist/nodes/CustomerIo/CustomerIo.node.js",
"dist/nodes/CustomerIo/CustomerIoTrigger.node.js",
"dist/nodes/DateTime/DateTime.node.js",
"dist/nodes/DebugHelper/DebugHelper.node.js",
"dist/nodes/DeepL/DeepL.node.js",
"dist/nodes/Demio/Demio.node.js",
"dist/nodes/Dhl/Dhl.node.js",
@ -806,6 +807,7 @@
"lossless-json": "^1.0.4",
"luxon": "^3.3.0",
"mailparser": "^3.2.0",
"minifaker": "^1.34.1",
"moment": "~2.29.2",
"moment-timezone": "^0.5.28",
"mongodb": "^4.9.1",
@ -813,6 +815,7 @@
"mssql": "^8.1.2",
"mysql2": "~2.3.0",
"n8n-workflow": "workspace:*",
"nanoid": "^3.3.6",
"node-html-markdown": "^1.1.3",
"node-ssh": "^12.0.0",
"nodemailer": "^6.7.1",

View file

@ -1087,6 +1087,9 @@ importers:
mailparser:
specifier: ^3.2.0
version: 3.5.0
minifaker:
specifier: ^1.34.1
version: 1.34.1
moment:
specifier: ~2.29.2
version: 2.29.4
@ -1108,6 +1111,9 @@ importers:
n8n-workflow:
specifier: workspace:*
version: link:../workflow
nanoid:
specifier: ^3.3.6
version: 3.3.6
node-html-markdown:
specifier: ^1.1.3
version: 1.2.0
@ -6734,7 +6740,7 @@ packages:
fetch-retry: 5.0.3
fs-extra: 11.1.0
isomorphic-unfetch: 3.1.0
nanoid: 3.3.4
nanoid: 3.3.6
read-pkg-up: 7.0.1
transitivePeerDependencies:
- encoding
@ -7647,7 +7653,6 @@ packages:
/@types/uuid@8.3.4:
resolution: {integrity: sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==}
dev: true
/@types/uuid@9.0.0:
resolution: {integrity: sha512-kr90f+ERiQtKWMz5rP32ltJ/BtULDI5RVO0uavn1HQUOwjx0R1h0rnDYNL0CepF1zL5bSY6FISAfd9tOdDhU5Q==}
@ -14028,7 +14033,7 @@ packages:
/ics@2.40.0:
resolution: {integrity: sha512-PPkE9ij60sGhqdTxZZzsXQPB/TCXAB/dD3NqUf1I/GkbJzPeJHHMzaoMQiYAsm1pFaHRp2OIhFDgUBihkk8s/w==}
dependencies:
nanoid: 3.3.4
nanoid: 3.3.6
yup: 0.32.11
dev: false
@ -16575,6 +16580,14 @@ packages:
engines: {node: '>=4'}
dev: true
/minifaker@1.34.1:
resolution: {integrity: sha512-O9+c6GaUETgtKe65bJkpDTJxGcAALiUPqJtDv97dT3o0uP2HmyUVEguEGm6PLKuoSzZUmHqSTZ4cS7m8xKFEAg==}
dependencies:
'@types/uuid': 8.3.4
nanoid: 3.3.6
uuid: 8.3.2
dev: false
/minimalistic-assert@1.0.1:
resolution: {integrity: sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A==}
dev: false
@ -16933,8 +16946,8 @@ packages:
resolution: {integrity: sha512-wynEP02LmIbLpcYw8uBKpcfF6dmg2vcpKqxeH5UcoKEYdExslsdUA4ugFauuaeYdTB76ez6gJW8XAZ6CgkXYxA==}
dev: false
/nanoid@3.3.4:
resolution: {integrity: sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==}
/nanoid@3.3.6:
resolution: {integrity: sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==}
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
hasBin: true
@ -18241,7 +18254,7 @@ packages:
resolution: {integrity: sha512-tP7u/Sn/dVxK2NnruI4H9BG+x+Wxz6oeZ1cJ8P6G/PZY0IKk4k/63TDsQf2kQq3+qoJeLm2kIBUNlZe3zgb4Zg==}
engines: {node: ^10 || ^12 || >=14}
dependencies:
nanoid: 3.3.4
nanoid: 3.3.6
picocolors: 1.0.0
source-map-js: 1.0.2