UNPKG

@sentry-internal/lynx-plugin

Version:
68 lines (65 loc) 2.83 kB
import * as path from 'node:path'; import { symbolicateFrames } from './symbolicator.mjs'; import { logger } from '@sentry/core'; import { PREFIX } from './prefix.mjs'; import { tryCatch } from './utils/tryCatch.mjs'; import { getRawBody } from './utils/getRawBody.mjs'; const SENTRY_SYMBOLICATE_ENDPOINT = '/__sentry__/symbolicate'; const createSentrySymbolicatorMiddleware = (config) => { var _a, _b, _c; if (!config.root) { logger.warn(`${PREFIX} No project root path provided. Symbolication will not work.`); return noopMiddleware; } const projectRootPath = config.root; const distPath = (_c = (_b = (_a = config.output) === null || _a === void 0 ? void 0 : _a.distPath) === null || _b === void 0 ? void 0 : _b.root) !== null && _c !== void 0 ? _c : './'; const serverPublicPath = path.isAbsolute(distPath) ? distPath : path.join(projectRootPath, distPath); return async (req, res, next) => { var _a; if ((_a = req.url) === null || _a === void 0 ? void 0 : _a.startsWith(SENTRY_SYMBOLICATE_ENDPOINT)) { try { await processSymbolicateRequest({ req, res, projectRootPath, serverPublicPath }); } catch (error) { logger.error(`${PREFIX} Error processing symbolicate request: ${error}`); res.setHeader('Content-Type', 'application/json'); res.statusCode = 500; res.end('{ "error": "Internal Server Error" }'); } } else { next(); } }; }; const noopMiddleware = (_req, _res, next) => { next(); }; async function processSymbolicateRequest({ req, res, projectRootPath, serverPublicPath, }) { const body = await getRawBody(req); res.setHeader('Content-Type', 'application/json'); const { data, error } = tryCatch(() => JSON.parse(body)); if (error) { logger.error(`${PREFIX} Error parsing symbolicate request body: ${body}\nerror: ${error}`); res.statusCode = 400; res.end('{ "error": "Bad Request" }'); return; } logger.debug(`${PREFIX} Symbolicating ${data.frames.length} frames.`); const { data: symbolicatedFrames, error: symbolicationError } = await tryCatch(() => symbolicateFrames({ frames: data.frames, projectRootPath, serverPublicPath, })); if (symbolicationError) { logger.error(`${PREFIX} Error symbolicating frames: ${symbolicationError}`); res.statusCode = 500; res.end('{ "error": "Internal Server Error" }'); return; } res.end(JSON.stringify({ frames: symbolicatedFrames })); } export { SENTRY_SYMBOLICATE_ENDPOINT, createSentrySymbolicatorMiddleware, noopMiddleware, processSymbolicateRequest }; //# sourceMappingURL=middleware.mjs.map