Skip to content

Instantly share code, notes, and snippets.

@Nuhvi
Last active March 25, 2022 13:06
Show Gist options
  • Save Nuhvi/721368b93729dcfa00049c9de230f2ad to your computer and use it in GitHub Desktop.
Save Nuhvi/721368b93729dcfa00049c9de230f2ad to your computer and use it in GitHub Desktop.
// npm i hypercore@next hyperswarm@next random-access-memory
// Run `node resolve-hypercore.js <key you get from https://gist.github.com/Nazeh/60291b111f93928d8110ff0ae9ecea25>
const Hypercore = require('hypercore');
const Hyperswarm = require('hyperswarm');
const ram = require('random-access-memory');
const resolveCore = async (key) => {
const core = new Hypercore(ram, key, { valueEncoding: 'json' });
await core.ready();
const swarm = new Hyperswarm();
swarm.on('connection', (conn) => {
core.replicate(conn);
});
swarm.join(core.discoveryKey, { client: true, server: false });
console.time('first request resolved in');
await swarm.flush();
await core.update();
// Log the latest block.
const latest = await core.get(core.length - 1);
console.timeEnd('first request resolved in');
console.log(' Latest Block:', core.length, latest)
console.time('second request resolved in');
const first = await core.get(0)
console.timeEnd('second request resolved in');
console.log(' First Block :', 0, first);
swarm.destroy();
};
const main = async () => {
try {
const key = process.argv[2];
await resolveCore(Buffer.from(key, 'hex'));
} catch (error) {}
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment