index.js
981 Bytes
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
var fs = require('fs');
var Writable = require('readable-stream/writable');
var exists = function(path) {
try {
return fs.existsSync(path);
} catch (err) {
return false;
}
};
module.exports = function() {
var s = new Writable({highWaterMark:0});
var cb;
var data;
var tries = 0;
var offset = 0;
var write = function() {
fs.write(1, data, offset, data.length - offset, null, onwrite);
};
var onwrite = function(err, written) {
if (err && err.code === 'EPIPE') return cb()
if (err && err.code === 'EAGAIN' && tries++ < 30) return setTimeout(write, 10);
if (err) return cb(err);
tries = 0;
if (offset + written >= data.length) return cb();
offset += written;
write();
};
s._write = function(_data, enc, _cb) {
offset = 0;
cb = _cb;
data = _data;
write();
};
s._isStdio = true;
s.isTTY = process.stdout.isTTY;
s.on('finish', function() {
fs.close(1, function(err) {
if (err) s.emit('error', err);
});
});
return s;
}();