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
68 lines (54 loc) · 1.71 KB
/
NodeFileStorage.ts
File metadata and controls
68 lines (54 loc) · 1.71 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
import { KeyValueStorageBase } from './KeyValueStorageBase';
import * as Fs from 'fs';
import * as Path from 'path';
export class NodeFileStorage extends KeyValueStorageBase {
private directory: string;
private prefix: string;
private fs: any;
constructor(namespace: string, folder?: string, prefix: string = 'ex-', maxItems: number = 20, fs?: any) {
super(maxItems);
if (!folder) {
folder = require.main && require.main.filename ? Path.join(Path.dirname(require.main.filename), '.exceptionless') : '.exceptionless';
}
const subFolder = Path.join(folder, namespace);
this.directory = Path.resolve(subFolder);
this.prefix = prefix;
this.fs = fs ? fs : Fs;
this.mkdir(this.directory);
}
public write(key: string, value: string) {
this.fs.writeFileSync(key, value);
}
public read(key: string) {
return this.fs.readFileSync(key, 'utf8');
}
public readAllKeys() {
return this.fs.readdirSync(this.directory)
.filter((file) => file.indexOf(this.prefix) === 0)
.map((file) => Path.join(this.directory, file));
}
public delete(key: string) {
this.fs.unlinkSync(key);
}
public getKey(timestamp): string {
return Path.join(this.directory, `${this.prefix}${timestamp}.json`);
}
public getTimestamp(key: string) {
return parseInt(Path.basename(key, '.json')
.substr(this.prefix.length), 10);
}
private mkdir(path: string) {
const dirs = path.split(Path.sep);
let root = '';
while (dirs.length > 0) {
const dir = dirs.shift();
if (dir === '') {
root = Path.sep;
}
if (!this.fs.existsSync(root + dir)) {
this.fs.mkdirSync(root + dir);
}
root += dir + Path.sep;
}
}
}