Skip to content

Instantly share code, notes, and snippets.

@andrewosh
Created May 11, 2020 02:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewosh/1f3ae698ba42f7a382a5b85ab5305b88 to your computer and use it in GitHub Desktop.
Save andrewosh/1f3ae698ba42f7a382a5b85ab5305b88 to your computer and use it in GitHub Desktop.
A smal,l end-to-end example of using Hyperdrive/Hyperswarm to replicate a drive

Note: To keep this even smaller, we're using a small helper module called the hyperswarm replicator, which wraps a few Hyperswarm setup details.

const hyperdrive = require('hyperdrive')
const replicate = require('@hyperswarm/replicator')
const driveToShare = hyperdrive('./writer-storage')
driveToShare.writeFile('hello.txt', 'world', err => {
if (err) return console.error(err)
const swarm = replicate(driveToShare)
process.on('SIGINT', () => swarm.destroy())
// Let's say the drive's key is 1df13252...
const keyString = driveToShare.key.toString('hex')
console.log('Seeding drive:', keyString)
})
const hyperdrive = require('hyperdrive')
const replicate = require('@hyperswarm/replicator')
// Assuming this is the key generated above.
const key = Buffer.from('1df13252...', 'hex')
const driveToRead = hyperdrive('./reader-storage', key)
const swarm = replicate(driveToRead)
process.on('SIGINT', () => swarm.destroy())
driveToRead.on('peer-add', onPeerAdded)
function onPeerAdded () {
const readOpts = { encoding: 'utf-8' }
driveToRead.readFile('hello.txt', readOpts, (err, contents) => {
if (err) return console.error(err)
console.log('Got contents:', contents)
})
}
@raphael10-collab
Copy link

node hyper_drive_swarm_play_firstpeer.js 
/home/raphy/hypercore/hyperdrive/playing/hyper_drive_swarm_play_firstpeer.js:12
  const swarm = replicate(driveToShare)
                ^

TypeError: Class constructor Replicator cannot be invoked without 'new'
    at /home/raphy/hypercore/hyperdrive/playing/hyper_drive_swarm_play_firstpeer.js:12:17
    at Duplexify.<anonymous> (/home/raphy/hypercore/hyperdrive/playing/node_modules/hyperdrive/index.js:599:28)
    at Duplexify.emit (events.js:327:22)
    at finishMaybe (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:630:14)
    at afterWrite (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:492:3)
    at onwrite (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:483:7)
    at Duplexify.WritableState.onwrite (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:180:5)
    at Object.onceWrapper (events.js:421:28)
    at Duplexify.emit (events.js:315:20)

@draeder
Copy link

draeder commented Nov 22, 2021

node hyper_drive_swarm_play_firstpeer.js 
/home/raphy/hypercore/hyperdrive/playing/hyper_drive_swarm_play_firstpeer.js:12
  const swarm = replicate(driveToShare)
                ^

TypeError: Class constructor Replicator cannot be invoked without 'new'
    at /home/raphy/hypercore/hyperdrive/playing/hyper_drive_swarm_play_firstpeer.js:12:17
    at Duplexify.<anonymous> (/home/raphy/hypercore/hyperdrive/playing/node_modules/hyperdrive/index.js:599:28)
    at Duplexify.emit (events.js:327:22)
    at finishMaybe (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:630:14)
    at afterWrite (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:492:3)
    at onwrite (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:483:7)
    at Duplexify.WritableState.onwrite (/home/raphy/hypercore/hyperdrive/playing/node_modules/readable-stream/lib/_stream_writable.js:180:5)
    at Object.onceWrapper (events.js:421:28)
    at Duplexify.emit (events.js:315:20)

Same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment