Browse Source

pass group constructor class to store

second location
Kieran Gibb 2 years ago
parent
commit
e5db6f6eb3
No known key found for this signature in database
2 changed files with 38 additions and 24 deletions
  1. 12
    10
      index.js
  2. 26
    14
      test/index.test.js

+ 12
- 10
index.js View File

@@ -1,4 +1,3 @@
1
-const { Encrypted, Decrypted } = require('cobox-group')
2 1
 const Config = require('cobox-config')
3 2
 const crypto = require('cobox-crypto')
4 3
 const path = require('path')
@@ -9,22 +8,23 @@ const maybe = require('call-me-maybe')
9 8
 
10 9
 const { isString } = require('./util')
11 10
 
12
-module.exports = (storage, opts) => new GroupStore(storage, opts)
11
+module.exports = (createGroup, storage, opts) => new GroupStore(createGroup, storage, opts)
13 12
 
14 13
 class GroupStore {
15
-  constructor (storage, opts = {}) {
14
+  constructor (createGroup, storage, opts = {}) {
16 15
     this._id = crypto.randomBytes(16).toString('hex')
17 16
     this.root = storage || constants.storage
18 17
     this.storage = path.join(this.root, 'groups')
19 18
     this.config = opts.config
20 19
     this.opts = opts
20
+    this.createGroup = createGroup
21 21
 
22 22
     this.groups = this.config.groups.list().reduce((collection, groupOpts) => {
23 23
       var { address, encryptionKey } = groupOpts
24 24
 
25 25
       var group = encryptionKey
26
-        ? Decrypted(this.storage, address, Object.assign(this.opts, { encryptionKey }))
27
-        : Encrypted(this.storage, address, this.opts)
26
+        ? this.createGroup(this.storage, address, Object.assign(this.opts, { encryptionKey }))
27
+        : this.createGroup(this.storage, address, this.opts)
28 28
 
29 29
       collection[address] = group
30 30
       collection[group.address] = group
@@ -47,8 +47,9 @@ class GroupStore {
47 47
   all () {
48 48
     var self = this
49 49
     return new Promise((resolve, reject) => {
50
-      self.ready(() => {
51
-        return resolve(Array.from(new Set(Object.values(self.groups))))
50
+      self.ready((err) => {
51
+        if (err) return reject(err)
52
+        resolve(Array.from(new Set(Object.values(self.groups))))
52 53
       })
53 54
     })
54 55
   }
@@ -56,7 +57,8 @@ class GroupStore {
56 57
   get (id) {
57 58
     var self = this
58 59
     return new Promise((resolve, reject) => {
59
-      self.ready(() => {
60
+      self.ready((err) => {
61
+        if (err) return reject(err)
60 62
         var key, name, keys
61 63
 
62 64
         if (crypto.isKey(id)) key = id
@@ -84,8 +86,8 @@ class GroupStore {
84 86
         if (groupOpts && name && groupOpts.name !== name) return reject(new Error('invalid: group with this name already exists'))
85 87
 
86 88
         var group = encryptionKey
87
-          ? Decrypted(this.storage, address, Object.assign(this.opts, { encryptionKey }))
88
-          : Encrypted(this.storage, address, this.opts)
89
+          ? this.createGroup(this.storage, address, Object.assign(this.opts, { encryptionKey }))
90
+          : this.createGroup(this.storage, address, this.opts)
89 91
 
90 92
         this.groups[address.toString('hex')] = group
91 93
         this.groups[address] = group

+ 26
- 14
test/index.test.js View File

@@ -1,41 +1,49 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const Config = require('cobox-config')
3
-const Group = require('cobox-group')
3
+const { Decrypted, Encrypted } = require('cobox-group')
4 4
 const crypto = require('cobox-crypto')
5 5
 const Store = require('../')
6 6
 
7 7
 const { tmp, cleanup } = require('./util')
8 8
 
9 9
 describe('group store: basic', (context) => {
10
-  let storage, config
11
-
12
-  context.beforeEach((c) => {
13
-    storage = tmp()
14
-    config = Config(storage)
15
-  })
16
-
17 10
   context('on load it caches existing groups from the config', function (assert, next) {
18
-    var group = crypto.keySet()
11
+    let storage = tmp(),
12
+      config = Config(storage),
13
+      group = crypto.keySet()
14
+
19 15
     config.groups.set(group.address, group)
20
-    const store = Store(storage, { config })
21 16
 
22
-    store.ready(() => {
17
+    const store = Store(Decrypted, storage, { config })
18
+
19
+    store.ready((err) => {
20
+      assert.error(err, 'no error')
21
+
23 22
       assert.ok(Object.values(store.groups).length, 'builds then caches groups from the config')
24 23
       assert.ok(store.groups[group.address], 'accessible using buffer')
25 24
       assert.ok(store.groups[group.address.toString('hex')], 'accessible using hex')
25
+
26 26
       cleanup(storage, next)
27 27
     })
28 28
   })
29 29
 
30 30
   context('create a single group', async function (assert, next) {
31
-    const store = Store(storage, { config })
31
+    let storage = tmp(),
32
+      config = Config(storage)
33
+
34
+    const store = Store(Decrypted, storage, { config })
35
+
32 36
     var a = await store.get()
33 37
     assert.ok(a, 'creates a new group')
34 38
     cleanup(storage, next)
35 39
   })
36 40
 
37 41
   context('get a group by key', async function (assert, next) {
38
-    const store = Store(storage, { config })
42
+    let storage = tmp(),
43
+      config = Config(storage)
44
+
45
+    const store = Store(Decrypted, storage, { config })
46
+
39 47
     var a = await store.get()
40 48
     assert.ok(a, 'creates a new group')
41 49
 
@@ -46,7 +54,11 @@ describe('group store: basic', (context) => {
46 54
   })
47 55
 
48 56
   context('get a group by name', async function (assert, next) {
49
-    const store = Store(storage, { config })
57
+    let storage = tmp(),
58
+      config = Config(storage)
59
+
60
+    const store = Store(Decrypted, storage, { config })
61
+
50 62
     var a = await store.get("Silly String")
51 63
     assert.ok(a, 'creates a new group')
52 64