UNPKG

firebase-tools

Version:
170 lines (169 loc) • 7.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_MAX_FRAMES = exports.EVENT_DUMP_OPTIONS = void 0; exports.listEvents = listEvents; exports.normalizeEventResourceName = normalizeEventResourceName; exports.batchGetEvents = batchGetEvents; exports.formatFrames = formatFrames; exports.toText = toText; const js_yaml_1 = require("js-yaml"); const logger_1 = require("../logger"); const error_1 = require("../error"); const utils_1 = require("./utils"); const types_1 = require("./types"); const filters_1 = require("./filters"); exports.EVENT_DUMP_OPTIONS = { lineWidth: 200 }; exports.DEFAULT_MAX_FRAMES = 20; const DEFAULT_MAX_LOGS = 100; const DEFAULT_MAX_BREADCRUMBS = 10; async function listEvents(appId, filter, pageSize = 1, filterExpression) { var _a; const requestProjectNumber = (0, utils_1.parseProjectNumber)(appId); const queryParams = (0, filters_1.filterToUrlSearchParams)(filter); queryParams.set("page_size", `${pageSize}`); if (filterExpression) { queryParams.set("filter_expression", filterExpression); } logger_1.logger.debug(`[crashlytics] listEvents called with appId: ${appId}, filter: ${queryParams.toString()}, pageSize: ${pageSize}`); const response = await utils_1.CRASHLYTICS_API_CLIENT.request({ method: "GET", headers: { "Content-Type": "application/json", }, path: `/projects/${requestProjectNumber}/apps/${appId}/events`, queryParams: queryParams, timeout: utils_1.TIMEOUT, }); (_a = response.body).events ?? (_a.events = []); return response.body; } function escapeRegExp(value) { return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } function normalizeEventResourceName(appId, eventIdOrName) { const trimmed = eventIdOrName.trim(); if (!trimmed) { throw new error_1.FirebaseError("Event ID cannot be empty."); } const requestProjectNumber = (0, utils_1.parseProjectNumber)(appId); const fullNamePattern = new RegExp(`^projects/[^/]+/apps/${escapeRegExp(appId)}/events/[^/]+$`); if (fullNamePattern.test(trimmed)) { return trimmed; } if (trimmed.includes("/")) { throw new error_1.FirebaseError(`Invalid event resource name '${trimmed}'. Expected an event ID or 'projects/{project}/apps/${appId}/events/{event_id}'.`); } return `projects/${requestProjectNumber}/apps/${appId}/events/${trimmed}`; } async function batchGetEvents(appId, eventNames) { var _a; const requestProjectNumber = (0, utils_1.parseProjectNumber)(appId); if (eventNames.length > 100) throw new error_1.FirebaseError("Too many events in batchGet request"); logger_1.logger.debug(`[crashlytics] batchGetEvents called with appId: ${appId}, eventNames: ${eventNames.join(", ")}`); const queryParams = new URLSearchParams(); eventNames.forEach((en) => { queryParams.append("names", en); }); const response = await utils_1.CRASHLYTICS_API_CLIENT.request({ method: "GET", headers: { "Content-Type": "application/json", }, path: `/projects/${requestProjectNumber}/apps/${appId}/events:batchGet`, queryParams: queryParams, timeout: utils_1.TIMEOUT, }); (_a = response.body).events ?? (_a.events = []); return response.body; } function formatFrames(origFrames, maxFrames = exports.DEFAULT_MAX_FRAMES) { const frames = origFrames || []; const shouldTruncate = frames.length > maxFrames; const sliceCount = maxFrames > 1 ? maxFrames - 1 : 1; const framesToFormat = shouldTruncate ? frames.slice(0, sliceCount) : frames; const formatted = framesToFormat.map((frame) => { let line = `at`; if (frame.symbol) { line += ` ${frame.symbol}`; } if (frame.file) { line += ` (${frame.file}`; if (frame.line) { line += `:${frame.line}`; } line += ")"; } return line; }); if (shouldTruncate) { formatted.push("... frames omitted ..."); } return formatted; } function toText(event, options = {}) { if (!event) { return {}; } const { maxFrames = exports.DEFAULT_MAX_FRAMES, full = false } = options; const effectiveMaxFrames = full ? Infinity : maxFrames; const result = {}; for (const [key, value] of Object.entries(event)) { if (key === "logs") { const logs = value || []; const slicedLogs = !full && logs.length > DEFAULT_MAX_LOGS ? logs.slice(logs.length - DEFAULT_MAX_LOGS) : logs; const logLines = slicedLogs.map((log) => `[${log.logTime}] ${log.message}`); result["logs"] = logLines.join("\n"); } else if (key === "breadcrumbs") { const breadcrumbs = value || []; const slicedBreadcrumbs = !full && breadcrumbs.length > DEFAULT_MAX_BREADCRUMBS ? breadcrumbs.slice(-DEFAULT_MAX_BREADCRUMBS) : breadcrumbs; const breadcrumbLines = slicedBreadcrumbs.map((b) => { const paramString = Object.entries(b.params || {}) .map(([k, v]) => `${k}: ${v}`) .join(", "); const params = paramString ? ` { ${paramString} }` : ""; return `[${b.eventTime}] ${b.title}${params}`; }); result["breadcrumbs"] = breadcrumbLines.join("\n"); } else if (key === "threads") { let threads = value || []; if (!full && (event.issue?.errorType === types_1.ErrorType.FATAL || event.issue?.errorType === types_1.ErrorType.ANR)) { threads = threads.filter((t) => t.crashed || t.blamed); } const threadStrings = threads.map((thread) => { const header = `Thread: ${thread.name || thread.threadId || ""}${thread.crashed ? " (crashed)" : ""}`; const frameStrings = formatFrames(thread.frames || [], effectiveMaxFrames); return [header, ...frameStrings].join("\n"); }); result["threads"] = threadStrings.join("\n\n"); } else if (key === "exceptions") { const exceptions = value || []; const exceptionStrings = exceptions.map((exception) => { const header = exception.nested ? "Caused by: " : ""; const exceptionHeader = `${header}${exception.type || ""}: ${exception.exceptionMessage || ""}`; const frameStrings = formatFrames(exception.frames || [], effectiveMaxFrames); return [exceptionHeader, ...frameStrings].join("\n"); }); result["exceptions"] = exceptionStrings.join("\n\n"); } else if (key === "errors") { const errors = value || []; const errorStrings = errors.map((error) => { const header = `Error: ${error.title || "error"}`; const frameStrings = formatFrames(error.frames || [], effectiveMaxFrames); return [header, ...frameStrings].join("\n"); }); result["errors"] = errorStrings.join("\n\n"); } else { result[key] = (0, js_yaml_1.dump)(value, exports.EVENT_DUMP_OPTIONS); } } return result; }