UNPKG

the-shepherd

Version:
38 lines (34 loc) 889 B
// Generated by CoffeeScript 2.5.1 (function() { var $; $ = require('bling'); module.exports.read_stream = function(socket, cb) { var buf, ret; ret = new $.Promise(); buf = ""; socket.on('data', function(data) { var item, rest, results; buf += data.toString("utf8"); results = []; while (buf.length > 0) { [item, rest] = $.TNET.parseOne(buf); if (rest.length === buf.length) { // if we didn't consume anything, wait for the next data to resume parsing break; } buf = rest; results.push(cb(item)); } return results; }); socket.on('error', function(err) { return ret.reject(err); }); socket.on('close', function() { return ret.resolve(); }); socket.on('end', function() { return ret.resolve(); }); return ret; }; }).call(this);