Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (85 loc) · 2.81 KB
/
Copy pathindex.js
File metadata and controls
98 lines (85 loc) · 2.81 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
'use strict';
const fs = require('fs');
const Writable = require('stream').Writable;
const debug = require('debug')('node-rotate');
class NodeRotate extends Writable {
constructor(options) {
const superOptions = {
highWaterMark: options.highWaterMark
};
const selfOptions = {
filename: options.filename,
retryCount: options.retryCount || 3,
reconnectCount: options.reconnectCount || 3,
overwrite: options.overwrite || []
};
super(superOptions);
this.filename = selfOptions.filename;
this.retryCount = selfOptions.retryCount;
this.reconnectCount = selfOptions.reconnectCount;
this.overwrite = selfOptions.overwrite;
this.errCount = 0;
this.writer = this._open();
if (this.overwrite instanceof Array && this.overwrite.length > 0) {
for (const item of this.overwrite) {
if (item === 'stdout') {
process.stdout.write = this.write.bind(this);
} else if (item === 'stderr') {
process.stderr.write = this.write.bind(this);
}
}
}
}
_write(chunk, encoding, callback) {
this._rewrite(this.retryCount, chunk, encoding, function (err) {
return callback(err);
});
}
/**
* rewrite data to writer stream
* @param {Number} retryCount retry count
* @param {String|Buffer} chunk data write to stream
* @param {String} encoding encoding of chunk
* @param {Function} callback callback
*/
_rewrite(retryCount, chunk, encoding, callback) {
debug(`retryCount: ${retryCount}`);
const _this = this;
_this.writer.write(chunk, encoding, function (err) {
if (err) {
debug(`fs write stream error: ${err.message}`);
retryCount -= 1;
if (retryCount > 0) {
_this._increaseErrCount();
_this._rewrite(retryCount, chunk, encoding, callback);
} else {
return callback(err);
}
} else {
return callback();
}
});
}
/**
* increase error count of write data to writer stream
*/
_increaseErrCount() {
this.errCount += 1;
if (this.errCount >= this.reconnectCount) {
this.errCount = 0;
this.writer.end();
this.writer = this._open();
}
}
/**
* return fs.createWriteStream
* @return {Stream} fs.createWriteStream
*/
_open() {
if (!this.filename) {
throw new Error('Lack of filename.');
}
return fs.createWriteStream(this.filename, { flags: 'a' });
}
}
module.exports = NodeRotate;