Browse Source

initial commit, test basic replicator

Kieran Gibb 2 years ago
commit
39be92736a
No known key found for this signature in database
6 changed files with 2255 additions and 0 deletions
  1. 3
    0
      README.md
  2. 14
    0
      index.js
  3. 29
    0
      package.json
  4. 25
    0
      test/index.test.js
  5. 63
    0
      test/util.js
  6. 2121
    0
      yarn.lock

+ 3
- 0
README.md View File

@@ -0,0 +1,3 @@
1
+# cobox-blind-replicator
2
+
3
+Blind Replicator class for cobox groups.

+ 14
- 0
index.js View File

@@ -0,0 +1,14 @@
1
+const Base = require('cobox-group-base')
2
+
3
+module.exports = (storage, address, opts = {}) => (
4
+  new BlindReplicator(storage, address, opts)
5
+)
6
+
7
+class BlindReplicator extends Base {
8
+  constructor (storage, address, opts = {}) {
9
+    super(storage, address, opts)
10
+    this._initFeeds(opts)
11
+    this._readyCallback = this.multifeed.ready.bind(this.multifeed)
12
+  }
13
+}
14
+

+ 29
- 0
package.json View File

@@ -0,0 +1,29 @@
1
+{
2
+  "name": "cobox-blind-replicator",
3
+  "version": "0.1.0",
4
+  "description": "blind replicator class",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "tape test/**/*.test.js | tap-spec"
8
+  },
9
+  "repository": {
10
+    "type": "git",
11
+    "url": "https://ledger-git.dyne.org/cobox/cobox-blind-replicator"
12
+  },
13
+  "keywords": [
14
+    "cobox"
15
+  ],
16
+  "author": "magma collective",
17
+  "license": "AGPL-3.0-or-later",
18
+  "dependencies": {
19
+    "cobox-group-base": "git+https://ledger-git.dyne.org/cobox/cobox-group-base"
20
+  },
21
+  "devDependencies": {
22
+    "nyc": "^14.1.1",
23
+    "rimraf": "^3.0.0",
24
+    "tap-spec": "^5.0.0",
25
+    "tape": "^4.11.0",
26
+    "tape-plus": "^1.0.0",
27
+    "tmp": "^0.1.0"
28
+  }
29
+}

+ 25
- 0
test/index.test.js View File

@@ -0,0 +1,25 @@
1
+const { describe } = require('tape-plus')
2
+const crypto = require('cobox-crypto')
3
+const path = require('path')
4
+
5
+const BlindReplictor = require('../')
6
+const { tmp, cleanup } = require('./util')
7
+
8
+describe('basic', (context) => {
9
+  context('inherits from cobox-group-base', async (assert, next) => {
10
+    var storage = tmp()
11
+    var address = crypto.address()
12
+    var group = BlindReplictor(storage, address)
13
+    assert.ok(group.config, 'has a config')
14
+    assert.ok(group.address, 'group created')
15
+    assert.ok(Buffer.isBuffer(group.address), 'address is a buffer')
16
+    assert.same(group.path, path.join(storage, address.toString('hex')))
17
+    assert.ok(group.storage, 'has random access file storage')
18
+    assert.ok(group.multifeed, 'has a multifeed instance')
19
+    assert.ok(typeof group.replicate === 'function', 'has a replicate function')
20
+
21
+    await group.ready()
22
+
23
+    cleanup(storage, next)
24
+  })
25
+})

+ 63
- 0
test/util.js View File

@@ -0,0 +1,63 @@
1
+const rimraf = require('rimraf')
2
+const debug = require('debug')('cleanup')
3
+const tmpdir = require('tmp').dirSync
4
+const mkdirp = require('mkdirp')
5
+
6
+function cleanup (dirs, cb) {
7
+  if (!cb) cb = noop
8
+  if (!Array.isArray(dirs)) dirs = [dirs]
9
+  var pending = 1
10
+
11
+  function next (n) {
12
+    var dir = dirs[n]
13
+    if (!dir) return done()
14
+    ++pending
15
+    process.nextTick(next, n + 1)
16
+
17
+    rimraf(dir, (err) => {
18
+      if (err) return done(err)
19
+      done()
20
+    })
21
+  }
22
+
23
+  function done (err) {
24
+    if (err) {
25
+      pending = Infinity
26
+      return cb(err)
27
+    }
28
+    if (!--pending) return cb()
29
+  }
30
+
31
+  next(0)
32
+}
33
+
34
+function tmp () {
35
+  var path = '.'+tmpdir().name
36
+  mkdirp.sync(path)
37
+  return path
38
+}
39
+
40
+function replicate (a, b, opts, cb) {
41
+  if (typeof opts === 'function') return replicate(a, b, {}, opts)
42
+  if (!cb) cb = noop
43
+
44
+  var s = a.replicate(true, Object.assign({ live: false }, opts))
45
+  var d = b.replicate(false, Object.assign({ live: false }, opts))
46
+
47
+  s.pipe(d).pipe(s)
48
+
49
+  s.on('error', (err) => {
50
+    if (err) return cb(err)
51
+  })
52
+
53
+  s.on('end', cb)
54
+}
55
+
56
+function uniq (array) {
57
+  if (!Array.isArray(array)) array = [array]
58
+  return Array.from(new Set(array))
59
+}
60
+
61
+function noop () {}
62
+
63
+module.exports = { cleanup, tmp, replicate, uniq }

+ 2121
- 0
yarn.lock
File diff suppressed because it is too large
View File