@tabcat/orbit-db-fsstore
Advanced tools
Comparing version 1.2.0 to 2.0.0
{ | ||
"name": "@tabcat/orbit-db-fsstore", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "a custom orbit-db store representing a file system", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -13,5 +13,9 @@ | ||
MV: 'MV', | ||
CP: 'CP' | ||
CP: 'CP', | ||
BATCH: 'BATCH' | ||
} | ||
const lowercase = Object.keys(opcodes) | ||
.reduce((a, k) => ({ ...a, [k]: opcodes[k].toLowerCase() }), {}) | ||
// content types | ||
@@ -57,3 +61,3 @@ const cTypes = { | ||
// make directory at path + name | ||
function mkdir (fs, path, name) { | ||
function mkdir (fs, { path, name }) { | ||
if ( | ||
@@ -69,3 +73,3 @@ nameValid(name) && | ||
// remove directory at path | ||
function rmdir (fs, path) { | ||
function rmdir (fs, { path }) { | ||
if (fs.has(path) && fs.get(path).type === cTypes.dir) { | ||
@@ -80,3 +84,3 @@ const paths = [path, ...tree(fs, path)] | ||
// move directory at path to destination path + name | ||
function mvdir (fs, path, dest, name) { | ||
function mvdir (fs, { path, dest, name }) { | ||
if ( | ||
@@ -98,3 +102,3 @@ nameValid(name) && | ||
// copy directory at path to destination path + name | ||
function cpdir (fs, path, dest, name) { | ||
function cpdir (fs, { path, dest, name }) { | ||
if ( | ||
@@ -115,3 +119,3 @@ nameValid(name) && | ||
// make file at path + name | ||
function mk (fs, path, name) { | ||
function mk (fs, { path, name }) { | ||
if ( | ||
@@ -127,3 +131,3 @@ nameValid(name) && | ||
// write json data to path | ||
function write (fs, path, json) { | ||
function write (fs, { path, json }) { | ||
if (fs.has(path) && fs.get(path).type === cTypes.file) { | ||
@@ -135,3 +139,3 @@ fs.set(path, { type: cTypes.file, json }) | ||
// remove file at path | ||
function rm (fs, path) { | ||
function rm (fs, { path }) { | ||
if (fs.has(path) && fs.get(path).type === cTypes.file) { | ||
@@ -143,3 +147,3 @@ fs.delete(path) | ||
// move file at path to destination path + name | ||
function mv (fs, path, dest, name) { | ||
function mv (fs, { path, dest, name }) { | ||
if ( | ||
@@ -156,3 +160,3 @@ nameValid(name) && | ||
// copy file at path to destination path + name | ||
function cp (fs, path, dest, name) { | ||
function cp (fs, { path, dest, name }) { | ||
if ( | ||
@@ -167,9 +171,11 @@ nameValid(name) && | ||
module.exports = { | ||
create, | ||
exists, | ||
content, | ||
read, | ||
tree, | ||
ls, | ||
function batch (fs, { payloads }) { | ||
if (Array.isArray(payloads)) { | ||
for (const payload of payloads) { | ||
if (opcodes[payload.op]) ops[lowercase[payload.op]](fs, payload) | ||
} | ||
} | ||
} | ||
const ops = { | ||
mkdir, | ||
@@ -184,3 +190,15 @@ rmdir, | ||
cp, | ||
batch | ||
} | ||
module.exports = { | ||
ops, | ||
create, | ||
exists, | ||
content, | ||
read, | ||
tree, | ||
ls, | ||
opcodes, | ||
lowercase, | ||
cTypes, | ||
@@ -187,0 +205,0 @@ pathValid, |
@@ -5,35 +5,7 @@ | ||
const FS = require('./FS') | ||
const { opcodes } = FS | ||
const { opcodes, lowercase } = FS | ||
const fsReducer = (state) => ({ payload } = {}) => { | ||
const fsReducer = (fs) => ({ payload } = {}) => { | ||
try { | ||
switch (payload.op) { | ||
case opcodes.MKDIR: | ||
FS.mkdir(state, payload.path, payload.name) | ||
break | ||
case opcodes.RMDIR: | ||
FS.rmdir(state, payload.path) | ||
break | ||
case opcodes.MVDIR: | ||
FS.mvdir(state, payload.path, payload.dest, payload.name) | ||
break | ||
case opcodes.CPDIR: | ||
FS.cpdir(state, payload.path, payload.dest, payload.name) | ||
break | ||
case opcodes.MK: | ||
FS.mk(state, payload.path, payload.name) | ||
break | ||
case opcodes.WRITE: | ||
FS.write(state, payload.path, payload.json) | ||
break | ||
case opcodes.RM: | ||
FS.rm(state, payload.path) | ||
break | ||
case opcodes.MV: | ||
FS.mv(state, payload.path, payload.dest, payload.name) | ||
break | ||
case opcodes.CP: | ||
FS.cp(state, payload.path, payload.dest, payload.name) | ||
break | ||
} | ||
if (opcodes[payload.op]) FS.ops[lowercase[payload.op]](fs, payload) | ||
} catch (e) { | ||
@@ -55,3 +27,2 @@ console.log(e) | ||
const fs = FS.create() | ||
oplog.values.map(fsReducer(fs)) | ||
@@ -58,0 +29,0 @@ this._index = fs |
@@ -15,2 +15,47 @@ | ||
const paramCheckKeys = { | ||
path: 'path', | ||
pathName: 'pathName', | ||
pathDestName: 'pathDestName', | ||
payloads: 'payloads' | ||
} | ||
const paramChecks = (self) => ({ | ||
path: ({ path }) => { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!self.exists(path)) throw errors.pathExistNo(path) | ||
return true | ||
}, | ||
pathName: ({ path, name }) => { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!self.exists(path)) throw errors.pathExistNo(path) | ||
if (self.exists(joinPath(path, name))) throw errors.pathExistYes(joinPath(path, name)) | ||
return true | ||
}, | ||
pathDestName: ({ path, dest, name }) => { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!pathValid(dest)) throw errors.pathValidNo(dest) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!self.exists(path)) throw errors.pathExistNo(path) | ||
if (!self.exists(dest)) throw errors.pathExistNo(dest) | ||
if (self.exists(joinPath(dest, name))) throw errors.pathExistYes(joinPath(dest, name)) | ||
return true | ||
}, | ||
payloads: ({ payloads }) => Array.isArray(payloads) | ||
}) | ||
const paramKeys = { | ||
[opcodes.MKDIR]: paramCheckKeys.pathName, | ||
[opcodes.RMDIR]: paramCheckKeys.path, | ||
[opcodes.MVDIR]: paramCheckKeys.pathDestName, | ||
[opcodes.CPDIR]: paramCheckKeys.pathDestName, | ||
[opcodes.MK]: paramCheckKeys.pathName, | ||
[opcodes.WRITE]: paramCheckKeys.path, | ||
[opcodes.RM]: paramCheckKeys.path, | ||
[opcodes.MV]: paramCheckKeys.pathDestName, | ||
[opcodes.CP]: paramCheckKeys.pathDestName, | ||
[opcodes.BATCH]: paramCheckKeys.payloads | ||
} | ||
const type = 'fsstore' | ||
@@ -30,2 +75,4 @@ | ||
this.ls = (path = '') => fs.ls(this._index.get(), path) | ||
this.paramChecks = paramChecks(this) | ||
} | ||
@@ -35,77 +82,62 @@ | ||
_addOp ({ op, ...payload }) { | ||
this.paramChecks[paramKeys[op]](payload) | ||
return this._addOperation({ op, ...payload }) | ||
} | ||
mkdir (path, name) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
if (this.exists(joinPath(path, name))) throw errors.pathExistYes(joinPath(path, name)) | ||
return this._addOperation({ op: opcodes.MKDIR, path, name }) | ||
return this._addOp({ op: opcodes.MKDIR, path, name }) | ||
} | ||
rmdir (path) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
return this._addOperation({ op: opcodes.RMDIR, path }) | ||
return this._addOp({ op: opcodes.RMDIR, path }) | ||
} | ||
mvdir (path, dest, name) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!pathValid(dest)) throw errors.pathValidNo(dest) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
if (!this.exists(dest)) throw errors.pathExistNo(dest) | ||
if (this.exists(joinPath(dest, name))) throw errors.pathExistYes(joinPath(dest, name)) | ||
return this._addOperation({ op: opcodes.MVDIR, path, dest, name }) | ||
return this._addOp({ op: opcodes.MVDIR, path, dest, name }) | ||
} | ||
cpdir (path, dest, name) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!pathValid(dest)) throw errors.pathValidNo(dest) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
if (!this.exists(dest)) throw errors.pathExistNo(dest) | ||
if (this.exists(joinPath(dest, name))) throw errors.pathExistYes(joinPath(dest, name)) | ||
return this._addOperation({ op: opcodes.CPDIR, path, dest, name }) | ||
return this._addOp({ op: opcodes.CPDIR, path, dest, name }) | ||
} | ||
mk (path, name) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
if (this.exists(joinPath(path, name))) throw errors.pathExistYes(joinPath(path, name)) | ||
return this._addOperation({ op: opcodes.MK, path, name }) | ||
return this._addOp({ op: opcodes.MK, path, name }) | ||
} | ||
write (path, json) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
return this._addOperation({ op: opcodes.WRITE, path, json }) | ||
return this._addOp({ op: opcodes.WRITE, path, json }) | ||
} | ||
rm (path) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
return this._addOperation({ op: opcodes.RM, path }) | ||
return this._addOp({ op: opcodes.RM, path }) | ||
} | ||
mv (path, dest, name) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!pathValid(dest)) throw errors.pathValidNo(dest) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
if (!this.exists(dest)) throw errors.pathExistNo(dest) | ||
if (this.exists(joinPath(dest, name))) throw errors.pathExistYes(joinPath(dest, name)) | ||
return this._addOperation({ op: opcodes.MV, path, dest, name }) | ||
return this._addOp({ op: opcodes.MV, path, dest, name }) | ||
} | ||
cp (path, dest, name) { | ||
if (!pathValid(path)) throw errors.pathValidNo(path) | ||
if (!pathValid(dest)) throw errors.pathValidNo(dest) | ||
if (!nameValid(name)) throw errors.nameValidNo(name) | ||
if (!this.exists(path)) throw errors.pathExistNo(path) | ||
if (!this.exists(dest)) throw errors.pathExistNo(dest) | ||
if (this.exists(joinPath(dest, name))) throw errors.pathExistYes(joinPath(dest, name)) | ||
return this._addOperation({ op: opcodes.CP, path, dest, name }) | ||
return this._addOp({ op: opcodes.CP, path, dest, name }) | ||
} | ||
batch () { | ||
const pls = [] | ||
const makeBatchOp = (payloads) => this._addOp({ op: opcodes.BATCH, payloads }) | ||
return { | ||
mkdir: (...p) => pls.push({ op: opcodes.MKDIR, path: p[0], name: p[1] }), | ||
rmdir: (...p) => pls.push({ op: opcodes.RMDIR, path: p[0] }), | ||
mvdir: (...p) => pls.push({ op: opcodes.MVDIR, path: p[0], dest: p[1], name: p[2] }), | ||
cpdir: (...p) => pls.push({ op: opcodes.CPDIR, path: p[0], dest: p[1], name: p[2] }), | ||
mk: (...p) => pls.push({ op: opcodes.MK, path: p[0], name: p[1] }), | ||
write: (...p) => pls.push({ op: opcodes.WRITE, path: p[0], json: p[1] }), | ||
rm: (...p) => pls.push({ op: opcodes.RM, path: p[0] }), | ||
mv: (...p) => pls.push({ op: opcodes.MV, path: p[0], dest: p[1], name: p[2] }), | ||
cp: (...p) => pls.push({ op: opcodes.CP, path: p[0], dest: p[1], name: p[2] }), | ||
execute: () => makeBatchOp(pls) | ||
} | ||
} | ||
} | ||
module.exports = FSStore |
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
316
15219