├── .gitignore
├── .travis.yml
├── collaborators.md
├── example.js
├── index.js
├── package.json
├── readme.md
└── test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 |
3 | node_js:
4 | - "4"
5 | - "5"
6 | - "6"
7 |
8 | sudo: false
9 |
10 | script:
11 | - npm test
12 |
--------------------------------------------------------------------------------
/collaborators.md:
--------------------------------------------------------------------------------
1 | ## Collaborators
2 |
3 | hyperdrive-archive-swarm is only possible due to the excellent work of the following collaborators:
4 |
5 |
10 |
--------------------------------------------------------------------------------
/example.js:
--------------------------------------------------------------------------------
1 | var hyperdrive = require('hyperdrive')
2 | var memdb = require('memdb')
3 | var swarm = require('.')
4 |
5 | var drive = hyperdrive(memdb())
6 | var archive = drive.createArchive()
7 | archive.finalize(function () {
8 | var sw = swarm(archive)
9 | sw.on('connection', function (peer, type) {
10 | console.log('got', peer, type) // type is 'webrtc-swarm' or 'discovery-swarm'
11 | console.log('connected to', sw.connections, 'peers')
12 | peer.on('close', function () {
13 | console.log('peer disconnected')
14 | })
15 | })
16 | })
17 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var swarmDefaults = require('datland-swarm-defaults')
2 | var disc = require('discovery-swarm')
3 |
4 | module.exports = HyperdriveSwarm
5 |
6 | function HyperdriveSwarm (archive, opts) {
7 | if (!(this instanceof HyperdriveSwarm)) return new HyperdriveSwarm(archive, opts)
8 | if (!opts) opts = {}
9 | this.archive = archive
10 | this.uploading = !(opts.upload === false)
11 | this.downloading = !(opts.download === false)
12 | var self = this
13 | this.swarm = disc(swarmDefaults({
14 | id: archive.id,
15 | hash: false,
16 | stream: function (peer) {
17 | return archive.replicate({
18 | upload: self.uploading,
19 | download: self.downloading
20 | })
21 | },
22 | utp: opts.utp,
23 | tcp: opts.tcp
24 | }))
25 |
26 | this.swarm.once('error', function () {
27 | self.swarm.listen(0)
28 | })
29 | this.swarm.listen(opts.port || 3282)
30 | this.swarm.join(this.archive.discoveryKey)
31 |
32 | return this.swarm
33 | }
34 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "hyperdrive-archive-swarm",
3 | "version": "5.0.5",
4 | "description": "Join the p2p swarm for the given hyperdrive archive",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "standard && node test.js"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/karissa/hyperdrive-archive-swarm.git"
12 | },
13 | "author": "",
14 | "license": "ISC",
15 | "bugs": {
16 | "url": "https://github.com/karissa/hyperdrive-archive-swarm/issues"
17 | },
18 | "homepage": "https://github.com/karissa/hyperdrive-archive-swarm#readme",
19 | "dependencies": {
20 | "datland-swarm-defaults": "^1.0.0",
21 | "discovery-swarm": "^4.0.2"
22 | },
23 | "devDependencies": {
24 | "hypercore": "^4.11.0",
25 | "memdb": "^1.3.1",
26 | "standard": "^7.1.2",
27 | "tape": "^4.6.0"
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # hyperdrive-archive-swarm
2 |
3 | No longer maintained. Please use
4 | [hyperdiscovery](http://github.com/karissa/hyperdiscovery) instead.
5 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | var tape = require('tape')
2 | var hypercore = require('hypercore')
3 | var memdb = require('memdb')
4 | var swarm = require('.')
5 |
6 | var core1 = hypercore(memdb())
7 | var core2 = hypercore(memdb())
8 |
9 | function getSwarms (opts) {
10 | var feed1 = core1.createFeed()
11 | var feed2 = core2.createFeed(feed1.key)
12 | var write = swarm(feed1, opts)
13 | var read = swarm(feed2, opts)
14 | return [write, read]
15 | }
16 |
17 | tape('connect and close', function (t) {
18 | t.plan(6)
19 | var swarms = getSwarms()
20 |
21 | var write = swarms[0]
22 | var read = swarms[1]
23 | var missing = 2
24 |
25 | write.once('connection', function (peer, type) {
26 | t.ok(1, 'write connected')
27 | t.equals(write.connections.length, 1)
28 | done()
29 | })
30 |
31 | read.once('connection', function (peer, type) {
32 | t.ok(1, 'read connected')
33 | t.equals(read.connections.length, 1)
34 | done()
35 | })
36 |
37 | function done () {
38 | if (--missing) return
39 | write.close(function () {
40 | t.ok(1, 'write closed')
41 | read.close(function () {
42 | t.ok(1, 'read closed')
43 | })
44 | })
45 | }
46 | })
47 |
48 | tape('connect without utp', function (t) {
49 | t.plan(6)
50 | var swarms = getSwarms({utp: false})
51 |
52 | var write = swarms[0]
53 | var read = swarms[1]
54 | var missing = 2
55 |
56 | write.once('connection', function (peer, type) {
57 | t.ok(1, 'write connected')
58 | t.equals(write.connections.length, 1)
59 | done()
60 | })
61 |
62 | read.once('connection', function (peer, type) {
63 | t.ok(1, 'read connected')
64 | t.equals(read.connections.length, 1, 'connection length')
65 | done()
66 | })
67 |
68 | function done () {
69 | if (--missing) return
70 | write.close(function () {
71 | t.ok(1, 'write closed')
72 | read.close(function () {
73 | t.ok(1, 'read closed')
74 | })
75 | })
76 | }
77 | })
78 |
--------------------------------------------------------------------------------