Skip to content

Commit

Permalink
package
Browse files Browse the repository at this point in the history
  • Loading branch information
shikhars371 committed Jun 21, 2020
0 parents commit 291002c
Show file tree
Hide file tree
Showing 7 changed files with 3,274 additions and 0 deletions.
9 changes: 9 additions & 0 deletions collaborators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Collaborators

hyperdrive-archive-swarm is only possible due to the excellent work of the following collaborators:

<table><tbody><tr><th align="left">juliangruber</th><td><a href="https://github.com/juliangruber">GitHub/juliangruber</a></td></tr>
<tr><th align="left">karissa</th><td><a href="https://github.com/karissa">GitHub/karissa</a></td></tr>
<tr><th align="left">ogd</th><td><a href="https://github.com/ogd">GitHub/ogd</a></td></tr>
<tr><th align="left">laurengarcia</th><td><a href="https://github.com/laurengarcia">GitHub/laurengarcia</a></td></tr>
</tbody></table>
19 changes: 19 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var hyperdrive = require('hyperdrive')
var ram = require('random-access-memory')
var swarm = require('.')
var Buffer = require('safe-buffer').Buffer

var key = process.argv[2] && new Buffer(process.argv[2], 'hex')
var archive = hyperdrive(ram, key)
archive.ready(function (err) {
if (err) throw err
console.log('key', archive.key.toString('hex'))
var sw = swarm(archive)
sw.on('connection', function (peer, type) {
console.log('got', peer, type) // type is 'webrtc-swarm' or 'discovery-swarm'
console.log('connected to', sw.connections, 'peers')
peer.on('close', function () {
console.log('peer disconnected')
})
})
})
47 changes: 47 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var swarmDefaults = require('dat-swarm-defaults')
var disc = require('discovery-swarm')
var xtend = require('xtend')

module.exports = HyperdriveSwarm

function HyperdriveSwarm (archive, opts) {
if (!(this instanceof HyperdriveSwarm)) return new HyperdriveSwarm(archive, opts)
if (!opts) opts = {}

var self = this
this.archive = archive
this.uploading = !(opts.upload === false)
this.downloading = !(opts.download === false)
this.live = !(opts.live === false)

var isHyperdbInstance = !!(archive.get && archive.put && archive.replicate && archive.authorize)

if (isHyperdbInstance && !archive.local) {
throw new Error('hyperdiscovery swarm must be created after the local hyperdb instance is ready!')
}

// Discovery Swarm Options
opts = xtend({
port: 3282,
id: isHyperdbInstance ? archive.local.id.toString('hex') : archive.id,
hash: false,
stream: function (peer) {
return archive.replicate(xtend({
live: self.live,
upload: self.uploading,
download: self.downloading
}, isHyperdbInstance ? {
userData: archive.local.key
} : {}))
}
}, opts)

this.swarm = disc(swarmDefaults(opts))
this.swarm.once('error', function () {
self.swarm.listen(0)
})

this.swarm.listen(opts.port)
this.swarm.join(this.archive.discoveryKey)
return this.swarm
}
Loading

0 comments on commit 291002c

Please sign in to comment.