|
1 | 1 | import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
|
2 | 2 | import { Integration } from '@sentry/types';
|
3 |
| -import * as lsmod from 'lsmod'; |
| 3 | +import { existsSync, readFileSync } from 'fs'; |
| 4 | +import { dirname, join } from 'path'; |
4 | 5 |
|
5 | 6 | let moduleCache: { [key: string]: string };
|
6 | 7 |
|
| 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 | + |
7 | 59 | /** Add node modules / packages to the event */
|
8 | 60 | export class Modules implements Integration {
|
9 | 61 | /**
|
@@ -34,7 +86,7 @@ export class Modules implements Integration {
|
34 | 86 | private getModules(): { [key: string]: string } {
|
35 | 87 | if (!moduleCache) {
|
36 | 88 | // tslint:disable-next-line:no-unsafe-any
|
37 |
| - moduleCache = lsmod(); |
| 89 | + moduleCache = collectModules(); |
38 | 90 | }
|
39 | 91 | return moduleCache;
|
40 | 92 | }
|
|
0 commit comments