forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeFileStorage.ts
More file actions
79 lines (61 loc) · 1.58 KB
/
NodeFileStorage.ts
File metadata and controls
79 lines (61 loc) · 1.58 KB
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
import {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
unlinkSync,
writeFileSync
} from "fs";
import {
basename,
dirname,
join,
resolve,
sep
} from "path";
import { argv } from "process";
import { KeyValueStorageBase } from "@exceptionless/core";
export class NodeFileStorage extends KeyValueStorageBase {
private directory: string;
constructor(namespace: string, folder?: string, maxItems: number = 20) {
super(maxItems);
if (!folder) {
folder = argv && argv.length > 1 ? join(dirname(argv[1]), ".exceptionless") : ".exceptionless";
}
const subFolder = join(folder, namespace);
this.directory = resolve(subFolder);
this.mkdir(this.directory);
}
public writeValue(key: string, value: string): void {
writeFileSync(key, value);
}
public readValue(key: string): string {
return readFileSync(key, "utf8");
}
public removeValue(key: string): void {
unlinkSync(key);
}
public getAllKeys(): string[] {
return readdirSync(this.directory).map((file) => join(this.directory, file));
}
public getKey(timestamp: number): string {
return join(this.directory, `${timestamp}.json`);
}
public getTimestamp(key: string): number {
return parseInt(basename(key, ".json"), 10);
}
private mkdir(directory: string): void {
const dirs = directory.split(sep);
let root = "";
while (dirs.length > 0) {
const dir = dirs.shift();
if (dir === "") {
root = sep;
}
if (!existsSync(root + dir)) {
mkdirSync(root + dir);
}
root += dir + sep;
}
}
}