@tabcat/orbit-db-fsstore
Advanced tools
Comparing version 3.1.3 to 3.1.4
{ | ||
"name": "@tabcat/orbit-db-fsstore", | ||
"version": "3.1.3", | ||
"version": "3.1.4", | ||
"description": "a custom orbit-db store representing a file system", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -28,5 +28,5 @@ | ||
const defaultRoot = '/r' | ||
const setRoot = (fs) => { | ||
fs.root = defaultRoot | ||
return fs.set(defaultRoot, { type: cTypes.dir }) | ||
const setRoot = (fs, root = defaultRoot) => { | ||
fs.root = root | ||
return fs.set(root, { type: cTypes.dir }) | ||
} | ||
@@ -211,2 +211,3 @@ const root = (fs) => fs.root | ||
ops, | ||
setRoot, | ||
root, | ||
@@ -213,0 +214,0 @@ create, |
@@ -5,3 +5,3 @@ | ||
const FS = require('./FS') | ||
const { opcodes, lowercase } = FS | ||
const { opcodes, lowercase, setRoot } = FS | ||
const { ab2str } = require('./util') | ||
@@ -11,12 +11,18 @@ const b64 = require('base64-js') | ||
const fsReducer = (crypter) => async (fs, { payload } = {}) => { | ||
fs = await fs | ||
try { | ||
fs = await fs | ||
const fsCopy = new Map(fs) | ||
if (crypter) { | ||
const bytes = await crypter.decrypt( | ||
b64.toByteArray(payload.cipherbytes).buffer, | ||
b64.toByteArray(payload.iv) | ||
) | ||
payload = JSON.parse(ab2str(bytes)) | ||
if (payload.cipherbytes && payload.iv) { | ||
const bytes = await crypter.decrypt( | ||
b64.toByteArray(payload.cipherbytes).buffer, | ||
b64.toByteArray(payload.iv) | ||
) | ||
payload = JSON.parse(ab2str(bytes)) | ||
if (opcodes[payload.op]) FS.ops[lowercase[payload.op]](fsCopy, payload) | ||
} | ||
} else { | ||
if (opcodes[payload.op]) FS.ops[lowercase[payload.op]](fsCopy, payload) | ||
} | ||
if (opcodes[payload.op]) FS.ops[lowercase[payload.op]](fs, payload) | ||
fs = fsCopy | ||
} catch (e) { | ||
@@ -28,15 +34,15 @@ console.log(e) | ||
const passOptionsToIndex = (options = {}) => | ||
class FSIndex { | ||
constructor () { | ||
this._index = FS.create() | ||
} | ||
class FSIndex { | ||
constructor () { | ||
this._index = FS.create() | ||
this._crypter = null | ||
} | ||
async updateIndex (oplog) { | ||
const fs = FS.create() | ||
await oplog.values.reduce(fsReducer(options.crypter), fs) | ||
this._index = fs | ||
} | ||
async updateIndex (oplog) { | ||
const fs = FS.create() | ||
this._index = await oplog.values.reduce(fsReducer(this._crypter), fs) | ||
setRoot(this._index, fs.root) | ||
} | ||
} | ||
module.exports = passOptionsToIndex | ||
module.exports = FSIndex |
@@ -68,3 +68,3 @@ | ||
constructor (ipfs, identity, dbname, options = {}) { | ||
options = Object.assign({}, options, { Index: FSIndex(options) }) | ||
options = Object.assign({}, options, { Index: FSIndex }) | ||
super(ipfs, identity, dbname, options) | ||
@@ -83,3 +83,8 @@ this._type = FSStore.type | ||
this.crypter = options.crypter | ||
this._crypter = null | ||
this.setCrypter = (crypter) => { | ||
this._crypter = crypter | ||
this._index._crypter = crypter | ||
} | ||
if (options.crypter) this.setCrypter(options.crypter) | ||
} | ||
@@ -95,5 +100,5 @@ | ||
let payload = { op, ...params } | ||
if (this.crypter) { | ||
if (this._crypter) { | ||
const serializedPayload = str2ab(JSON.stringify(payload)) | ||
const { cipherbytes, iv } = await this.crypter.encrypt(serializedPayload) | ||
const { cipherbytes, iv } = await this._crypter.encrypt(serializedPayload) | ||
payload = { | ||
@@ -100,0 +105,0 @@ cipherbytes: b64.fromByteArray(new Uint8Array(cipherbytes)), |
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
18133
393