Takes in a readable stream and returns a promise. The promise is resolved
when all the data is read. Or reject if the stream has errors.

@param {Stream} stream - Stream to read from to resolve the promise with.

@returns {Promise}
484300
function readableStream(stream) {
  return new Promise(function(resolve, reject) {
    var chunks = {
      data: []
    };

    function onData(chunk) {
      if (!chunks.type) {
        chunks.type = chunkTypes[typeof chunk];
      }

      chunks.data.push(chunk);
    }

    function onEnd() {
      if (chunks.type === chunkTypes.object) {
        resolve(Buffer.concat(chunks.data));
      }
      else {
        resolve(chunks.data.join(""));
      }
    }

    function onError(error) {
      reject(error);
    }

    stream
      .on("error", onError)
      .on("data", onData)
      .on("end", onEnd);
  });
}
727559
function copy(src, dest) {
	return new Promise((resolve, reject) => {
		var cbCalled = false;
		var rd = fs.createReadStream(src);
		rd.on("error", (err) => {
			done(err);
		});
		var wr = fs.createWriteStream(dest);
		wr.on("error", (err) => {
			done(err);
		});
		wr.on("close", () => {
			done();
		});
		rd.pipe(wr);

		function done(err) {
			if (!cbCalled) {
				cbCalled = true;
				!err ? resolve() : reject(err);
			}
		}
	});
}
1155944
function writeStream(filename, st) {
    var d = Q.defer();

    var wstream = fs.createWriteStream(filename);

    wstream.on('finish', function () {
        d.resolve();
    });
    wstream.on('error', function (err) {
        d.reject(err);
    });

    st.on('error', function(err) {
        d.reject(err);
    });

    st.pipe(wstream);

    return d.promise;
}
1229381
function getStreamStream (readable, file) {
  return () => {
    const counter = new stream.Transform()
    counter._transform = function (buf, enc, done) {
      file.length += buf.length
      this.push(buf)
      done()
    }
    readable.pipe(counter)
    return counter
  }
}
571496
function readLines(path, callback) {
    return new Promise(function(resolve, reject) {
        const stream = fsExtra.createReadStream(path);
        stream.on('error', reject);
        stream.on('end', resolve);

        const lineReader = readline.createInterface({
            input : stream
        });
        lineReader.on('line', callback);
    });
}
384363
function writeStreamToFile(fileMeta, stream) {
  var deferred = q.defer();
  stream.on('end', function() {
    deferred.resolve(fileMeta);
  });
  stream.on('error', function(error) {
    deferred.reject(error);
  });
  var filename = imageDir + '/' + fileMeta.uid;
  stream.pipe(fs.createWriteStream(filename));
  return deferred.promise;
}
592794
function readPNG(file) {
    const deferred = Q.defer();
    const onerror = deferred.reject.bind(deferred);
    const stream = fs.createReadStream(file);
    stream.on("error", onerror);
    pngparse.parseStream(stream, function(err, image) {
        if (err) {
            console.log("Failed to load " + file);
            onerror(err);
            return;
        }
        deferred.resolve(image);
    });
    return deferred.promise;
}
1315938
def promise(*args, &callback)
      parent, headers, flags = *args
      promise = new_stream(parent: parent)
      promise.send(
        type: :push_promise,
        flags: flags,
        stream: parent.id,
        promise_stream: promise.id,
        payload: headers.to_a,
      )

      callback.call(promise)
    end
91056
function promisedRequest(params, callback) {
  var deferred = Promise.defer();
  var req;
  var createRequest = function() {
    if (!req) {
      req = request(params, function(err, response) {
        if (err) {
          deferred.reject(err);
        } else {
          deferred.resolve(response);
        }
      });
    }
    return req;
  };
  return streamify(deferred.promise, createRequest).thenCall(callback);
}
446708
function stream (env) {
    let parseStream = parseFilter(env)

    let filter = through.obj((file, enc, cb) => cb(), function (cb) {
      parseStream.promise.then(data => {
        this.push(data)
        cb()
      }, cb)
    })

    return pipe(parseStream, filter)
  }
232720
function charybdis (iterator) {
  iterator = iterator || K
  const stream = new Writable({objectMode: true})
  const deferred = Promise.defer()

  const stats = stream.stats = {
    objects: 0,
    operations: 0,
    pending: 0,
    start: Date.now(),
    inEnd: 0,
    end: 0,
  }

  var sources = refcount()
  stream.on('pipe', onPipe)
  function onPipe (src) {
    sources.push()
    src.on('end', function () {
      sources.pop()
    })
    src.once('error', cleanup)
  }
  sources.on('clear', function () {
    stats.inEnd = Date.now()
    stream.emit('inEnd')
    tryResolve()
  })

  stream._write = function(obj, encoding, cb) {
    stats.objects++
    var op = iterator(obj)
    if (!op || !op.then) {
      tick()
      return cb()
    }

    stats.pending++
    stats.operations++

    tick()

    op.then(function () {
      stats.pending--
      tryResolve()
    },
    cleanup)

    cb()
  }

  function tryResolve() {
    // in streams ended and no operations pending?
    if (stats.inEnd && !stats.pending) { cleanup() }
    else tick()
  }

  function tick() {
    stream.emit('tick', stats)
  }

  function cleanup(err) {
    stream.removeListener('pipe', onPipe)
    sources.removeAllListeners()

    if (err) {
      deferred.reject(err)
    }
    stats.end = Date.now()

    deferred.resolve(stats)
  }

  stream.then = deferred.promise.then.bind(deferred.promise)

  return stream
}
992161
function( stream ){
			if( typeof stream == 'string' ){
				//if node.js create a writeable stream with this filename
			} else if( internals.hasProperties(stream,['write','end','writable'] && stream.writable)){
				this.objStream = stream;
			} else {
			}
			this._handleBeginSave();
		}
316224
async function* streamToGenerator(stream) {
  let promise;

  stream.on('data', data => promise.resolve(data));
  stream.once('end', () => promise.resolve(null));

  stream.on('error', error => {
    stream.removeAllListeners();
    promise.reject(error);
  });

  while (true) {
    promise = defer();

    yield promise;
  }
}
354271
function setUpReadable(stream, deserialize) {
  stream.deserialize = common.wrapIgnoreNull(deserialize);
  stream.finished = false;
  stream.reading = false;

  stream.terminate = function() {
    stream.finished = true;
    stream.on('data', function() {});
  };

  stream.on('cancelled', function() {
    stream.terminate();
  });
}
792776
function isStreamReady(self) {
	const method = 'isStreamReady';
	let ready = false;
	if (self._stream) {
		const stream = self._stream;
		ready = stream.readable && stream.writable && stream.reading;
		logger.debug('%s - stream.readable %s :: %s', method, stream.readable, self.getPeerAddr());
		logger.debug('%s - stream.writable %s :: %s', method, stream.writable, self.getPeerAddr());
		logger.debug('%s - stream.reading %s :: %s', method, stream.reading, self.getPeerAddr());
		logger.debug('%s - stream.read_status %s :: %s', method, stream.read_status, self.getPeerAddr());
		logger.debug('%s - stream.received_status %s :: %s', method, stream.received_status, self.getPeerAddr());
	}

	return ready;
}
1140451
function ChunkStreamWithStream(stream, options) {
  ChunkStream.call(this, options);

  stream.pause(); // Pause stream and wait for data listener. It's useful for node v0.8
  this._stream = stream;
  this._stream.on('end', this.end.bind(this)); // Should catch the end event for node v0.8
}
732055
function readerToStream(reader) {
    return new ReadableStream({
        pull(controller) {
            return reader.read()
                .then(res => {
                    if (res.done) {
                        controller.close();
                    } else {
                        controller.enqueue(res.value);
                    }
                });
        },
        cancel(reason) { return reader.cancel(reason); }
    });
}