UNPKG

raygun

Version:

Raygun package for Node.js, written in TypeScript

132 lines (131 loc) 4.69 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addBreadcrumb = addBreadcrumb; exports.getBreadcrumbs = getBreadcrumbs; exports.runWithBreadcrumbs = runWithBreadcrumbs; exports.runWithBreadcrumbsAsync = runWithBreadcrumbsAsync; exports.clear = clear; var debug = require("debug")("raygun"); var asyncLocalStorage = null; try { asyncLocalStorage = new (require("async_hooks").AsyncLocalStorage)(); debug("[raygun.breadcrumbs.ts] initialized successfully"); } catch (e) { debug("[raygun.breadcrumbs.ts] failed to load async_hooks.AsyncLocalStorage - initialization failed\n", e); } function returnCallerSite(err, callsites) { for (var _i = 0, callsites_1 = callsites; _i < callsites_1.length; _i++) { var callsite = callsites_1[_i]; var fileName = callsite.getFileName() || ""; if (fileName.startsWith(__dirname)) { continue; } return { fileName: fileName, functionName: callsite.getFunctionName() || "<anonymous>", lineNumber: callsite.getLineNumber(), }; } return null; } function getCallsite() { var originalPrepareStacktrace = Error.prepareStackTrace; Error.prepareStackTrace = returnCallerSite; // Ignore use of any, required for captureStackTrace // eslint-disable-next-line @typescript-eslint/no-explicit-any var output = {}; Error.captureStackTrace(output); var callsite = output.stack; Error.prepareStackTrace = originalPrepareStacktrace; return callsite; } /** * Adds Breadcrumb to current scope * @param breadcrumb - either String message or BreadcrumbMessage object * @param type - defaults to manual, values as defined in Breadcrumb type */ function addBreadcrumb(breadcrumb, type) { if (type === void 0) { type = "manual"; } var crumbs = getBreadcrumbs(); if (!crumbs) { return; } if (typeof breadcrumb === "string") { var expandedBreadcrumb = { message: breadcrumb, level: "info", category: "", }; breadcrumb = expandedBreadcrumb; } var callsite = getCallsite(); var internalCrumb = __assign(__assign({}, breadcrumb), { category: breadcrumb.category || "", message: breadcrumb.message || "", level: breadcrumb.level || "info", timestamp: Number(new Date()), type: type, className: callsite === null || callsite === void 0 ? void 0 : callsite.fileName, methodName: callsite === null || callsite === void 0 ? void 0 : callsite.functionName, lineNumber: (callsite === null || callsite === void 0 ? void 0 : callsite.lineNumber) || undefined }); debug("[raygun.breadcrumbs.ts] recorded breadcrumb: ".concat(internalCrumb.message)); crumbs.push(internalCrumb); } /** * Obtain list of Breadcrumbs in current scope * @returns List of Breadcrumbs or null if no local storage */ function getBreadcrumbs() { if (!asyncLocalStorage) { return null; } var store = asyncLocalStorage.getStore(); if (store) { return store; } var newStore = []; debug("[raygun.breadcrumbs.ts] enter with new store"); asyncLocalStorage.enterWith(newStore); return newStore; } /** * Run synchronous function in Breadcrumb scope * @param f - function to run * @param store - optional Breadcrumb scope store */ function runWithBreadcrumbs(f, store) { if (store === void 0) { store = []; } if (!asyncLocalStorage) { f(); return; } debug("[raygun.breadcrumbs.ts] running function with breadcrumbs"); asyncLocalStorage.run(store, f); } /** * Run asynchronous function returning a Promise in Breadcrumb scope * @param f - asynchronous function to run * @param store - optional Breadcrumb scope store */ function runWithBreadcrumbsAsync(f, store) { if (store === void 0) { store = []; } if (!asyncLocalStorage) { return f(); } debug("[raygun.breadcrumbs.ts] running async function with breadcrumbs"); return asyncLocalStorage.run(store, f); } /** * Clear the stored Breadcrumbs in current scope */ function clear() { if (!asyncLocalStorage) { return; } debug("[raygun.breadcrumbs.ts] clearing stored breadcrumbs, entering with new store"); asyncLocalStorage.enterWith([]); }