Browse Source

Merge branch 'get-set-additional-options' of CoBox/cobox-config into master

kyphae 2 years ago
parent
commit
8d0e9dfb2e
4 changed files with 98 additions and 39 deletions
  1. 10
    0
      index.js
  2. 22
    10
      lib/key-handler.js
  3. 9
    0
      lib/map-handler.js
  4. 57
    29
      test/index.test.js

+ 10
- 0
index.js View File

@@ -10,8 +10,14 @@ const CONFIG_FILE = 'config.yml'
10 10
 module.exports = (storage) => new CoBoxConfig(storage)
11 11
 
12 12
 const KeyHandler = require('./lib/key-handler')
13
+const MapHandler = require('./lib/map-handler')
14
+
15
+const defaultOptions = () => ({
16
+  mount: '/cobox'
17
+})
13 18
 
14 19
 const defaultConfig = () => ({
20
+  options: defaultOptions(),
15 21
   groups: { byKey: {}, byName: {} },
16 22
   identities: { byKey: {}, byName: {} }
17 23
 })
@@ -33,6 +39,7 @@ class CoBoxConfig {
33 39
       fs.writeFileSync(this.storage, yaml.safeDump(defaultConfig(), { sortKeys: true }))
34 40
       this._groups = config.groups
35 41
       this._identities = config.identities
42
+      this._options = config.options
36 43
     } else {
37 44
       this.load()
38 45
     }
@@ -42,6 +49,7 @@ class CoBoxConfig {
42 49
 
43 50
     this.groups = KeyHandler(this._groups)
44 51
     this.identities = KeyHandler(this._identities)
52
+    this.options = MapHandler(this._options)
45 53
   }
46 54
 
47 55
   save () {
@@ -49,6 +57,7 @@ class CoBoxConfig {
49 57
       var config = defaultConfig()
50 58
       config.groups = this._groups
51 59
       config.identities = this._identities
60
+      config.options = this._options
52 61
 
53 62
       fs.writeFileSync(this.storage, yaml.safeDump(config, { sortKeys: true }))
54 63
       return true
@@ -63,6 +72,7 @@ class CoBoxConfig {
63 72
       const config = yaml.safeLoad(fs.readFileSync(this.storage, 'utf8'))
64 73
       this._groups = config.groups
65 74
       this._identities = config.identities
75
+      this._options = config.options || defaultOptions()
66 76
       return true
67 77
     } catch (err) {
68 78
       console.error(err)

+ 22
- 10
lib/key-handler.js View File

@@ -10,25 +10,29 @@ const KeyHandler = module.exports = (collection) => ({
10 10
     if (isName(id)) return collection.byName[id]
11 11
     return false
12 12
   },
13
-  add: (entry) => {
13
+  set: (id, entry) => {
14
+    if (!isValid(entry)) return false
15
+    if (!entry) return false
14 16
     var clone = Object.assign({}, entry)
15 17
     hexify(clone)
16 18
     compact(clone)
17
-    if (collection.byKey[clone.publicKey]) clone = Object.assign(collection.byKey[clone.publicKey], clone)
18
-    else collection.byKey[clone.publicKey] = clone
19
-    if (clone.name) collection.byName[clone.name] = clone
19
+    Object.keys(clone).forEach((k) => {
20
+      if (crypto.isKey(clone[k])) clone[k] = clone[k].toString('hex')
21
+    })
22
+    if (crypto.isKey(id)) collection.byKey[id.toString('hex')] = clone
23
+    if (isName(id)|| clone.name) collection.byName[clone.name] = clone
20 24
     return true
21 25
   },
22
-  remove: (entry) => {
23
-    if (isPubKey(entry)) {
24
-      entry = collection.byKey[entry.toString('hex')]
26
+  delete: (id) => {
27
+    if (crypto.isKey(id)) {
28
+      entry = collection.byKey[id.toString('hex')]
25 29
       if (!entry) return false
26 30
       if (entry.name) delete collection.byName[entry.name]
27 31
       return delete collection.byKey[entry.publicKey.toString('hex')]
28
-    } else if (isName(entry)) {
29
-      entry = collection.byName[entry]
32
+    } else if (isName(id)) {
33
+      entry = collection.byName[id]
30 34
       if (!entry) return false
31
-      return delete collection.byName[entry.name] && delete collection.byKey[entry.publicKey.toString('hex')]
35
+      return delete collection.byName[id] && delete collection.byKey[entry.publicKey.toString('hex')]
32 36
     }
33 37
   },
34 38
   list: () => Object.values(collection.byKey)
@@ -46,3 +50,11 @@ function compact (obj) {
46 50
     else if (obj[key] == null) delete obj[key]
47 51
   })
48 52
 }
53
+
54
+function isValid (entry) {
55
+ return entry.publicKey
56
+    && crypto.isKey(entry.publicKey)
57
+    && !entry.encryptionKey || crypto.isKey(entry.encryptionKey)
58
+    && !entry.symmetricKey || crypto.isKey(entry.symmetricKey)
59
+    && !entry.name || isName(entry.name)
60
+}

+ 9
- 0
lib/map-handler.js View File

@@ -0,0 +1,9 @@
1
+module.exports = function MapHandler (collection) {
2
+  this.collection = collection
3
+  this.map = new Map(Object.keys(this.collection).map((k, i) => [k, Object.values(this.collection)[i]]))
4
+  return {
5
+    get: (id) => this.map.get(id),
6
+    set: (id, entry) => this.map.set(id, entry) && (this.collection[id] = entry),
7
+    delete: (id) => this.map.delete(id) && (delete this.collection[id])
8
+  }
9
+}

+ 57
- 29
test/index.test.js View File

@@ -26,7 +26,35 @@ describe('load', (context) => {
26 26
   })
27 27
 })
28 28
 
29
-describe('add', (context) => {
29
+describe('options', (context) => {
30
+  var storage
31
+
32
+  context.beforeEach((c) => {
33
+    storage = tmp()
34
+  })
35
+
36
+  context.afterEach((c) => {
37
+    cleanup(storage.root)
38
+  })
39
+
40
+  context('get', (assert, next) => {
41
+    var config = Config(storage)
42
+    assert.same(config.options.get('mount'), '/cobox', 'mount defaults to /cobox')
43
+    next()
44
+  })
45
+
46
+  context('set', (assert, next) => {
47
+    var config = Config(storage)
48
+    config.options.set('mount', './mnt')
49
+    assert.same(config._options['mount'], './mnt', 'sets mount option to ./mnt')
50
+    config.save()
51
+    config.load()
52
+    assert.same(config.options.get('mount'), './mnt', 'saved options')
53
+    next()
54
+  })
55
+})
56
+
57
+describe('set', (context) => {
30 58
   var storage
31 59
 
32 60
   context.beforeEach((c) => {
@@ -42,8 +70,8 @@ describe('add', (context) => {
42 70
     var group = crypto.unpack(crypto.accessKey())
43 71
     var identity = crypto.keyPair()
44 72
 
45
-    config.groups.add(group)
46
-    config.identities.add(identity)
73
+    config.groups.set(group.publicKey, group)
74
+    config.identities.set(identity.publicKey, identity)
47 75
 
48 76
     config.save()
49 77
 
@@ -61,8 +89,8 @@ describe('add', (context) => {
61 89
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
62 90
     var identity = Object.assign({ name: 'Alice' }, crypto.keyPair())
63 91
 
64
-    config.groups.add(group)
65
-    config.identities.add(identity)
92
+    config.groups.set(group.publicKey, group)
93
+    config.identities.set(identity.publicKey, identity)
66 94
 
67 95
     config.save()
68 96
 
@@ -99,8 +127,8 @@ describe('get', (context) => {
99 127
     var group = crypto.unpack(crypto.accessKey())
100 128
     var identity = crypto.keyPair()
101 129
 
102
-    config.groups.add(group)
103
-    config.identities.add(identity)
130
+    config.groups.set(group.publicKey, group)
131
+    config.identities.set(identity.publicKey, identity)
104 132
 
105 133
     config.save()
106 134
 
@@ -113,7 +141,7 @@ describe('get', (context) => {
113 141
   })
114 142
 })
115 143
 
116
-describe('remove', (context) => {
144
+describe('del', (context) => {
117 145
   var storage
118 146
 
119 147
   context.beforeEach((c) => {
@@ -124,51 +152,51 @@ describe('remove', (context) => {
124 152
     cleanup(storage)
125 153
   })
126 154
 
127
-  context('removes relevant entry using buffers', (assert, next) => {
155
+  context('deletes relevant entry using buffers', (assert, next) => {
128 156
     var config = Config(storage)
129 157
     var group = crypto.unpack(crypto.accessKey())
130 158
     var identity = crypto.keyPair()
131 159
 
132
-    config.groups.add(group)
133
-    config.identities.add(identity)
160
+    config.groups.set(group.publicKey, group)
161
+    config.identities.set(identity.publicKey, identity)
134 162
 
135 163
     config.save()
136 164
     config.load()
137 165
 
138
-    config.groups.remove(group.publicKey)
139
-    config.identities.remove(identity.publicKey)
166
+    config.groups.delete(group.publicKey)
167
+    config.identities.delete(identity.publicKey)
140 168
 
141
-    assert.same(config.groups.list(), [], 'removed the group')
142
-    assert.same(config.identities.list(), [], 'removed the identity')
169
+    assert.same(config.groups.list(), [], 'deleted the group')
170
+    assert.same(config.identities.list(), [], 'deleted the identity')
143 171
     next()
144 172
   })
145 173
 
146
-  context('removes relevant entry using buffers', (assert, next) => {
174
+  context('deletes relevant entry using buffers', (assert, next) => {
147 175
     var config = Config(storage)
148 176
     var group = crypto.unpack(crypto.accessKey())
149 177
     var identity = crypto.keyPair()
150 178
 
151
-    config.groups.add(group)
152
-    config.identities.add(identity)
179
+    config.groups.set(group.publicKey, group)
180
+    config.identities.set(identity.publicKey, identity)
153 181
 
154 182
     config.save()
155 183
     config.load()
156 184
 
157
-    config.groups.remove(group.publicKey.toString('hex'))
158
-    config.identities.remove(identity.publicKey.toString('hex'))
185
+    config.groups.delete(group.publicKey.toString('hex'))
186
+    config.identities.delete(identity.publicKey.toString('hex'))
159 187
 
160
-    assert.same(config.groups.list(), [], 'removed the group')
161
-    assert.same(config.identities.list(), [], 'removed the identity')
188
+    assert.same(config.groups.list(), [], 'deleted the group')
189
+    assert.same(config.identities.list(), [], 'deleted the identity')
162 190
     next()
163 191
   })
164 192
 
165
-  context('removes relevant entry using names', (assert, next) => {
193
+  context('deletes relevant entry using names', (assert, next) => {
166 194
     var config = Config(storage)
167 195
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
168 196
     var identity = Object.assign({ name: 'Alice' }, crypto.keyPair())
169 197
 
170
-    config.groups.add(group)
171
-    config.identities.add(identity)
198
+    config.groups.set(group.publicKey, group)
199
+    config.identities.set(identity.publicKey, identity)
172 200
 
173 201
     config.save()
174 202
     config.load()
@@ -176,14 +204,14 @@ describe('remove', (context) => {
176 204
     assert.same(config.groups.list().length, 1, 'saved the group')
177 205
     assert.same(config.identities.list().length, 1, 'saved the identity')
178 206
 
179
-    config.groups.remove(group.name)
180
-    config.identities.remove(identity.name)
207
+    config.groups.delete(group.name)
208
+    config.identities.delete(identity.name)
181 209
 
182 210
     config.save()
183 211
     config.load()
184 212
 
185
-    assert.same(config.groups.list(), [], 'removed the group')
186
-    assert.same(config.identities.list(), [], 'removed the identity')
213
+    assert.same(config.groups.list(), [], 'deleted the group')
214
+    assert.same(config.identities.list(), [], 'deleted the identity')
187 215
     next()
188 216
   })
189 217
 })