Skip to content

Instantly share code, notes, and snippets.

@bobpoekert
Created November 12, 2010 00:34
Show Gist options
  • Save bobpoekert/673518 to your computer and use it in GitHub Desktop.
Save bobpoekert/673518 to your computer and use it in GitHub Desktop.
tail
var tail = function(fname, onData, ender) {
fs.open(fname, 'a+', function(err, fd) {
if (err) {
puts('error!: '+err);
if (ender(fd, 0) !== false) {
setTimeout(function(){
tail(fname, onData);
}, 10);
}
} else {
puts('allocating buffer');
var size = 1024;
var buffer = new Buffer(size);
var pos = 0;
var reader = function() {
fs.read(fd, buffer, 0, size, pos, function(err, bytesRead) {
if (bytesRead > 0) {
pos += bytesRead;
onData(buffer.slice(0, bytesRead).toString('utf-8'), fd);
}
if (ender && ender(fd, pos) === false) {
fs.close(fd);
} else {
if (bytesRead == 0) {
setTimeout(reader, 10);
} else {
reader();
}
}
});
};
reader();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment