Jump To …
node-get-file.js encodings.js getutil.js index.js node-get.js

# getutil.js |
--- | ---
¶ A near-exact reimplementation of sys.pump, but sys.pump fails in regards to correct encoding between buffers. |
    var pump = function(readStream, writeStream, callback) {
      var callbackCalled = false;

      function call(a, b, c) {
        if (callback && !callbackCalled) {
          callback(a, b, c);
          callbackCalled = true;
        }
      }

      if (!readStream.pause) {
        readStream.pause = function() {readStream.emit('pause');};
      }

      if (!readStream.resume) {
        readStream.resume = function() {readStream.emit('resume');};
      }

      readStream.addListener('data', function(chunk) {
        if (writeStream.write(chunk, readStream.encoding) === false) readStream.pause();
      });

      writeStream.addListener('pause', function() {
        readStream.pause();
      });

      writeStream.addListener('drain', function() {
        readStream.resume();
      });

      writeStream.addListener('resume', function() {
        readStream.resume();
      });

      readStream.addListener('end', function() {
        writeStream.end();
        call();
      });

      readStream.addListener('close', function() {
        call();
      });

      readStream.addListener('error', function(err) {
        writeStream.end();
        call(err);
      });

      writeStream.addListener('error', function(err) {
        readStream.destroy();
        call(err);
      });
    };

    module.exports = { pump: pump };