Skip to content

Instantly share code, notes, and snippets.

@poga
Last active April 8, 2017 23:02
Show Gist options
  • Save poga/8a25554ac62236bc4a75679d4b1d7c26 to your computer and use it in GitHub Desktop.
Save poga/8a25554ac62236bc4a75679d4b1d7c26 to your computer and use it in GitHub Desktop.
const hyperdrive = require('hyperdrive')
const swarm = require('hyperdiscovery')
const ram = require('random-access-memory')
var a1 = hyperdrive(ram)
var a2
a1.ready(function () {
swarm(a1)
a1.writeFile('/foo', 'bar', done)
a2 = hyperdrive(ram, a1.key)
a2.ready(function () {
swarm(a2)
a2.metadata.on('download', (idx, data) => console.log('download', idx, data))
})
})
function done () {
a2.readFile('/foo', function (err, data) {
if (err) console.log(err)
console.log('file', data.toString()) // === 'bar'
// after download completed, readdir returns the correct result
a2.readdir('/', function (err, data) {
if (err) console.log(err)
console.log('dir', data) // === ['foo]
})
})
a2.readdir('/', function (err, data) {
// somehow this will not wait metadata to download before firing callback
if (err) console.log(err)
console.log('dir', data) // === []
})
}
{
"name": "hd8",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"hyperdiscovery": "^1.3.0",
"hyperdrive": "^8.1.0",
"random-access-memory": "^2.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment