Browse Source

Merge branch 'fix-too-many-event-listeners-bug' of CoBox/kappa-drive-mount into master

kyphae 3 years ago
parent
commit
7fc3370b92
2 changed files with 81 additions and 57 deletions
  1. 29
    23
      index.js
  2. 52
    34
      test/index.test.js

+ 29
- 23
index.js View File

@@ -1,35 +1,41 @@
1 1
 const rimraf = require('rimraf')
2
-const { mount, unmount } = require('hyperdrive-fuse')
2
+const hyperdriveFuse = require('hyperdrive-fuse')
3 3
 const mkdirp = require('mkdirp')
4 4
 
5
-module.exports = Mount
6
-module.exports.cleanup = cleanup
7
-module.exports.unmount = Unmount
5
+// module.exports = Mount
6
+// module.exports.cleanup = cleanup
7
+// module.exports.unmount = Unmount
8
+module.exports = () => new MountDrive()
9
+class MountDrive {
10
+  async mount (drive, dest) {
11
+    mkdirp.sync(dest)
12
+    var { destroy } = await hyperdriveFuse.mount(drive, dest)
13
+    this.destroy = destroy
14
+    this.dest = dest
8 15
 
9
-async function Mount (drive, dest) {
10
-  mkdirp.sync(dest)
11
-  var { destroy } = await mount(drive, dest)
16
+    process.once('SIGINT', this.callCleanup)
17
+    return destroy
18
+  }
12 19
 
13
-  process.once('SIGINT', () => (
14
-    cleanup(destroy, dest)
15
-  ))
20
+  callCleanup () {
21
+    this.cleanup(this.destroy, this.dest)
22
+  }
16 23
 
17
-  return destroy
18
-}
19
-
20
-function cleanup (destroy, dest) {
21
-  return new Promise((resolve, reject) => {
22
-    destroy(err => {
23
-      if (err) return reject(err)
24
-      rimraf(dest, err => {
24
+  cleanup (destroy, dest) {
25
+    return new Promise((resolve, reject) => {
26
+      destroy(err => {
25 27
         if (err) return reject(err)
26
-        return resolve()
28
+        process.removeListener('SIGINT', this.callCleanup)
29
+        rimraf(dest, err => {
30
+          if (err) return reject(err)
31
+          return resolve()
32
+        })
27 33
       })
28 34
     })
29
-  })
35
+  }
30 36
 }
31 37
 
32 38
 // TODO this should include cleanup
33
-function Unmount (dest, cb) {
34
-  return unmount(dest, cb)
35
-}
39
+// function Unmount (dest, cb) {
40
+//   return unmount(dest, cb)
41
+// }

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

@@ -1,8 +1,7 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const ram = require('random-access-memory')
3 3
 const fs = require('fs')
4
-const mount = require('../')
5
-const cleanup = mount.cleanup
4
+const MountDrive = require('../')
6 5
 const KappaDrive = require('kappa-drive')
7 6
 const path = require('path')
8 7
 
@@ -12,19 +11,20 @@ const { replicate, tmp, run } = require('./util')
12 11
 
13 12
 describe('mount', (context) => {
14 13
   context('read mounted directory', (assert, next) => {
14
+    var mountDrive = MountDrive()
15 15
     var drive = KappaDrive(ram)
16 16
     var storage = tmp()
17 17
     drive.ready(() => {
18 18
       drive.writeFile('/hello.txt', 'world', (err) => {
19 19
         assert.error(err, 'no error')
20
-        mount(drive, storage).then((destroy) => {
20
+        mountDrive.mount(drive, storage).then((destroy) => {
21 21
           fs.readdir(storage, (err, files) => {
22 22
             assert.error(err, 'no error')
23 23
             assert.same(files, ['hello.txt'], 'readdir returns the written file')
24 24
             fs.readFile(path.join(storage, 'hello.txt'), 'utf-8', (err, data) => {
25 25
               assert.error(err, 'no error')
26 26
               assert.same(data, 'world', 'the file has the correct content')
27
-              cleanup(destroy, storage).then(next)
27
+              mountDrive.cleanup(destroy, storage).then(next)
28 28
             })
29 29
           })
30 30
         })
@@ -33,16 +33,17 @@ describe('mount', (context) => {
33 33
   })
34 34
 
35 35
   context('write to mounted directory', (assert, next) => {
36
+    var mountDrive = MountDrive()
36 37
     var drive = KappaDrive(ram)
37 38
     var storage = tmp()
38 39
     drive.ready(() => {
39
-      mount(drive, storage).then((destroy) => {
40
+      mountDrive.mount(drive, storage).then((destroy) => {
40 41
         fs.writeFile(path.join(storage, 'hello.txt'), 'world', (err) => {
41 42
           assert.error(err, 'no error')
42 43
           drive.readdir('/', (err, files) => {
43 44
             assert.error(err, 'no error')
44 45
             assert.same(files, ['hello.txt'], 'drive.readdir returns the written file')
45
-            cleanup(destroy, storage).then(next)
46
+            mountDrive.cleanup(destroy, storage).then(next)
46 47
           })
47 48
         })
48 49
       })
@@ -50,16 +51,17 @@ describe('mount', (context) => {
50 51
   })
51 52
 
52 53
   context('mkdir in mounted directory', (assert, next) => {
54
+    var mountDrive = MountDrive()
53 55
     var drive = KappaDrive(ram)
54 56
     var storage = tmp()
55 57
     drive.ready(() => {
56
-      mount(drive, storage).then((destroy) => {
58
+      mountDrive.mount(drive, storage).then((destroy) => {
57 59
         fs.mkdir(path.join(storage, 'antelopes'), (err) => {
58 60
           assert.error(err, 'no error')
59 61
           drive.readdir('/', (err, files) => {
60 62
             assert.error(err, 'no error')
61 63
             assert.same(files, ['antelopes'], 'drive.readdir returns directory name')
62
-            cleanup(destroy, storage).then(next)
64
+            mountDrive.cleanup(destroy, storage).then(next)
63 65
           })
64 66
         })
65 67
       })
@@ -67,10 +69,11 @@ describe('mount', (context) => {
67 69
   })
68 70
 
69 71
   context('readdir in mounted newly created directory', (assert, next) => {
72
+    var mountDrive = MountDrive()
70 73
     var drive = KappaDrive(ram)
71 74
     var storage = tmp()
72 75
     drive.ready(() => {
73
-      mount(drive, storage).then((destroy) => {
76
+      mountDrive.mount(drive, storage).then((destroy) => {
74 77
         fs.mkdir(path.join(storage, 'antelopes'), (err) => {
75 78
           assert.error(err, 'no error')
76 79
           fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
@@ -81,7 +84,7 @@ describe('mount', (context) => {
81 84
               fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
82 85
                 assert.error(err, 'no error')
83 86
                 assert.same(files, ['tree.txt'], 'drive.readdir returns directory name')
84
-                cleanup(destroy, storage).then(next)
87
+                mountDrive.cleanup(destroy, storage).then(next)
85 88
               })
86 89
             })
87 90
           })
@@ -91,10 +94,11 @@ describe('mount', (context) => {
91 94
   })
92 95
 
93 96
   context('unlink in mounted directory', (assert, next) => {
97
+    var mountDrive = MountDrive()
94 98
     var drive = KappaDrive(tmp())
95 99
     var storage = tmp()
96 100
     drive.ready(() => {
97
-      mount(drive, storage).then((destroy) => {
101
+      mountDrive.mount(drive, storage).then((destroy) => {
98 102
         fs.writeFile(path.join(storage, 'tree.txt'), 'is this a tree', (err) => {
99 103
           assert.error(err, 'no error')
100 104
           fs.readdir(storage, (err, files) => {
@@ -105,7 +109,7 @@ describe('mount', (context) => {
105 109
               fs.readdir(storage, (err, files) => {
106 110
                 assert.error(err, 'no error')
107 111
                 assert.same(files, [], 'drive.readdir returns nothing')
108
-                cleanup(destroy, storage).then(next)
112
+                mountDrive.cleanup(destroy, storage).then(next)
109 113
               })
110 114
             })
111 115
           })
@@ -124,10 +128,12 @@ describe('mount', (context) => {
124 128
 
125 129
     var storage = {}
126 130
     var destroy = {}
131
+    var mountDrive = {}
127 132
 
128 133
     function setup (drive, colour, cb) {
129 134
       storage[colour] = tmp()
130
-      mount(drive, storage[colour]).then((destroyFn) => {
135
+      mountDrive[colour] = MountDrive()
136
+      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
131 137
         destroy[colour] = destroyFn
132 138
         drive.ready(cb)
133 139
       })
@@ -147,8 +153,8 @@ describe('mount', (context) => {
147 153
       fs.readdir(storage.blue, (err, files) => {
148 154
         assert.error(err, 'no error on readdir')
149 155
         assert.same(files, ['tree.txt'], 'fs.readdir returns file')
150
-        cleanup(destroy.orange, storage.orange).then(() => {
151
-          cleanup(destroy.blue, storage.blue).then(next)
156
+        mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
157
+          mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
152 158
         })
153 159
       })
154 160
     }
@@ -164,10 +170,12 @@ describe('mount', (context) => {
164 170
 
165 171
     var storage = {}
166 172
     var destroy = {}
173
+    var mountDrive = {}
167 174
 
168 175
     function setup (drive, colour, cb) {
169 176
       storage[colour] = tmp()
170
-      mount(drive, storage[colour]).then((destroyFn) => {
177
+      mountDrive[colour] = MountDrive()
178
+      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
171 179
         destroy[colour] = destroyFn
172 180
         drive.ready(cb)
173 181
       })
@@ -202,8 +210,8 @@ describe('mount', (context) => {
202 210
               run(`cat ${storage.orange}/hello.txt`, (err, output) => {
203 211
                 assert.error(err, 'no error on cat out file')
204 212
                 assert.same(output, ['what noise does a dog make?\nWOOF\n'], 'woof has correctly replicated')
205
-                cleanup(destroy.orange, storage.orange).then(() => {
206
-                  cleanup(destroy.blue, storage.blue).then(next)
213
+                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
214
+                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
207 215
                 })
208 216
               })
209 217
             })
@@ -223,10 +231,12 @@ describe('mount', (context) => {
223 231
 
224 232
     var storage = {}
225 233
     var destroy = {}
234
+    var mountDrive = {}
226 235
 
227 236
     function setup (drive, colour, cb) {
228 237
       storage[colour] = tmp()
229
-      mount(drive, storage[colour]).then((destroyFn) => {
238
+      mountDrive[colour] = MountDrive()
239
+      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
230 240
         destroy[colour] = destroyFn
231 241
         drive.ready(cb)
232 242
       })
@@ -259,10 +269,10 @@ describe('mount', (context) => {
259 269
               assert.error(err, 'no error on replicate')
260 270
               run(`ls ${storage.orange}`, (err, output) => {
261 271
                 assert.error(err, 'no error')
262
-                //TODO this line should also use .split:
272
+                // TODO this line should also use .split:
263 273
                 assert.ok(output.indexOf('hello.txt\n') < 0, 'ls does not find the file')
264
-                cleanup(destroy.orange, storage.orange).then(() => {
265
-                  cleanup(destroy.blue, storage.blue).then(next)
274
+                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
275
+                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
266 276
                 })
267 277
               })
268 278
             })
@@ -282,10 +292,12 @@ describe('mount', (context) => {
282 292
 
283 293
     var storage = {}
284 294
     var destroy = {}
295
+    var mountDrive = {}
285 296
 
286 297
     function setup (drive, colour, cb) {
287 298
       storage[colour] = tmp()
288
-      mount(drive, storage[colour]).then((destroyFn) => {
299
+      mountDrive[colour] = MountDrive()
300
+      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
289 301
         destroy[colour] = destroyFn
290 302
         drive.ready(cb)
291 303
       })
@@ -319,8 +331,8 @@ describe('mount', (context) => {
319 331
               run(`ls ${storage.orange}`, (err, output) => {
320 332
                 assert.error(err, 'no error')
321 333
                 assert.false(output[0].split('\n').indexOf('cabbage') > -1, 'ls no longer lists dir')
322
-                cleanup(destroy.orange, storage.orange).then(() => {
323
-                  cleanup(destroy.blue, storage.blue).then(next)
334
+                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
335
+                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
324 336
                 })
325 337
               })
326 338
             })
@@ -340,10 +352,12 @@ describe('mount', (context) => {
340 352
 
341 353
     var storage = {}
342 354
     var destroy = {}
355
+    var mountDrive = {}
343 356
 
344 357
     function setup (drive, colour, cb) {
345 358
       storage[colour] = tmp()
346
-      mount(drive, storage[colour]).then((destroyFn) => {
359
+      mountDrive[colour] = MountDrive()
360
+      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
347 361
         destroy[colour] = destroyFn
348 362
         drive.ready(cb)
349 363
       })
@@ -372,8 +386,8 @@ describe('mount', (context) => {
372 386
           fs.readFile(path.join(storage.blue, 'hello.txt'), 'utf-8', (err, data) => {
373 387
             assert.error(err, 'no error on read file')
374 388
             assert.same(data, 'what noise', 'returns expected contents')
375
-            cleanup(destroy.orange, storage.orange).then(() => {
376
-              cleanup(destroy.blue, storage.blue).then(next)
389
+            mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
390
+              mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
377 391
             })
378 392
           })
379 393
         })
@@ -391,10 +405,12 @@ describe('mount', (context) => {
391 405
 
392 406
     var storage = {}
393 407
     var destroy = {}
408
+    var mountDrive = {}
394 409
 
395 410
     function setup (drive, colour, cb) {
396 411
       storage[colour] = tmp()
397
-      mount(drive, storage[colour]).then((destroyFn) => {
412
+      mountDrive[colour] = MountDrive()
413
+      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
398 414
         destroy[colour] = destroyFn
399 415
         drive.ready(() => {
400 416
           cb()
@@ -430,8 +446,8 @@ describe('mount', (context) => {
430 446
               run(`cat ${storage.orange}/hello.txt`, (err, output) => {
431 447
                 assert.error(err, 'no error on cat out file')
432 448
                 assert.same(output, ['WOOF\n'], 'woof has correctly replicated')
433
-                cleanup(destroy.orange, storage.orange).then(() => {
434
-                  cleanup(destroy.blue, storage.blue).then(next)
449
+                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
450
+                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
435 451
                 })
436 452
               })
437 453
             })
@@ -452,10 +468,12 @@ describe('mount', (context) => {
452 468
 
453 469
     var storage = {}
454 470
     var destroy = {}
471
+    var mountDrive = {}
455 472
 
456 473
     function setup (drive, colour, cb) {
457 474
       storage[colour] = tmp()
458
-      mount(drive, storage[colour]).then((destroyFn) => {
475
+      mountDrive[colour] = MountDrive()
476
+      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
459 477
         destroy[colour] = destroyFn
460 478
         drive.ready(cb)
461 479
       })
@@ -496,8 +514,8 @@ describe('mount', (context) => {
496 514
                             fs.readFile(path.join(storage.yellow, 'hello.txt'), 'utf-8', (err, data) => {
497 515
                               assert.error(err, 'no error')
498 516
                               assert.same(data, 'mundo', 'data corrently overwritten and replicted')
499
-                              cleanup(destroy.yellow, storage.yellow).then(() => {
500
-                                cleanup(destroy.purple, storage.purple).then(next)
517
+                              mountDrive.yellow.cleanup(destroy.yellow, storage.yellow).then(() => {
518
+                                mountDrive.purple.cleanup(destroy.purple, storage.purple).then(next)
501 519
                               })
502 520
                             })
503 521
                           })