Skip to content

Commit 86e2d4e

Browse files
committed
Make async
1 parent 82573f4 commit 86e2d4e

File tree

4 files changed

+41
-47
lines changed

4 files changed

+41
-47
lines changed

packages/node/src/integrations/contextlines.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { getCurrentHub } from '@sentry/core';
21
import { Event, EventProcessor, Integration } from '@sentry/types';
32
import { addContextToFrame } from '@sentry/utils';
4-
import { readFileSync } from 'fs';
3+
import { readFile } from 'fs';
54
import { LRUMap } from 'lru_map';
65

7-
import { NodeClient } from '../client';
8-
96
const FILE_CONTENT_CACHE = new LRUMap<string, string | null>(100);
107

8+
function readTextFileAsync(path: string): Promise<string> {
9+
return new Promise((resolve, reject) => {
10+
readFile(path, 'utf8', (err, data) => {
11+
if (err) reject(err);
12+
else resolve(data);
13+
});
14+
});
15+
}
16+
1117
/**
1218
* Resets the file cache. Exists for testing purposes.
1319
* @hidden
@@ -16,9 +22,9 @@ export function resetFileContentCache(): void {
1622
FILE_CONTENT_CACHE.clear();
1723
}
1824

19-
type ContextLinesOptions = {
25+
interface ContextLinesOptions {
2026
frameContextLines?: number;
21-
};
27+
}
2228

2329
/** Add node modules / packages to the event */
2430
export class ContextLines implements Integration {
@@ -32,27 +38,18 @@ export class ContextLines implements Integration {
3238
*/
3339
public name: string = ContextLines.id;
3440

35-
private _linesOfContext?: number;
36-
37-
public constructor(options: ContextLinesOptions = {}) {
38-
this._linesOfContext = options.frameContextLines;
39-
}
41+
public constructor(private readonly _options: ContextLinesOptions = {}) {}
4042

4143
/**
4244
* @inheritDoc
4345
*/
4446
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void {
45-
const optLinesOfContext = getCurrentHub()
46-
.getClient<NodeClient>()
47-
?.getOptions()?.frameContextLines;
48-
49-
addGlobalEventProcessor(event => this.process(event, optLinesOfContext));
47+
addGlobalEventProcessor(event => this.addToEvent(event));
5048
}
5149

5250
/** Processes an event and adds context lines */
53-
public process(event: Event, optLinesOfContext?: number): Event {
54-
const contextLines =
55-
optLinesOfContext != undefined ? optLinesOfContext : this._linesOfContext != undefined ? this._linesOfContext : 7;
51+
public async addToEvent(event: Event): Promise<Event> {
52+
const contextLines = this._options.frameContextLines != undefined ? this._options.frameContextLines : 7;
5653

5754
const frames = event.exception?.values?.[0].stacktrace?.frames;
5855

@@ -65,7 +62,7 @@ export class ContextLines implements Integration {
6562
}
6663
}
6764

68-
const sourceFiles = readSourceFiles(filenames);
65+
const sourceFiles = await readSourceFiles(filenames);
6966

7067
for (const frame of frames) {
7168
if (frame.filename && sourceFiles[frame.filename]) {
@@ -91,7 +88,7 @@ export class ContextLines implements Integration {
9188
*
9289
* @param filenames Array of filepaths to read content from.
9390
*/
94-
function readSourceFiles(filenames: string[]): Record<string, string | null> {
91+
async function readSourceFiles(filenames: string[]): Promise<Record<string, string | null>> {
9592
// we're relying on filenames being de-duped already
9693
if (!filenames.length) {
9794
return {};
@@ -113,11 +110,11 @@ function readSourceFiles(filenames: string[]): Record<string, string | null> {
113110
continue;
114111
}
115112

116-
let content: string | null;
113+
let content: string | null = null;
117114
try {
118-
content = readFileSync(filename, 'utf8');
119-
} catch (_e) {
120-
content = null;
115+
content = await readTextFileAsync(filename);
116+
} catch (_) {
117+
//
121118
}
122119

123120
FILE_CONTENT_CACHE.set(filename, content);

packages/node/src/types.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ export interface NodeOptions extends Options {
2020
/** HTTPS proxy certificates path */
2121
caCerts?: string;
2222

23-
/** Sets the number of context lines for each frame when loading a file. */
24-
frameContextLines?: number;
25-
2623
/** Callback that is executed when a fatal global error occurs. */
2724
onFatalError?(error: Error): void;
2825
}

packages/node/test/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ describe('SentryNode', () => {
176176
expect(event.exception).not.toBeUndefined();
177177
expect(event.exception!.values![0]).not.toBeUndefined();
178178
expect(event.exception!.values![0].stacktrace!).not.toBeUndefined();
179-
expect(event.exception!.values![0].stacktrace!.frames![2]).not.toBeUndefined();
180-
expect(Array.isArray(event.exception!.values![0].stacktrace!.frames![2].pre_context)).toBe(true);
181-
expect(Array.isArray(event.exception!.values![0].stacktrace!.frames![2].post_context)).toBe(true);
179+
expect(event.exception!.values![0].stacktrace!.frames![1]).not.toBeUndefined();
180+
expect(event.exception!.values![0].stacktrace!.frames![1].pre_context).not.toBeUndefined();
181+
expect(event.exception!.values![0].stacktrace!.frames![1].post_context).not.toBeUndefined();
182182
expect(event.exception!.values![0].type).toBe('Error');
183183
expect(event.exception!.values![0].value).toBe('test');
184184
expect(event.exception!.values![0].stacktrace).toBeTruthy();

packages/node/test/parsers.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ describe('parsers.ts', () => {
1111
let spy: jest.SpyInstance;
1212
let contextLines: ContextLines;
1313

14-
function addContext(frames: StackFrame[]) {
15-
contextLines.process({ exception: { values: [{ stacktrace: { frames } }] } });
14+
async function addContext(frames: StackFrame[]): Promise<void> {
15+
await contextLines.addToEvent({ exception: { values: [{ stacktrace: { frames } }] } });
1616
}
1717

1818
beforeEach(() => {
19-
spy = jest.spyOn(fs, 'readFileSync');
19+
spy = jest.spyOn(fs, 'readFile');
2020
frames = stacktrace.parse(new Error('test'));
2121
contextLines = new ContextLines();
2222
resetFileContentCache();
@@ -27,22 +27,22 @@ describe('parsers.ts', () => {
2727
});
2828

2929
describe('lru file cache', () => {
30-
test('parseStack with same file', () => {
30+
test('parseStack with same file', async () => {
3131
expect.assertions(1);
3232

3333
let mockCalls = 0;
3434
let parsedFrames = Parsers.parseStack(frames);
35-
addContext(parsedFrames);
35+
await addContext(parsedFrames);
3636

3737
mockCalls = spy.mock.calls.length;
3838
parsedFrames = Parsers.parseStack(frames);
39-
addContext(parsedFrames);
39+
await addContext(parsedFrames);
4040

4141
// Calls to readFile shouldn't increase if there isn't a new error
4242
expect(spy).toHaveBeenCalledTimes(mockCalls);
4343
});
4444

45-
test('parseStack with ESM module names', () => {
45+
test('parseStack with ESM module names', async () => {
4646
expect.assertions(1);
4747

4848
const framesWithFilePath: stacktrace.StackFrame[] = [
@@ -57,31 +57,31 @@ describe('parsers.ts', () => {
5757
},
5858
];
5959
const parsedFrames = Parsers.parseStack(framesWithFilePath);
60-
addContext(parsedFrames);
60+
await addContext(parsedFrames);
6161
expect(spy).toHaveBeenCalledTimes(1);
6262
});
6363

64-
test('parseStack with adding different file', () => {
64+
test('parseStack with adding different file', async () => {
6565
expect.assertions(2);
6666
let mockCalls = 0;
6767
let newErrorCalls = 0;
6868
let parsedFrames = Parsers.parseStack(frames);
69-
addContext(parsedFrames);
69+
await addContext(parsedFrames);
7070

7171
mockCalls = spy.mock.calls.length;
7272
parsedFrames = Parsers.parseStack(stacktrace.parse(getError()));
73-
addContext(parsedFrames);
73+
await addContext(parsedFrames);
7474

7575
newErrorCalls = spy.mock.calls.length;
7676
expect(newErrorCalls).toBeGreaterThan(mockCalls);
7777

7878
parsedFrames = Parsers.parseStack(stacktrace.parse(getError()));
79-
addContext(parsedFrames);
79+
await addContext(parsedFrames);
8080

8181
expect(spy).toHaveBeenCalledTimes(newErrorCalls);
8282
});
8383

84-
test('parseStack with duplicate files', () => {
84+
test('parseStack with duplicate files', async () => {
8585
expect.assertions(1);
8686
const framesWithDuplicateFiles: stacktrace.StackFrame[] = [
8787
{
@@ -114,16 +114,16 @@ describe('parsers.ts', () => {
114114
];
115115

116116
const parsedFrames = Parsers.parseStack(framesWithDuplicateFiles);
117-
addContext(parsedFrames);
117+
await addContext(parsedFrames);
118118
expect(spy).toHaveBeenCalledTimes(1);
119119
});
120120

121-
test('parseStack with no context', () => {
121+
test('parseStack with no context', async () => {
122122
contextLines = new ContextLines({ frameContextLines: 0 });
123123

124124
expect.assertions(1);
125125
const parsedFrames = Parsers.parseStack(frames);
126-
addContext(parsedFrames);
126+
await addContext(parsedFrames);
127127
expect(spy).toHaveBeenCalledTimes(0);
128128
});
129129
});

0 commit comments

Comments
 (0)