noOp variable in n8n source code.
In this article, we review a variable named noOp. This is something I found in logger.ts file in the n8n source code.
When I saw this line below, what it tells me straight away is that it’s something to do with no operation :
const noOp = () => {};
When you look at this, it’s doing nothing. I mean, it’s just an arrow function that returns an empty object So you might be wondering how is this used? So that’s what we will find out.
When I click on the no-op, these are the symbols I found on the right side and in the same file, I see that this variable is used at line 103.
When I click and jump to that line, you will see that this is inside a function called setLevel.
private setLevel() {
const { levels } = this.internalLogger;
for (const logLevel of LOG_LEVELS) {
if (levels[logLevel] > levels[this.level]) {
// numerically higher (less severe) log levels become no-op
// to prevent overhead from `callsites` calls
Object.defineProperty(this, logLevel, { value: noOp });
}
}
}
and then you get the levels from internalLogger
. There’s LOG_LEVELS, obviously that’s a constant we are not looking into as we are only focusing on the noOp, So this noop is an arrow function that returns an empty object But obviously I would be more interested in the comment that is defined just above this line 103.
So what does this comment tell us?
// numerically higher (less severe) log levels become no-op
// to prevent overhead fromcallsites
calls
Tbh, I don’t know how exactly no-op prevents overhead from callsites
calls. I know for a fact that callsites is something you would use to get the trace in the v8 JavaScript engine that is used in the Google Chrome. for example, callsites provides you with API like get function name. You can also find out the function that’s calling this function there are bunch of other APIs too.
But again we are focusing on the no-op so obviously the value is an empty object and they have used defined property.
Object.defineProperty(this, logLevel, { value: noOp });
noOp usage in workflow package
import type { Logger } from './Interfaces';
const noOp = () => {};
export let error: Logger['error'] = noOp;
export let warn: Logger['warn'] = noOp;
export let info: Logger['info'] = noOp;
export let debug: Logger['debug'] = noOp;
export const init = (logger: Logger) => {
error = (message, meta) => logger.error(message, meta);
warn = (message, meta) => logger.warn(message, meta);
info = (message, meta) => logger.info(message, meta);
debug = (message, meta) => logger.debug(message, meta);
};
This above code snippet is picked from packages/workflow/src/LoggerProxy.ts.
NoOp.node.ts
import type {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeConnectionTypes } from 'n8n-workflow';
export class NoOp implements INodeType {
description: INodeTypeDescription = {
displayName: 'No Operation, do nothing',
name: 'noOp',
icon: 'fa:arrow-right',
iconColor: 'gray',
group: ['organization'],
version: 1,
description: 'No Operation',
defaults: {
name: 'No Operation, do nothing',
color: '#b0b0b0',
},
inputs: [NodeConnectionTypes.Main],
outputs: [NodeConnectionTypes.Main],
properties: [],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
return [items];
}
}
This above code is picked from packages/nodes-base/nodes/…/NoOp.node.ts.
This is just another example picked from n8n codebase for the noOp.
About me:
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.
Configure features such as Changesets in your Next.js project using Think Throo CLI.
Email — ramu@thinkthroo.com
My Github — https://github.com/ramu-narasinga
My website — https://ramunarasinga.com
My YouTube channel — https://www.youtube.com/@ramu-narasinga
Learning platform — https://thinkthroo.com
Codebase Architecture — https://app.thinkthroo.com/architecture
Best practices — https://app.thinkthroo.com/best-practices
Production-grade projects — https://app.thinkthroo.com/production-grade-projects
References
-
https://github.com/n8n-io/n8n/blob/master/packages/core/src/logging/logger.ts#L18
-
https://github.com/n8n-io/n8n/blob/master/packages/workflow/src/LoggerProxy.ts#L4
-
https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/NoOp/NoOp.node.ts#L12