-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathdefault_stream.js
52 lines (48 loc) · 1.06 KB
/
default_stream.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
'use strict';
var through = require('@ljharb/through');
var fs = require('fs');
module.exports = function () {
var line = '';
var stream = through(write, flush);
return stream;
function write(buf) {
if (
buf == null // eslint-disable-line eqeqeq
|| (Object(buf) !== buf && typeof buf !== 'string')
) {
flush();
return;
}
for (var i = 0; i < buf.length; i++) {
var c = typeof buf === 'string'
? buf.charAt(i)
: String.fromCharCode(buf[i]);
if (c === '\n') {
flush();
} else {
line += c;
}
}
}
function flush() {
if (fs.writeSync && (/^win/).test(process.platform)) {
try {
fs.writeSync(1, line + '\n');
} catch (e) {
stream.emit('error', e);
}
} else {
try {
if (typeof console !== 'undefined' && console.log) { // eslint-disable-line no-console
console.log(line); // eslint-disable-line no-console
} else if (typeof document !== 'undefined') {
// for IE < 9
document.body.innerHTML += line + '<br />';
}
} catch (e) {
stream.emit('error', e);
}
}
line = '';
}
};