Skip to content

Commit b77b920

Browse files
committed
ref(utils): use test instead of indexof
1 parent c025921 commit b77b920

File tree

2 files changed

+33
-45
lines changed

2 files changed

+33
-45
lines changed

packages/node/src/module.ts

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
1-
import { posix, sep } from 'path';
2-
3-
const isWindowsPlatform = sep === '\\';
1+
import { basename, dirname } from '@sentry/utils';
42

53
/** normalizes Windows paths */
6-
function normalizeWindowsPath(path: string): string {
4+
function normalizePath(path: string): string {
75
return path
86
.replace(/^[A-Z]:/, '') // remove Windows-style prefix
97
.replace(/\\/g, '/'); // replace all `\` instances with `/`
108
}
119

1210
/** Gets the module from a filename */
13-
export function getModule(
14-
filename: string | undefined,
15-
normalizeWindowsPathSeparator: boolean = isWindowsPlatform,
16-
): string | undefined {
11+
export function getModule(filename: string | undefined): string | undefined {
1712
if (!filename) {
1813
return;
1914
}
2015

21-
const normalizedFilename = normalizeWindowsPathSeparator ? normalizeWindowsPath(filename) : filename;
22-
23-
// eslint-disable-next-line prefer-const
24-
let { root, dir, base: basename, ext } = posix.parse(normalizedFilename);
16+
const normalizedFilename = normalizePath(filename);
2517

26-
const base = (require && require.main && require.main.filename && dir) || global.process.cwd();
27-
28-
const normalizedBase = `${base}/`;
18+
// We could use optional chaining here but webpack does like that mixed with require
19+
const base = normalizePath(
20+
`${(require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd()}/`,
21+
);
2922

3023
// It's specifically a module
31-
let file = basename;
32-
33-
if (ext === '.js') {
34-
file = file.slice(0, file.length - '.js'.length);
35-
}
36-
37-
if (!root && !dir) {
38-
// No dirname whatsoever
39-
dir = '.';
40-
}
24+
const file = basename(normalizedFilename, '.js');
4125

42-
let n = dir.lastIndexOf('/node_modules/');
26+
const path = dirname(normalizedFilename);
27+
let n = path.lastIndexOf('/node_modules/');
4328
if (n > -1) {
4429
// /node_modules/ is 14 chars
45-
return `${dir.slice(n + 14).replace(/\//g, '.')}:${file}`;
30+
return `${path.slice(n + 14).replace(/\//g, '.')}:${file}`;
4631
}
4732
// Let's see if it's a part of the main module
4833
// To be a part of main module, it has to share the same base
49-
n = `${dir}/`.lastIndexOf(normalizedBase, 0);
34+
n = `${path}/`.lastIndexOf(base, 0);
5035

5136
if (n === 0) {
52-
let moduleName = dir.slice(normalizedBase.length).replace(/\//g, '.');
37+
let moduleName = path.slice(base.length).replace(/\//g, '.');
5338
if (moduleName) {
5439
moduleName += ':';
5540
}

packages/utils/src/stacktrace.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,31 @@ export function stripSentryFramesAndReverse(stack: StackFrame[]): StackFrame[] {
6464

6565
let localStack = stack;
6666

67-
const firstFrameFunction = localStack[0].function || '';
68-
const lastFrameFunction = localStack[localStack.length - 1].function || '';
69-
70-
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
71-
if (firstFrameFunction.indexOf('captureMessage') !== -1 || firstFrameFunction.indexOf('captureException') !== -1) {
72-
localStack = localStack.slice(1);
67+
if (stack.length >= STACKTRACE_LIMIT) {
68+
localStack.slice(0, STACKTRACE_LIMIT);
7369
}
7470

71+
const lastFrameFunction = localStack[localStack.length - 1].function;
7572
// If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call)
76-
if (lastFrameFunction.indexOf('sentryWrapped') !== -1) {
77-
localStack = localStack.slice(0, -1);
73+
if (lastFrameFunction && /sentryWrapped/.test(lastFrameFunction)) {
74+
localStack.pop();
75+
}
76+
77+
// Reversing in the middle of the procedure allows us to just pop the values off the stack
78+
localStack = localStack.reverse();
79+
80+
const firstFrameFunction = localStack[localStack.length - 1].function;
81+
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
82+
if (firstFrameFunction && /captureMessage|captureException/.test(firstFrameFunction)) {
83+
localStack.pop();
7884
}
7985

8086
// The frame where the crash happened, should be the last entry in the array
81-
return localStack
82-
.slice(0, STACKTRACE_LIMIT)
83-
.map(frame => ({
84-
...frame,
85-
filename: frame.filename || localStack[0].filename,
86-
function: frame.function || '?',
87-
}))
88-
.reverse();
87+
return localStack.map(frame => ({
88+
...frame,
89+
filename: frame.filename || localStack[0].filename,
90+
function: frame.function || '?',
91+
}));
8992
}
9093

9194
const defaultFunctionName = '<anonymous>';

0 commit comments

Comments
 (0)