Skip to content

Commit b7966ce

Browse files
committed
Don't do the funky require thing
🤦‍♂️ This is better
1 parent b54d7cf commit b7966ce

File tree

2 files changed

+75
-79
lines changed

2 files changed

+75
-79
lines changed

packages/node/src/eventbuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { node } from './stack-parser';
1616
* Extracts stack frames from the error.stack string
1717
*/
1818
export function extractStackFromError(error: Error): StackFrame[] {
19-
return createStackParser(node(require))(error.stack || '');
19+
return createStackParser(node)(error.stack || '');
2020
}
2121

2222
/**

packages/node/src/stack-parser.ts

Lines changed: 74 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
import { basename, dirname, StackLineParser } from '@sentry/utils';
22

33
/** Gets the module */
4-
function getModule(nodeRequire: NodeRequire | undefined, filename: string | undefined): string | undefined {
5-
if (!filename || !nodeRequire) {
4+
function getModule(filename: string | undefined): string | undefined {
5+
if (!filename) {
66
return;
77
}
88

9-
const base = `${
10-
(nodeRequire.main && nodeRequire.main.filename && dirname(nodeRequire.main.filename)) || global.process.cwd()
11-
}/`;
9+
const base = `${(require?.main?.filename && dirname(require.main.filename)) || global.process.cwd()}/`;
1210

1311
// It's specifically a module
1412
const file = basename(filename, '.js');
15-
// eslint-disable-next-line no-param-reassign
16-
filename = dirname(filename);
17-
let n = filename.lastIndexOf('/node_modules/');
13+
14+
const path = dirname(filename);
15+
let n = path.lastIndexOf('/node_modules/');
1816
if (n > -1) {
1917
// /node_modules/ is 14 chars
20-
return `${filename.substr(n + 14).replace(/\//g, '.')}:${file}`;
18+
return `${path.substr(n + 14).replace(/\//g, '.')}:${file}`;
2119
}
2220
// Let's see if it's a part of the main module
2321
// To be a part of main module, it has to share the same base
24-
n = `${filename}/`.lastIndexOf(base, 0);
22+
n = `${path}/`.lastIndexOf(base, 0);
2523

2624
if (n === 0) {
27-
let moduleName = filename.substr(base.length).replace(/\//g, '.');
25+
let moduleName = path.substr(base.length).replace(/\//g, '.');
2826
if (moduleName) {
2927
moduleName += ':';
3028
}
@@ -37,83 +35,81 @@ function getModule(nodeRequire: NodeRequire | undefined, filename: string | unde
3735
const FILENAME_MATCH = /^\s*[-]{4,}$/;
3836
const FULL_MATCH = /at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
3937

40-
export const node = (nodeRequire: NodeRequire | undefined): StackLineParser => {
41-
return (line: string) => {
42-
if (line.match(FILENAME_MATCH)) {
43-
return {
44-
filename: line,
45-
};
46-
}
47-
48-
const lineMatch = line.match(FULL_MATCH);
49-
if (!lineMatch) {
50-
return undefined;
51-
}
52-
53-
let object: string | undefined;
54-
let method: string | undefined;
55-
let functionName: string | undefined;
56-
let typeName: string | undefined;
57-
let methodName: string | undefined;
38+
export const node: StackLineParser = (line: string) => {
39+
if (line.match(FILENAME_MATCH)) {
40+
return {
41+
filename: line,
42+
};
43+
}
5844

59-
if (lineMatch[1]) {
60-
functionName = lineMatch[1];
45+
const lineMatch = line.match(FULL_MATCH);
46+
if (!lineMatch) {
47+
return undefined;
48+
}
6149

62-
let methodStart = functionName.lastIndexOf('.');
63-
if (functionName[methodStart - 1] === '.') {
64-
// eslint-disable-next-line no-plusplus
65-
methodStart--;
66-
}
50+
let object: string | undefined;
51+
let method: string | undefined;
52+
let functionName: string | undefined;
53+
let typeName: string | undefined;
54+
let methodName: string | undefined;
6755

68-
if (methodStart > 0) {
69-
object = functionName.substr(0, methodStart);
70-
method = functionName.substr(methodStart + 1);
71-
const objectEnd = object.indexOf('.Module');
72-
if (objectEnd > 0) {
73-
functionName = functionName.substr(objectEnd + 1);
74-
object = object.substr(0, objectEnd);
75-
}
76-
}
77-
typeName = undefined;
78-
}
56+
if (lineMatch[1]) {
57+
functionName = lineMatch[1];
7958

80-
if (method) {
81-
typeName = object;
82-
methodName = method;
59+
let methodStart = functionName.lastIndexOf('.');
60+
if (functionName[methodStart - 1] === '.') {
61+
// eslint-disable-next-line no-plusplus
62+
methodStart--;
8363
}
8464

85-
if (method === '<anonymous>') {
86-
methodName = undefined;
87-
functionName = undefined;
65+
if (methodStart > 0) {
66+
object = functionName.substr(0, methodStart);
67+
method = functionName.substr(methodStart + 1);
68+
const objectEnd = object.indexOf('.Module');
69+
if (objectEnd > 0) {
70+
functionName = functionName.substr(objectEnd + 1);
71+
object = object.substr(0, objectEnd);
72+
}
8873
}
74+
typeName = undefined;
75+
}
8976

90-
let fn;
91-
try {
92-
fn = functionName || `${typeName}.${methodName || '<anonymous>'}`;
93-
} catch (_) {
94-
// This seems to happen sometimes when using 'use strict',
95-
// stemming from `getTypeName`.
96-
// [TypeError: Cannot read property 'constructor' of undefined]
97-
fn = '<anonymous>';
98-
}
77+
if (method) {
78+
typeName = object;
79+
methodName = method;
80+
}
9981

100-
const filename = lineMatch[2];
101-
const isNative = lineMatch[5] === 'native';
102-
const isInternal =
103-
isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1);
82+
if (method === '<anonymous>') {
83+
methodName = undefined;
84+
functionName = undefined;
85+
}
10486

105-
// in_app is all that's not an internal Node function or a module within node_modules
106-
// note that isNative appears to return true even for node core libraries
107-
// see https://github.com/getsentry/raven-node/issues/176
108-
const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/');
87+
let fn;
88+
try {
89+
fn = functionName || `${typeName}.${methodName || '<anonymous>'}`;
90+
} catch (_) {
91+
// This seems to happen sometimes when using 'use strict',
92+
// stemming from `getTypeName`.
93+
// [TypeError: Cannot read property 'constructor' of undefined]
94+
fn = '<anonymous>';
95+
}
10996

110-
return {
111-
filename,
112-
module: getModule(nodeRequire, filename),
113-
function: fn,
114-
lineno: parseInt(lineMatch[3], 10) || undefined,
115-
colno: parseInt(lineMatch[4], 10) || undefined,
116-
in_app,
117-
};
97+
const filename = lineMatch[2];
98+
const isNative = lineMatch[5] === 'native';
99+
const isInternal =
100+
isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1);
101+
102+
// in_app is all that's not an internal Node function or a module within node_modules
103+
// note that isNative appears to return true even for node core libraries
104+
// see https://github.com/getsentry/raven-node/issues/176
105+
const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/');
106+
107+
return {
108+
filename,
109+
module: getModule(filename),
110+
function: fn,
111+
lineno: parseInt(lineMatch[3], 10) || undefined,
112+
colno: parseInt(lineMatch[4], 10) || undefined,
113+
in_app,
118114
};
119115
};

0 commit comments

Comments
 (0)