UNPKG

@sentry/node

Version:

Sentry Node SDK using OpenTelemetry for performance instrumentation

70 lines (69 loc) • 2.36 kB
import { GLOBAL_OBJ, defineIntegration } from "@sentry/core"; import { collectInstalledModules } from "@sentry/server-utils"; import { existsSync, readFileSync } from "node:fs"; import { Module } from "node:module"; // src/integrations/modules.ts let moduleCache; const INTEGRATION_NAME = "Modules"; /** * 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() { if (typeof __SENTRY_SERVER_MODULES__ !== "undefined") return __SENTRY_SERVER_MODULES__; return GLOBAL_OBJ.__SENTRY_SERVER_MODULES__ ?? {}; } const _modulesIntegration = (() => { return { name: INTEGRATION_NAME, processEvent(event) { event.modules = { ...event.modules, ..._getModules() }; return event; }, getModules: _getModules }; }); /** * Adds the app's packages to the event. Versions injected at build time (e.g. by the Next.js SDK) are used as is. * Otherwise, it uses exact installed versions where they resolve from the entry file, and declared ranges for the rest. */ const modulesIntegration = defineIntegration(_modulesIntegration); function readFile(path) { try { return existsSync(path) ? readFileSync(path, "utf8") : void 0; } catch { return; } } /** Files loaded through the CommonJS loader, which includes CommonJS packages imported from ESM. */ function getLoadedCommonJsFiles() { try { return Object.keys(Module._cache || {}); } catch { return []; } } function collectModules() { const serverModules = getServerModules(); if (Object.keys(serverModules).length) return serverModules; return collectInstalledModules({ readFile, entryPath: process.argv[1], cwd: process.cwd(), loadedFiles: getLoadedCommonJsFiles() }); } /** Fetches the list of modules and the versions loaded by the entry file for your node.js app. */ function _getModules() { if (!moduleCache) moduleCache = collectModules(); return moduleCache; } export { modulesIntegration }; //# sourceMappingURL=modules.js.map