import { consoleSandbox } from '@sentry/core'; import * as fs from 'fs'; import * as path from 'path'; import { resolvePath } from '@nuxt/kit'; async function getNitroMajorVersion() { try { const { getPackageInfo } = await import('local-pkg'); const info = await getPackageInfo("nitro"); if (info?.version) { const major = parseInt(info.version.split(".")[0] ?? "2", 10); return isNaN(major) ? 2 : major; } } catch { } return 2; } async function findDefaultSdkInitFile(type, nuxt, options) { const possibleFileExtensions = ["ts", "js", "mjs", "cjs", "mts", "cts"]; const relativePaths = []; if (type === "server") { for (const ext of possibleFileExtensions) { relativePaths.push(`sentry.${type}.config.${ext}`); relativePaths.push(path.join("public", `instrument.${type}.${ext}`)); } } else { for (const ext of possibleFileExtensions) { relativePaths.push(`sentry.${type}.config.${ext}`); } } const layers = [...nuxt?.options._layers ?? []].reverse(); for (const layer of layers) { for (const relativePath of relativePaths) { const fullPath = path.resolve(layer.cwd, relativePath); if (fs.existsSync(fullPath)) { return fullPath; } } } const rootDir = options?.configDir ? await resolvePath(options.configDir, { type: "dir" }) : process.cwd(); for (const relativePath of relativePaths) { const fullPath = path.resolve(rootDir, relativePath); if (fs.existsSync(fullPath)) { return fullPath; } } return void 0; } function getFilenameFromNodeStartCommand(nodeCommand) { const regex = /[^/\\]+\.[^/\\]+$/; const match = nodeCommand.match(regex); return match ? match[0] : null; } const SENTRY_WRAPPED_ENTRY = "?sentry-query-wrapped-entry"; const SENTRY_WRAPPED_FUNCTIONS = "?sentry-query-wrapped-functions="; const SENTRY_REEXPORTED_FUNCTIONS = "?sentry-query-reexported-functions="; const QUERY_END_INDICATOR = "SENTRY-QUERY-END"; function removeSentryQueryFromPath(url) { const regex = new RegExp(`\\${SENTRY_WRAPPED_ENTRY}.*?\\${QUERY_END_INDICATOR}`); return url.replace(regex, ""); } function extractFunctionReexportQueryParameters(query) { const wrapRegex = new RegExp( `\\${SENTRY_WRAPPED_FUNCTIONS}(.*?)(\\${QUERY_END_INDICATOR}|\\${SENTRY_REEXPORTED_FUNCTIONS})` ); const reexportRegex = new RegExp(`\\${SENTRY_REEXPORTED_FUNCTIONS}(.*?)(\\${QUERY_END_INDICATOR})`); const wrapMatch = query.match(wrapRegex); const reexportMatch = query.match(reexportRegex); const wrap = wrapMatch?.[1]?.split(",").filter((param) => param !== "").map((str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) || []; const reexport = reexportMatch?.[1]?.split(",").filter((param) => param !== "" && param !== "default").map((str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) || []; return { wrap, reexport }; } function constructWrappedFunctionExportQuery(exportedBindings, entrypointWrappedFunctions, debug) { const functionsToExport = { wrap: [], reexport: [] }; Object.values(exportedBindings || {}).forEach( (functions) => functions.forEach((fn) => { if (entrypointWrappedFunctions.includes(fn)) { functionsToExport.wrap.push(fn); } else { functionsToExport.reexport.push(fn); } }) ); if (debug && functionsToExport.wrap.length === 0) { consoleSandbox( () => ( // eslint-disable-next-line no-console console.warn( "[Sentry] No functions found to wrap. In case the server needs to export async functions other than `handler` or `server`, consider adding the name(s) to Sentry's build options `sentry.experimental_entrypointWrappedFunctions` in `nuxt.config.ts`." ) ) ); } const wrapQuery = functionsToExport.wrap.length ? `${SENTRY_WRAPPED_FUNCTIONS}${functionsToExport.wrap.join(",")}` : ""; const reexportQuery = functionsToExport.reexport.length ? `${SENTRY_REEXPORTED_FUNCTIONS}${functionsToExport.reexport.join(",")}` : ""; return [wrapQuery, reexportQuery].join(""); } function constructFunctionReExport(pathWithQuery, entryId) { const { wrap: wrapFunctions, reexport: reexportFunctions } = extractFunctionReexportQueryParameters(pathWithQuery); return wrapFunctions.reduce( (functionsCode, currFunctionName) => functionsCode.concat( `async function ${currFunctionName}_sentryWrapped(...args) { const res = await import(${JSON.stringify(entryId)}); return res.${currFunctionName}.call(this, ...args); } export { ${currFunctionName}_sentryWrapped as ${currFunctionName} }; ` ), "" ).concat( reexportFunctions.reduce( (functionsCode, currFunctionName) => functionsCode.concat(`export { ${currFunctionName} } from ${JSON.stringify(entryId)};`), "" ) ); } function addOTelCommonJSImportAlias(nuxt, isNitroV3 = false) { if (!nuxt.options.dev || isNitroV3) { return; } if (!nuxt.options.alias) { nuxt.options.alias = {}; } if (!nuxt.options.alias["@opentelemetry/resources"]) { nuxt.options.alias["@opentelemetry/resources"] = "@opentelemetry/resources/build/src/index.js"; } } export { QUERY_END_INDICATOR, SENTRY_REEXPORTED_FUNCTIONS, SENTRY_WRAPPED_ENTRY, SENTRY_WRAPPED_FUNCTIONS, addOTelCommonJSImportAlias, constructFunctionReExport, constructWrappedFunctionExportQuery, extractFunctionReexportQueryParameters, findDefaultSdkInitFile, getFilenameFromNodeStartCommand, getNitroMajorVersion, removeSentryQueryFromPath }; //# sourceMappingURL=utils.js.map