Browse Source

handle map correctly

Kieran Gibb 2 years ago
parent
commit
77f8a10360
No account linked to committer's email address
4 changed files with 48 additions and 41 deletions
  1. 2
    1
      index.js
  2. 2
    5
      lib/key-handler.js
  3. 9
    0
      lib/map-handler.js
  4. 35
    35
      test/index.test.js

+ 2
- 1
index.js View File

@@ -10,6 +10,7 @@ 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')
13 14
 
14 15
 const defaultOptions = () => ({
15 16
   mount: '/cobox'
@@ -48,7 +49,7 @@ class CoBoxConfig {
48 49
 
49 50
     this.groups = KeyHandler(this._groups)
50 51
     this.identities = KeyHandler(this._identities)
51
-    this.options = new Map(Object.keys(this._options).map((k, i) => [k, Object.values(this._options)[i]]))
52
+    this.options = MapHandler(this._options)
52 53
   }
53 54
 
54 55
   save () {

+ 2
- 5
lib/key-handler.js View File

@@ -11,10 +11,7 @@ const KeyHandler = module.exports = (collection) => ({
11 11
     return false
12 12
   },
13 13
   set: (id, entry) => {
14
-    if (!isValid(entry)) {
15
-      console.log("INVALID")
16
-      return false
17
-    }
14
+    if (!isValid(entry)) return false
18 15
     if (!entry) return false
19 16
     var clone = Object.assign({}, entry)
20 17
     hexify(clone)
@@ -26,7 +23,7 @@ const KeyHandler = module.exports = (collection) => ({
26 23
     if (isName(id)|| clone.name) collection.byName[clone.name] = clone
27 24
     return true
28 25
   },
29
-  del: (id) => {
26
+  delete: (id) => {
30 27
     if (crypto.isKey(id)) {
31 28
       entry = collection.byKey[id.toString('hex')]
32 29
       if (!entry) return false

+ 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
+}

+ 35
- 35
test/index.test.js View File

@@ -39,22 +39,22 @@ describe('options', (context) => {
39 39
 
40 40
   context('get', (assert, next) => {
41 41
     var config = Config(storage.path)
42
-    assert.same(config.get('mount'), '/cobox', 'mount defaults to /cobox')
42
+    assert.same(config.options.get('mount'), '/cobox', 'mount defaults to /cobox')
43 43
     next()
44 44
   })
45 45
 
46 46
   context('set', (assert, next) => {
47 47
     var config = Config(storage.path)
48
-    config.set('mount', './mnt')
48
+    config.options.set('mount', './mnt')
49 49
     assert.same(config._options['mount'], './mnt', 'sets mount option to ./mnt')
50 50
     config.save()
51 51
     config.load()
52
-    assert.same(config.get('mount'), './mnt', 'saved options')
52
+    assert.same(config.options.get('mount'), './mnt', 'saved options')
53 53
     next()
54 54
   })
55 55
 })
56 56
 
57
-describe('add', (context) => {
57
+describe('set', (context) => {
58 58
   var storage
59 59
 
60 60
   context.beforeEach((c) => {
@@ -70,8 +70,8 @@ describe('add', (context) => {
70 70
     var group = crypto.unpack(crypto.accessKey())
71 71
     var identity = crypto.keyPair()
72 72
 
73
-    config.groups.add(group)
74
-    config.identities.add(identity)
73
+    config.groups.set(group.publicKey, group)
74
+    config.identities.set(identity.publicKey, identity)
75 75
 
76 76
     config.save()
77 77
 
@@ -89,8 +89,8 @@ describe('add', (context) => {
89 89
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
90 90
     var identity = Object.assign({ name: 'Alice' }, crypto.keyPair())
91 91
 
92
-    config.groups.add(group)
93
-    config.identities.add(identity)
92
+    config.groups.set(group.publicKey, group)
93
+    config.identities.set(identity.publicKey, identity)
94 94
 
95 95
     config.save()
96 96
 
@@ -127,8 +127,8 @@ describe('get', (context) => {
127 127
     var group = crypto.unpack(crypto.accessKey())
128 128
     var identity = crypto.keyPair()
129 129
 
130
-    config.groups.add(group)
131
-    config.identities.add(identity)
130
+    config.groups.set(group.publicKey, group)
131
+    config.identities.set(identity.publicKey, identity)
132 132
 
133 133
     config.save()
134 134
 
@@ -141,7 +141,7 @@ describe('get', (context) => {
141 141
   })
142 142
 })
143 143
 
144
-describe('remove', (context) => {
144
+describe('del', (context) => {
145 145
   var storage
146 146
 
147 147
   context.beforeEach((c) => {
@@ -152,51 +152,51 @@ describe('remove', (context) => {
152 152
     cleanup(storage)
153 153
   })
154 154
 
155
-  context('removes relevant entry using buffers', (assert, next) => {
156
-    var config = Config(storage)
155
+  context('deletes relevant entry using buffers', (assert, next) => {
156
+    var config = Config(storage.path)
157 157
     var group = crypto.unpack(crypto.accessKey())
158 158
     var identity = crypto.keyPair()
159 159
 
160
-    config.groups.add(group)
161
-    config.identities.add(identity)
160
+    config.groups.set(group.publicKey, group)
161
+    config.identities.set(identity.publicKey, identity)
162 162
 
163 163
     config.save()
164 164
     config.load()
165 165
 
166
-    config.groups.remove(group.publicKey)
167
-    config.identities.remove(identity.publicKey)
166
+    config.groups.delete(group.publicKey)
167
+    config.identities.delete(identity.publicKey)
168 168
 
169
-    assert.same(config.groups.list(), [], 'removed the group')
170
-    assert.same(config.identities.list(), [], 'removed the identity')
169
+    assert.same(config.groups.list(), [], 'deld the group')
170
+    assert.same(config.identities.list(), [], 'deld the identity')
171 171
     next()
172 172
   })
173 173
 
174
-  context('removes relevant entry using buffers', (assert, next) => {
175
-    var config = Config(storage)
174
+  context('deletes relevant entry using buffers', (assert, next) => {
175
+    var config = Config(storage.path)
176 176
     var group = crypto.unpack(crypto.accessKey())
177 177
     var identity = crypto.keyPair()
178 178
 
179
-    config.groups.add(group)
180
-    config.identities.add(identity)
179
+    config.groups.set(group.publicKey, group)
180
+    config.identities.set(identity.publicKey, identity)
181 181
 
182 182
     config.save()
183 183
     config.load()
184 184
 
185
-    config.groups.remove(group.publicKey.toString('hex'))
186
-    config.identities.remove(identity.publicKey.toString('hex'))
185
+    config.groups.delete(group.publicKey.toString('hex'))
186
+    config.identities.delete(identity.publicKey.toString('hex'))
187 187
 
188
-    assert.same(config.groups.list(), [], 'removed the group')
189
-    assert.same(config.identities.list(), [], 'removed the identity')
188
+    assert.same(config.groups.list(), [], 'deld the group')
189
+    assert.same(config.identities.list(), [], 'deld the identity')
190 190
     next()
191 191
   })
192 192
 
193
-  context('removes relevant entry using names', (assert, next) => {
194
-    var config = Config(storage)
193
+  context('deletes relevant entry using names', (assert, next) => {
194
+    var config = Config(storage.path)
195 195
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
196 196
     var identity = Object.assign({ name: 'Alice' }, crypto.keyPair())
197 197
 
198
-    config.groups.add(group)
199
-    config.identities.add(identity)
198
+    config.groups.set(group.publicKey, group)
199
+    config.identities.set(identity.publicKey, identity)
200 200
 
201 201
     config.save()
202 202
     config.load()
@@ -204,14 +204,14 @@ describe('remove', (context) => {
204 204
     assert.same(config.groups.list().length, 1, 'saved the group')
205 205
     assert.same(config.identities.list().length, 1, 'saved the identity')
206 206
 
207
-    config.groups.remove(group.name)
208
-    config.identities.remove(identity.name)
207
+    config.groups.delete(group.name)
208
+    config.identities.delete(identity.name)
209 209
 
210 210
     config.save()
211 211
     config.load()
212 212
 
213
-    assert.same(config.groups.list(), [], 'removed the group')
214
-    assert.same(config.identities.list(), [], 'removed the identity')
213
+    assert.same(config.groups.list(), [], 'deld the group')
214
+    assert.same(config.identities.list(), [], 'deld the identity')
215 215
     next()
216 216
   })
217 217
 })