You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
866 B
45 lines
866 B
var Stream = require("stream") |
|
var console = require("console") |
|
|
|
var NEW_LINE = "\n" |
|
|
|
module.exports = ConsoleStream |
|
|
|
function ConsoleStream() { |
|
var stream = new Stream() |
|
stream.writable = true |
|
var buffered = "" |
|
|
|
stream.write = write |
|
stream.destroy = destroy |
|
stream.end = end |
|
|
|
return stream |
|
|
|
function write(buffer) { |
|
var s = buffered + String(buffer) |
|
var lines = s.split(NEW_LINE) |
|
for (var i = 0; i < lines.length - 1; i++) { |
|
console.log(lines[i]) |
|
} |
|
|
|
buffered = lines[i] |
|
} |
|
|
|
function destroy() { |
|
stream.writable = false |
|
stream.emit("close") |
|
} |
|
|
|
function end(buffer) { |
|
if (arguments.length === 1) { |
|
stream.write(buffer) |
|
} |
|
|
|
if (buffered) { |
|
console.log(buffered) |
|
} |
|
|
|
stream.destroy() |
|
} |
|
}
|
|
|