modules.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const fs = require('fs');
  3. const path = require('path');
  4. const core = require('@sentry/core');
  5. let moduleCache;
  6. const INTEGRATION_NAME = 'Modules';
  7. /** Extract information about paths */
  8. function getPaths() {
  9. try {
  10. return require.cache ? Object.keys(require.cache ) : [];
  11. } catch (e) {
  12. return [];
  13. }
  14. }
  15. /** Extract information about package.json modules */
  16. function collectModules()
  17. {
  18. const mainPaths = (require.main && require.main.paths) || [];
  19. const paths = getPaths();
  20. const infos
  21. = {};
  22. const seen
  23. = {};
  24. paths.forEach(path$1 => {
  25. let dir = path$1;
  26. /** Traverse directories upward in the search of package.json file */
  27. const updir = () => {
  28. const orig = dir;
  29. dir = path.dirname(orig);
  30. if (!dir || orig === dir || seen[orig]) {
  31. return undefined;
  32. }
  33. if (mainPaths.indexOf(dir) < 0) {
  34. return updir();
  35. }
  36. const pkgfile = path.join(orig, 'package.json');
  37. seen[orig] = true;
  38. if (!fs.existsSync(pkgfile)) {
  39. return updir();
  40. }
  41. try {
  42. const info = JSON.parse(fs.readFileSync(pkgfile, 'utf8'))
  43. ;
  44. infos[info.name] = info.version;
  45. } catch (_oO) {
  46. // no-empty
  47. }
  48. };
  49. updir();
  50. });
  51. return infos;
  52. }
  53. /** Fetches the list of modules and the versions loaded by the entry file for your node.js app. */
  54. function _getModules() {
  55. if (!moduleCache) {
  56. moduleCache = collectModules();
  57. }
  58. return moduleCache;
  59. }
  60. const _modulesIntegration = (() => {
  61. return {
  62. name: INTEGRATION_NAME,
  63. // TODO v8: Remove this
  64. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  65. processEvent(event) {
  66. event.modules = {
  67. ...event.modules,
  68. ..._getModules(),
  69. };
  70. return event;
  71. },
  72. };
  73. }) ;
  74. const modulesIntegration = core.defineIntegration(_modulesIntegration);
  75. /**
  76. * Add node modules / packages to the event.
  77. * @deprecated Use `modulesIntegration()` instead.
  78. */
  79. // eslint-disable-next-line deprecation/deprecation
  80. const Modules = core.convertIntegrationFnToClass(INTEGRATION_NAME, modulesIntegration)
  81. ;
  82. // eslint-disable-next-line deprecation/deprecation
  83. exports.Modules = Modules;
  84. exports.modulesIntegration = modulesIntegration;
  85. //# sourceMappingURL=modules.js.map