@cypsela/sailplane-node
Advanced tools
Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "@cypsela/sailplane-node", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "collaborative file system on ipfs", | ||
@@ -33,3 +33,3 @@ "files": [ | ||
"dependencies": { | ||
"@tabcat/orbit-db-fsstore": "^1.0.0", | ||
"@tabcat/orbit-db-fsstore": "^1.1.2", | ||
"it-all": "^1.0.2", | ||
@@ -36,0 +36,0 @@ "it-last": "^1.0.2", |
'use strict' | ||
const EventEmitter = require('events').EventEmitter | ||
const { default: PQueue } = require('p-queue') | ||
@@ -11,3 +12,4 @@ const all = require('it-all') | ||
pathExistYes: (path) => new Error(`path '${path}' already exists`), | ||
pathDirNo: (path) => new Error(`path '${path}' is not a directory`) | ||
pathDirNo: (path) => new Error(`path '${path}' is not a directory`), | ||
pathFileNo: (path) => new Error(`path '${path}' is not a file`) | ||
} | ||
@@ -21,2 +23,10 @@ | ||
const validCid = function (CID, cid) { | ||
try { | ||
return !!new CID(cid) | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
class SharedFS { | ||
@@ -27,5 +37,5 @@ constructor (db, ipfs, options = {}) { | ||
this.options = { ...defaultOptions, ...options } | ||
this.events = new EventEmitter() | ||
this.address = this._db.address | ||
this.events = this._db.events | ||
@@ -43,4 +53,9 @@ this.fs = {} | ||
this._updateQueue = new PQueue({ concurrency: 1 }) | ||
this._onDbUpdate = () => | ||
this._updateQueue.size === 0 && this._updateQueue.add(() => this._getCid()) | ||
this._onDbUpdate = () => { | ||
this.events.emit('updated') | ||
this._updateQueue.size === 0 && | ||
this._updateQueue.add(() => this._getCid()) | ||
} | ||
this._emptyFile = null | ||
this._CID = null | ||
@@ -58,5 +73,12 @@ this.running = null | ||
if (this.running !== null) { return } | ||
this.events.on('replicated', this._onDbUpdate) | ||
this._emptyFile = await last(this._ipfs.add('')) | ||
this._CID = this._emptyFile.cid.constructor | ||
if (this.options.load) await this._db.load() | ||
this._onDbUpdate() | ||
this._db.events.on('replicated', this._onDbUpdate) | ||
this.events.on('upload', this._onDbUpdate) | ||
this.events.on('mkdir', this._onDbUpdate) | ||
this.events.on('mkfile', this._onDbUpdate) | ||
this.events.on('write', this._onDbUpdate) | ||
if (this.options.load) await this._db.load() | ||
this.events.on('remove', this._onDbUpdate) | ||
this.running = true | ||
@@ -69,4 +91,8 @@ this.events.emit('start') | ||
await this._onStop() | ||
this.events.removeListener('replicated', this._onDbUpdate) | ||
this._db.events.removeListener('replicated', this._onDbUpdate) | ||
this.events.removeListener('upload', this._onDbUpdate) | ||
this.events.removeListener('mkdir', this._onDbUpdate) | ||
this.events.removeListener('mkfile', this._onDbUpdate) | ||
this.events.removeListener('write', this._onDbUpdate) | ||
this.events.removeListener('remove', this._onDbUpdate) | ||
await this._updateQueue.onIdle() | ||
@@ -125,2 +151,28 @@ drop ? await this._db.drop() : await this._db.close() | ||
async mkdir (path, name) { | ||
if (!this.fs.exists(path)) throw errors.pathExistNo(path) | ||
if (this.fs.exists(this.fs.joinPath(path, name))) { | ||
throw errors.pathExistYes(this.fs.joinPath(path, name)) | ||
} | ||
await this._db.mkdir(path, name) | ||
this.events.emit('mkdir') | ||
} | ||
async mkfile (path, name) { | ||
if (!this.fs.exists(path)) throw errors.pathExistNo(path) | ||
if (this.fs.exists(this.fs.joinPath(path, name))) { | ||
throw errors.pathExistYes(this.fs.joinPath(path, name)) | ||
} | ||
await this._db.mk(path, name) | ||
this.events.emit('mkfile') | ||
} | ||
async write (path, cid) { | ||
if (!this.fs.exists(path)) throw errors.pathExistNo(path) | ||
if (this.fs.content(path) !== 'file') throw errors.pathFileNo(path) | ||
if (!validCid(this._CID, cid)) throw new Error('invalid cid') | ||
await this._db.write(path, cid.toString()) | ||
this.events.emit('write') | ||
} | ||
async read (path) { | ||
@@ -142,9 +194,7 @@ if (!this.fs.exists(path)) throw errors.pathExistNo(path) | ||
function useCidClass (CID) { | ||
return function validCid (cid) { | ||
try { | ||
return !!new CID(cid) | ||
} catch (e) { | ||
return false | ||
} | ||
const fileCid = (cid) => { | ||
try { | ||
return new this._CID(cid) | ||
} catch (e) { | ||
return this._emptyFile.cid | ||
} | ||
@@ -154,4 +204,2 @@ } | ||
async function * ipfsTree (path) { | ||
const emptyFile = await last(this._ipfs.add('')) | ||
const validCid = useCidClass(emptyFile.cid.constructor) | ||
const fsStruct = [path, ...this.fs.tree(path)] | ||
@@ -161,6 +209,3 @@ .map((p) => ({ | ||
content: this.fs.content(p) === 'file' | ||
? this._ipfs.cat( | ||
(validCid(this.fs.read(p)) && this.fs.read(p)) || | ||
emptyFile.cid | ||
) | ||
? this._ipfs.cat(fileCid(this.fs.read(p))) | ||
: undefined | ||
@@ -172,2 +217,5 @@ })) | ||
try { | ||
if (this.fs.content(path) === 'file') { | ||
return fileCid(this.fs.read(path)) | ||
} | ||
const { cid } = await last(ipfsTree.bind(this)(path)) | ||
@@ -174,0 +222,0 @@ return cid |
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
15623
6
230