Last active
July 3, 2017 19:17
-
-
Save gmaclennan/d16583f39cc3926edcb5f77064f0ad93 to your computer and use it in GitHub Desktop.
Hyperlog http replicate test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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