Skip to content

ref(node): Move stack parser to utils so it can be used from Electron #5286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
extractRequestData as _extractRequestData,
getGlobalObject,
logger,
nodeStackLineParser,
stackParserFromStackParserOptions,
} from '@sentry/utils';
import * as cookie from 'cookie';
Expand All @@ -19,7 +20,6 @@ import * as url from 'url';
import { NodeClient } from './client';
import { Console, ContextLines, Http, LinkedErrors, OnUncaughtException, OnUnhandledRejection } from './integrations';
import { getModule } from './module';
import { nodeStackLineParser } from './stack-parser';
import { makeNodeTransport } from './transports';
import { NodeClientOptions, NodeOptions } from './types';

Expand Down
89 changes: 0 additions & 89 deletions packages/node/src/stack-parser.ts

This file was deleted.

95 changes: 94 additions & 1 deletion packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StackFrame, StackLineParser, StackParser } from '@sentry/types';
import { StackFrame, StackLineParser, StackLineParserFn, StackParser } from '@sentry/types';

const STACKTRACE_LIMIT = 50;

Expand Down Expand Up @@ -94,3 +94,96 @@ export function getFunctionName(fn: unknown): string {
return defaultFunctionName;
}
}

type GetModuleFn = (filename: string | undefined) => string | undefined;

// eslint-disable-next-line complexity
function node(getModule?: GetModuleFn): StackLineParserFn {
const FILENAME_MATCH = /^\s*[-]{4,}$/;
const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;

// eslint-disable-next-line complexity
return (line: string) => {
if (line.match(FILENAME_MATCH)) {
return {
filename: line,
};
}

const lineMatch = line.match(FULL_MATCH);
if (!lineMatch) {
return undefined;
}

let object: string | undefined;
let method: string | undefined;
let functionName: string | undefined;
let typeName: string | undefined;
let methodName: string | undefined;

if (lineMatch[1]) {
functionName = lineMatch[1];

let methodStart = functionName.lastIndexOf('.');
if (functionName[methodStart - 1] === '.') {
// eslint-disable-next-line no-plusplus
methodStart--;
}

if (methodStart > 0) {
object = functionName.substr(0, methodStart);
method = functionName.substr(methodStart + 1);
const objectEnd = object.indexOf('.Module');
if (objectEnd > 0) {
functionName = functionName.substr(objectEnd + 1);
object = object.substr(0, objectEnd);
}
}
typeName = undefined;
}

if (method) {
typeName = object;
methodName = method;
}

if (method === '<anonymous>') {
methodName = undefined;
functionName = undefined;
}

if (functionName === undefined) {
methodName = methodName || '<anonymous>';
functionName = typeName ? `${typeName}.${methodName}` : methodName;
}

const filename = lineMatch[2]?.startsWith('file://') ? lineMatch[2].substr(7) : lineMatch[2];
const isNative = lineMatch[5] === 'native';
const isInternal =
isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1);

// in_app is all that's not an internal Node function or a module within node_modules
// note that isNative appears to return true even for node core libraries
// see https://github.com/getsentry/raven-node/issues/176
const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/');

return {
filename,
module: getModule?.(filename),
function: functionName,
lineno: parseInt(lineMatch[3], 10) || undefined,
colno: parseInt(lineMatch[4], 10) || undefined,
in_app,
};
};
}

/**
* Node.js stack line parser
*
* This is in @sentry/utils so it can be used from the Electron SDK in the browser for when `nodeIntegration == true`.
* This allows it to be used without referencing or importing any node specific code which causes bundlers to complain
*/
export function nodeStackLineParser(getModule?: GetModuleFn): StackLineParser {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave a comment why we are exporting this from utils instead of the node package?

return [90, node(getModule)];
}