keyv-fs
Version:
file system storage adapter for keyv
222 lines (196 loc) • 6.78 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var EventEmitter = require('events');
var join = require('path').join;
var localFs = require('fs');
var rimraf = require('rimraf');
var pify = require('pify');
var rimrafPified = pify(rimraf);
var KeyvFs = function (_EventEmitter) {
_inherits(KeyvFs, _EventEmitter);
function KeyvFs(fs, rootDir, clean) {
_classCallCheck(this, KeyvFs);
var _this = _possibleConstructorReturn(this, (KeyvFs.__proto__ || Object.getPrototypeOf(KeyvFs)).call(this));
_this.folder = null;
_this.namespaceLength = 0;
if (fs) {
if (!fs.lstat) {
if (fs.stat) {
fs.lstat = fs.stat;
} else {
_this.throwError('method stat/lstat is required');
}
}
} else {
fs = localFs;
}
if (rootDir) {
if (!fs.existsSync(rootDir)) {
_this.throwError('rootDir directory doesn\'t exist, ' + rootDir);
}
} else {
rootDir = process.cwd();
}
var apiResult = fs.lstat('/abc', function () {});
_this.fs = {};
if (!apiResult || typeof apiResult.then !== 'function') {
['lstat', 'readFile', 'writeFile', 'unlink', 'rmdir', 'mkdir'].forEach(function (method) {
if (!fs[method]) {
_this.throwError('file system doesn\'t support method - ' + method);
}
_this.fs[method] = pify(fs[method].bind(fs));
});
}
_this.rootDir = rootDir;
_this.fsOriginal = fs;
_this.clean = clean; // Clear existing folder
return _this;
}
_createClass(KeyvFs, [{
key: 'throwError',
value: function throwError(msg) {
throw new Error('keyv-fs - ' + msg);
}
}, {
key: 'createMeta',
value: function createMeta() {
var _this2 = this;
return Promise.resolve().then(function () {
_this2.meta = {};
return _this2.fs.writeFile(_this2.metaFilePath, '{}');
});
}
}, {
key: 'updateMetaFile',
value: function updateMetaFile() {
var _this3 = this;
return Promise.resolve().then(function () {
return _this3.fs.writeFile(_this3.metaFilePath, JSON.stringify(_this3.meta));
});
}
}, {
key: 'checkFolder',
value: function checkFolder() {
var _this4 = this;
// This can be prevented if `keyv` exposes its namespace at initialization.
return Promise.resolve().then(function () {
if (_this4.folder) {
return;
}
_this4.namespaceLength = _this4.namespace.length + 1;
_this4.folder = join(_this4.rootDir, _this4.namespace);
_this4.metaFilePath = join(_this4.folder, '.meta');
_this4.meta = {};
return _this4.fs.lstat(_this4.folder).then(function () {
return _this4.fs.lstat(_this4.metaFilePath).then(function () {
if (_this4.clean) {
return _this4.clear();
}
return _this4.fs.readFile(_this4.metaFilePath).then(function (ctn) {
if (ctn) {
_this4.meta = JSON.parse(ctn);
} else {
_this4.meta = {};
}
});
}, function () {
return _this4.createMeta();
});
}, function () {
return _this4.fs.mkdir(_this4.folder).then(function () {
return _this4.createMeta();
});
});
});
}
}, {
key: 'generateFilename',
value: function generateFilename() {
return new Date().valueOf().toString(16) + Math.random().toString(16).substr(2, 4);
}
}, {
key: 'getFilePath',
value: function getFilePath(key) {
var filepath = key.substr(this.namespaceLength);
var filename = void 0;
if (Object.prototype.hasOwnProperty.call(this.meta, filepath)) {
filename = this.meta[filepath];
} else {
filename = this.generateFilename();
this.meta[filepath] = filename;
this.updateMetaFile();
}
return join(this.folder, filename);
}
}, {
key: 'deletePath',
value: function deletePath(filepath) {
var _this5 = this;
return Promise.resolve().then(function () {
if (Object.prototype.hasOwnProperty.call(_this5.meta, filepath)) {
delete _this5.meta[filepath];
return _this5.updateMetaFile();
}
});
}
}, {
key: 'clearPaths',
value: function clearPaths() {
this.meta = {};
return this.updateMetaFile();
}
}, {
key: 'get',
value: function get(key) {
var _this6 = this;
return this.checkFolder().then(function () {
var filepath = _this6.getFilePath(key);
return _this6.fs.readFile(filepath).then(function (buf) {
debugger
return buf.toString();
}, function () {});
});
}
}, {
key: 'set',
value: function set(key, value) {
var _this7 = this;
return this.checkFolder().then(function () {
var filepath = _this7.getFilePath(key);
return _this7.fs.writeFile(filepath, value);
});
}
}, {
key: 'delete',
value: function _delete(key) {
var _this8 = this;
return this.checkFolder().then(function () {
var filepath = _this8.getFilePath(key);
return _this8.deletePath(filepath).then(function () {
return _this8.fs.unlink(filepath).then(function () {
return true;
}, function () {
return false;
});
});
});
}
}, {
key: 'clear',
value: function clear() {
var _this9 = this;
return this.checkFolder().then(function () {
return rimrafPified(_this9.folder, _this9.fs);
}).then(function () {
return _this9.fs.lstat(_this9.folder);
}).then(function () {}, function () {
return _this9.fs.mkdir(_this9.folder);
});
}
}]);
return KeyvFs;
}(EventEmitter);
module.exports = KeyvFs;