1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297import {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import {
generateCreditCard,
generateNanoid,
generateRandomAddress,
generateRandomEmail,
generateRandomUser,
generateUUID,
} 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"] + ": " + $parameter["operation"]}}',
description: 'Causes problems intentionally',
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: 'User Data',
value: 'user',
},
{
name: 'Email',
value: 'email',
},
{
name: 'Address',
value: 'address',
},
{
name: 'Credit Card',
value: 'creditCard',
},
{
name: 'UUIDs',
value: 'uuid',
},
{
name: 'NanoIds',
value: 'nanoid',
},
],
default: 'user',
displayOptions: {
show: {
category: ['randomData'],
},
},
},
{
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: '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: items[i].pairedItem,
};
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 'nanoid':
randomFn = generateNanoid;
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: items[i].pairedItem,
});
}
}
break;
default:
break;
}
} catch (error) {
console.log(error);
if (this.continueOnFail()) {
returnData.push({ error: error.message, json: {}, itemIndex: i });
continue;
}
throw error;
}
}
return this.prepareOutputData(returnData);
}
}