Skip to content

Instantly share code, notes, and snippets.

@gmaclennan
Last active July 3, 2017 19:17
Show Gist options
  • Select an option

  • Save gmaclennan/d16583f39cc3926edcb5f77064f0ad93 to your computer and use it in GitHub Desktop.

Select an option

Save gmaclennan/d16583f39cc3926edcb5f77064f0ad93 to your computer and use it in GitHub Desktop.

Revisions

  1. gmaclennan revised this gist Jul 3, 2017. No changes.
  2. gmaclennan created this gist Jul 3, 2017.
    25 changes: 25 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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)
    }
    19 changes: 19 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)