UNPKG

raygun

Version:

Raygun package for Node.js, written in TypeScript

88 lines (87 loc) 3.71 kB
/* * raygun * https://github.com/MindscapeHQ/raygun4node * * Copyright (c) 2015 MindscapeHQ * Licensed under the MIT license. */ "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OfflineStorage = void 0; var fs_1 = __importDefault(require("fs")); var path_1 = __importDefault(require("path")); var debug = require("debug")("raygun"); var OfflineStorage = /** @class */ (function () { function OfflineStorage(transport) { this.cachePath = ""; this.cacheLimit = 100; this.transport = transport; } OfflineStorage.prototype._sendAndDelete = function (item) { var _this = this; fs_1.default.readFile(path_1.default.join(this.cachePath, item), "utf8", function (err, cacheContents) { // Attempt to send stored messages after going online _this.transport.send(cacheContents); // Ignore result, delete stored content nevertheless fs_1.default.unlink(path_1.default.join(_this.cachePath, item), function () { }); }); }; OfflineStorage.prototype.init = function (offlineStorageOptions) { if (!offlineStorageOptions || !offlineStorageOptions.cachePath) { throw new Error("Cache Path must be set before Raygun can cache offline"); } this.cachePath = offlineStorageOptions.cachePath; this.cacheLimit = offlineStorageOptions.cacheLimit || 100; debug("[raygun.offline.ts] Offline storage - initialized (cachePath=".concat(this.cachePath, ", cacheLimit=").concat(this.cacheLimit, ")")); if (!fs_1.default.existsSync(this.cachePath)) { fs_1.default.mkdirSync(this.cachePath); } return this; }; OfflineStorage.prototype.save = function (transportItem, callback) { var _this = this; var filename = path_1.default.join(this.cachePath, Date.now() + ".json"); fs_1.default.readdir(this.cachePath, function (err, files) { if (err) { console.error("[Raygun4Node] Error reading cache folder", err); return callback(err); } if (files.length > _this.cacheLimit) { console.error("[Raygun4Node] Error cache reached limit"); return callback(null); } fs_1.default.writeFile(filename, transportItem, "utf8", function (err) { if (!err) { debug("[raygun.offline.ts] Offline storage - wrote message to ".concat(filename)); return callback(null); } console.error("[Raygun4Node] Error writing to cache folder", err); return callback(err); }); }); }; OfflineStorage.prototype.retrieve = function (callback) { fs_1.default.readdir(this.cachePath, callback); }; OfflineStorage.prototype.send = function (callback) { var _this = this; this.retrieve(function (err, items) { if (err) { console.error("[Raygun4Node] Error reading cache folder", err); return callback(err); } if (items.length > 0) { debug("[raygun.offline.ts] Offline storage - transporting ".concat(items.length, " message(s) from cache")); } for (var i = 0; i < items.length; i++) { _this._sendAndDelete(items[i]); } callback(err, items); }); }; return OfflineStorage; }()); exports.OfflineStorage = OfflineStorage;