Instantly share code, notes, and snippets.

Embed
What would you like to do?
fetch a value from a hypercore
Fetching 78b728..a0 dk: 79fe13..2c
Length 0
Wed, 04 Jul 2018 22:11:10 GMT discovery-channel whoami() started
Wed, 04 Jul 2018 22:11:10 GMT discovery-channel chan=79fe13..2c join()
Wed, 04 Jul 2018 22:11:10 GMT discovery-channel chan=79fe13..2c dns announce { port: 56304, publicPort: 0, multicast: true }
Wed, 04 Jul 2018 22:11:10 GMT discovery-channel whoami() failed
Wed, 04 Jul 2018 22:11:10 GMT discovery-channel chan=79fe13..2c dns discovery peer=10.0.1.11:3282
Wed, 04 Jul 2018 22:11:10 GMT discovery-swarm connecting 10.0.1.11:3282@79fe137598ce1967eabacac91aa3bbfd1b85ae28689c779c5c844c434aa06c2c retries=0
Wed, 04 Jul 2018 22:11:10 GMT discovery-swarm onconnect 10.0.1.11:3282@79fe137598ce1967eabacac91aa3bbfd1b85ae28689c779c5c844c434aa06c2c type=tcp
Swarm connect
Jim protocol out 0-feed id: b15e11..49 dk: 79fe13..2c nonce: 9d8089..6a
Jim protocol out 1-handshake:
{ id:
<Buffer b1 5e 11 92 71 08 8c c4 67 e1 68 5b 53 a6 98 94 ca 7a 0f 7e fb d7 3a 3f f7 27 65 eb d3 60 08 49>,
live: false,
userData: null,
extensions: [],
ack: false }
Jim protocol out 5-want:
{ start: 0 }
To swarm <Buffer 3d 00 0a 20 79 fe 13 75 98 ce 19 67 ea ba ca c9 1a a3 bb fd 1b 85 ae 28 68 9c 77 9c 5c 84 4c 43 4a a0 6c 2c 12 18 9d 80 89 e4 6d e8 39 c4 f5 48 d0 e7 ... >
To swarm <Buffer 60 6d 65 55 3b 6c 37 b0 e3 fb 4a 6e 43 fa 12 2f c9 5d 01 15 fb 02 38 c5 b6 27 9f 7e 7b c4 26 aa 8b c3 0d 29 b1 7c d5 1d>
To swarm <Buffer 0f 36 81 e3>
From swarm <Buffer 3d 00 0a 20 79 fe 13 75 98 ce 19 67 ea ba ca c9 1a a3 bb fd 1b 85 ae 28 68 9c 77 9c 5c 84 4c 43 4a a0 6c 2c 12 18 79 97 44 aa 96 b1 2c c4 66 8a ad 06 ... >
Jim protocol in 0-feed id: 0 dk: 79fe13..2c nonce: 799744..24
Jim protocol in 1-handshake:
{ id:
<Buffer 58 e6 94 32 2d 98 54 30 68 81 62 d8 19 30 4f c9 c3 c0 2d 95 49 0a 47 bb 76 6c a0 b7 0f 65 5e 1f>,
live: false,
userData: null,
extensions: [],
ack: false }
new connection 10.0.1.11 3282 outgoing
Jim protocol in 5-want:
{ start: 0, length: 0 }
Jim protocol out 3-have:
{ start: 0, bitfield: <Buffer > }
To swarm <Buffer 45 2f f4 e6 8c 2d>
From swarm <Buffer 32 23 47 bc 91 8b cf b1>
Jim protocol in 3-have:
{ start: 0, length: 1, bitfield: <Buffer 02 e0> }
Jim protocol out 7-request:
{ tick: 6, bytes: 0, index: 0, hash: false, nodes: 0 }
To swarm <Buffer 1e 6d 6f d6 91 ca ea 05 ab 25>
From swarm <Buffer a4 b1 54 1e 08 b4 60 46 d6 55 e2 7a 11 09 6d 83 a8 f6 06 64 d9 15 15 20 ae 88 58 9f 6b fa c2 f0 42 99 4e c1 c2 34 cd 04 95 85 a8 53 92 e7 cc 62 57 a6 ... >
Jim protocol in 9-data:
{ index: 0,
value: <Buffer 61>,
nodes:
[ { index: 2,
hash:
<Buffer 94 c1 70 54 00 59 42 a0 02 c7 c3 9f bb 9c 61 83 51 86 91 fb 40 14 36 f1 a2 f3 29 b3 80 23 0a f8>,
size: 1 },
{ index: 4,
hash:
<Buffer 1d 2f ad c9 ce 60 4c 7e 59 29 49 ed c9 64 e4 5a aa 10 99 0d 7e e5 33 28 43 9e f9 b2 cf 8a a6 ff>,
size: 1 } ],
signature:
<Buffer 1e 60 10 a4 cf 3d 37 77 dd e4 96 1d 4f 47 76 41 17 1a a7 51 4a 41 08 21 81 43 a7 31 46 c7 cb f0 b2 b7 8c 45 9b 40 22 54 61 27 e8 80 01 61 fe e1 ef 63 ... > }
Get 0 null a
peer disconnected
Swarm pump done premature close
const fs = require('fs')
const crypto = require('hypercore-crypto')
const swarmDefaults = require('dat-swarm-defaults')
const discoverySwarm = require('discovery-swarm')
const hypercore = require('hypercore')
const pump = require('pump')
const through2 = require('through2')
const ram = require('random-access-memory')
const prettyHash = require('pretty-hash')
const key = fs.readFileSync('./feed/key')
const discoveryKey = crypto.discoveryKey(key)
console.log(`Fetching ${prettyHash(key)} dk: ${prettyHash(discoveryKey)}`)
const feed = hypercore(ram, key, {sparse: true})
feed.ready(() => {
const sw = discoverySwarm(swarmDefaults({
utp: false,
dht: false,
live: false,
hash: false,
dns: {
server: null, domain: 'dat.local'
},
stream: info => feed.replicate(),
connect: (feedStream, swarmStream) => {
console.log('Swarm connect')
pump(
swarmStream,
through2(function (chunk, enc, cb) {
console.log('From swarm', chunk)
this.push(chunk)
cb()
}),
feedStream,
through2(function (chunk, enc, cb) {
console.log('To swarm', chunk)
this.push(chunk)
cb()
}),
swarmStream,
err => {
console.log('Swarm pump done', err && err.message)
}
)
}
}))
sw.join(discoveryKey)
sw.on('connection', function (peer, info) {
console.log('new connection', info.host, info.port,
info.initiator ? 'outgoing' : 'incoming')
peer.on('close', function () {
console.log('peer disconnected')
})
})
console.log('Length', feed.length)
feed.get(0, (err, data) => {
console.log('Get 0', err, data.toString())
sw.close()
})
})
#! /bin/bash
DEBUG_COLORS=0 DEBUG=discovery-channel,discovery-swarm node read-feed.js 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment