Skip to content

Commit 2efe40d

Browse files
committed
Don't do the funky require thing
1 parent c644ae7 commit 2efe40d

File tree

2 files changed

+69
-71
lines changed

2 files changed

+69
-71
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(module.require))(error.stack || '');
2020
}
2121

2222
/**

packages/node/src/stack-parser.ts

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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

99
const base = `${
10-
(nodeRequire.main && nodeRequire.main.filename && dirname(nodeRequire.main.filename)) || global.process.cwd()
10+
(require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd()
1111
}/`;
1212

1313
// It's specifically a module
@@ -37,83 +37,81 @@ function getModule(nodeRequire: NodeRequire | undefined, filename: string | unde
3737
const FILENAME_MATCH = /^\s*[-]{4,}$/;
3838
const FULL_MATCH = /at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
3939

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-
}
40+
export const node: StackLineParser = (line: string) => {
41+
if (line.match(FILENAME_MATCH)) {
42+
return {
43+
filename: line,
44+
};
45+
}
5246

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;
47+
const lineMatch = line.match(FULL_MATCH);
48+
if (!lineMatch) {
49+
return undefined;
50+
}
5851

59-
if (lineMatch[1]) {
60-
functionName = lineMatch[1];
52+
let object: string | undefined;
53+
let method: string | undefined;
54+
let functionName: string | undefined;
55+
let typeName: string | undefined;
56+
let methodName: string | undefined;
6157

62-
let methodStart = functionName.lastIndexOf('.');
63-
if (functionName[methodStart - 1] === '.') {
64-
// eslint-disable-next-line no-plusplus
65-
methodStart--;
66-
}
58+
if (lineMatch[1]) {
59+
functionName = lineMatch[1];
6760

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;
61+
let methodStart = functionName.lastIndexOf('.');
62+
if (functionName[methodStart - 1] === '.') {
63+
// eslint-disable-next-line no-plusplus
64+
methodStart--;
7865
}
7966

80-
if (method) {
81-
typeName = object;
82-
methodName = method;
83-
}
84-
85-
if (method === '<anonymous>') {
86-
methodName = undefined;
87-
functionName = undefined;
67+
if (methodStart > 0) {
68+
object = functionName.substr(0, methodStart);
69+
method = functionName.substr(methodStart + 1);
70+
const objectEnd = object.indexOf('.Module');
71+
if (objectEnd > 0) {
72+
functionName = functionName.substr(objectEnd + 1);
73+
object = object.substr(0, objectEnd);
74+
}
8875
}
76+
typeName = undefined;
77+
}
8978

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-
}
79+
if (method) {
80+
typeName = object;
81+
methodName = method;
82+
}
9983

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

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/');
89+
let fn;
90+
try {
91+
fn = functionName || `${typeName}.${methodName || '<anonymous>'}`;
92+
} catch (_) {
93+
// This seems to happen sometimes when using 'use strict',
94+
// stemming from `getTypeName`.
95+
// [TypeError: Cannot read property 'constructor' of undefined]
96+
fn = '<anonymous>';
97+
}
10998

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

0 commit comments

Comments
 (0)