-
Notifications
You must be signed in to change notification settings - Fork 29
/
raygun.offline.ts
118 lines (95 loc) · 2.99 KB
/
raygun.offline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* raygun
* https://github.com/MindscapeHQ/raygun4node
*
* Copyright (c) 2015 MindscapeHQ
* Licensed under the MIT license.
*/
"use strict";
import fs from "fs";
import path from "path";
import {
MessageTransport,
IOfflineStorage,
OfflineStorageOptions,
} from "./types";
const debug = require("debug")("raygun");
export class OfflineStorage implements IOfflineStorage {
cachePath: string = "";
cacheLimit: number = 100;
transport: MessageTransport;
constructor(transport: MessageTransport) {
this.transport = transport;
}
private _sendAndDelete(item: string) {
fs.readFile(
path.join(this.cachePath, item),
"utf8",
(err, cacheContents) => {
// Attempt to send stored messages after going online
this.transport.send(cacheContents);
// Ignore result, delete stored content nevertheless
fs.unlink(path.join(this.cachePath, item), () => {});
},
);
}
init(offlineStorageOptions: OfflineStorageOptions | undefined) {
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=${this.cachePath}, cacheLimit=${this.cacheLimit})`,
);
if (!fs.existsSync(this.cachePath)) {
fs.mkdirSync(this.cachePath);
}
return this;
}
save(transportItem: string, callback: (err: Error | null) => void) {
const filename = path.join(this.cachePath, Date.now() + ".json");
fs.readdir(this.cachePath, (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.writeFile(filename, transportItem, "utf8", function (err) {
if (!err) {
debug(
`[raygun.offline.ts] Offline storage - wrote message to ${filename}`,
);
return callback(null);
}
console.error("[Raygun4Node] Error writing to cache folder", err);
return callback(err);
});
});
}
retrieve(
callback: (error: NodeJS.ErrnoException | null, items: string[]) => void,
) {
fs.readdir(this.cachePath, callback);
}
send(callback: (error: Error | null, items?: string[]) => void) {
this.retrieve((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 ${items.length} message(s) from cache`,
);
}
for (let i = 0; i < items.length; i++) {
this._sendAndDelete(items[i]);
}
callback(err, items);
});
}
}