Skip to content

Commit 051ce83

Browse files
committed
ref: Remove lsmod dependency
1 parent 38a12ff commit 051ce83

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

packages/integrations/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"@sentry/hub": "4.6.3",
1818
"@sentry/types": "4.5.3",
1919
"@sentry/utils": "4.6.3",
20-
"lsmod": "1.0.0",
2120
"tslib": "^1.9.3"
2221
},
2322
"devDependencies": {

packages/integrations/src/modules.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,61 @@
11
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
22
import { Integration } from '@sentry/types';
3-
import * as lsmod from 'lsmod';
3+
import { existsSync, readFileSync } from 'fs';
4+
import { dirname, join } from 'path';
45

56
let moduleCache: { [key: string]: string };
67

8+
/** Extract information about package.json modules */
9+
function collectModules(): {
10+
[name: string]: string;
11+
} {
12+
const mainPaths = (require.main && require.main.paths) || [];
13+
const paths = require.cache ? Object.keys(require.cache as {}) : [];
14+
const infos: {
15+
[name: string]: string;
16+
} = {};
17+
const seen: {
18+
[path: string]: boolean;
19+
} = {};
20+
21+
paths.forEach(path => {
22+
let dir = path;
23+
24+
/** Traverse directories upward in the search of package.json file */
25+
const updir = (): void | (() => void) => {
26+
const orig = dir;
27+
dir = dirname(orig);
28+
29+
if (!dir || orig === dir || seen[orig]) {
30+
return undefined;
31+
} else if (mainPaths.indexOf(dir) < 0) {
32+
return updir();
33+
}
34+
35+
const pkgfile = join(orig, 'package.json');
36+
seen[orig] = true;
37+
38+
if (!existsSync(pkgfile)) {
39+
return updir();
40+
}
41+
42+
try {
43+
const info = JSON.parse(readFileSync(pkgfile, 'utf8')) as {
44+
name: string;
45+
version: string;
46+
};
47+
infos[info.name] = info.version;
48+
} catch (_oO) {
49+
// no-empty
50+
}
51+
};
52+
53+
updir();
54+
});
55+
56+
return infos;
57+
}
58+
759
/** Add node modules / packages to the event */
860
export class Modules implements Integration {
961
/**
@@ -34,7 +86,7 @@ export class Modules implements Integration {
3486
private getModules(): { [key: string]: string } {
3587
if (!moduleCache) {
3688
// tslint:disable-next-line:no-unsafe-any
37-
moduleCache = lsmod();
89+
moduleCache = collectModules();
3890
}
3991
return moduleCache;
4092
}

0 commit comments

Comments
 (0)