From 4a5f1ed90232ab0ddfe17b61c1c46dd437eb22f1 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Sun, 18 Jul 2021 10:39:12 +0200 Subject: [PATCH] fix: delete residual folders by specifying relative file paths --- README.md | 4 +++- index.js | 26 ++++++++++++++++++-------- package.json | 1 - 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7c37d5f..25ddcfe 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 0cbc35d..b106747 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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') } @@ -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) @@ -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 @@ -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) + } }) } diff --git a/package.json b/package.json index db3439b..5823ec4 100644 --- a/package.json +++ b/package.json @@ -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" },