Example #1
0
  return content.map((data) => {
    // Buffer input
    if (Buffer.isBuffer(data)) {
      data = {
        path: '',
        content: pull.values([data])
      }
    }

    // Readable stream input
    if (isStream.isReadable(data)) {
      data = {
        path: '',
        content: toPull.source(data)
      }
    }

    if (data && data.content && typeof data.content !== 'function') {
      if (Buffer.isBuffer(data.content)) {
        data.content = pull.values([data.content])
      }

      if (isStream.isReadable(data.content)) {
        data.content = toPull.source(data.content)
      }
    }

    return data
  })
Example #2
0
tape('a stream errors', function (t) {
  var err = new Error('test-error')
  var aborted = []
  function check (read, i) {
    return function (abort, cb) {
      aborted[i] = true
      read(abort, function (end, data) {
        if(end) aborted[i] = true
        cb(end, data)
      })
    }
  }

  pull(
    many([
      check(pull.values(rand('a', 5)), 0),
      check(pull.values(rand('b', 4)), 1),
      error(err)
    ]),
    pull.collect(function (err, ary) {
      t.deepEqual(aborted, [true, true])
      t.end()
    })
  )
})
Example #3
0
 createReadStream: function (opts) {
   if(!opts)
     return pull.values(ary)
   var i = search(ary, {key: opts.gt || opts.gte}, compareKeys)
   //just doing one way streams for now...
   //handle full lt, lte, gt, gte LATER!
   return pull.values(ary, i)
 },
Example #4
0
test('abort - with empty', function (t) {
  pull(
    cat([pull.values([1,2,3]), null, pull.values([4,5,6])]),
    function (read) {
      read(true, function (err) {
        t.equal(err, true)
        t.end()
      })
    }
  )
})
Example #5
0
test('cat', function (t) {

  pull(
    cat([pull.values([1,2,3]), pull.values([4,5,6])]),
    pull.collect(function (err, ary) {
      console.log(err, ary)
      t.notOk(err)
      t.deepEqual(ary, [1,2,3,4,5,6])
      t.end()
    })
  )

})
Example #6
0
  function upto (opts) {
    opts = opts || {}
    var ary = Object.keys(replicate).map(function (k) {
      return { id: k, sequence: toSend[k]||0 }
    })
    if(opts.live)
      return Cat([
        pull.values(ary),
        pull.once({sync: true}),
        newPeers.listen()
      ])

    return pull.values(ary)
  }
Example #7
0
test('cat - with empty stream', function (t) {
  var ended = false
  var justEnd = function (err, cb) { ended = true; cb(true) }

  pull(
    cat([pull.values([1,2,3]), justEnd, pull.values([4,5,6])]),
    pull.collect(function (err, ary) {
      console.log(err, ary)
      t.ok(ended)
      t.notOk(err)
      t.deepEqual(ary, [1,2,3,4,5,6])
      t.end()
    })
  )
})
Example #8
0
tape('simple', function (t) {

  t.plan(4)

  var expected = [
    [null, 1],
    [1, 2],
    [2, 3],
    [3, null]
  ]

  pull(
    pull.values([1, 2, 3]),
    pairs(function (a, b) {
      t.deepEqual([a, b], expected.shift())
    }),
    pull.drain(null, function (err) {
      if(err) throw err

      t.end()
    })

  )

})
Example #9
0
tape('protect against reordering', function (t) {

  var input = randomBuffers(1024, 100)
  var key = testKey('reordering')

  pull(
    pull.values(input),
    boxes.createEncryptStream(key),
    pull.collect(function (err, valid) {
      //randomly switch two blocks
      var invalid = valid.slice()
      //since every even packet is a header,
      //moving those will produce valid messages
      //but the counters will be wrong.
      var i = rand(valid.length/2)*2
      var j = rand(valid.length/2)*2
      invalid[i] = valid[j]
      invalid[i+1] = valid[j+1]
      invalid[j] = valid[i]
      invalid[j+1] = valid[i+1]
      pull(
        pull.values(invalid),
        boxes.createDecryptStream(key),
        pull.collect(function (err, output) {
          t.notEqual(output.length, input.length)
          t.ok(err)
          console.log(err)
          t.end()
        })
      )
    })
  )
})
 function choose(pkgs) {
   pkgs.forEach(function (pkg) {
     pkg.shasum = pkg.shasum || pkg.dist.shasum
     pkg.tarball = pkg.dist.tarball
   })
   return pull.values(pkgs)
 }
Example #11
0
interleave.test(function (async) {

  var n = 2
  var err = new Error('closes both streams')

  var err1, err2
  pull(
    cat([pull.values([1,2,3]), error(err)]),
    async.through(),
    tomany([
      pull.collect(function (err) {
        err1 = err
        done()
      }),
      pull.collect(function (err) {
        err2 = err
        done()
      })
    ])
  )

  function done(err, ary) {
    if(--n) return
    if(!err1 || !err2)
      throw new Error('test failed')

    async.done()
  }

})
Example #12
0
interleave.test(function (async) {
  var n = 2, results = []

  var f = fork(
    function (data, sinks) {
      return data % 2
    },
    function (i, sinks) {
      return pull(
              async.through(),
              pull.collect(function (err, value) {
                if(err) throw err
                results[i] = value
                done()
              }))
    })

  pull(pull.values([1,2,3,4,5,6,7,8]), async.through(), f)

  var values

  function done () {
    if(--n) return
    assert.deepEqual(results, [
      [2, 4, 6, 8],
      [1, 3, 5, 7]
    ])
    async.done()
  }

})
Example #13
0
test('tee-async', function (t) {
  var a, b

  pull(
    pull.values([1, 2, 3, 4, 5]),
    tee(pull(
        randAsync(),
        pull.collect(function (err, _a) {
          a = _a
          if(b && a) next()
        })
      )
    ),
    randAsync(),
    pull.collect(function (err, _b) {
      b = _b
      if(b && a) next()
    })
  )

  function next () {
    t.deepEqual(a, b)
    t.end()
  }
})
Example #14
0
module.exports = function addDefaultAssets (self, log, callback) {
  const initDocsPath = path.join(__dirname, '../../init-files/init-docs')
  const index = initDocsPath.lastIndexOf(path.sep)

  pull(
    pull.values([initDocsPath]),
    pull.asyncMap((val, cb) =>
      glob(path.join(val, '/**/*'), { nodir: true }, cb)
    ),
    pull.flatten(),
    pull.map(element => {
      const addPath = element.substring(index + 1)
      return { path: addPath, content: file(element) }
    }),
    self.addPullStream(),
    pull.through(file => {
      if (file.path === 'init-docs') {
        const cid = new CID(file.hash)
        log('to get started, enter:\n')
        log(`\tjsipfs cat /ipfs/${cid.toBaseEncodedString()}/readme\n`)
      }
    }),
    pull.collect((err) => {
      if (err) {
        return callback(err)
      }

      callback(null, true)
    })
  )
}
Example #15
0
test('sink', async t => {
    try {
        const { registry, entitySet } = await initialise({ loadEntities: true });
        const receivingES = registry.createEntitySet();

        // receivingES.on('all', (name,...args) => Log.debug('[receivingES]', name))

        Pull(
            Pull.values([
                { '@e': 2, '@i': 5, '@s': 1, addr: '192.3.0.1' },
                { '@e': 2, '@i': 6, '@s': 2, expires_at: -300 },
                { '@e': 7, '@i': 10, '@s': 1, addr: '192.3.0.2' },
                { '@e': 11, '@i': 14, '@s': 1, addr: '192.3.0.3' },
                { '@e': 15, '@i': 18, '@s': 1, addr: '192.3.0.4' },
                { '@cmd': 'rme', eid: 11 }
            ]),
            PullMap(v => [v, {}]), // because normally the stream provides a tuple of [value,options]
            receivingES.sink({}, err => {
                // Log.debug('[sink]', entityToString(receivingES));
                t.equals(receivingES.size(), 3);
                t.end();
            })
        );
    } catch (err) {
        Log.error(err.stack);
    }
});
      pull.collect((err, encoded) => {
        if (err) throw err

        expect(
          encoded
        ).to.be.eql(
          input.map(data => {
            const len = varint.encode(data.length)
            return Buffer.concat([
              Buffer.alloc(len.length, len, 'utf8'),
              Buffer.alloc(data.length, data, 'utf8')
            ])
          }))

        pull(
          pull.values(encoded),
          lp.decode(),
          pull.collect((err, output) => {
            if (err) throw err
            expect(
              input
            ).to.be.eql(
              output
            )
            done()
          })
        )
      })
tape('buffer in string out', function(t) {
  t.plan(3)
  pull(
    pull.values([new Buffer('buffer goes in and string comes out')]),
    encrypt({password : pass}),
    pull.collect(function(err, r) {
      if (err) {
        throw new Error(err)
      }
      var encrypted = Buffer.concat(r, totalLength(r))
      t.equal(Buffer.isBuffer(encrypted), true, "Should receive buffer after encryption")
      pull(
        pull.values([encrypted]),
        decrypt({
          password : pass,
          encoding : 'ascii'
        }),
        pull.collect(function(err, d) {
          if (err) {
            throw err
          }
          t.equal((typeof d[0] === 'string'), true, "Output of decrypt should be a string")
          var decrypted = d.join('')
          t.equal('buffer goes in and string comes out', decrypted, "original string put into buffer should match the decrypted text")
        })
      )
    })
  )
})
Example #18
0
require('tape')('abort', function (t) {

  t.plan(2)

  var ts = through()
  ts.on('close', function () {
    t.ok(true)
  })
  pull.values([.1, .4, .6, 0.7, .94, 0.3])
//  pull.infinite()
  .pipe(toPull(ts))
  .pipe(function (read) {
    console.log('reader!')
    read(null, function next (end, data) {
      console.log('>>>', end, data) 
      if(data > 0.9) {
        console.log('ABORT')
        read(true, function (end) {
          t.ok(true)
          t.end()
        })
      } else {
        read(null, next)
      }
    })

  })

})
 fs.readdir(directory, function (error, read) {
   deferred.resolve(
     error
       ? pull.error(error)
       : pull(pull.values(read), map(decode))
   )
 })
Example #20
0
  fs.readdir(targetPath, function(err, files) {
    if (err) return callback(err);

    pull(
      pull.values(files),

      // join the path names and stat the files
      pull.map(path.join.bind(null, targetPath)),

      // stat the files in parallel but return in order
      pull.paraMap(function(filename, callback) {
        fs.stat(filename, function(err, stats) {
          // create a compound object so we don't lose the filename
          // in the map transformation
          callback(err, { stats: stats, filename : filename });
        });
      }),

      // only keep directories
      pull.filter(function(data) {
        return data.stats.isDirectory();
      }),

      // transform back to a simple list of filenames
      pull.map(function(data) {
        return path.basename(data.filename);
      }),

      pull.collect(callback)
    );
  });
Example #21
0
  }).listen(5678, function () {

    pull(
      pull.values([1,2,3]),
      //need a delay, because otherwise ws hangs up wrong.
      //otherwise use pull-goodbye.
      function (read) {
        return function (err, cb) {
          setTimeout(function () {
            read(null, cb)
          }, 10)
        }
      },
      JSONDL.stringify(),
      WS.connect('ws://localhost:5678'),
      JSONDL.parse(),
      pull.collect(function (err, ary) {
        if(err) throw err
        t.deepEqual(ary, [1,2,3])
        server.close(function () {
          t.end()
        })
      })
    )
  })
Example #22
0
tape('simple', function (t) {

  t.plan(5)

  var expected = [
    [null, 1],
    [1, 2],
    [2, 3],
    [3, null]
  ]

  pull(
    pull.values([1, 2, 3]),
    pairs(function (a, b) {
      t.deepEqual([a, b], expected.shift())
      return b
    }),
    pull.collect(function (err, arr) {
      if(err) throw err
      t.deepEqual(arr, [1, 2, 3, null], 'values')
    })

  )

})
Example #23
0
tape('close after uniplex streams end 2', function (t) {
  t.plan(4)

  var A = mux(client, null, codec) ()
  var B = mux(null, client, codec) ({
    things: function () {
      t.ok(true)
      return pull.collect(function (err, ary) {
        t.deepEqual(ary, [1, 2, 3, 4, 5])
      })
    }
  })

  pull(pull.values([1, 2, 3, 4, 5]), A.things())

  var bs = B.createStream()
  var as = A.createStream()

  pull(as, bs, as)

  B.close(function (err) {
    console.log('B CLOSE')
    t.notOk(err, 'bs is closed')
  })

  A.close(function (err) {
    console.log('A CLOSE')
    t.notOk(err, 'as is closed')
  })
})
Example #24
0
tape('async', function (t) {

  t.plan(5)

  var expected = [
    [null, 1],
    [1, 2],
    [2, 3],
    [3, null]
  ]

  pull(
    pull.values([1, 2, 3]),
    pairs.async(function (a, b, callback) {
      t.deepEqual([a, b], expected.shift())
      setTimeout(function () {
        // call back with the normal output.
        return callback(null, b)
      }, 500)
    }),
    pull.collect(function (err, arr) {
      if(err) throw err
      t.deepEqual(arr, [1, 2, 3, null], 'values')
    })

  )

})
Example #25
0
tape('simple debtor', function (t) {

  var dr = debtor(128)

  var buffer = crypto.randomBytes(1024)

  var i = setInterval(function () {
    dr.credit(128)
  }, 10)

  pull(
    pull.values([buffer]),
    randomsp(64, 256),
    dr,
    pull.through(function (data) {
      if(data.length > 128) {
        clearInterval(i)
        throw new Error('too much data')
      }
    }),
    pull.collect(function (err, ary) {
      console.log(ary)
      clearInterval(i)
      t.deepEqual(Buffer.concat(ary), buffer)
      t.end()
    })
  )

})
      pull.collect((err, encoded) => {
        if (err) throw err

        expect(
          encoded
        ).to.be.eql([
          Buffer.concat([
            new Buffer(varint.encode('hello '.length)),
            new Buffer('hello ')
          ]),
          Buffer.concat([
            new Buffer(varint.encode('world'.length)),
            new Buffer('world')
          ])
        ])

        pull(
          pull.values(encoded),
          lp.decode(),
          pull.collect((err, output) => {
            if (err) throw err
            expect(
              input
            ).to.be.eql(
              output
            )
            done()
          })
        )
      })
Example #27
0
tape('detect flipped bits', function (t) {

  var input = randomBuffers(1024, 100)
  var key = testKey('bit flipper')

  pull(
    pull.values(input),
    boxes.createEncryptStream(key),
    pull.map(function (data) {

      if(Math.random() < 0.1) {
        var rbit = 1<<(8*Math.random())
        var i = ~~(Math.random()*data.length)
        data[i] = data[i]^rbit
      }

      return data

    }),
    boxes.createDecryptStream(key),
    pull.collect(function (err, output) {
      t.ok(err)
      t.notEqual(output.length, input.length)
      t.end()
    })
  )

})
Example #28
0
interleaving.test(function (async) {

  var done = async(function done (err, ary) {
    var a = ary[0]
    var b = ary[1]

    assert.deepEqual(a, [1, 3, 5, 7])
    assert.deepEqual(b, [2, 4, 6, 8])

    async.done()
  })

  var s = N(2, done)

  pull(
    pull.values([1, 2, 3, 4, 5, 6, 7, 8]),
    async.through('C'),
    fork([
      pull(async.through('A'), pull.collect(s())),
      pull(async.through('B'), pull.collect(s()))
    ],
      function (e) { return (e + 1) % 2 }
    )
  )


})
Example #29
0
tape('encrypt/decrypt', function (t) {

  var input = randomBuffers(1024*512, 2*10)
  var start = Date.now()
  console.log('e/d')
  var key = testKey('encrypt/decrypt a stream')

  pull(
    pull.values(input),
    split(),
    boxes.createEncryptStream(key),
    split(),
    boxes.createDecryptStream(key),

    pull.collect(function (err, output) {
      var time = Date.now() - start
      console.log(100/(time/1000), 'mb/s')

      if(err) throw err

      
      output = concat(output)
      input = concat(input)
      t.equal(output.length, input.length)
      t.deepEqual(output, input)
      t.end()
    })
  )
})
Example #30
0
File: add.js Project: ipfs/js-ipfs
  const add = promisify((data, options, callback) => {
    if (typeof options === 'function') {
      callback = options
      options = {}
    }

    options = options || {}

    try {
      validateAddInput(data)
    } catch (err) {
      return callback(err)
    }

    pull(
      pull.values([data]),
      self.addPullStream(options),
      sort((a, b) => {
        if (a.path < b.path) return 1
        if (a.path > b.path) return -1
        return 0
      }),
      pull.collect(callback)
    )
  })