Blog
callsites usage in n8n source code to get callsites from the V8 stack trace API

callsites usage in n8n source code to get callsites from the V8 stack trace API

In this article, we review a function called callsites you see in the logger.ts.

There is an import at line 5, it is imported from call sites. 

import callsites from 'callsites';

By the way, this logger.ts is part of core package found in n8n so you might be wondering what n8n is so it’s a fair code workflow automation platform with native AI capabilities it also lets you combine visual building with custom code self host or cloud it’s called about 400 plus integrations so this tool automates things like if someone has submitted a type form and you want a notification coming into your Slack group. You can set up such automations. That’s just one example that I’ve picked for the N8n.

Callsites

So when I quickly Googled callsites NPM, I found this package and it basically lets you get callsites from v8 stack trace API.

Install

npm install callsites

Usage

import callsites from 'callsites';

function unicorn() {
 console.log(callsites()[0].getFileName());
 //=> '/Users/sindresorhus/dev/callsites/test.js'
}

unicorn();

API

Returns an array of callsite objects with the following methods:

  • getThis: Returns the value of this.

  • getTypeName: Returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property.

  • getFunction: Returns the current function.

  • getFunctionName: Returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context.

  • getMethodName: Returns the name of the property of this or one of its prototypes that holds the current function.

  • getFileName: If this function was defined in a script returns the name of the script.

  • getLineNumber: If this function was defined in a script returns the current line number.

  • getColumnNumber: If this function was defined in a script returns the current column number

  • getEvalOrigin: If this function was created using a call to eval returns a string representing the location where eval was called.

  • isToplevel: Returns true if this is a top-level invocation, that is, if it's a global object.

  • isEval: Returns true if this call takes place in code defined by a call to eval.

  • isNative: Returns true if this call is in native V8 code.

  • isConstructor: Returns true if this is a constructor call.

  • isAsync(): Returns true if this call is asynchronous (i.e. await, Promise.all(), or Promise.any()).

  • isPromiseAll(): Returns true if this is an asynchronous call to Promise.all().

  • getPromiseIndex(): Returns the index of the promise element that was followed in Promise.all() or Promise.any() for async stack traces, or null if the CallSite is not an asynchronous Promise.all() or Promise.any() call.

I copied the installation, usage and API from the documentation.

V8

V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others. It implements ECMAScript and WebAssembly, and runs on Windows, macOS, and Linux systems that use x64, IA-32, or ARM processors. V8 can be embedded into any C++ application

callsites usage in n8n

private log(level: LogLevel, message: string, metadata: LogMetadata) {
  const location: LogLocationMetadata = {};

  const caller = callsites().at(2); // zeroth and first are this file, second is caller

  if (caller !== undefined) {
   location.file = basename(caller.getFileName() ?? '');
   const fnName = caller.getFunctionName();
   if (fnName) location.function = fnName;
  }

  this.internalLogger.log(level, message, { ...metadata, ...location });
 }

This above code is picked from logger.ts.

const caller = callsites().at(2);

I got the below information from ChatGPT:

1. callsites()
A function from the callsites package that returns an array of call site 
objects (stack trace entries).

2. .at(2)
Retrieves the 3rd call site in the array (arrays are zero-indexed). That is:
- at(0) → current function
- at(1) → immediate caller
- at(2) → caller of the caller

3. caller
Now holds a CallSite object, which includes methods like:

- getFileName()
- getLineNumber()
- getColumnNumber()
- getFunctionName()

This information is used to log the function name and the file, evident from the below code:

const caller = callsites().at(2); // zeroth and first are this file, second is caller

if (caller !== undefined) {
 location.file = basename(caller.getFileName() ?? '');
 const fnName = caller.getFunctionName();
 if (fnName) location.function = fnName;
}

this.internalLogger.log(level, message, { ...metadata, ...location });

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

  1. https://github.com/sindresorhus/callsites

  2. https://github.com/n8n-io/n8n/blob/master/packages/core/src/logging/logger.ts#L85

  3. https://www.npmjs.com/package/callsites

  4. https://v8.dev/docs/stack-trace-api