forked from iccicci/rotating-file-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
214 lines (162 loc) · 4.39 KB
/
utils.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict";
var fs = require("fs");
var path = require("path");
function checkMeasure(v, what, units) {
var ret = {};
ret.num = parseInt(v);
if(isNaN(ret.num))
throw new Error("Unknown 'options." + what + "' format: " + v);
if(ret.num <= 0)
throw new Error("A positive integer number is expected for 'options." + what + "'");
ret.unit = v.replace(/^[ 0]*/g, "").substr((ret.num + "").length, 1);
if(ret.unit.length === 0)
throw new Error("Missing unit for 'options." + what + "'");
if(! units[ret.unit])
throw new Error("Unknown 'options." + what + "' unit: " + ret.unit);
return ret;
}
var intervalUnits = {
d: true,
h: true,
m: true,
s: true
};
function checkInterval(v) {
var ret = checkMeasure(v, "interval", intervalUnits);
switch(ret.unit) {
case "m":
if(parseInt(60 / ret.num) * ret.num != 60)
throw new Error("An integer divider of 60 is expected as minutes for 'options.interval'");
break;
case "h":
if(parseInt(24 / ret.num) * ret.num != 24)
throw new Error("An integer divider of 24 is expected as hours for 'options.interval'");
break;
case "s":
if(parseInt(60 / ret.num) * ret.num != 60)
throw new Error("An integer divider of 60 is expected as seconds for 'options.interval'");
break;
}
return ret;
}
var sizeUnits = {
B: true,
G: true,
K: true,
M: true
};
function checkSize(v) {
var ret = checkMeasure(v, "size", sizeUnits);
if(ret.unit == "K")
return ret.num * 1024;
if(ret.unit == "M")
return ret.num * 1048576;
if(ret.unit == "G")
return ret.num * 1073741824;
return ret.num;
}
var checks = {
"compress": function(typ, options, val) {
if(! val)
throw new Error("A value for 'options.compress' must be specified");
if(typ == "boolean")
options.compress = function(src, dst) { return "cat " + src + " | gzip -c9 > " + dst; };
else
if(typ == "string") {
//if(val != "bzip" && val != "gzip")
if(val != "gzip")
throw new Error("Don't know how to handle compression method: " + val);
}
else
if(typ != "function")
throw new Error("Don't know how to handle 'options.compress' type: " + typ);
},
"highWaterMark": function() {},
"interval": function(typ, options, val) {
if(typ != "string")
throw new Error("Don't know how to handle 'options.interval' type: " + typ);
options.interval = checkInterval(val);
},
"mode": function() {},
"path": function(typ) {
if(typ != "string")
throw new Error("Don't know how to handle 'options.path' type: " + typ);
},
"rotate": function(typ, options, val) {
var rotate = parseInt(val);
if(rotate != val || rotate <= 0)
throw new Error("'rotate' option must be a positive integer number");
},
"rotationTime": function() {},
"size": function(typ, options, val) {
if(typ != "string")
throw new Error("Don't know how to handle 'options.size' type: " + typ);
options.size = checkSize(val);
}
};
function checkOptions(options) {
for(var opt in options) {
var val = options[opt];
var typ = typeof val;
if(! (opt in checks))
throw new Error("Unknown option: " + opt);
checks[opt](typ, options, val);
}
}
function pad(num) {
return (num > 9 ? "" : "0") + num;
}
function createClassical(filename) {
return function(index) {
if(! index)
return filename;
return filename + "." + index;
};
}
function createGenerator(filename) {
return function(time, index) {
if(! time)
return filename;
var month = time.getFullYear() + "" + pad(time.getMonth() + 1);
var day = pad(time.getDate());
var hour = pad(time.getHours());
var minute = pad(time.getMinutes());
return month + day + "-" + hour + minute + "-" + pad(index) + "-" + filename;
};
}
function makePath(name, callback) {
var dir = path.parse(name).dir;
fs.mkdir(dir, function(e) {
if(e) {
if(e.code == "ENOENT")
return makePath(dir, callback);
return callback(e);
}
callback();
});
}
function setEvents(self) {
self.once("error", function(err) {
self.err = err;
self.end();
});
self.once("finish", self._clear.bind(self));
self.on("rotated", function() {
self.rotation = null;
self._rewrite();
});
self.end = function(chunk, encoding, callback) {
self.ending = true;
if(chunk)
self.write(chunk, encoding, callback);
else
self._rewrite();
};
}
module.exports = {
checkOptions: checkOptions,
createClassical: createClassical,
createGenerator: createGenerator,
makePath: makePath,
setEvents: setEvents,
};