forked from iccicci/rotating-file-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
275 lines (205 loc) · 5.38 KB
/
index.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"use strict";
var compress = require("./compress");
var fs = require("fs");
var interval = require("./interval");
var path = require("path");
var util = require("util");
var utils = require("./utils");
var Writable = require("stream").Writable;
function RotatingFileStream(filename, options) {
if(! (this instanceof RotatingFileStream))
return new RotatingFileStream(filename, options);
if(! options)
options = {};
else {
if(typeof options != "object")
throw new Error("Don't know how to handle 'options' type: " + typeof options);
var tmp = {};
for(var i in options)
tmp[i] = options[i];
options = tmp;
}
utils.checkOptions(options);
if(typeof filename == "function")
this.generator = filename;
else
if(typeof filename == "string") {
if(options.rotate)
this.generator = utils.createClassical(filename);
else
this.generator = utils.createGenerator(filename);
}
else
throw new Error("Don't know how to handle 'filename' type: " + typeof filename);
if(options.path) {
var generator = this.generator;
this.generator = function(time, index) {
return path.join(options.path, generator(time, index));
};
}
var opt = {};
if(options.highWaterMark)
opt.highWaterMark = options.highWaterMark;
if(options.mode)
opt.mode = options.mode;
Writable.call(this, opt);
this.chunks = [];
this.options = options;
this.size = 0;
utils.setEvents(this);
this.firstOpen();
}
util.inherits(RotatingFileStream, Writable);
RotatingFileStream.prototype._close = function(done) {
if(this.stream) {
this.stream.on("finish", done);
this.stream.end();
this.stream = null;
}
else
done();
};
RotatingFileStream.prototype._rewrite = function() {
var self = this;
var callback = function() {
if(self.ending)
self._close(Writable.prototype.end.bind(self));
};
if(this.err) {
for(var i in this.chunks)
if(this.chunks[i].cb)
this.chunks[i].cb();
this.chunks = [];
return callback();
}
if(this.writing || this.rotation)
return;
if(this.options.size && this.size >= this.options.size)
return this.rotate();
if(! this.stream)
return;
if(! this.chunks.length)
return callback();
var chunk = this.chunks[0];
this.chunks.shift();
this.size += chunk.chunk.length;
this.writing = true;
this.stream.write(chunk.chunk, function(err) {
self.writing = false;
if(err)
self.emit("error", err);
if(chunk.cb)
chunk.cb();
process.nextTick(self._rewrite.bind(self));
});
};
RotatingFileStream.prototype._write = function(chunk, encoding, callback) {
this.chunks.push({ chunk: chunk, cb: callback });
this._rewrite();
};
RotatingFileStream.prototype._writev = function(chunks, callback) {
chunks[chunks.length - 1].cb = callback;
this.chunks = this.chunks.concat(chunks);
this._rewrite();
};
RotatingFileStream.prototype.firstOpen = function() {
var self = this;
try {
this.name = this.generator(null);
}
catch(err) {
return process.nextTick(function() {
self.emit("error", err);
process.nextTick(self._rewrite.bind(self));
});
}
this.once("open", this.interval.bind(this));
fs.stat(this.name, function(err, stats) {
if(err) {
if(err.code == "ENOENT")
return self.open();
return self.emit("error", err);
}
if(! stats.isFile())
return self.emit("error", new Error("Can't write on: " + self.name + " (it is not a file)"));
self.size = stats.size;
if((! self.options.size) || stats.size < self.options.size)
return self.open();
if(self.options.interval)
self._interval(self.now());
self.rotate();
});
};
RotatingFileStream.prototype.move = function(retry) {
var name;
var self = this;
var callback = function(err) {
if(err)
return self.emit("error", err);
self.open();
if(self.options.compress)
self.compress(name);
else {
self.emit("rotated", name);
self.interval();
}
};
this.findName({}, self.options.compress, function(err, found) {
if(err)
return callback(err);
name = found;
fs.rename(self.name, name, function(err) {
if(err && err.code != "ENOENT" && ! retry)
return callback(err);
if(! err)
return callback();
utils.makePath(name, function(err) {
if(err)
return callback(err);
self.move(true);
});
});
});
};
RotatingFileStream.prototype.now = function() {
return new Date().getTime();
};
RotatingFileStream.prototype.open = function(retry) {
var fd;
var self = this;
var options = { flags: "a" };
var callback = function(err) {
if(err)
self.emit("error", err);
process.nextTick(self._rewrite.bind(self));
};
if("mode" in this.options)
options.mode = this.options.mode;
var stream = fs.createWriteStream(this.name, options);
stream.once("open", function() {
self.stream = stream;
self.emit("open");
callback();
});
stream.once("error", function(err) {
if(err.code != "ENOENT" && ! retry)
return callback(err);
utils.makePath(self.name, function(err) {
if(err)
return callback(err);
self.open(true);
});
});
};
RotatingFileStream.prototype.rotate = function() {
this.size = 0;
this.rotation = new Date();
this._clear();
this._close(this.options.rotate ? this.classical.bind(this, this.options.rotate) : this.move.bind(this));
this.emit("rotation");
};
for(var i in compress)
RotatingFileStream.prototype[i] = compress[i];
for(var i in interval)
RotatingFileStream.prototype[i] = interval[i];
module.exports = RotatingFileStream;