Browse Source

initial commit

ameba23 3 years ago
commit
1d8ae0198b
5 changed files with 1652 additions and 0 deletions
  1. 13
    0
      index.js
  2. 21
    0
      mount.js
  3. 1578
    0
      package-lock.json
  4. 22
    0
      package.json
  5. 18
    0
      swarm.js

+ 13
- 0
index.js View File

@@ -0,0 +1,13 @@
1
+const KappaDrive = require('peerfs')
2
+const peerfsMount = require('./mount')
3
+const ram = require('random-access-memory')
4
+const MOUNTDIR = './mnt'
5
+
6
+// temporary - get key as command line arg
7
+const key = process.argv[2] ? Buffer.from(process.argv[2], 'hex') : null
8
+
9
+var drive = KappaDrive(ram, key)
10
+drive.ready(() => {
11
+  peerfsMount(drive, MOUNTDIR)
12
+  swarm(drive)
13
+})

+ 21
- 0
mount.js View File

@@ -0,0 +1,21 @@
1
+const rimraf = require('rimraf')
2
+const { mount, unmount, getHandlers  } = require('hyperdrive-fuse')
3
+const mkdirp = require('mkdirp').sync
4
+
5
+module.exports = async function peerfsMount (drive, mountDir) {
6
+  mkdirp(mountDir)
7
+  var { destroy } = await mount(drive, mountDir)
8
+  process.once('SIGINT', () => cleanup(destroy))
9
+}
10
+
11
+function cleanup (destroy) {
12
+  return new Promise((resolve, reject) => {
13
+    destroy(err => {
14
+      if (err) return reject(err)
15
+      rimraf('./mnt', err => {
16
+        if (err) return reject(err)
17
+        return resolve()
18
+      })
19
+    })
20
+  })
21
+}

+ 1578
- 0
package-lock.json
File diff suppressed because it is too large
View File


+ 22
- 0
package.json View File

@@ -0,0 +1,22 @@
1
+{
2
+  "name": "peerfs-swarm",
3
+  "version": "1.0.0",
4
+  "description": "",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "author": "",
10
+  "license": "ISC",
11
+  "dependencies": {
12
+    "dat-swarm-defaults": "^1.0.2",
13
+    "debug": "^4.1.1",
14
+    "peerfs": "github:coboxcoop/peerfs#mount",
15
+    "discovery-swarm": "^6.0.0",
16
+    "hyperdrive-fuse": "^1.1.2",
17
+    "mkdirp": "^0.5.1",
18
+    "pump": "^3.0.0",
19
+    "random-access-memory": "^3.1.1",
20
+    "rimraf": "^2.6.3"
21
+  }
22
+}

+ 18
- 0
swarm.js View File

@@ -0,0 +1,18 @@
1
+const discovery = require('discovery-swarm')
2
+const pump = require('pump')
3
+const config = require('dat-swarm-defaults')
4
+const debug = require('debug')('kappa-drive')
5
+
6
+function swarm (drive, opts = {}) {
7
+  // TODO: add id property with local key  (cabal does this)
8
+  var swarm = discovery(config())
9
+
10
+  debug(`[SWARM] on ${drive.discoveryKey.toString('hex')}`)
11
+  swarm.join(drive.discoveryKey)
12
+
13
+  swarm.on('connection', (connection, peer) => {
14
+    debug(`[PEER] ${peer.id.toString('hex')}`)
15
+
16
+    pump(connection, drive.replicate({ live: true }), connection)
17
+  })
18
+}