modules.js 2.3 KB

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