Comparing version 0.2.8 to 0.2.9
# Disklet | ||
## 0.2.9 (2018-11-12) | ||
* Add complete Flow typing coverage. | ||
* Fix binary data on React Native (`setData` / `getData` were corrupting data before). | ||
## 0.2.8 (2018-11-03) | ||
@@ -4,0 +9,0 @@ |
@@ -12,86 +12,2 @@ 'use strict'; | ||
/** | ||
* Interprets a path as a series of folder lookups, | ||
* handling special components like `.` and `..`. | ||
*/ | ||
function followPath(folder, parts) { | ||
var i = 0; // Read index | ||
var j = 0; // Write index | ||
// Shift down good elements, dropping bad ones: | ||
while (i < parts.length) { | ||
var part = parts[i++]; | ||
if (part === '..') j--;else if (part !== '.' && part !== '') parts[j++] = part; | ||
if (j < 0) throw new Error('Path would escape folder'); | ||
} // Navigate the folder: | ||
for (i = 0; i < j; ++i) { | ||
folder = folder.folder(parts[i]); | ||
} | ||
return folder; | ||
} | ||
/** | ||
* Navigates down to the file indicated by the path. | ||
*/ | ||
function locateFile(folder, path) { | ||
var parts = path.split('/'); | ||
var filename = parts.pop(); | ||
return followPath(folder, parts).file(filename); | ||
} | ||
/** | ||
* Navigates down to the sub-folder indicated by the path. | ||
*/ | ||
function locateFolder(folder, path) { | ||
var parts = path.split('/'); | ||
return followPath(folder, parts); | ||
} | ||
/** | ||
* Applies an async function to all the files in a folder. | ||
*/ | ||
function mapFiles(folder, f) { | ||
return folder.listFiles().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.file(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Applies an async function to all the sub-folders in a folder. | ||
*/ | ||
function mapFolders(folder, f) { | ||
return folder.listFolders().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.folder(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Recursively applies an async function to all the files in a folder tree. | ||
* The file names are expanded into paths, and the result is a flat array. | ||
*/ | ||
function mapAllFiles(folder, f) { | ||
function recurse(folder, f, prefix) { | ||
return Promise.all([mapFiles(folder, function (file, name) { | ||
return f(file, prefix + name, folder); | ||
}), mapFolders(folder, function (folder, name) { | ||
return recurse(folder, f, prefix + name + '/'); | ||
})]).then(function (_ref) { | ||
var files = _ref[0], | ||
folders = _ref[1]; | ||
return files.concat.apply(files, folders); | ||
}); | ||
} | ||
return recurse(folder, f, ''); | ||
} | ||
/** | ||
* Verifies that a name contains no slashes. | ||
@@ -111,9 +27,10 @@ */ | ||
function storageKeys(storage) { | ||
var keys = []; | ||
var out = []; | ||
for (var i = 0; i < storage.length; ++i) { | ||
keys.push(storage.key(i)); | ||
var key = storage.key(i); | ||
if (key) out.push(key); | ||
} | ||
return keys; | ||
return out; | ||
} | ||
@@ -244,110 +161,11 @@ /** | ||
return new LocalStorageFolder(storage, opts.prefix || ''); | ||
var _opts = opts, | ||
_opts$prefix = _opts.prefix, | ||
prefix = _opts$prefix === void 0 ? '' : _opts$prefix; | ||
return new LocalStorageFolder(storage, prefix); | ||
} | ||
function logConsole(path, operation) { | ||
console.log(operation + " \"" + path + "\""); | ||
} | ||
function log(path, operation, opts) { | ||
var f = opts.callback != null ? opts.callback : logConsole; | ||
if (opts.verbose || /set|delete/.test(operation)) { | ||
f(path, operation); | ||
} | ||
} | ||
var LoggedFile = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFile(file, path, opts) { | ||
this._file = file; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto = LoggedFile.prototype; | ||
_proto.delete = function _delete() { | ||
log(this._path, 'delete file', this._opts); | ||
return this._file.delete(); | ||
}; | ||
_proto.getData = function getData() { | ||
log(this._path, 'get data', this._opts); | ||
return this._file.getData(); | ||
}; | ||
_proto.getText = function getText() { | ||
log(this._path, 'get text', this._opts); | ||
return this._file.getText(); | ||
}; | ||
_proto.setData = function setData(data) { | ||
log(this._path, 'set data', this._opts); | ||
return this._file.setData(data); | ||
}; | ||
_proto.setText = function setText(text) { | ||
log(this._path, 'set text', this._opts); | ||
return this._file.setText(text); | ||
}; | ||
_proto.getPath = function getPath() { | ||
log(this._path, 'get path', this._opts); | ||
return this._file; | ||
}; | ||
return LoggedFile; | ||
}(); | ||
var LoggedFolder = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFolder(folder, path, opts) { | ||
this._folder = folder; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto2 = LoggedFolder.prototype; | ||
_proto2.delete = function _delete() { | ||
log(this._path, 'delete folder', this._opts); | ||
return this._folder.delete(); | ||
}; | ||
_proto2.file = function file(name) { | ||
return new LoggedFile(this._folder.file(name), this._path + name, this._opts); | ||
}; | ||
_proto2.folder = function folder(name) { | ||
return new LoggedFolder(this._folder.folder(name), this._path + name + '/', this._opts); | ||
}; | ||
_proto2.listFiles = function listFiles() { | ||
log(this._path, 'list files', this._opts); | ||
return this._folder.listFiles(); | ||
}; | ||
_proto2.listFolders = function listFolders() { | ||
log(this._path, 'list folders', this._opts); | ||
return this._folder.listFolders(); | ||
}; | ||
return LoggedFolder; | ||
}(); | ||
function makeLoggedFolder(folder, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new LoggedFolder(folder, '', opts); | ||
} | ||
/** | ||
* A single file stored in memory. | ||
*/ | ||
var MemoryFile = | ||
@@ -370,3 +188,12 @@ /*#__PURE__*/ | ||
var item = this._storage[this._path]; | ||
return item != null ? Promise.resolve(item) : Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
if (item == null) { | ||
return Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
} | ||
if (typeof item === 'string') { | ||
return Promise.reject(new Error("\"" + this._path + "\" is a text file.")); | ||
} | ||
return Promise.resolve(item); | ||
}; | ||
@@ -376,7 +203,18 @@ | ||
var item = this._storage[this._path]; | ||
return item != null ? Promise.resolve(item) : Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
if (item == null) { | ||
return Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
} | ||
if (typeof item !== 'string') { | ||
return Promise.reject(new Error("\"" + this._path + "\" is a binary file.")); | ||
} | ||
return Promise.resolve(item); | ||
}; | ||
_proto.setData = function setData(data) { | ||
this._storage[this._path] = data; | ||
// We use `any` here becase Flow is too dumb to know that `ArrayLike` | ||
var flowHack = data; | ||
this._storage[this._path] = Uint8Array.from(flowHack); | ||
return Promise.resolve(); | ||
@@ -470,3 +308,3 @@ }; | ||
return new Promise(function (resolve, reject) { | ||
return fs.mkdir(path, function (err) { | ||
return fs.mkdir(path, void 0, function (err) { | ||
return err && err.code !== 'EEXIST' ? reject(err) : resolve(); | ||
@@ -493,10 +331,2 @@ }); | ||
function readFile(path, opts) { | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(path, opts, function (err, out) { | ||
return err ? reject(err) : resolve(out); | ||
}); | ||
}); | ||
} | ||
function stat(path) { | ||
@@ -597,11 +427,24 @@ return new Promise(function (resolve, reject) { | ||
_proto.getData = function getData() { | ||
return readFile(this._path, null); | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(_this._path, {}, function (err, out) { | ||
return err ? reject(err) : resolve(out); | ||
}); | ||
}); | ||
}; | ||
_proto.getText = function getText() { | ||
return readFile(this._path, 'utf8'); | ||
var _this2 = this; | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(_this2._path, 'utf8', function (err, out) { | ||
return err ? reject(err) : resolve(out); | ||
}); | ||
}); | ||
}; | ||
_proto.setData = function setData(data) { | ||
return writeFileDeep(this._path, Uint8Array.from(data), null); | ||
var flowHack = data; | ||
return writeFileDeep(this._path, Buffer.from(flowHack), {}); | ||
}; | ||
@@ -630,3 +473,3 @@ | ||
_proto2.delete = function _delete() { | ||
var _this = this; | ||
var _this3 = this; | ||
@@ -644,8 +487,8 @@ return readdirStat(this._path).then(function (lists) { | ||
return Promise.all(files.map(function (name) { | ||
return _this.file(name).delete(); | ||
return _this3.file(name).delete(); | ||
}).concat(folders.map(function (name) { | ||
return _this.folder(name).delete(); | ||
return _this3.folder(name).delete(); | ||
}))); | ||
}).then(function () { | ||
return rmdir(_this._path); | ||
return rmdir(_this3._path); | ||
}).catch(ignoreMissing()); | ||
@@ -691,6 +534,190 @@ }; | ||
function makeReactNativeFolder(path) { | ||
function makeReactNativeFolder() { | ||
throw new Error('Not available on node.js'); | ||
} | ||
/** | ||
* Interprets a path as a series of folder lookups, | ||
* handling special components like `.` and `..`. | ||
*/ | ||
function followPath(folder, parts) { | ||
var i = 0; // Read index | ||
var j = 0; // Write index | ||
// Shift down good elements, dropping bad ones: | ||
while (i < parts.length) { | ||
var part = parts[i++]; | ||
if (part === '..') j--;else if (part !== '.' && part !== '') parts[j++] = part; | ||
if (j < 0) throw new Error('Path would escape folder'); | ||
} // Navigate the folder: | ||
for (i = 0; i < j; ++i) { | ||
folder = folder.folder(parts[i]); | ||
} | ||
return folder; | ||
} | ||
/** | ||
* Navigates down to the file indicated by the path. | ||
*/ | ||
function locateFile(folder, path) { | ||
var parts = path.split('/'); | ||
var filename = parts.pop(); | ||
return followPath(folder, parts).file(filename); | ||
} | ||
/** | ||
* Navigates down to the sub-folder indicated by the path. | ||
*/ | ||
function locateFolder(folder, path) { | ||
var parts = path.split('/'); | ||
return followPath(folder, parts); | ||
} | ||
/** | ||
* Applies an async function to all the files in a folder. | ||
*/ | ||
function mapFiles(folder, f) { | ||
return folder.listFiles().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.file(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Applies an async function to all the sub-folders in a folder. | ||
*/ | ||
function mapFolders(folder, f) { | ||
return folder.listFolders().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.folder(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Recursively applies an async function to all the files in a folder tree. | ||
* The file names are expanded into paths, and the result is a flat array. | ||
*/ | ||
function mapAllFiles(folder, f) { | ||
function recurse(folder, f, prefix) { | ||
return Promise.all([mapFiles(folder, function (file, name) { | ||
return f(file, prefix + name, folder); | ||
}), mapFolders(folder, function (folder, name) { | ||
return recurse(folder, f, prefix + name + '/'); | ||
})]).then(function (_ref) { | ||
var files = _ref[0], | ||
folders = _ref[1]; | ||
return files.concat.apply(files, folders); | ||
}); | ||
} | ||
return recurse(folder, f, ''); | ||
} | ||
function logConsole(path, operation) { | ||
console.log(operation + " \"" + path + "\""); | ||
} | ||
function log(path, operation, opts) { | ||
var f = opts.callback != null ? opts.callback : logConsole; | ||
if (opts.verbose || /set|delete/.test(operation)) { | ||
f(path, operation); | ||
} | ||
} | ||
var LoggedFile = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFile(file, path, opts) { | ||
this._file = file; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto = LoggedFile.prototype; | ||
_proto.delete = function _delete() { | ||
log(this._path, 'delete file', this._opts); | ||
return this._file.delete(); | ||
}; | ||
_proto.getData = function getData() { | ||
log(this._path, 'get data', this._opts); | ||
return this._file.getData(); | ||
}; | ||
_proto.getText = function getText() { | ||
log(this._path, 'get text', this._opts); | ||
return this._file.getText(); | ||
}; | ||
_proto.setData = function setData(data) { | ||
log(this._path, 'set data', this._opts); | ||
return this._file.setData(data); | ||
}; | ||
_proto.setText = function setText(text) { | ||
log(this._path, 'set text', this._opts); | ||
return this._file.setText(text); | ||
}; | ||
_proto.getPath = function getPath() { | ||
return this._path; | ||
}; | ||
return LoggedFile; | ||
}(); | ||
var LoggedFolder = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFolder(folder, path, opts) { | ||
this._folder = folder; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto2 = LoggedFolder.prototype; | ||
_proto2.delete = function _delete() { | ||
log(this._path, 'delete folder', this._opts); | ||
return this._folder.delete(); | ||
}; | ||
_proto2.file = function file(name) { | ||
return new LoggedFile(this._folder.file(name), this._path + name, this._opts); | ||
}; | ||
_proto2.folder = function folder(name) { | ||
return new LoggedFolder(this._folder.folder(name), this._path + name + '/', this._opts); | ||
}; | ||
_proto2.listFiles = function listFiles() { | ||
log(this._path, 'list files', this._opts); | ||
return this._folder.listFiles(); | ||
}; | ||
_proto2.listFolders = function listFolders() { | ||
log(this._path, 'list folders', this._opts); | ||
return this._folder.listFolders(); | ||
}; | ||
return LoggedFolder; | ||
}(); | ||
function makeLoggedFolder(folder, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new LoggedFolder(folder, '', opts); | ||
} | ||
function removeDuplicates(master, fallback) { | ||
@@ -824,5 +851,2 @@ var blacklist = {}; | ||
exports.locateFolder = locateFolder; | ||
exports.mapAllFiles = mapAllFiles; | ||
exports.mapFiles = mapFiles; | ||
exports.mapFolders = mapFolders; | ||
exports.makeLocalStorageFolder = makeLocalStorageFolder; | ||
@@ -834,2 +858,5 @@ exports.makeLoggedFolder = makeLoggedFolder; | ||
exports.makeUnionFolder = makeUnionFolder; | ||
exports.mapAllFiles = mapAllFiles; | ||
exports.mapFiles = mapFiles; | ||
exports.mapFolders = mapFolders; | ||
//# sourceMappingURL=disklet.cjs.js.map |
@@ -6,86 +6,2 @@ import { base64 } from 'rfc4648'; | ||
/** | ||
* Interprets a path as a series of folder lookups, | ||
* handling special components like `.` and `..`. | ||
*/ | ||
function followPath(folder, parts) { | ||
var i = 0; // Read index | ||
var j = 0; // Write index | ||
// Shift down good elements, dropping bad ones: | ||
while (i < parts.length) { | ||
var part = parts[i++]; | ||
if (part === '..') j--;else if (part !== '.' && part !== '') parts[j++] = part; | ||
if (j < 0) throw new Error('Path would escape folder'); | ||
} // Navigate the folder: | ||
for (i = 0; i < j; ++i) { | ||
folder = folder.folder(parts[i]); | ||
} | ||
return folder; | ||
} | ||
/** | ||
* Navigates down to the file indicated by the path. | ||
*/ | ||
function locateFile(folder, path) { | ||
var parts = path.split('/'); | ||
var filename = parts.pop(); | ||
return followPath(folder, parts).file(filename); | ||
} | ||
/** | ||
* Navigates down to the sub-folder indicated by the path. | ||
*/ | ||
function locateFolder(folder, path) { | ||
var parts = path.split('/'); | ||
return followPath(folder, parts); | ||
} | ||
/** | ||
* Applies an async function to all the files in a folder. | ||
*/ | ||
function mapFiles(folder, f) { | ||
return folder.listFiles().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.file(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Applies an async function to all the sub-folders in a folder. | ||
*/ | ||
function mapFolders(folder, f) { | ||
return folder.listFolders().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.folder(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Recursively applies an async function to all the files in a folder tree. | ||
* The file names are expanded into paths, and the result is a flat array. | ||
*/ | ||
function mapAllFiles(folder, f) { | ||
function recurse(folder, f, prefix) { | ||
return Promise.all([mapFiles(folder, function (file, name) { | ||
return f(file, prefix + name, folder); | ||
}), mapFolders(folder, function (folder, name) { | ||
return recurse(folder, f, prefix + name + '/'); | ||
})]).then(function (_ref) { | ||
var files = _ref[0], | ||
folders = _ref[1]; | ||
return files.concat.apply(files, folders); | ||
}); | ||
} | ||
return recurse(folder, f, ''); | ||
} | ||
/** | ||
* Verifies that a name contains no slashes. | ||
@@ -105,9 +21,10 @@ */ | ||
function storageKeys(storage) { | ||
var keys = []; | ||
var out = []; | ||
for (var i = 0; i < storage.length; ++i) { | ||
keys.push(storage.key(i)); | ||
var key = storage.key(i); | ||
if (key) out.push(key); | ||
} | ||
return keys; | ||
return out; | ||
} | ||
@@ -238,110 +155,11 @@ /** | ||
return new LocalStorageFolder(storage, opts.prefix || ''); | ||
var _opts = opts, | ||
_opts$prefix = _opts.prefix, | ||
prefix = _opts$prefix === void 0 ? '' : _opts$prefix; | ||
return new LocalStorageFolder(storage, prefix); | ||
} | ||
function logConsole(path, operation) { | ||
console.log(operation + " \"" + path + "\""); | ||
} | ||
function log(path, operation, opts) { | ||
var f = opts.callback != null ? opts.callback : logConsole; | ||
if (opts.verbose || /set|delete/.test(operation)) { | ||
f(path, operation); | ||
} | ||
} | ||
var LoggedFile = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFile(file, path, opts) { | ||
this._file = file; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto = LoggedFile.prototype; | ||
_proto.delete = function _delete() { | ||
log(this._path, 'delete file', this._opts); | ||
return this._file.delete(); | ||
}; | ||
_proto.getData = function getData() { | ||
log(this._path, 'get data', this._opts); | ||
return this._file.getData(); | ||
}; | ||
_proto.getText = function getText() { | ||
log(this._path, 'get text', this._opts); | ||
return this._file.getText(); | ||
}; | ||
_proto.setData = function setData(data) { | ||
log(this._path, 'set data', this._opts); | ||
return this._file.setData(data); | ||
}; | ||
_proto.setText = function setText(text) { | ||
log(this._path, 'set text', this._opts); | ||
return this._file.setText(text); | ||
}; | ||
_proto.getPath = function getPath() { | ||
log(this._path, 'get path', this._opts); | ||
return this._file; | ||
}; | ||
return LoggedFile; | ||
}(); | ||
var LoggedFolder = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFolder(folder, path, opts) { | ||
this._folder = folder; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto2 = LoggedFolder.prototype; | ||
_proto2.delete = function _delete() { | ||
log(this._path, 'delete folder', this._opts); | ||
return this._folder.delete(); | ||
}; | ||
_proto2.file = function file(name) { | ||
return new LoggedFile(this._folder.file(name), this._path + name, this._opts); | ||
}; | ||
_proto2.folder = function folder(name) { | ||
return new LoggedFolder(this._folder.folder(name), this._path + name + '/', this._opts); | ||
}; | ||
_proto2.listFiles = function listFiles() { | ||
log(this._path, 'list files', this._opts); | ||
return this._folder.listFiles(); | ||
}; | ||
_proto2.listFolders = function listFolders() { | ||
log(this._path, 'list folders', this._opts); | ||
return this._folder.listFolders(); | ||
}; | ||
return LoggedFolder; | ||
}(); | ||
function makeLoggedFolder(folder, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new LoggedFolder(folder, '', opts); | ||
} | ||
/** | ||
* A single file stored in memory. | ||
*/ | ||
var MemoryFile = | ||
@@ -364,3 +182,12 @@ /*#__PURE__*/ | ||
var item = this._storage[this._path]; | ||
return item != null ? Promise.resolve(item) : Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
if (item == null) { | ||
return Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
} | ||
if (typeof item === 'string') { | ||
return Promise.reject(new Error("\"" + this._path + "\" is a text file.")); | ||
} | ||
return Promise.resolve(item); | ||
}; | ||
@@ -370,7 +197,18 @@ | ||
var item = this._storage[this._path]; | ||
return item != null ? Promise.resolve(item) : Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
if (item == null) { | ||
return Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
} | ||
if (typeof item !== 'string') { | ||
return Promise.reject(new Error("\"" + this._path + "\" is a binary file.")); | ||
} | ||
return Promise.resolve(item); | ||
}; | ||
_proto.setData = function setData(data) { | ||
this._storage[this._path] = data; | ||
// We use `any` here becase Flow is too dumb to know that `ArrayLike` | ||
var flowHack = data; | ||
this._storage[this._path] = Uint8Array.from(flowHack); | ||
return Promise.resolve(); | ||
@@ -464,3 +302,3 @@ }; | ||
return new Promise(function (resolve, reject) { | ||
return fs.mkdir(path, function (err) { | ||
return fs.mkdir(path, void 0, function (err) { | ||
return err && err.code !== 'EEXIST' ? reject(err) : resolve(); | ||
@@ -487,10 +325,2 @@ }); | ||
function readFile(path, opts) { | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(path, opts, function (err, out) { | ||
return err ? reject(err) : resolve(out); | ||
}); | ||
}); | ||
} | ||
function stat(path) { | ||
@@ -591,11 +421,24 @@ return new Promise(function (resolve, reject) { | ||
_proto.getData = function getData() { | ||
return readFile(this._path, null); | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(_this._path, {}, function (err, out) { | ||
return err ? reject(err) : resolve(out); | ||
}); | ||
}); | ||
}; | ||
_proto.getText = function getText() { | ||
return readFile(this._path, 'utf8'); | ||
var _this2 = this; | ||
return new Promise(function (resolve, reject) { | ||
return fs.readFile(_this2._path, 'utf8', function (err, out) { | ||
return err ? reject(err) : resolve(out); | ||
}); | ||
}); | ||
}; | ||
_proto.setData = function setData(data) { | ||
return writeFileDeep(this._path, Uint8Array.from(data), null); | ||
var flowHack = data; | ||
return writeFileDeep(this._path, Buffer.from(flowHack), {}); | ||
}; | ||
@@ -624,3 +467,3 @@ | ||
_proto2.delete = function _delete() { | ||
var _this = this; | ||
var _this3 = this; | ||
@@ -638,8 +481,8 @@ return readdirStat(this._path).then(function (lists) { | ||
return Promise.all(files.map(function (name) { | ||
return _this.file(name).delete(); | ||
return _this3.file(name).delete(); | ||
}).concat(folders.map(function (name) { | ||
return _this.folder(name).delete(); | ||
return _this3.folder(name).delete(); | ||
}))); | ||
}).then(function () { | ||
return rmdir(_this._path); | ||
return rmdir(_this3._path); | ||
}).catch(ignoreMissing()); | ||
@@ -685,6 +528,190 @@ }; | ||
function makeReactNativeFolder(path) { | ||
function makeReactNativeFolder() { | ||
throw new Error('Not available on node.js'); | ||
} | ||
/** | ||
* Interprets a path as a series of folder lookups, | ||
* handling special components like `.` and `..`. | ||
*/ | ||
function followPath(folder, parts) { | ||
var i = 0; // Read index | ||
var j = 0; // Write index | ||
// Shift down good elements, dropping bad ones: | ||
while (i < parts.length) { | ||
var part = parts[i++]; | ||
if (part === '..') j--;else if (part !== '.' && part !== '') parts[j++] = part; | ||
if (j < 0) throw new Error('Path would escape folder'); | ||
} // Navigate the folder: | ||
for (i = 0; i < j; ++i) { | ||
folder = folder.folder(parts[i]); | ||
} | ||
return folder; | ||
} | ||
/** | ||
* Navigates down to the file indicated by the path. | ||
*/ | ||
function locateFile(folder, path) { | ||
var parts = path.split('/'); | ||
var filename = parts.pop(); | ||
return followPath(folder, parts).file(filename); | ||
} | ||
/** | ||
* Navigates down to the sub-folder indicated by the path. | ||
*/ | ||
function locateFolder(folder, path) { | ||
var parts = path.split('/'); | ||
return followPath(folder, parts); | ||
} | ||
/** | ||
* Applies an async function to all the files in a folder. | ||
*/ | ||
function mapFiles(folder, f) { | ||
return folder.listFiles().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.file(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Applies an async function to all the sub-folders in a folder. | ||
*/ | ||
function mapFolders(folder, f) { | ||
return folder.listFolders().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.folder(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Recursively applies an async function to all the files in a folder tree. | ||
* The file names are expanded into paths, and the result is a flat array. | ||
*/ | ||
function mapAllFiles(folder, f) { | ||
function recurse(folder, f, prefix) { | ||
return Promise.all([mapFiles(folder, function (file, name) { | ||
return f(file, prefix + name, folder); | ||
}), mapFolders(folder, function (folder, name) { | ||
return recurse(folder, f, prefix + name + '/'); | ||
})]).then(function (_ref) { | ||
var files = _ref[0], | ||
folders = _ref[1]; | ||
return files.concat.apply(files, folders); | ||
}); | ||
} | ||
return recurse(folder, f, ''); | ||
} | ||
function logConsole(path, operation) { | ||
console.log(operation + " \"" + path + "\""); | ||
} | ||
function log(path, operation, opts) { | ||
var f = opts.callback != null ? opts.callback : logConsole; | ||
if (opts.verbose || /set|delete/.test(operation)) { | ||
f(path, operation); | ||
} | ||
} | ||
var LoggedFile = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFile(file, path, opts) { | ||
this._file = file; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto = LoggedFile.prototype; | ||
_proto.delete = function _delete() { | ||
log(this._path, 'delete file', this._opts); | ||
return this._file.delete(); | ||
}; | ||
_proto.getData = function getData() { | ||
log(this._path, 'get data', this._opts); | ||
return this._file.getData(); | ||
}; | ||
_proto.getText = function getText() { | ||
log(this._path, 'get text', this._opts); | ||
return this._file.getText(); | ||
}; | ||
_proto.setData = function setData(data) { | ||
log(this._path, 'set data', this._opts); | ||
return this._file.setData(data); | ||
}; | ||
_proto.setText = function setText(text) { | ||
log(this._path, 'set text', this._opts); | ||
return this._file.setText(text); | ||
}; | ||
_proto.getPath = function getPath() { | ||
return this._path; | ||
}; | ||
return LoggedFile; | ||
}(); | ||
var LoggedFolder = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFolder(folder, path, opts) { | ||
this._folder = folder; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto2 = LoggedFolder.prototype; | ||
_proto2.delete = function _delete() { | ||
log(this._path, 'delete folder', this._opts); | ||
return this._folder.delete(); | ||
}; | ||
_proto2.file = function file(name) { | ||
return new LoggedFile(this._folder.file(name), this._path + name, this._opts); | ||
}; | ||
_proto2.folder = function folder(name) { | ||
return new LoggedFolder(this._folder.folder(name), this._path + name + '/', this._opts); | ||
}; | ||
_proto2.listFiles = function listFiles() { | ||
log(this._path, 'list files', this._opts); | ||
return this._folder.listFiles(); | ||
}; | ||
_proto2.listFolders = function listFolders() { | ||
log(this._path, 'list folders', this._opts); | ||
return this._folder.listFolders(); | ||
}; | ||
return LoggedFolder; | ||
}(); | ||
function makeLoggedFolder(folder, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new LoggedFolder(folder, '', opts); | ||
} | ||
function removeDuplicates(master, fallback) { | ||
@@ -816,3 +843,3 @@ var blacklist = {}; | ||
export { locateFile, locateFolder, mapAllFiles, mapFiles, mapFolders, makeLocalStorageFolder, makeLoggedFolder, makeMemoryFolder, makeNodeFolder, makeReactNativeFolder, makeUnionFolder }; | ||
export { locateFile, locateFolder, makeLocalStorageFolder, makeLoggedFolder, makeMemoryFolder, makeNodeFolder, makeReactNativeFolder, makeUnionFolder, mapAllFiles, mapFiles, mapFolders }; | ||
//# sourceMappingURL=disklet.js.map |
@@ -11,86 +11,2 @@ 'use strict'; | ||
/** | ||
* Interprets a path as a series of folder lookups, | ||
* handling special components like `.` and `..`. | ||
*/ | ||
function followPath(folder, parts) { | ||
var i = 0; // Read index | ||
var j = 0; // Write index | ||
// Shift down good elements, dropping bad ones: | ||
while (i < parts.length) { | ||
var part = parts[i++]; | ||
if (part === '..') j--;else if (part !== '.' && part !== '') parts[j++] = part; | ||
if (j < 0) throw new Error('Path would escape folder'); | ||
} // Navigate the folder: | ||
for (i = 0; i < j; ++i) { | ||
folder = folder.folder(parts[i]); | ||
} | ||
return folder; | ||
} | ||
/** | ||
* Navigates down to the file indicated by the path. | ||
*/ | ||
function locateFile(folder, path) { | ||
var parts = path.split('/'); | ||
var filename = parts.pop(); | ||
return followPath(folder, parts).file(filename); | ||
} | ||
/** | ||
* Navigates down to the sub-folder indicated by the path. | ||
*/ | ||
function locateFolder(folder, path) { | ||
var parts = path.split('/'); | ||
return followPath(folder, parts); | ||
} | ||
/** | ||
* Applies an async function to all the files in a folder. | ||
*/ | ||
function mapFiles(folder, f) { | ||
return folder.listFiles().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.file(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Applies an async function to all the sub-folders in a folder. | ||
*/ | ||
function mapFolders(folder, f) { | ||
return folder.listFolders().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.folder(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Recursively applies an async function to all the files in a folder tree. | ||
* The file names are expanded into paths, and the result is a flat array. | ||
*/ | ||
function mapAllFiles(folder, f) { | ||
function recurse(folder, f, prefix) { | ||
return Promise.all([mapFiles(folder, function (file, name) { | ||
return f(file, prefix + name, folder); | ||
}), mapFolders(folder, function (folder, name) { | ||
return recurse(folder, f, prefix + name + '/'); | ||
})]).then(function (_ref) { | ||
var files = _ref[0], | ||
folders = _ref[1]; | ||
return files.concat.apply(files, folders); | ||
}); | ||
} | ||
return recurse(folder, f, ''); | ||
} | ||
/** | ||
* Verifies that a name contains no slashes. | ||
@@ -110,9 +26,10 @@ */ | ||
function storageKeys(storage) { | ||
var keys = []; | ||
var out = []; | ||
for (var i = 0; i < storage.length; ++i) { | ||
keys.push(storage.key(i)); | ||
var key = storage.key(i); | ||
if (key) out.push(key); | ||
} | ||
return keys; | ||
return out; | ||
} | ||
@@ -243,110 +160,11 @@ /** | ||
return new LocalStorageFolder(storage, opts.prefix || ''); | ||
var _opts = opts, | ||
_opts$prefix = _opts.prefix, | ||
prefix = _opts$prefix === void 0 ? '' : _opts$prefix; | ||
return new LocalStorageFolder(storage, prefix); | ||
} | ||
function logConsole(path, operation) { | ||
console.log(operation + " \"" + path + "\""); | ||
} | ||
function log(path, operation, opts) { | ||
var f = opts.callback != null ? opts.callback : logConsole; | ||
if (opts.verbose || /set|delete/.test(operation)) { | ||
f(path, operation); | ||
} | ||
} | ||
var LoggedFile = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFile(file, path, opts) { | ||
this._file = file; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto = LoggedFile.prototype; | ||
_proto.delete = function _delete() { | ||
log(this._path, 'delete file', this._opts); | ||
return this._file.delete(); | ||
}; | ||
_proto.getData = function getData() { | ||
log(this._path, 'get data', this._opts); | ||
return this._file.getData(); | ||
}; | ||
_proto.getText = function getText() { | ||
log(this._path, 'get text', this._opts); | ||
return this._file.getText(); | ||
}; | ||
_proto.setData = function setData(data) { | ||
log(this._path, 'set data', this._opts); | ||
return this._file.setData(data); | ||
}; | ||
_proto.setText = function setText(text) { | ||
log(this._path, 'set text', this._opts); | ||
return this._file.setText(text); | ||
}; | ||
_proto.getPath = function getPath() { | ||
log(this._path, 'get path', this._opts); | ||
return this._file; | ||
}; | ||
return LoggedFile; | ||
}(); | ||
var LoggedFolder = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFolder(folder, path, opts) { | ||
this._folder = folder; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto2 = LoggedFolder.prototype; | ||
_proto2.delete = function _delete() { | ||
log(this._path, 'delete folder', this._opts); | ||
return this._folder.delete(); | ||
}; | ||
_proto2.file = function file(name) { | ||
return new LoggedFile(this._folder.file(name), this._path + name, this._opts); | ||
}; | ||
_proto2.folder = function folder(name) { | ||
return new LoggedFolder(this._folder.folder(name), this._path + name + '/', this._opts); | ||
}; | ||
_proto2.listFiles = function listFiles() { | ||
log(this._path, 'list files', this._opts); | ||
return this._folder.listFiles(); | ||
}; | ||
_proto2.listFolders = function listFolders() { | ||
log(this._path, 'list folders', this._opts); | ||
return this._folder.listFolders(); | ||
}; | ||
return LoggedFolder; | ||
}(); | ||
function makeLoggedFolder(folder, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new LoggedFolder(folder, '', opts); | ||
} | ||
/** | ||
* A single file stored in memory. | ||
*/ | ||
var MemoryFile = | ||
@@ -369,3 +187,12 @@ /*#__PURE__*/ | ||
var item = this._storage[this._path]; | ||
return item != null ? Promise.resolve(item) : Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
if (item == null) { | ||
return Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
} | ||
if (typeof item === 'string') { | ||
return Promise.reject(new Error("\"" + this._path + "\" is a text file.")); | ||
} | ||
return Promise.resolve(item); | ||
}; | ||
@@ -375,7 +202,18 @@ | ||
var item = this._storage[this._path]; | ||
return item != null ? Promise.resolve(item) : Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
if (item == null) { | ||
return Promise.reject(new Error("Cannot load \"" + this._path + "\"")); | ||
} | ||
if (typeof item !== 'string') { | ||
return Promise.reject(new Error("\"" + this._path + "\" is a binary file.")); | ||
} | ||
return Promise.resolve(item); | ||
}; | ||
_proto.setData = function setData(data) { | ||
this._storage[this._path] = data; | ||
// We use `any` here becase Flow is too dumb to know that `ArrayLike` | ||
var flowHack = data; | ||
this._storage[this._path] = Uint8Array.from(flowHack); | ||
return Promise.resolve(); | ||
@@ -607,3 +445,5 @@ }; | ||
_proto.getData = function getData() { | ||
return readFile(this._path, null); | ||
return readFile(this._path, 'base64').then(function (data) { | ||
return rfc4648.base64.parse(data); | ||
}); | ||
}; | ||
@@ -616,3 +456,3 @@ | ||
_proto.setData = function setData(data) { | ||
return writeFileDeep(this._path, Uint8Array.from(data), null); | ||
return writeFileDeep(this._path, rfc4648.base64.stringify(data), 'base64'); | ||
}; | ||
@@ -696,6 +536,190 @@ | ||
function makeReactNativeFolder() { | ||
function makeReactNativeFolder$1() { | ||
return new RNFolder(RNFS.DocumentDirectoryPath); | ||
} | ||
/** | ||
* Interprets a path as a series of folder lookups, | ||
* handling special components like `.` and `..`. | ||
*/ | ||
function followPath(folder, parts) { | ||
var i = 0; // Read index | ||
var j = 0; // Write index | ||
// Shift down good elements, dropping bad ones: | ||
while (i < parts.length) { | ||
var part = parts[i++]; | ||
if (part === '..') j--;else if (part !== '.' && part !== '') parts[j++] = part; | ||
if (j < 0) throw new Error('Path would escape folder'); | ||
} // Navigate the folder: | ||
for (i = 0; i < j; ++i) { | ||
folder = folder.folder(parts[i]); | ||
} | ||
return folder; | ||
} | ||
/** | ||
* Navigates down to the file indicated by the path. | ||
*/ | ||
function locateFile(folder, path) { | ||
var parts = path.split('/'); | ||
var filename = parts.pop(); | ||
return followPath(folder, parts).file(filename); | ||
} | ||
/** | ||
* Navigates down to the sub-folder indicated by the path. | ||
*/ | ||
function locateFolder(folder, path) { | ||
var parts = path.split('/'); | ||
return followPath(folder, parts); | ||
} | ||
/** | ||
* Applies an async function to all the files in a folder. | ||
*/ | ||
function mapFiles(folder, f) { | ||
return folder.listFiles().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.file(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Applies an async function to all the sub-folders in a folder. | ||
*/ | ||
function mapFolders(folder, f) { | ||
return folder.listFolders().then(function (names) { | ||
return Promise.all(names.map(function (name) { | ||
return f(folder.folder(name), name, folder); | ||
})); | ||
}); | ||
} | ||
/** | ||
* Recursively applies an async function to all the files in a folder tree. | ||
* The file names are expanded into paths, and the result is a flat array. | ||
*/ | ||
function mapAllFiles(folder, f) { | ||
function recurse(folder, f, prefix) { | ||
return Promise.all([mapFiles(folder, function (file, name) { | ||
return f(file, prefix + name, folder); | ||
}), mapFolders(folder, function (folder, name) { | ||
return recurse(folder, f, prefix + name + '/'); | ||
})]).then(function (_ref) { | ||
var files = _ref[0], | ||
folders = _ref[1]; | ||
return files.concat.apply(files, folders); | ||
}); | ||
} | ||
return recurse(folder, f, ''); | ||
} | ||
function logConsole(path, operation) { | ||
console.log(operation + " \"" + path + "\""); | ||
} | ||
function log(path, operation, opts) { | ||
var f = opts.callback != null ? opts.callback : logConsole; | ||
if (opts.verbose || /set|delete/.test(operation)) { | ||
f(path, operation); | ||
} | ||
} | ||
var LoggedFile = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFile(file, path, opts) { | ||
this._file = file; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto = LoggedFile.prototype; | ||
_proto.delete = function _delete() { | ||
log(this._path, 'delete file', this._opts); | ||
return this._file.delete(); | ||
}; | ||
_proto.getData = function getData() { | ||
log(this._path, 'get data', this._opts); | ||
return this._file.getData(); | ||
}; | ||
_proto.getText = function getText() { | ||
log(this._path, 'get text', this._opts); | ||
return this._file.getText(); | ||
}; | ||
_proto.setData = function setData(data) { | ||
log(this._path, 'set data', this._opts); | ||
return this._file.setData(data); | ||
}; | ||
_proto.setText = function setText(text) { | ||
log(this._path, 'set text', this._opts); | ||
return this._file.setText(text); | ||
}; | ||
_proto.getPath = function getPath() { | ||
return this._path; | ||
}; | ||
return LoggedFile; | ||
}(); | ||
var LoggedFolder = | ||
/*#__PURE__*/ | ||
function () { | ||
function LoggedFolder(folder, path, opts) { | ||
this._folder = folder; | ||
this._path = path; | ||
this._opts = opts; | ||
} | ||
var _proto2 = LoggedFolder.prototype; | ||
_proto2.delete = function _delete() { | ||
log(this._path, 'delete folder', this._opts); | ||
return this._folder.delete(); | ||
}; | ||
_proto2.file = function file(name) { | ||
return new LoggedFile(this._folder.file(name), this._path + name, this._opts); | ||
}; | ||
_proto2.folder = function folder(name) { | ||
return new LoggedFolder(this._folder.folder(name), this._path + name + '/', this._opts); | ||
}; | ||
_proto2.listFiles = function listFiles() { | ||
log(this._path, 'list files', this._opts); | ||
return this._folder.listFiles(); | ||
}; | ||
_proto2.listFolders = function listFolders() { | ||
log(this._path, 'list folders', this._opts); | ||
return this._folder.listFolders(); | ||
}; | ||
return LoggedFolder; | ||
}(); | ||
function makeLoggedFolder(folder, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new LoggedFolder(folder, '', opts); | ||
} | ||
function removeDuplicates(master, fallback) { | ||
@@ -829,5 +853,2 @@ var blacklist = {}; | ||
exports.locateFolder = locateFolder; | ||
exports.mapAllFiles = mapAllFiles; | ||
exports.mapFiles = mapFiles; | ||
exports.mapFolders = mapFolders; | ||
exports.makeLocalStorageFolder = makeLocalStorageFolder; | ||
@@ -837,4 +858,7 @@ exports.makeLoggedFolder = makeLoggedFolder; | ||
exports.makeNodeFolder = makeNodeFolder; | ||
exports.makeReactNativeFolder = makeReactNativeFolder; | ||
exports.makeReactNativeFolder = makeReactNativeFolder$1; | ||
exports.makeUnionFolder = makeUnionFolder; | ||
exports.mapAllFiles = mapAllFiles; | ||
exports.mapFiles = mapFiles; | ||
exports.mapFolders = mapFolders; | ||
//# sourceMappingURL=disklet.rn.js.map |
{ | ||
"name": "disklet", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"description": "A tiny, composable filesystem API", | ||
@@ -27,3 +27,3 @@ "repository": { | ||
"build": "rimraf lib && rollup -c", | ||
"precommit": "lint-staged && flow && npm test && npm run build", | ||
"precommit": "lint-staged && flow && npm test", | ||
"flow": "flow", | ||
@@ -33,3 +33,4 @@ "format": "import-sort -l --write '*.js' 'src/**/*.js' 'test/**/*.js'; prettier-eslint --write '*.js' 'src/**/*.js' 'test/**/*.js'", | ||
"prepare": "npm run build", | ||
"test": "rollup -c && mocha -r esm -r source-map-support/register test/test.js" | ||
"pretest": "rollup -c test/rollup.config.js", | ||
"test": "mocha -r source-map-support/register build/tests.js" | ||
}, | ||
@@ -60,3 +61,2 @@ "browser": { | ||
"eslint-plugin-standard": "^4.0.0", | ||
"esm": "^3.0.84", | ||
"flow-bin": "^0.78.0", | ||
@@ -63,0 +63,0 @@ "husky": "^0.13.3", |
// @flow | ||
export { | ||
import { makeLocalStorageFolder } from './backends/local-storage.js' | ||
import { makeMemoryFolder } from './backends/memory.js' | ||
import { makeNodeFolder } from './backends/node.js' | ||
import { makeReactNativeFolder } from './backends/react-native.js' | ||
import { | ||
locateFile, | ||
@@ -10,9 +14,18 @@ locateFolder, | ||
} from './helpers.js' | ||
import { makeLoggedFolder } from './loggedFolder.js' | ||
import { makeUnionFolder } from './unionFolder.js' | ||
export { makeLocalStorageFolder } from './localStorageFolder.js' | ||
export { makeLoggedFolder } from './loggedFolder.js' | ||
export { makeMemoryFolder } from './memoryFolder.js' | ||
export { makeNodeFolder } from './nodeFolder.js' | ||
export { makeReactNativeFolder } from './reactNativeFolder.js' | ||
export { makeUnionFolder } from './unionFolder.js' | ||
export { | ||
locateFile, | ||
locateFolder, | ||
makeLocalStorageFolder, | ||
makeLoggedFolder, | ||
makeMemoryFolder, | ||
makeNodeFolder, | ||
makeReactNativeFolder, | ||
makeUnionFolder, | ||
mapAllFiles, | ||
mapFiles, | ||
mapFolders | ||
} | ||
@@ -19,0 +32,0 @@ export type ArrayLike<T> = |
@@ -0,1 +1,24 @@ | ||
// @flow | ||
import { | ||
type ArrayLike, | ||
type DiskletFile, | ||
type DiskletFolder | ||
} from './index.js' | ||
type LogOperation = | ||
| 'delete file' | ||
| 'delete folder' | ||
| 'get data' | ||
| 'get text' | ||
| 'list files' | ||
| 'list folders' | ||
| 'set data' | ||
| 'set text' | ||
type LogOptions = { | ||
callback?: (path: string, operation: LogOperation) => mixed, | ||
verbose?: boolean | ||
} | ||
function logConsole (path, operation) { | ||
@@ -5,3 +28,3 @@ console.log(`${operation} "${path}"`) | ||
function log (path, operation, opts) { | ||
function log (path, operation: LogOperation, opts) { | ||
const f = opts.callback != null ? opts.callback : logConsole | ||
@@ -15,3 +38,7 @@ | ||
class LoggedFile { | ||
constructor (file, path, opts) { | ||
_file: DiskletFile | ||
_opts: LogOptions | ||
_path: string | ||
constructor (file: DiskletFile, path: string, opts: LogOptions) { | ||
this._file = file | ||
@@ -22,3 +49,3 @@ this._path = path | ||
delete () { | ||
delete (): Promise<mixed> { | ||
log(this._path, 'delete file', this._opts) | ||
@@ -28,3 +55,3 @@ return this._file.delete() | ||
getData () { | ||
getData (): Promise<Uint8Array> { | ||
log(this._path, 'get data', this._opts) | ||
@@ -34,3 +61,3 @@ return this._file.getData() | ||
getText () { | ||
getText (): Promise<string> { | ||
log(this._path, 'get text', this._opts) | ||
@@ -40,3 +67,3 @@ return this._file.getText() | ||
setData (data) { | ||
setData (data: ArrayLike<number>): Promise<mixed> { | ||
log(this._path, 'set data', this._opts) | ||
@@ -46,3 +73,3 @@ return this._file.setData(data) | ||
setText (text) { | ||
setText (text: string): Promise<mixed> { | ||
log(this._path, 'set text', this._opts) | ||
@@ -52,5 +79,4 @@ return this._file.setText(text) | ||
getPath () { | ||
log(this._path, 'get path', this._opts) | ||
return this._file | ||
getPath (): string { | ||
return this._path | ||
} | ||
@@ -60,3 +86,7 @@ } | ||
class LoggedFolder { | ||
constructor (folder, path, opts) { | ||
_folder: DiskletFolder | ||
_opts: LogOptions | ||
_path: string | ||
constructor (folder: DiskletFolder, path: string, opts: LogOptions) { | ||
this._folder = folder | ||
@@ -67,3 +97,3 @@ this._path = path | ||
delete () { | ||
delete (): Promise<mixed> { | ||
log(this._path, 'delete folder', this._opts) | ||
@@ -73,3 +103,3 @@ return this._folder.delete() | ||
file (name) { | ||
file (name: string): DiskletFile { | ||
return new LoggedFile( | ||
@@ -82,3 +112,3 @@ this._folder.file(name), | ||
folder (name) { | ||
folder (name: string): DiskletFolder { | ||
return new LoggedFolder( | ||
@@ -91,3 +121,3 @@ this._folder.folder(name), | ||
listFiles () { | ||
listFiles (): Promise<Array<string>> { | ||
log(this._path, 'list files', this._opts) | ||
@@ -97,3 +127,3 @@ return this._folder.listFiles() | ||
listFolders () { | ||
listFolders (): Promise<Array<string>> { | ||
log(this._path, 'list folders', this._opts) | ||
@@ -104,4 +134,7 @@ return this._folder.listFolders() | ||
export function makeLoggedFolder (folder, opts = {}) { | ||
export function makeLoggedFolder ( | ||
folder: DiskletFolder, | ||
opts: LogOptions = {} | ||
): DiskletFolder { | ||
return new LoggedFolder(folder, '', opts) | ||
} |
@@ -0,2 +1,9 @@ | ||
// @flow | ||
import { mapFiles, mapFolders } from './helpers.js' | ||
import { | ||
type ArrayLike, | ||
type DiskletFile, | ||
type DiskletFolder | ||
} from './index.js' | ||
@@ -26,3 +33,11 @@ function removeDuplicates (master, fallback) { | ||
class UnionFile { | ||
constructor (master, fallback, whiteout) { | ||
_master: DiskletFile | ||
_fallback: DiskletFile | ||
_whiteout: DiskletFile | ||
constructor ( | ||
master: DiskletFile, | ||
fallback: DiskletFile, | ||
whiteout: DiskletFile | ||
) { | ||
this._master = master | ||
@@ -33,3 +48,3 @@ this._fallback = fallback | ||
delete () { | ||
delete (): Promise<mixed> { | ||
return Promise.all([ | ||
@@ -41,3 +56,3 @@ this._whiteout.setData([]), | ||
getData () { | ||
getData (): Promise<Uint8Array> { | ||
return this._master.getData().catch(e => | ||
@@ -53,3 +68,3 @@ this._whiteout.getData().then( | ||
getText () { | ||
getText (): Promise<string> { | ||
return this._master.getText().catch(e => | ||
@@ -65,11 +80,11 @@ this._whiteout.getData().then( | ||
setData (data) { | ||
setData (data: ArrayLike<number>): Promise<mixed> { | ||
return this._master.setData(data) | ||
} | ||
setText (text) { | ||
setText (text: string): Promise<mixed> { | ||
return this._master.setText(text) | ||
} | ||
getPath () { | ||
getPath (): string { | ||
throw new Error('Cannot call getPath on a Union Folder') | ||
@@ -84,3 +99,6 @@ } | ||
export class UnionFolder { | ||
constructor (master, fallback) { | ||
_master: DiskletFolder | ||
_fallback: DiskletFolder | ||
constructor (master: DiskletFolder, fallback: DiskletFolder) { | ||
this._master = master | ||
@@ -90,3 +108,3 @@ this._fallback = fallback | ||
delete () { | ||
delete (): Promise<mixed> { | ||
return Promise.all([ | ||
@@ -98,3 +116,3 @@ mapFiles(this, file => file.delete()), | ||
file (name) { | ||
file (name: string): DiskletFile { | ||
return new UnionFile( | ||
@@ -107,3 +125,3 @@ this._master.file(name), | ||
folder (name) { | ||
folder (name: string): DiskletFolder { | ||
return new UnionFolder( | ||
@@ -115,3 +133,3 @@ this._master.folder(name), | ||
listFiles () { | ||
listFiles (): Promise<Array<string>> { | ||
return Promise.all([ | ||
@@ -123,3 +141,3 @@ this._master.listFiles(), | ||
listFolders () { | ||
listFolders (): Promise<Array<string>> { | ||
return Promise.all([ | ||
@@ -132,4 +150,7 @@ this._master.listFolders(), | ||
export function makeUnionFolder (master, fallback) { | ||
export function makeUnionFolder ( | ||
master: DiskletFolder, | ||
fallback: DiskletFolder | ||
): DiskletFolder { | ||
return new UnionFolder(master, fallback) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
221780
35
3071
0
23