mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-07-05 20:12:32 +00:00
Remove obsolete code
This commit is contained in:
parent
04b3e262d0
commit
c10836b31f
@ -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;
|
|
@ -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();
|
|
@ -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));
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
@ -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;
|
|
@ -1,25 +1,22 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('asyncawait/async');
|
const async = require('asyncawait/async');
|
||||||
var await = require('asyncawait/await');
|
const await = require('asyncawait/await');
|
||||||
var Keystore = require('orbit-common/lib/Keystore');
|
const Keystore = require('orbit-common/lib/Keystore');
|
||||||
var Encryption = require('orbit-common/lib/Encryption');
|
const Encryption = require('orbit-common/lib/Encryption');
|
||||||
var ipfsDaemon = require('orbit-common/lib/ipfs-daemon');
|
const ipfsDaemon = require('orbit-common/lib/ipfs-daemon');
|
||||||
var ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
||||||
var HashCacheItem = require('./HashCacheItem').HashCacheItem;
|
const OrbitDBItem = require('./HashCacheItem').OrbitDBItem;
|
||||||
var OrbitDBItem = require('./HashCacheItem').OrbitDBItem;
|
const HashCacheOps = require('./HashCacheOps');
|
||||||
var HashCacheOps = require('./HashCacheOps');
|
const ItemTypes = require('./ItemTypes');
|
||||||
var ItemTypes = require('./ItemTypes');
|
const MetaInfo = require('./MetaInfo');
|
||||||
var MetaInfo = require('./MetaInfo');
|
const Post = require('./Post');
|
||||||
var Post = require('./Post');
|
const PubSub = require('./Pubsub');
|
||||||
var Aggregator = require('./Aggregator');
|
const List = require('./list/OrbitList');
|
||||||
var PubSub = require('./PubSub');
|
const DataStore = require('./DataStore');
|
||||||
var Timer = require('../examples/Timer');
|
|
||||||
const List = require('./list/OrbitList');
|
|
||||||
const DataStore = require('./DataStore');
|
|
||||||
|
|
||||||
var pubkey = Keystore.getKeys().publicKey;
|
const pubkey = Keystore.getKeys().publicKey;
|
||||||
var privkey = Keystore.getKeys().privateKey;
|
const privkey = Keystore.getKeys().privateKey;
|
||||||
|
|
||||||
class OrbitClient {
|
class OrbitClient {
|
||||||
constructor(ipfs) {
|
constructor(ipfs) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user