diff --git a/src/Aggregator.js b/src/Aggregator.js deleted file mode 100644 index 7ed3356..0000000 --- a/src/Aggregator.js +++ /dev/null @@ -1,99 +0,0 @@ -'use strict'; - -var async = require('asyncawait/async'); -var await = require('asyncawait/await'); -var ipfsAPI = require('orbit-common/lib/ipfs-api-promised'); -var Keystore = require('orbit-common/lib/Keystore'); -var Encryption = require('orbit-common/lib/Encryption'); -var HashCache = require('./HashCacheClient'); -var HashCacheItem = require('./HashCacheItem').EncryptedHashCacheItem; -var HashCacheOps = require('./HashCacheOps'); -var MemoryCache = require('./MemoryCache'); - -const pubkey = Keystore.getKeys().publicKey; -const privkey = Keystore.getKeys().privateKey; - -const DefaultAmount = 1; - -class Aggregator { - static fetchRecursive(ipfs, hash, password, options, currentAmount, deleted) { - const opts = { - amount: options.amount ? options.amount : DefaultAmount, - last: options.last ? options.last : null, - key: options.key ? options.key : null - }; - - let result = []; - let handledItems = deleted ? deleted : []; - - if(!currentAmount) currentAmount = 0; - - const item = await (this._fetchOne(ipfs, hash, password)); - - if(item) { - if((item.op === HashCacheOps.Put || item.op === HashCacheOps.Add) && !this._contains(handledItems, item.key)) { - if(!opts.key || (opts.key && opts.key === item.key)) { - result.push({ hash: hash, item: item }); - currentAmount ++; - handledItems.push(item.target); - } - } else if(item.op === HashCacheOps.Delete) { - handledItems.push(item.target); - } - - if(opts.key && item.key === opts.key) - return result; - - if(opts.last && hash === opts.last) - return result; - - if(!opts.last && opts.amount > -1 && currentAmount >= opts.amount) - return result; - - if(item.next) { - const items = this.fetchRecursive(ipfs, item.next, password, opts, currentAmount, handledItems); - result = result.concat(items); - } - } - - return result; - } - - static _fetchOne(ipfs, hash, password) { - // 1. Try fetching from memory - let data = MemoryCache.get(hash); - // TODO: 2. Try fetching from local cache - - // 3. Fetch from network - if(!data) - data = await (ipfsAPI.getObject(ipfs, hash)); - - // Cache the fetched item (encrypted) - MemoryCache.put(hash, data); - - // Decrypt the item - let item = HashCacheItem.fromEncrypted(data, pubkey, privkey, password); - - // TODO: add possibility to fetch content separately - // fetch and decrypt content - if(item.op === HashCacheOps.Add || item.op === HashCacheOps.Put) { - let payload = MemoryCache.get(item.target); - if(!payload) - payload = await (ipfsAPI.getObject(ipfs, item.target)); - - MemoryCache.put(item.target, payload); - - const contentEnc = JSON.parse(payload.Data)["content"]; - const contentDec = Encryption.decrypt(contentEnc, privkey, 'TODO: pubkey'); - item.Payload = contentDec; - } - - return item; - } - - static _contains(src, e) { - return src.filter((f) => f.toString() === e.toString()).length > 0; - } -} - -module.exports = Aggregator; diff --git a/src/BetterRequest.js b/src/BetterRequest.js deleted file mode 100644 index b080cab..0000000 --- a/src/BetterRequest.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict' - -var unirest = require('unirest') - -class Request { - constructor() { - this.url = ''; - this.method = 'GET'; - this.headers = {}; - this.body = {}; - } - - get(url) { - return this._init('GET', url); - } - - post(url) { - return this._init('POST', url); - } - - put(url) { - return this._init('PUT', url); - } - - delete(url) { - return this._init('DELETE', url); - } - - _init(method, url) { - this.url = url; - this.method = method; - this.body = {}; - this.headers = {}; - return this; - } - - set(key, value) { - this.headers[key] = value; - return this; - } - - send(body) { - this.body = body; - return this; - } - - end(callback) { - if(!this.url.startsWith("http")) - this.url = "http://" + this.url - - unirest(this.method, this.url) - .headers(this.headers) - .type('application/json') - .send(this.body) - .end((res) => { - if(res.error) - callback(res.body ? res.body.message : "Connection refused", null); - else - callback(null, res.body); - }); - } - -} - -module.exports = new Request(); diff --git a/src/HashCacheClient.js b/src/HashCacheClient.js deleted file mode 100644 index 05a9858..0000000 --- a/src/HashCacheClient.js +++ /dev/null @@ -1,85 +0,0 @@ -'use strict' - -var request = require('./BetterRequest'); - -class HashCacheClient { - constructor(host, credentials, info) { - this.host = host - this.credentials = credentials - this.info = info; - this.linkedList = this.linkedList.bind(this) - } - - linkedList(hash, password) { - return { - head: () => this._get(hash, password), - add: (head) => this._add(hash, password, head), - setMode: (mode) => this._setModes(hash, password, mode), - delete: () => this._delete(hash, password) - } - } - - _get(hash, password) { - return new Promise((resolve, reject) => { - request - .get(this.host + '/channel/' + hash) - .set('Authorization', this.credentials) - .send({ password: password }) - .end((err, res) => { this._resolveRequest(err, res, resolve, reject) }); - }) - } - - _add(hash, password, head) { - return new Promise((resolve, reject) => { - request - .put(this.host + '/channel/' + hash + '/add') - .set('Authorization', this.credentials) - .send({ head: head, password: password }) - .end((err, res) => { this._resolveRequest(err, res, resolve, reject) }); - }) - } - - _setModes(hash, password, modes) { - return new Promise((resolve, reject) => { - request - .post(this.host + '/channel/' + hash) - .set('Authorization', this.credentials) - .send({ modes: modes, password: password }) - .end((err, res) => { this._resolveRequest(err, res, resolve, reject) }); - }) - } - - _delete(hash, password) { - return new Promise((resolve, reject) => { - request - .delete(this.host + '/channel/' + hash) - .set('Authorization', this.credentials) - .send({ password: password }) - .end((err, res) => { this._resolveRequest(err, res, resolve, reject) }); - }) - } - - _resolveRequest(err, res, resolve, reject) { - if(err) - reject(res ? res : err.toString()); - else - resolve(res ? res : {}); - } -} - -module.exports = { - connect: (host, username, password) => { - var credentials = `Basic ${username}=${password}`; - return new Promise((resolve, reject) => { - request - .post(host + '/register') - .set('Authorization', credentials) - .end((err, res) => { - if(err) - reject(res ? res.body.message : err.toString()) - else - resolve(new HashCacheClient(host, credentials, res)); - }) - }) - } -} diff --git a/src/MemoryCache.js b/src/MemoryCache.js deleted file mode 100644 index d8cabf4..0000000 --- a/src/MemoryCache.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -let items = {}; - -class MemoryCache { - static put(hash, item) { - items[hash] = item; - } - - static get(hash) { - return items[hash]; - } -} - -module.exports = MemoryCache; diff --git a/src/OrbitClient.js b/src/OrbitClient.js index 42b083f..c3ac36a 100644 --- a/src/OrbitClient.js +++ b/src/OrbitClient.js @@ -1,25 +1,22 @@ 'use strict'; -var async = require('asyncawait/async'); -var await = require('asyncawait/await'); -var Keystore = require('orbit-common/lib/Keystore'); -var Encryption = require('orbit-common/lib/Encryption'); -var ipfsDaemon = require('orbit-common/lib/ipfs-daemon'); -var ipfsAPI = require('orbit-common/lib/ipfs-api-promised'); -var HashCacheItem = require('./HashCacheItem').HashCacheItem; -var OrbitDBItem = require('./HashCacheItem').OrbitDBItem; -var HashCacheOps = require('./HashCacheOps'); -var ItemTypes = require('./ItemTypes'); -var MetaInfo = require('./MetaInfo'); -var Post = require('./Post'); -var Aggregator = require('./Aggregator'); -var PubSub = require('./PubSub'); -var Timer = require('../examples/Timer'); -const List = require('./list/OrbitList'); -const DataStore = require('./DataStore'); +const async = require('asyncawait/async'); +const await = require('asyncawait/await'); +const Keystore = require('orbit-common/lib/Keystore'); +const Encryption = require('orbit-common/lib/Encryption'); +const ipfsDaemon = require('orbit-common/lib/ipfs-daemon'); +const ipfsAPI = require('orbit-common/lib/ipfs-api-promised'); +const OrbitDBItem = require('./HashCacheItem').OrbitDBItem; +const HashCacheOps = require('./HashCacheOps'); +const ItemTypes = require('./ItemTypes'); +const MetaInfo = require('./MetaInfo'); +const Post = require('./Post'); +const PubSub = require('./Pubsub'); +const List = require('./list/OrbitList'); +const DataStore = require('./DataStore'); -var pubkey = Keystore.getKeys().publicKey; -var privkey = Keystore.getKeys().privateKey; +const pubkey = Keystore.getKeys().publicKey; +const privkey = Keystore.getKeys().privateKey; class OrbitClient { constructor(ipfs) {