-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
default_stream.js
55 lines (51 loc) · 1.23 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
53
54
55
'use strict';
var through = require('@ljharb/through');
var fs = require('fs');
/** @type {import('./default_stream')} */
module.exports = function () {
var line = '';
var stream = through(write, flush);
return stream;
/** @type {(buf: unknown) => void} */
function write(buf) {
if (
buf == null // eslint-disable-line eqeqeq
|| (Object(buf) !== buf && typeof buf !== 'string')
) {
flush();
return;
}
var b = /** @type {string | ArrayLike<number>} */ (buf); // eslint-disable-line no-extra-parens
for (var i = 0; i < b.length; i++) {
var c = typeof b === 'string'
? b.charAt(i)
: String.fromCharCode(b[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 = '';
}
};