@sentry/node
Version:
Sentry Node SDK using OpenTelemetry for performance instrumentation
36 lines (33 loc) • 1.27 kB
JavaScript
import { posix, sep } from 'node:path';
import { dirname } from '@sentry/core';
function normalizeWindowsPath(path) {
return path.replace(/^[A-Z]:/, "").replace(/\\/g, "/");
}
function createGetModuleFromFilename(basePath = process.argv[1] ? dirname(process.argv[1]) : process.cwd(), isWindows = sep === "\\") {
const normalizedBase = isWindows ? normalizeWindowsPath(basePath) : basePath;
return (filename) => {
if (!filename) {
return;
}
const normalizedFilename = isWindows ? normalizeWindowsPath(filename) : filename;
let { dir, base: file, ext } = posix.parse(normalizedFilename);
if (ext === ".js" || ext === ".mjs" || ext === ".cjs") {
file = file.slice(0, ext.length * -1);
}
const decodedFile = decodeURIComponent(file);
if (!dir) {
dir = ".";
}
const n = dir.lastIndexOf("/node_modules");
if (n > -1) {
return `${dir.slice(n + 14).replace(/\//g, ".")}:${decodedFile}`;
}
if (dir.startsWith(normalizedBase)) {
const moduleName = dir.slice(normalizedBase.length + 1).replace(/\//g, ".");
return moduleName ? `${moduleName}:${decodedFile}` : decodedFile;
}
return decodedFile;
};
}
export { createGetModuleFromFilename };
//# sourceMappingURL=module.js.map