Exemplo n.º 1
0
function protoSegment() {
  var o = {objectMode: true}
  var a = stream.PassThrough(o)
  var b = stream.PassThrough(o)
  var side1 = duplexer2(o, a, b)
  var side2 = duplexer2(o, b, a)
  side1.side2 = side2
  return side1
}
Exemplo n.º 2
0
//             multiplexes protocols over the netpipe wire
//  dht --->                                                   ---> dht
// xchg ---> outgoing --> join --> wire --> split --> incoming ---> xchng
// idnt --->                                                   ---> idnt
function ipfsNetwork(opts, peerbook, protocolStreams) {
  opts = opts || {}

  // main network pipe
  var wire = netpipe(opts, peerbook)

  // make the multiplexing pipes
  var unknown = stream.PassThrough(sopts)
  var outgoing = stream.PassThrough(sopts)
  var incoming = stream.Writable(sopts)
  incoming._write = multiplexInput

  // wire pipes
  outgoing.pipe(joinPayloads()).pipe(wire.messages)
  wire.messages.pipe(splitPayloads()).pipe(incoming)

  // proto pipes (using numbers)
  var protocols = {}
  for (var k in protocolStreams) {
    var s = protocolStreams[k]
    protocols[k] = s
    s.pipe(outgoing) // pipe all into outgoing
  }

  return segment({
    wire: wire,
    unknown: unknown,
    protocols: protocols,
  })

  function multiplexInput(item, enc, next) {
    var proto = protocols[protoTable[item.protocol]]
    if (proto) {
      proto.write(item)
    } else {
      unknown.write({
        error: new Error('unknown protocol'),
        message: item,
      })
    }
    next()
  }
}
Exemplo n.º 3
0
    torrent.files[0].getBuffer(function (err, cipherText) {
      clearTimeout(timeout)
      if (err) return cb(err)

      var readable = stream.PassThrough()
      readable.end(cipherText)
      var decipher = crypto.createDecipher(cipherKey)

      var decipherStream = pump(readable, decipher, onError)

      concat(decipherStream, cb)
    })
Exemplo n.º 4
0
    function readArg2(tres, arg2) {
        // TODO: currently does the wrong thing and doesn't implement a
        // multi-map, due to assuming node-like mangling on the other side
        // to ensure singularity
        var arg2res = bufrw.fromBufferResult(HTTPResArg2.RW, arg2);
        if (arg2res.err) {
            callback(arg2res.err, null, null);
        } else if (tres.streamed) {
            callback(null, arg2res.value, tres.arg3);
        } else {
            var body = PassThrough();
            body.end(tres.arg3);
            callback(null, arg2res.value, body);
        }

    }
Exemplo n.º 5
0
FileMuxer.prototype.input = function(readable) {
  assert(typeof readable.pipe === 'function', 'Invalid input stream supplied');
  assert(this._added < this._shards, 'Inputs exceed defined number of shards');

  var self = this;
  var input = readable.pipe(stream.PassThrough()).pause();

  input.on('end', function() {
    self._inputs.splice(self._inputs.indexOf(input), 1);
    self.emit('drain', input);
  });

  this._added++;
  this._inputs.push(input);

  return this;
};
  npm.registry.fetch(u, { auth: auth }, function (er, response) {
    if (er) {
      log.error('fetch failed', u)
      return cb(er, response)
    }

    var tarball = writeStreamAtomic(tmp, { mode: npm.modes.file })
    tarball.on('error', function (er) {
      cb(er)
      tarball.destroy()
    })

    tarball.on('finish', function () {
      if (!shasum) {
        // Well, we weren't given a shasum, so at least sha what we have
        // in case we want to compare it to something else later
        return sha.get(tmp, function (er, shasum) {
          log.silly('fetchAndShaCheck', 'shasum', shasum)
          cb(er, response, shasum)
        })
      }

      // validate that the url we just downloaded matches the expected shasum.
      log.silly('fetchAndShaCheck', 'shasum', shasum)
      sha.check(tmp, shasum, function (er) {
        if (er && er.message) {
          // add original filename for better debuggability
          er.message = er.message + '\n' + 'From:     ' + u
        }
        return cb(er, response, shasum)
      })
    })

    // 0.8 http streams have a bug, where if they're paused with data in
    // their buffers when the socket closes, they call `end` before emptying
    // those buffers, which results in the entire pipeline ending and thus
    // the point that applied backpressure never being able to trigger a
    // `resume`.
    // We work around this by piping into a pass through stream that has
    // unlimited buffering. The pass through stream is from readable-stream
    // and is thus a current streams3 implementation that is free of these
    // bugs even on 0.8.
    response.pipe(PassThrough({highWaterMark: Infinity})).pipe(tarball)
  })
Exemplo n.º 7
0
 testExpectations(desc, expected, function run(expect, done) {
     var writer = ChunkWriter(frameRW);
     var stream = PassThrough({
         objectMode: true
     });
     frames.forEach(function(frame) {
         stream.push(frame);
     });
     stream.push(null);
     writer.on('data', function onData(buffer) {
         expect('buffer', buffer);
     });
     writer.on('error', function onError(err) {
         expect('error', err);
         writer.end();
     });
     writer.on('finish', done);
     writer.on('end', done);
     stream.pipe(writer);
 });
Exemplo n.º 8
0
    function onArg2(err, arg2) {
        if (err) {
            sendError(err);
            return;
        }

        var arg2res = bufrw.fromBufferResult(HTTPReqArg2.RW, arg2);
        if (arg2res.err) {
            sendError(arg2res.err);
            return;
        }

        hreq.head = arg2res.value;
        if (req.streamed) {
            hreq.body = req.arg3;
        } else {
            hreq.body = PassThrough();
            hreq.body.end(req.arg3);
        }

        handle();
    }