Browse Source

correct callback

Kieran Gibb 3 years ago
parent
commit
5f89dd8168
No account linked to committer's email address
2 changed files with 36 additions and 17 deletions
  1. 5
    5
      test/index.test.js
  2. 31
    12
      test/util.js

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

@@ -118,12 +118,12 @@ describe('hypercore', (context) => {
118 118
   context('encrypted the log', (assert, next) => {
119 119
     const key = crypto.symmetricKey()
120 120
     const encoder = crypto.encoder(key, { valueEncoding: 'utf-8' })
121
-    const feed = hypercore(storage.path, { valueEncoding: encoder })
121
+    const feed = hypercore(storage, { valueEncoding: encoder })
122 122
 
123 123
     feed.append('boop', (err) => {
124 124
       assert.error(err, 'no error')
125 125
 
126
-      var data = fs.readFileSync(path.join(storage.path, 'data'))
126
+      var data = fs.readFileSync(path.join(storage, 'data'))
127 127
       assert.notSame(data, 'boop', 'log entry is encrypted')
128 128
       assert.same(encoder.decode(data), 'boop', 'log entry is encrypted')
129 129
 
@@ -131,7 +131,7 @@ describe('hypercore', (context) => {
131 131
         assert.error(err, 'no error')
132 132
         assert.same('boop', entry, 'hypercore decrypts the message')
133 133
 
134
-        cleanup(storage.root, next)
134
+        cleanup(storage, next)
135 135
       })
136 136
     })
137 137
   })
@@ -139,7 +139,7 @@ describe('hypercore', (context) => {
139 139
   context('encrypted the log, with a json object', (assert, next) => {
140 140
     const key = crypto.symmetricKey()
141 141
     const encoder = crypto.encoder(key, { valueEncoding: 'json' })
142
-    const feed = hypercore(storage.path, { valueEncoding: encoder })
142
+    const feed = hypercore(storage, { valueEncoding: encoder })
143 143
 
144 144
     const message = { boop: 'beep' }
145 145
 
@@ -150,7 +150,7 @@ describe('hypercore', (context) => {
150 150
         assert.error(err, 'no error')
151 151
         assert.same(message, entry, 'hypercore decrypts the message')
152 152
 
153
-        cleanup(storage.root, next)
153
+        cleanup(storage, next)
154 154
       })
155 155
     })
156 156
   })

+ 31
- 12
test/util.js View File

@@ -1,22 +1,41 @@
1 1
 const tmpdir = require('tmp').dirSync
2 2
 const mkdirp = require('mkdirp')
3 3
 const rimraf = require('rimraf')
4
-const debug = require('debug')('cobox-crypto')
5
-
6
-function cleanup (dir, cb) {
7
-  rimraf(dir, (err) => {
8
-    debug(`[CLEANUP] cleaning ${dir} ${ err ? 'failed' : 'success'}`)
9
-    if (err) return console.error(err)
10
-    cb()
11
-  })
4
+
5
+function cleanup (dirs, cb) {
6
+  if (!cb) cb = noop
7
+  if (!Array.isArray(dirs)) dirs = [dirs]
8
+  var pending = 1
9
+
10
+  function next (n) {
11
+    var dir = dirs[n]
12
+    if (!dir) return done()
13
+    ++pending
14
+    process.nextTick(next, n + 1)
15
+
16
+    rimraf(dir, (err) => {
17
+      if (err) return done(err)
18
+      done()
19
+    })
20
+  }
21
+
22
+  function done (err) {
23
+    if (err) {
24
+      pending = Infinity
25
+      return cb(err)
26
+    }
27
+    if (!--pending) return cb()
28
+  }
29
+
30
+  next(0)
12 31
 }
13 32
 
14 33
 function tmp () {
15
-  var root = `./tmp/`
16
-  var path = `.${tmpdir().name}`
34
+  var path = tmpdir().name
17 35
   mkdirp.sync(path)
18
-  debug(`[TEMP] generated temp directory ${path}`)
19
-  return { path, root }
36
+  return path
20 37
 }
21 38
 
39
+function noop () {}
40
+
22 41
 module.exports = { cleanup, tmp }