lib/node-fs.js
function readLastChunk(path, start, end, callback) {
  nodeFs.open(path, "r", function (err, fd) {
    if (err) {
      if (err.code === "EACCES") return callback();
      return callback(err);
    }
    var buffer = new Buffer(4096);
    var length = 0;
    read();
    // Only the first read needs to seek.
    // All subsequent reads will continue from the end of the previous.
    start = null;

    function read() {
      if (buffer.length - length === 0) {
        grow();
      }
      nodeFs.read(fd, buffer, length, buffer.length - length, start, onread);
    }

    function onread(err, bytesRead) {
      if (err) return callback(err);
      length += bytesRead;
      if (bytesRead === 0) {
        return callback(null, buffer.slice(0, buffer.length + end));
      }
      read();
    }

    function grow() {
      var newBuffer = new Buffer(buffer.length * 2);
      buffer.copy(newBuffer);
      buffer = newBuffer;
    }
  });
}
function readLastChunk(path, start, end, callback) {
  nodeFs.open(path, "r", function (err, fd) {
    if (err) {
      if (err.code === "EACCES") return callback();
      return callback(err);
    }
    var buffer = new Buffer(4096);
    var length = 0;
    read();
    // Only the first read needs to seek.
    // All subsequent reads will continue from the end of the previous.
    start = null;

    function read() {
      if (buffer.length - length === 0) {
        grow();
      }
      nodeFs.read(fd, buffer, length, buffer.length - length, start, onread);
    }

    function onread(err, bytesRead) {
      if (err) return callback(err);
      length += bytesRead;
      if (bytesRead === 0) {
        return callback(null, buffer.slice(0, buffer.length + end));
      }
      read();
    }

    function grow() {
      var newBuffer = new Buffer(buffer.length * 2);
      buffer.copy(newBuffer);
      buffer = newBuffer;
    }
  });
}
function readLastChunk ( path , start , end , callback ) { nodeFs . open ( path , "r" , function ( err , fd ) { if ( err ) { if ( err . code === "EACCES" ) return callback ( ) ; return callback ( err ) ; } var buffer = new Buffer ( 4096 ) ; var length = 0 ; read ( ) ; // Only the first read needs to seek. // All subsequent reads will continue from the end of the previous. start = null ; function read ( ) { if ( buffer . length - length === 0 ) { grow ( ) ; } nodeFs . read ( fd , buffer , length , buffer . length - length , start , onread ) ; } function onread ( err , bytesRead ) { if ( err ) return callback ( err ) ; length += bytesRead ; if ( bytesRead === 0 ) { return callback ( null , buffer . slice ( 0 , buffer . length + end ) ) ; } read ( ) ; } function grow ( ) { var newBuffer = new Buffer ( buffer . length * 2 ) ; buffer . copy ( newBuffer ) ; buffer = newBuffer ; } } ) ; }
Node.js readable streams do not support reading from a position to the end of the file, but we can roll our own using the lower-level fs.open and fs.read on a file descriptor, which allows read to seek.
Node . js readable streams do not support reading from a position to the end of the file but we can roll our own using the lower - level fs . open and fs . read on a file descriptor which allows read to seek .