Browse Source

Merge branch 'development'

Kieran Gibb 2 years ago
parent
commit
634ed23967
No known key found for this signature in database
4 changed files with 135 additions and 3515 deletions
  1. 58
    30
      index.js
  2. 52
    30
      test/index.test.js
  3. 25
    0
      test/mock.js
  4. 0
    3455
      yarn.lock

+ 58
- 30
index.js View File

@@ -28,13 +28,14 @@ class Group extends Base {
28 28
 
29 29
     this.masterKey = opts.masterKey || crypto.masterKey()
30 30
 
31
-    this.encryptionKey = opts.encryptionKey
32
-      ? crypto.encryptionKey(opts.encryptionKey)
33
-      : this._loadEncryptionKey() || crypto.encryptionKey()
34
-    assert(this._saveEncryptionKey(), 'Problem writing encryptionKey')
35
-
31
+    this.encryptionKey = crypto.encryptionKey(opts.encryptionKey)
36 32
     if (!crypto.isKey(this.encryptionKey)) throw new Error('invalid: encryption key format')
37 33
 
34
+    // this.encryptionKey = opts.encryptionKey
35
+    //   ? crypto.encryptionKey(opts.encryptionKey)
36
+    //   : this._loadEncryptionKey() || crypto.encryptionKey()
37
+    // assert(this._saveEncryptionKey(), 'Problem writing encryptionKey')
38
+
38 39
     var groupOpts = this.config.groups.get(this.address)
39 40
     this.name = opts.name || (groupOpts && groupOpts.name)
40 41
     this.db = {}
@@ -123,14 +124,41 @@ class Group extends Base {
123 124
     return location
124 125
   }
125 126
 
127
+  async ls (subdir = '/') {
128
+    assert(typeof subdir === 'string', 'Path must be a string')
129
+    assert(this.drive._isReady, 'Drive must be ready')
130
+
131
+    const files = await new Promise((resolve, reject) => {
132
+      this.drive.readdir(subdir, (err, files) => {
133
+        if (err) reject(err)
134
+        resolve(files)
135
+      })
136
+    })
137
+
138
+    const stats = await Promise.all(files.map((file) => {
139
+      return new Promise((resolve, reject) => {
140
+        this.drive.lstat(file, (err, statObj) => {
141
+          if (err) reject(err)
142
+          resolve(statObj)
143
+        })
144
+      })
145
+    }))
146
+
147
+    return stats.reduce((acc, cur, i) => {
148
+      acc[files[i]] = cur
149
+      return acc
150
+    }, {})
151
+  }
152
+
126 153
   save () {
127 154
     this.config.groups.set(this.address, {
128 155
       address: this.address,
129
-      // encryptionKey: this.encryptionKey,
156
+      encryptionKey: this.encryptionKey,
130 157
       name: this.name
131 158
     })
132 159
 
133
-    return this._saveEncryptionKey() && this.config.save()
160
+    // return this._saveEncryptionKey() && this.config.save()
161
+    return this.config.save()
134 162
   }
135 163
 
136 164
   // ------------------------------ PRIVATE FUNCTIONS ------------------------------
@@ -182,29 +210,29 @@ class Group extends Base {
182 210
     this.db.network.put(peer, payload, callback)
183 211
   }
184 212
 
185
-  _saveEncryptionKey () {
186
-    const encryptionKeyPath = path.join(this.config.root, 'groups', this.address.toString('hex'), 'encryption_key')
187
-    try {
188
-      if (!fs.existsSync(encryptionKeyPath)) {
189
-        mkdirp.sync(path.dirname(encryptionKeyPath))
190
-        fs.writeFileSync(encryptionKeyPath, this.encryptionKey, { mode: fs.constants.S_IRUSR })
191
-      }
192
-    } catch (err) {
193
-      return false
194
-    }
195
-    return true
196
-  }
197
-
198
-  _loadEncryptionKey () {
199
-    const encryptionKeyPath = path.join(this.config.root, 'groups', this.address.toString('hex'), 'encryption_key')
200
-    let encryptionKey
201
-    try {
202
-      encryptionKey = fs.readFileSync(encryptionKeyPath)
203
-    } catch (err) {
204
-      return false
205
-    }
206
-    return encryptionKey
207
-  }
213
+  // _saveEncryptionKey () {
214
+  //   const encryptionKeyPath = path.join(this.config.root, 'groups', this.address.toString('hex'), 'encryption_key')
215
+  //   try {
216
+  //     if (!fs.existsSync(encryptionKeyPath)) {
217
+  //       mkdirp.sync(path.dirname(encryptionKeyPath))
218
+  //       fs.writeFileSync(encryptionKeyPath, this.encryptionKey, { mode: fs.constants.S_IRUSR })
219
+  //     }
220
+  //   } catch (err) {
221
+  //     return false
222
+  //   }
223
+  //   return true
224
+  // }
225
+  //
226
+  // _loadEncryptionKey () {
227
+  //   const encryptionKeyPath = path.join(this.config.root, 'groups', this.address.toString('hex'), 'encryption_key')
228
+  //   let encryptionKey
229
+  //   try {
230
+  //     encryptionKey = fs.readFileSync(encryptionKeyPath)
231
+  //   } catch (err) {
232
+  //     return false
233
+  //   }
234
+  //   return encryptionKey
235
+  // }
208 236
 }
209 237
 
210 238
 function setupLevel (levelPath) {

+ 52
- 30
test/index.test.js View File

@@ -37,37 +37,38 @@ describe('basic', (context) => {
37 37
     cleanup(storage, next)
38 38
   })
39 39
 
40
-  context('Group stores encryption key', (assert, next) => {
41
-    const config = Config(storage)
42
-    const address = crypto.address()
43
-    const encryptionKey = crypto.encryptionKey()
44
-    const group = Group(storage, address, { config, encryptionKey })
45
-    assert.ok(group, 'group loaded')
46
-    const encryptionKeyPath = path.join(config.root, 'groups', address.toString('hex'), 'encryption_key')
47
-    assert.equal(
48
-      fs.readFileSync(encryptionKeyPath).toString('hex'),
49
-      encryptionKey.toString('hex'),
50
-      'encryption key stored correctly'
51
-    )
52
-    cleanup(storage, next)
53
-  })
40
+  // context('Group stores encryption key', (assert, next) => {
41
+  //   const config = Config(storage)
42
+  //   const address = crypto.address()
43
+  //   const encryptionKey = crypto.encryptionKey()
44
+  //   const group = Group(storage, address, { config, encryptionKey })
45
+  //   assert.ok(group, 'group loaded')
46
+  //   const encryptionKeyPath = path.join(config.root, 'groups', address.toString('hex'), 'encryption_key')
47
+  //   assert.equal(
48
+  //     fs.readFileSync(encryptionKeyPath).toString('hex'),
49
+  //     encryptionKey.toString('hex'),
50
+  //     'encryption key stored correctly'
51
+  //   )
52
+  //   cleanup(storage, next)
53
+  // })
54
+  //
55
+  // context('Group loads encryption key', (assert, next) => {
56
+  //   const config = Config(storage)
57
+  //   const address = crypto.address()
58
+  //   const encryptionKey = crypto.encryptionKey()
59
+  //   const encryptionKeyPath = path.join(config.root, 'groups', address.toString('hex'), 'encryption_key')
60
+  //   mkdirp.sync(path.dirname(encryptionKeyPath))
61
+  //   fs.writeFileSync(encryptionKeyPath, encryptionKey, { mode: fs.constants.S_IRUSR })
62
+  //   const group = Group(storage, address, { config })
63
+  //   assert.ok(group, 'group loaded')
64
+  //   assert.equal(
65
+  //     group.encryptionKey.toString('hex'),
66
+  //     encryptionKey.toString('hex'),
67
+  //     'encryption key retrieved correctly'
68
+  //   )
69
+  //   cleanup(storage, next)
70
+  // })
54 71
 
55
-  context('Group loads encryption key', (assert, next) => {
56
-    const config = Config(storage)
57
-    const address = crypto.address()
58
-    const encryptionKey = crypto.encryptionKey()
59
-    const encryptionKeyPath = path.join(config.root, 'groups', address.toString('hex'), 'encryption_key')
60
-    mkdirp.sync(path.dirname(encryptionKeyPath))
61
-    fs.writeFileSync(encryptionKeyPath, encryptionKey, { mode: fs.constants.S_IRUSR })
62
-    const group = Group(storage, address, { config })
63
-    assert.ok(group, 'group loaded')
64
-    assert.equal(
65
-      group.encryptionKey.toString('hex'),
66
-      encryptionKey.toString('hex'),
67
-      'encryption key retrieved correctly'
68
-    )
69
-    cleanup(storage, next)
70
-  })
71 72
   context('returns a name', (assert, next) => {
72 73
     var config = Config(storage)
73 74
     var address = crypto.address()
@@ -169,6 +170,26 @@ describe('basic', (context) => {
169 170
       })
170 171
     })
171 172
   })
173
+
174
+  context('ls drive', (assert, next) => {
175
+    const config = Config(storage)
176
+    const address = crypto.address()
177
+    const group = Group(storage, address, { config })
178
+    assert.ok(group, 'group loaded')
179
+    group.ready(() => {
180
+      group.drive.writeFile('/hello.txt', 'world', (err) => {
181
+        assert.error(err, 'no error on writeFile')
182
+        group.ls().then((dirObj) => {
183
+          assert.equal(Object.keys(dirObj)[0], 'hello.txt', 'lists file')
184
+          assert.true(dirObj['hello.txt'].mtime > 10000, 'file has a timestamp')
185
+          cleanup(storage, next)
186
+        }, (err) => {
187
+          assert.error(err, 'no error')
188
+          cleanup(storage, next)
189
+        })
190
+      })
191
+    })
192
+  })
172 193
 })
173 194
 
174 195
 describe('connection', (context) => {
@@ -269,6 +290,7 @@ describe('connection', (context) => {
269 290
       }
270 291
     })
271 292
   })
293
+  
272 294
 })
273 295
 
274 296
 describe('replication', (context) => {

+ 25
- 0
test/mock.js View File

@@ -61,6 +61,31 @@ class Group {
61 61
   save () {
62 62
     return true
63 63
   }
64
+
65
+  async ls (subdir) {
66
+    return {
67
+      'hello.txt': {
68
+        dev: 0,
69
+        nlink: 1,
70
+        rdev: 0,
71
+        blksize: 0,
72
+        ino: 0,
73
+        mode: 33188,
74
+        uid: 0,
75
+        gid: 0,
76
+        size: 45,
77
+        offset: 0,
78
+        byteOffset: 0,
79
+        blocks: 1,
80
+        atime: 1576081505941,
81
+        mtime: 1576081505923,
82
+        ctime: 1576081505923,
83
+        linkname: null,
84
+        mount: null,
85
+        metadata: {}
86
+      }
87
+    }
88
+  }
64 89
 }
65 90
 
66 91
 async function noop () {}

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