Пример #1
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 }
    )
  )


})
Пример #2
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()
  }
})
Пример #3
0
  util.read('html/feeds.html').on('error', util.serve404(res)).pipe(concat(function(html) {
    var n = 0
    var fetchProfile = util.profileFetcher(backend)

    pull(
      toPull(backend.createFeedStream({reverse: true})),
      pull.asyncMap(fetchProfile),
      pull.collect(function (err, entries) {
        if (err) { return console.error(err), res.writeHead(500), res.end() }
        ctx.feed_entries = entries.map(util.renderMsg).join('')
        if (++n == 2) finish()
      })
    )

    pull(
      toPull(backend.following()),
      pull.asyncMap(fetchProfile),
      pull.collect(function (err, entries) {
        if (err) { return console.error(err), res.writeHead(500), res.end() }
        ctx.friends = entries.map(function(entry) { return '<a href="/profile/'+entry.key.toString('hex')+'">'+entry.nickname+'</a><br>'; }).join('')
        if (++n == 2) finish()
      })
    )

    function finish() {
      res.writeHead(200, {'Content-Type': 'text/html'})
      res.end(util.renderCtx(html.toString(), ctx))
    }
  }))
Пример #4
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()
  }

})
Пример #5
0
test('source continues to emit events', async t => {
    try {
        const { registry, entitySet } = await initialise({ loadEntities: true });
        const es2 = registry.createEntitySet();

        t.plan(2);

        Pull(
            es2.source(),
            PullMap(val => (Array.isArray(val) ? val[0] : val)),
            Pull.take(2),
            Pull.collect((err, data) => {
                t.equals(data.length, 2);
            })
        );

        Pull(
            entitySet.source(),
            PullMap(val => (Array.isArray(val) ? val[0] : val)),
            Pull.take(9),
            Pull.collect((err, data) => {
                t.equals(data.length, 9);
                t.end();
            })
        );

        es2.addEntity([{ '@c': '/name', name: 'susan' }, { '@c': '/ttl', expires_at: 2019 }]);

        // add a new entity with two components
        entitySet.addEntity([{ '@c': '/name', name: 'mike' }, { '@c': '/ttl', expires_at: 2018 }]);
    } catch (err) {
        Log.error(err.stack);
    }
});
 function done () {
   if(--n) return
   var A, B
   pull(pl.read(db1), pull.collect(function (err, ary) { A = ary; next() }))
   pull(pl.read(db2), pull.collect(function (err, ary) { B = ary; next() }))
   function next () {
     if(!(A&&B)) return
     cb(null, A, B)
   }
 }
Пример #7
0
  function compareDbs (a, b, cb) {

    var cbs = u.groups(next)

    pull(a.createFeedStream(), pull.collect(cbs()))
    pull(b.createFeedStream(), pull.collect(cbs()))

    function next(err, ary) {
      cb(err, ary && ary[0], ary && ary[1])
    }
  }
    function done2 (err) {
      if(err) throw err
      //now check that the databases have really been updated.

      var cbs = u.groups(next)

      pull(ssb1.createFeedStream(), pull.collect(cbs()))
      pull(ssb2.createFeedStream(), pull.collect(cbs()))

      function next (err, ary) {
        cb(err, ary)
      }
    }
        function done2 (err) {
          if(err) throw err
          //now check that the databases have really been updated.

          var cbs = u.groups(done3)

          pull(ssb1.createFeedStream(), pull.collect(cbs()))
          pull(ssb2.createFeedStream(), pull.collect(cbs()))

          function done3 (err, ary) {
            if(err) throw err
            t.deepEqual(ary[0].map(hash), ary[1].map(hash))
            t.end()
          }
        }
Пример #10
0
 messenger(function(err, source, sink) {
   t.ifError(err);
   pull(source, pull.collect(function(readErr, values) {
     t.ifError(readErr);
     t.deepEqual(values, ['a', 'b', 'c', 'd']);
   }));
 });
Пример #11
0
  var server = net.createServer(function (stream) {
 
    stream.pipe(
    duplex(
      pull(
        pull.map(function (e) {
          return '' + e
        }), pull.collect(function (err, ary) {
          console.log(ary)
          t.end()
        })
      ),
      pull(
        pull.infinite(),
        pull.asyncMap(function (e, cb) {
          process.nextTick(function () {
            cb(null, e.toString() + '\n')
          })
        }),
        pull.take(10)
      )
    )).on('end', function () {
      console.log('PULL -- END')
    })
    .pipe(stream).on('end', function () {
      server.close()
    })

  }).listen(0, function () {
Пример #12
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()
        })
      })
    )
  })
Пример #13
0
    function fetchBottomBy (amt) {
      var fetchopts = { reverse: true, type: 'phoenix-program', limit: 30, keys: true, values: true }
      fetchopts.lt = cursor && cursor.value.timestamp
      
      pull(app.ssb.messagesByType(fetchopts), pull.collect(function (err, _msgs) {
        if (_msgs && _msgs.length) {
          // nothing new? stop
          if (cursor && cursor.key === _msgs[_msgs.length - 1].key)
            return (cb && cb())

          // advance cursor
          cursor = _msgs[_msgs.length - 1]

          // filter
          if (opts.filter)
            _msgs = _msgs.filter(opts.filter)

          // render
          _msgs.forEach(function (msg) {
            var el = com.programSummary(app, msg)
            el && feed.appendChild(el)
          })

          // fetch more if needed
          var remaining = amt - _msgs.length
          if (remaining > 0)
            return fetchBottomBy(remaining)
        }

        cb && cb()
      }))
    }
Пример #14
0
test('read this file', function (t) {

  var fs = require('fs')
  var file = fs.readFileSync(__filename).toString()
  var lines = file.split('\n')
  var i = 0, block = 300

  pull(
    function (end, cb) {
      if (i > file.length)
        cb(true)
      else {
        var _i = i
        i += block
        cb(null, file.substring(_i, _i + block))
      }
    },
    split(),
    pull.collect(function (err, array){
      t.equal(array.length, lines.length)
      t.deepEqual(array, lines)
      t.end()
    })
  )
})
Пример #15
0
tape('simple', function (t) {

  pull(
    pull.count(100),
    pull.map(random),
    pull.collect(function (err, ary) {
      var buffer = Buffer.concat(ary.map(function (v) {
          return struct.encode(v)
        }))

      var o = ~~(Math.random()*buffer.length)

      pull(
        pull.values([buffer.slice(0, o), buffer.slice(o)]),
        decode(struct),
        pull.through(console.log),
        pull.collect(function (err, _ary) {
          console.log(_ary)
          t.deepEqual(_ary, ary)
          t.end()
        })
      )
    })
  )

})
Пример #16
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()
        })
      )
    })
  )
})
Пример #17
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(
          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()
          })
        )
      })
Пример #19
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')
    })

  )

})
Пример #20
0
 function doIt(t) {
   pull(reverseCache(), pull.collect(function (err, values) {
     t.error(err, 'collect')
     t.deepEquals(values.reverse(), vals, 'values reverse')
     t.end()
   }))
 }
Пример #21
0
    pl.write(db, function () {
      console.log('ready')
      var min, max
      var query = ['date', {
          min: min = new Date(+start + gap*total*0.25),
          max: max = new Date(+start + gap*total*0.3)
        }]

      console.log(index.explain(query))

      pull(

        ( true
        ? index.search(query)
        : pl.read(index, index.explain(query))),
        pull.through(function (e) {
          console.log(decode(e) || e)
        }),
        pull.collect(function (err, ary) {
          if(err) throw err
          ary.forEach(function (e) {
            var date = new Date(e.value.date)
            t.ok(date >= min, date + ' >= ' + min)
            t.ok(date >= min, date + ' <  ' + min)
          })
          t.end()
        })
      )
      //t.end()
    })
Пример #22
0
      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()
          })
        )
      })
Пример #23
0
exports.suggest = cont.to(function (word, cb) {
  if(!/^[@%&!]/.test(word[0])) return cb()
  if(word.length < 2) return cb()

  var sigil = word[0]
  var embed = ((sigil === '!') ? '!' : '')
  if(embed) sigil = '&'
  if(word[0] !== '@') word = word.substring(1)

  pull(
    sbot_links2({query: [
      {$filter: {rel: ['mentions', {$prefix: word}], dest: {$prefix: sigil}}},
      {$reduce: {id: 'dest', name: ['rel', 1], rank: {$count: true}}}
    ]}),
    pull.collect(function (err, ary) {

      ary = ary
      .filter(function (e) {
        if(!embed) return true
        return isImage(e.name)
      }).sort(function (a, b) {
        return b.rank - a.rank
      }).map(function (e) {
        return {
          title: e.name + ': ' + e.id.substring(0,10)+' ('+e.rank+')',
          value: embed+'['+e.name+']('+e.id+')',
          rank: e.rank,
          image: isImage(e.name) ? 'http://localhost:7777/'+e.id : undefined
        }
      })
      cb(null, ary)
    })
  )
})
Пример #24
0
  tape(name + ' abort', function (t) {

    var aborted = []
    pull(
      many(all.map(function (ary, i) {
        return pull(
          pull.values(ary),
          function (read) {
            return function (abort, cb) {
              aborted[i] = true
              read(abort, function (end, data) {
                if(end) aborted[i] = true
                cb(end, data)
              })
            }
          })
      }).map(maybeDelay)),
      pull.take(10),
      pull.collect(function (err, ary) {
        t.deepEqual(aborted, all.map(function () { return true }))
        partial(t, ary)
        t.end()
      })
    )
  })
Пример #25
0
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")
        })
      )
    })
  )
})
Пример #26
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')
    })

  )

})
Пример #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()
    })
  )

})
Пример #28
0
function buffer(stream, cb) {
  pull(toPull(stream), pull.collect(function (err, ary) {
    if(err) return cb(err)
    var b = Buffer.concat(ary)
    cb(null, b)
  }))
}
Пример #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()
    })
  )
})
Пример #30
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()
    })
  )
})