Repository navigation
Expand file tree
/
Copy pathmodules.ts
More file actions
156 lines (130 loc) · 4.26 KB
/
Copy pathmodules.ts
File metadata and controls
156 lines (130 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { existsSync, readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { GLOBAL_OBJ, type IntegrationFn } from '@sentry/core';
type ModuleInfo = Record<string, string>;
let moduleCache: ModuleInfo | undefined;
const INTEGRATION_NAME = 'Modules' as const;
declare const __SENTRY_SERVER_MODULES__: Record<string, string>;
/**
* Reads the modules injected at build time into `__SENTRY_SERVER_MODULES__` (e.g. by the Next.js SDK).
*
* Must be read lazily (per call), not captured at module-eval time: webpack replaces the token via
* `DefinePlugin` (available immediately), but Turbopack assigns `globalThis.__SENTRY_SERVER_MODULES__`
* at runtime, *after* this module's imports are hoisted and evaluated. A `const` capture would be
* empty under Turbopack. See getsentry/sentry-javascript#19147.
*/
function getServerModules(): Record<string, string> {
// webpack: the token is replaced with a literal at build time.
if (typeof __SENTRY_SERVER_MODULES__ !== 'undefined') {
return __SENTRY_SERVER_MODULES__;
}
// Turbopack: the value is assigned onto the global object at runtime.
return (
(GLOBAL_OBJ as typeof GLOBAL_OBJ & { __SENTRY_SERVER_MODULES__?: Record<string, string> })
.__SENTRY_SERVER_MODULES__ ?? {}
);
}
const _modulesIntegration = (() => {
return {
name: INTEGRATION_NAME,
processEvent(event) {
event.modules = {
...event.modules,
..._getModules(),
};
return event;
},
getModules: _getModules,
};
}) satisfies IntegrationFn;
/**
* Add node modules / packages to the event.
* For this, multiple sources are used:
* - They can be injected at build time into the __SENTRY_SERVER_MODULES__ variable (e.g. in Next.js)
* - They are extracted from the dependencies & devDependencies in the package.json file
* - They are extracted from the require.cache (CJS only)
*/
export const modulesIntegration = _modulesIntegration;
function getRequireCachePaths(): string[] {
try {
return require.cache ? Object.keys(require.cache) : [];
} catch {
return [];
}
}
/** Extract information about package.json modules */
function collectModules(): ModuleInfo {
return {
...getServerModules(),
...getModulesFromPackageJson(),
/*! rollup-include-cjs-only */
...collectRequireModules(),
/*! rollup-include-cjs-only-end */
};
}
/** Extract information about package.json modules from require.cache */
function collectRequireModules(): ModuleInfo {
const mainPaths = require.main?.paths || [];
const paths = getRequireCachePaths();
// We start with the modules from package.json (if possible)
// These may be overwritten by more specific versions from the require.cache
const infos: ModuleInfo = {};
const seen = new Set<string>();
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.has(orig)) {
return undefined;
}
if (mainPaths.indexOf(dir) < 0) {
return updir();
}
const pkgfile = join(orig, 'package.json');
seen.add(orig);
if (!existsSync(pkgfile)) {
return updir();
}
try {
const info = JSON.parse(readFileSync(pkgfile, 'utf8')) as {
name: string;
version: string;
};
infos[info.name] = info.version;
} catch {
// no-empty
}
};
updir();
});
return infos;
}
/** Fetches the list of modules and the versions loaded by the entry file for your node.js app. */
function _getModules(): ModuleInfo {
if (!moduleCache) {
moduleCache = collectModules();
}
return moduleCache;
}
interface PackageJson {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
}
function getPackageJson(): PackageJson {
try {
const filePath = join(process.cwd(), 'package.json');
const packageJson = JSON.parse(readFileSync(filePath, 'utf8')) as PackageJson;
return packageJson;
} catch {
return {};
}
}
function getModulesFromPackageJson(): ModuleInfo {
const packageJson = getPackageJson();
return {
...packageJson.dependencies,
...packageJson.devDependencies,
};
}