Skip to content

Instantly share code, notes, and snippets.

@gmaclennan
Last active July 3, 2017 19:17
Show Gist options
  • Save gmaclennan/d16583f39cc3926edcb5f77064f0ad93 to your computer and use it in GitHub Desktop.
Save gmaclennan/d16583f39cc3926edcb5f77064f0ad93 to your computer and use it in GitHub Desktop.
Hyperlog http replicate test
const request = require('request')
const duplexify = require('duplexify')
const hyperlog = require('hyperlog')
const memdb = require('memdb')
const log = hyperlog(memdb())
const url = 'http://localhost:4001/'
log.add(null, 'hello', function (err, node) {
if (err) throw err
log.add(node, 'world', function (err, node) {
if (err) throw err
replicate()
})
})
function replicate () {
var a = log.replicate()
var req = request.post(url, {forever: true})
var dup = duplexify(req)
req.on('response', function (res) {
dup.setReadable(res)
})
a.pipe(dup).pipe(a)
}
const http = require('http')
const duplexify = require('duplexify')
const hyperlog = require('hyperlog')
const memdb = require('memdb')
const log = hyperlog(memdb())
log.on('add', function (node) {
console.log('add', node.key)
})
const server = http.createServer(function (req, res) {
if (req.method !== 'POST') return res.status(404).end()
const a = duplexify(res, req)
const b = log.replicate()
b.pipe(a).pipe(b)
})
server.listen(4001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment