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
160 lines (136 loc) · 3.73 KB
/
NodeFileStorage.ts
File metadata and controls
160 lines (136 loc) · 3.73 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { IStorage } from './IStorage';
import { IStorageItem } from './IStorageItem';
import * as Fs from 'fs';
import * as Path from 'path';
interface IndexEntry {
name: string;
timestamp: number;
}
export class NodeFileStorage implements IStorage {
private directory: string;
private maxItems: number;
private timestamp: number;
private index: IndexEntry[];
private fs: any;
constructor(folder: string, maxItems: number = 20, fs?: any) {
this.directory = Path.resolve(folder);
this.maxItems = maxItems;
this.fs = fs ? fs : Fs;
mkdirParent(this.directory);
this.index = this.createIndex(this.directory);
this.timestamp = this.index.length > 0
? this.index[this.index.length - 1].timestamp
: 0;
}
save(path: string, value: any): boolean {
if (!path || !value) {
return false;
}
this.remove(path);
let entry = { name: path, timestamp: ++this.timestamp };
this.index.push(entry);
let fullPath = this.getFullPath(entry);
let json = JSON.stringify(value);
this.fs.writeFileSync(fullPath, json);
return true;
}
get(path: string): any {
try {
let entry = this.findEntry(path);
if (!entry) {
return null;
}
let fullPath = this.getFullPath(entry);
let json = this.fs.readFileSync(fullPath, 'utf8');
return JSON.parse(json, parseDate);
} catch (e) {
return null;
}
}
getList(searchPattern?: string, limit?: number): IStorageItem[] {
let entries = this.index;
if (searchPattern) {
let regex = new RegExp(searchPattern);
entries = entries.filter(entry => regex.test(entry.name));
}
if (entries.length > this.maxItems) {
entries = entries.slice(entries.length - this.maxItems);
}
if (entries.length > limit) {
entries = entries.slice(0, limit);
}
let items = entries.map(e => this.loadEntry(e));
return items;
}
remove(path: string): void {
try {
let entry = this.findEntry(path);
if (!entry) {
return null;
}
let fullPath = this.getFullPath(entry);
this.fs.unlinkSync(fullPath);
this.removeEntry(entry);
} catch (e) { }
}
private loadEntry(entry: IndexEntry) {
let fullPath = this.getFullPath(entry);
let created = this.fs.statSync(fullPath).birthtime.getTime();
let json = this.fs.readFileSync(fullPath, 'utf8');
let value = JSON.parse(json, parseDate);
return {
created: created,
path: entry.name,
value
};
}
private findEntry(path: string) {
for (let i = this.index.length - 1; i >= 0; i--) {
if (this.index[i].name === path) {
return this.index[i];
}
}
return null;
}
private removeEntry(entry: IndexEntry) {
let i = this.index.indexOf(entry);
if (i > -1) {
this.index.splice(i, 1);
}
}
private getFullPath(entry: IndexEntry) {
let filename = entry.name + '__' + entry.timestamp;
return Path.join(this.directory, filename);
}
private createIndex(path: string) {
let files = this.fs.readdirSync(path);
return files
.map(file => {
let parts = file.split('__');
return {
name: parts[0],
timestamp: parseInt(parts[1], 10)
};
}).sort((a, b) => a.timestamp - b.timestamp);
}
}
function parseDate(key, value) {
let dateRegx = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/g;
if (typeof value === 'string') {
let a = dateRegx.exec(value);
if (a) {
return new Date(value);
}
}
return value;
};
function mkdirParent(dirPath) {
try {
this.fs.mkdirSync(dirPath);
} catch (e) {
if (e.errno === 34) {
mkdirParent(Path.dirname(dirPath));
mkdirParent(dirPath);
}
}
}