@tabcat/orbit-db-fsstore
Advanced tools
Comparing version 3.1.0 to 3.1.1
{ | ||
"name": "@tabcat/orbit-db-fsstore", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "a custom orbit-db store representing a file system", | ||
@@ -32,5 +32,7 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"@tabcat/aes-gcm-crypter": "^1.0.1", | ||
"cross-env": "^7.0.2", | ||
"mocha": "^7.1.2", | ||
"orbit-db": "^0.23.1", | ||
"orbit-db-store": "^3.3.0", | ||
"orbit-db-test-utils": "^0.9.3", | ||
@@ -37,0 +39,0 @@ "rimraf": "^3.0.2" |
@@ -6,5 +6,14 @@ | ||
const { opcodes, lowercase } = FS | ||
const { ab2str } = require('./util') | ||
const fsReducer = (fs) => ({ payload } = {}) => { | ||
const fsReducer = (crypter) => async (fs, { payload } = {}) => { | ||
try { | ||
fs = await fs | ||
if (crypter) { | ||
const bytes = await crypter.decrypt( | ||
Uint8Array.from(payload.cipherbytes).buffer, | ||
Uint8Array.from(payload.iv) | ||
) | ||
payload = JSON.parse(ab2str(bytes)) | ||
} | ||
if (opcodes[payload.op]) FS.ops[lowercase[payload.op]](fs, payload) | ||
@@ -14,20 +23,18 @@ } catch (e) { | ||
} | ||
return fs | ||
} | ||
class FSIndex { | ||
constructor () { | ||
this._index = FS.create() | ||
} | ||
const passOptionsToIndex = (options = {}) => | ||
class FSIndex { | ||
constructor () { | ||
this._index = FS.create() | ||
} | ||
get () { | ||
return this._index | ||
async updateIndex (oplog) { | ||
const fs = FS.create() | ||
await oplog.values.reduce(fsReducer(options.crypter), fs) | ||
this._index = fs | ||
} | ||
} | ||
updateIndex (oplog) { | ||
const fs = FS.create() | ||
oplog.values.map(fsReducer(fs)) | ||
this._index = fs | ||
} | ||
} | ||
module.exports = FSIndex | ||
module.exports = passOptionsToIndex |
@@ -8,2 +8,3 @@ | ||
const { joinPath, pathName, pathValid, nameValid, opcodes, errors, ...fs } = FS | ||
const { str2ab } = require('./util') | ||
@@ -66,4 +67,4 @@ const paramCheckKeys = { | ||
class FSStore extends Store { | ||
constructor (ipfs, identity, dbname, options) { | ||
options = Object.assign({}, options, { Index: FSIndex }) | ||
constructor (ipfs, identity, dbname, options = {}) { | ||
options = Object.assign({}, options, { Index: FSIndex(options) }) | ||
super(ipfs, identity, dbname, options) | ||
@@ -81,2 +82,4 @@ this._type = FSStore.type | ||
this.paramChecks = paramChecks(this) | ||
this.crypter = options.crypter | ||
} | ||
@@ -88,6 +91,15 @@ | ||
_addOp ({ op, ...payload }) { | ||
async _addOp ({ op, ...params }) { | ||
const cType = op.slice(-3) === 'DIR' ? fs.cTypes.dir : fs.cTypes.file | ||
this.paramChecks[paramKeys[op]](payload, cType) | ||
return this._addOperation({ op, ...payload }) | ||
this.paramChecks[paramKeys[op]](params, cType) | ||
let payload = { op, ...params } | ||
if (this.crypter) { | ||
const serializedPayload = str2ab(JSON.stringify(payload)) | ||
const { cipherbytes, iv } = await this.crypter.encrypt(serializedPayload) | ||
payload = { | ||
cipherbytes: Array.from(new Uint8Array(cipherbytes)), | ||
iv: Array.from(iv) | ||
} | ||
} | ||
return this._addOperation(payload) | ||
} | ||
@@ -94,0 +106,0 @@ |
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
17692
8
379
7