DailyRotateFile.prototype.stream = function (options) {
  var file = path.join(this.dirname, this._getFilename());
  options = options || {};
  var stream = new Stream();

  var tail = {
    file: file,
    start: options.start
  };

  stream.destroy = common.tailFile(tail, function (err, line) {
    if (err) {
      return stream.emit('error', err);
    }

    try {
      stream.emit('data', line);
      line = JSON.parse(line);
      stream.emit('log', line);
    } catch (e) {
      stream.emit('error', e);
    }
  });

  if (stream.resume) {
    stream.resume();
  }

  return stream;
};
示例#2
0
文件: ftp.js 项目: fjakobs/vfs-ftp
        ftpClient.ls(path, function(err, list) {
            if (err || !list) {
                // Officially only ftp codes 450, 550 and 451 mean strictly that
                // the file doesn't exist, but let's assume that file doesn't exist
                // anyway if we reached this point
                var e = new Error("ENOENT - No such directory: " + path);
                e.code = "ENOENT";
                return callback(e);
            }

            var meta = {};
            if (options.head)
                return callback(null, meta);

            // There can be no proper ETag support since there is no way to
            // determine whether the directory contents have changed or not
            // without getting its listing, which defeats the purpose of caching.
            /*
            meta.etag = calcEtag(stat);
            if (options.etag === meta.etag) {
                meta.notModified = true;
                return callback(null, meta);
            }
            */

            var stream = new Stream();
            var paused;

            stream.readable = true;
            stream.pause = function () {
                if (paused === true) return;
                paused = true;
            };

            stream.resume = function () {
                if (paused === false) return;
                paused = false;
                getNext();
            };

            meta.stream = stream;
            callback(null, meta);

            var index = 0;
            stream.resume();
            function getNext() {
                if (index === list.length)
                    return done();

                var file = list[index++];
                var entry = {
                    name: file.name,
                    path: path,
                    href: "#",
                    mime: (file.type === 1) ? "inode/directory" : getMime(file.name),
                    size: parseInt(file.size, 10),
                    etag: calcEtag(file.name, file.time, file.size)
                };

                stream.emit("data", entry);

                if (!paused) {
                    getNext();
                }
            }
            function done() {
                stream.emit("end");
            }
        });