How to use the streams.ReadableStream function in streams

To help you get started, we’ve selected a few streams examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mozilla-b2g / gaia-email-libs-and-more / js / mime-streams.js View on Github external
exports.SocketStream = function(socket) {
  socket = util.makeEventTarget(socket);

  function maybeCloseSocket() {
    if (socket.readyState !== 'closing' && socket.readyState !== 'closed') {
      socket.close();
    }
  }

  var out;

  this.readable = new streams.ReadableStream({
    start: function(c) {
      out = c;
      socket.addEventListener('data', (evt) => {
        c.enqueue(new Uint8Array(evt.data))
      });
      socket.addEventListener('close', () => {
        try {
          c.close();
        } catch(e) {
          // The stream has already been closed.
        }
      });
      socket.addEventListener('error', (evt) => c.error(evt.data || evt));
    },
    cancel: function() {
      maybeCloseSocket();
github mozilla-b2g / gaia-email-libs-and-more / js / imap / protocol / message_chunked_part_stream.js View on Github external
return function messageChunkedPartStream({
    pimap, folderInfo, uid, parts, downloadChunkSize, saveChunkSize }) {
  // Pull the parts off as we go.
  let remainingPartsToFetch = parts.slice();

  // A pull stream, where each pull() corresponds to fetching a single part and
  // the generator will enqueue once for each blob and once to close out the
  // part.
  return new ReadableStream({
    start() {
    },

    pull: co.wrap(function*(out) {
      if (!remainingPartsToFetch.length) {
        out.close();
        return;
      }

      let blobIndex = 0;
      let partInfo = remainingPartsToFetch.shift();
      let mimeStream = chunkedDownloadMimeStream({
        pimap,
        folderInfo,
        uid,
        partInfo,
github mozilla-b2g / gaia-email-libs-and-more / js / imap / protocol / chunked_download_mime_stream.js View on Github external
return function chunkedDownloadMimeStream(
  { ctx, pimap, folderInfo, uid, partInfo, downloadChunkSize, saveChunkSize }) {
  let byteIndex = 0;

  let byteStream = new ReadableStream({
    start(out) {
      out.enqueue(
        mimefuncs.charset.encode(
          'Content-Type: ' + partInfo.type + '\r\n' +
          'Content-Transfer-Encoding: ' + partInfo.encoding + '\r\n' +
          '\r\n'));
    },
    pull(out) {
      return pimap.fetchBody(
        ctx,
        folderInfo,
        {
          uid,
          part: partInfo.part,
          byteRange: {
            offset: byteIndex,
github mozilla-b2g / gaia-email-libs-and-more / js / mime-streams.js View on Github external
startPart(jsmimePartNum, jsmimeHeaders) {
      var partNum = (jsmimePartNum === '' ? '1' : '1.' + jsmimePartNum);
      var headers = MimeHeaderInfo.fromJSMime(jsmimeHeaders, {
        parentContentType:
          partNum.indexOf('.') !== -1
            ? partToContentTypeMap.get(
                partNum.slice(0, partNum.lastIndexOf('.')))
            : null
      });

      partToContentTypeMap.set(partNum, headers.contentType);

      var bodyStream = null;
      if (headers.mediatype !== 'multipart') {
        bodyStream = new streams.ReadableStream({
          start(controller) {
            partToStreamControllerMap.set(partNum, controller);
          }
        }).pipeThrough(new exports.BlobTransformStream());
      }

      out.enqueue({
        partNum: partNum,
        headers: headers,
        bodyStream: bodyStream
      });
    },
    endPart(jsmimePartNum) {

streams

A lazy streams library for functional composition in JavaScript.

MIT
Latest version published 21 days ago

Package Health Score

70 / 100
Full package analysis