Last active
August 9, 2019 09:31
-
-
Save Frando/c8dfa113db5adc7f7eb6f9bf6d671a84 to your computer and use it in GitHub Desktop.
hyperswarm local replication 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
{ | |
"name": "hyperswarm-replication-test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"keywords": [], | |
"author": "", | |
"license": "MIT", | |
"dependencies": { | |
"@hyperswarm/replicator": "^1.1.0", | |
"hyperdrive": "^9.16.0", | |
"tape": "^4.11.0", | |
"random-access-memory": "^3.1.1" | |
} | |
} |
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 hyperdrive = require('hyperdrive') | |
const replicator = require('@hyperswarm/replicator') | |
const tape = require('tape') | |
const ram = require('random-access-memory') | |
const util = require('util') | |
const opts = { timeout: 10000 } | |
tape('replication via direct stream', opts, t => { | |
makeDrivesAndReplicate(replicateDirect, (err, cleanup) => { | |
t.error(err, 'no error') | |
console.log('synchronization successful!') | |
cleanup(t.end) | |
}) | |
}) | |
tape('replication via hyperswarm', opts, t => { | |
makeDrivesAndReplicate(replicateHyperswarm, (err, cleanup) => { | |
t.error(err) | |
console.log('synchronization successful!') | |
cleanup(t.end) | |
}) | |
}) | |
function replicateHyperswarm (drive1, drive2) { | |
const swarm1 = replicator(drive1, { live: true, announce: true, lookup: true }) | |
const swarm2 = replicator(drive2, { live: true, announce: true, lookup: true }) | |
logEvents(swarm1, 'swarm1') | |
logEvents(swarm2, 'swarm2') | |
return (cb) => { | |
swarm1.destroy(() => swarm2.destroy(cb)) | |
} | |
} | |
function replicateDirect (drive1, drive2) { | |
const stream = drive1.replicate({ live: true }) | |
stream.pipe(drive2.replicate({ live: true })).pipe(stream) | |
return cb => cb() | |
} | |
function makeDrivesAndReplicate (replicationFunction, cb) { | |
const drive1 = hyperdrive(ram) | |
const path = 'hello' | |
const value = 'world' | |
drive1.ready(() => { | |
const drive2 = hyperdrive(ram, drive1.key) | |
drive1.writeFile(path, Buffer.from(value), err => { | |
if (err) return cb(err) | |
console.log('write on drive1: ' + value) | |
const cleanup = replicationFunction(drive1, drive2) | |
drive2.readFile(path, (err, data) => { | |
if (err) return cb(err) | |
console.log('read on drive2: ' + data.toString()) | |
cb(null, cleanup) | |
}) | |
}) | |
}) | |
} | |
function logEvents (emitter, name) { | |
let emit = emitter.emit | |
emitter.emit = (...args) => { | |
console.log(`(${name}) \x1b[33m${args[0]}\x1b[0m`, util.inspect(args.slice(1), { depth: 0, colors: true })) | |
emit.apply(emitter, args) | |
} | |
} |
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
(same results with node v10, v11, v12) | |
(also tested different combinations of lookup/announce) | |
> node test.js | |
TAP version 13 | |
# replication via direct stream | |
write on drive1: world | |
read on drive2: world | |
ok 1 no error | |
synchronization successful! | |
# replication via hyperswarm | |
write on drive1: world | |
(swarm1) listening [] | |
(swarm1) join [ <Buffer fe 39 33 9b ea fb a6 08 7e 84 5a c9 b8 fe 4e da dc f2 ce 30 7b 02 1f 1c 8e 26 ea c7 c1 e3 d2 2e>, | |
[Object] ] | |
(swarm2) listening [] | |
(swarm2) join [ <Buffer fe 39 33 9b ea fb a6 08 7e 84 5a c9 b8 fe 4e da dc f2 ce 30 7b 02 1f 1c 8e 26 ea c7 c1 e3 d2 2e>, | |
[Object] ] | |
(swarm2) peer [ [Object] ] | |
(swarm1) updated [ [Object] ] | |
(swarm2) updated [ [Object] ] | |
not ok 2 test timed out after 10000ms |
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
(same results with node v10, v11, v12) | |
(also tested different combinations of lookup/announce) | |
> node test.js | |
TAP version 13 | |
# replication via direct stream | |
write on drive1: world | |
read on drive2: world | |
ok 1 no error | |
synchronization successful! | |
# replication via hyperswarm | |
write on drive1: world | |
(swarm1) listening [] | |
(swarm1) join [ | |
<Buffer 2b ae 92 03 82 c2 51 35 3a 49 8b 5b f2 ae ee 95 a2 5c 60 97 b3 ed 58 6c d8 f3 0b aa 37 b4 8a 36>, | |
[Object] | |
] | |
(swarm2) listening [] | |
(swarm2) join [ | |
<Buffer 2b ae 92 03 82 c2 51 35 3a 49 8b 5b f2 ae ee 95 a2 5c 60 97 b3 ed 58 6c d8 f3 0b aa 37 b4 8a 36>, | |
[Object] | |
] | |
(swarm1) peer [ [Object] ] | |
(swarm2) peer [ [Object] ] | |
(swarm2) peer [ [Object] ] | |
(swarm1) peer [ [Object] ] | |
(swarm1) peer [ [Object] ] | |
(swarm2) peer [ [Object] ] | |
(swarm2) connection [ [Socket], [PeerInfo] ] | |
(swarm1) connection [ [Socket], [PeerInfo] ] | |
(swarm1) connection [ [Socket], [PeerInfo] ] | |
(swarm2) connection [ [Socket], [PeerInfo] ] | |
read on drive2: world | |
ok 2 null | |
synchronization successful! | |
(swarm2) disconnection [ [Socket], [PeerInfo] ] | |
(swarm2) disconnection [ [Socket], [PeerInfo] ] | |
(swarm1) disconnection [ [Socket], [PeerInfo] ] | |
(swarm1) disconnection [ [Socket], [PeerInfo] ] | |
(swarm1) close [] | |
(swarm2) close [] | |
1..2 | |
# tests 2 | |
# pass 2 | |
# ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment