Skip to content

Commit

Permalink
fix: delete residual folders by specifying relative file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Jul 18, 2021
1 parent fe3bcc5 commit 4a5f1ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ var chunks = new FSChunkStore(10, {
{ path: 'folder/file1.txt', length: 12 },
{ path: 'folder/file2.txt', length: 8 },
{ path: 'folder/file3.txt', length: 30 }
]
],
path: 'C:/user/' // optional: if specified the file paths will be treated as relative, not absolute
})
```
Specifying a path to the store will create a folder in that path, and on destroy, will delete the folder along with all it's contents

### put, get, close, destroy

Expand Down
26 changes: 18 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const path = require('path')
const queueMicrotask = require('queue-microtask')
const raf = require('random-access-file')
const randombytes = require('randombytes')
const rimraf = require('rimraf')
const thunky = require('thunky')

let TMP
Expand All @@ -25,8 +24,10 @@ function Storage (chunkLength, opts) {

self.chunkLength = Number(chunkLength)
if (!self.chunkLength) throw new Error('First argument must be a chunk length')
self.name = opts.name || path.join('fs-chunk-store', randombytes(20).toString('hex'))

if (opts.files) {
self.path = opts.path
if (!Array.isArray(opts.files)) {
throw new Error('`files` option must be an array')
}
Expand All @@ -41,6 +42,7 @@ function Storage (chunkLength, opts) {
file.offset = prevFile.offset + prevFile.length
}
}
if (self.path) file.path = path.resolve(path.join(self.path, file.path))
return file
})
self.length = self.files.reduce(function (sum, file) { return sum + file.length }, 0)
Expand All @@ -51,7 +53,7 @@ function Storage (chunkLength, opts) {
const len = Number(opts.length) || Infinity
self.files = [{
offset: 0,
path: path.resolve(opts.path || path.join(TMP, 'fs-chunk-store', randombytes(20).toString('hex'))),
path: path.resolve(opts.path || path.join(TMP, self.name)),
length: len
}]
self.length = len
Expand Down Expand Up @@ -223,12 +225,20 @@ Storage.prototype.close = function (cb) {
Storage.prototype.destroy = function (cb) {
const self = this
self.close(function () {
const tasks = self.files.map(function (file) {
return function (cb) {
rimraf(file.path, { maxBusyTries: 10 }, cb)
}
})
parallel(tasks, cb)
if (self.path) {
fs.rm(path.resolve(self.path), { recursive: true, maxRetries: 10 }, function (err) {
err && err.code === 'ENOENT' ? cb() : cb(err)
})
} else {
const tasks = self.files.map(function (file) {
return function (cb) {
fs.rm(file.path, { recursive: true, maxRetries: 10 }, function (err) {
err && err.code === 'ENOENT' ? cb() : cb(err)
})
}
})
parallel(tasks, cb)
}
})
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"queue-microtask": "^1.2.2",
"random-access-file": "^2.0.1",
"randombytes": "^2.0.3",
"rimraf": "^3.0.0",
"run-parallel": "^1.1.2",
"thunky": "^1.0.1"
},
Expand Down

0 comments on commit 4a5f1ed

Please sign in to comment.