From 051ce830be281b8fa7e93a6a07dbfd53954062fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Thu, 21 Feb 2019 17:05:55 +0100 Subject: [PATCH] ref: Remove lsmod dependency --- packages/integrations/package.json | 1 - packages/integrations/src/modules.ts | 56 +++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/packages/integrations/package.json b/packages/integrations/package.json index b85ca5e608c6..dc036a85aa3b 100644 --- a/packages/integrations/package.json +++ b/packages/integrations/package.json @@ -17,7 +17,6 @@ "@sentry/hub": "4.6.3", "@sentry/types": "4.5.3", "@sentry/utils": "4.6.3", - "lsmod": "1.0.0", "tslib": "^1.9.3" }, "devDependencies": { diff --git a/packages/integrations/src/modules.ts b/packages/integrations/src/modules.ts index 8d1a508c116f..2adbd2e0db7d 100644 --- a/packages/integrations/src/modules.ts +++ b/packages/integrations/src/modules.ts @@ -1,9 +1,61 @@ import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core'; import { Integration } from '@sentry/types'; -import * as lsmod from 'lsmod'; +import { existsSync, readFileSync } from 'fs'; +import { dirname, join } from 'path'; let moduleCache: { [key: string]: string }; +/** Extract information about package.json modules */ +function collectModules(): { + [name: string]: string; +} { + const mainPaths = (require.main && require.main.paths) || []; + const paths = require.cache ? Object.keys(require.cache as {}) : []; + const infos: { + [name: string]: string; + } = {}; + const seen: { + [path: string]: boolean; + } = {}; + + paths.forEach(path => { + let dir = path; + + /** Traverse directories upward in the search of package.json file */ + const updir = (): void | (() => void) => { + const orig = dir; + dir = dirname(orig); + + if (!dir || orig === dir || seen[orig]) { + return undefined; + } else if (mainPaths.indexOf(dir) < 0) { + return updir(); + } + + const pkgfile = join(orig, 'package.json'); + seen[orig] = true; + + if (!existsSync(pkgfile)) { + return updir(); + } + + try { + const info = JSON.parse(readFileSync(pkgfile, 'utf8')) as { + name: string; + version: string; + }; + infos[info.name] = info.version; + } catch (_oO) { + // no-empty + } + }; + + updir(); + }); + + return infos; +} + /** Add node modules / packages to the event */ export class Modules implements Integration { /** @@ -34,7 +86,7 @@ export class Modules implements Integration { private getModules(): { [key: string]: string } { if (!moduleCache) { // tslint:disable-next-line:no-unsafe-any - moduleCache = lsmod(); + moduleCache = collectModules(); } return moduleCache; }