Examples for writing and reading

This commit is contained in:
haad 2015-12-27 17:27:17 +02:00
parent c39a991edf
commit a22e70a6cd
5 changed files with 132 additions and 28 deletions

View File

@ -14,11 +14,11 @@ class EncryptedHashCacheItem extends HashCacheItem {
constructor(sequenceNumber, targetHash, metaInfo, publicKey, privateKey, salt) {
super(sequenceNumber, targetHash, metaInfo);
this.pubkey = publicKey;
this.target = encryption.encrypt(targetHash, privateKey, publicKey);
this.payload = this.target; // old hash-cache api compatibility
this.meta = encryption.encrypt(JSON.stringify(metaInfo), privateKey, publicKey);
try {
this.sig = encryption.sign(this.target, privateKey, this.seq, salt || "");
this.target = encryption.encrypt(targetHash, privateKey, publicKey);
this.payload = this.target; // old hash-cache api compatibility
this.meta = encryption.encrypt(JSON.stringify(metaInfo), privateKey, publicKey);
this.sig = encryption.sign(this.target, privateKey, this.seq, salt || "");
} catch(e) {
console.log("Signing HashCacheItem failed:", e);
}

View File

@ -29,38 +29,41 @@ orbit-server uses linked lists on top of IPFS.
## Usage
```javascript
var async = require('asyncawait/async');
var OrbitClient = require('./OrbitClient');
var host = 'localhost:3006'; // orbit-server address
// Connect
var orbit = OrbitClient.connect(host, username, password); // OrbitClient
async(() => {
// Connect
var orbit = OrbitClient.connect(host, username, password); // OrbitClient
var channelName = 'hello-world';
var channelPwd = '';
var channelName = 'hello-world';
var channelPwd = '';
// Send a message
var head = orbit.channel(channelName, channelPwd).send('hello'); // <ipfs-hash>
// Send a message
var head = orbit.channel(channelName, channelPwd).send('hello'); // <ipfs-hash>
// Iterator options
var options = { limit: -1 }; // fetch all messages
// {
// gt: <hash>,
// gte: <hash>,
// lt: <hash>,
// lte: <hash>,
// limit: 10,
// reverse: true
// }
// Iterator options
var options = { limit: -1 }; // fetch all messages
// {
// gt: <hash>,
// gte: <hash>,
// lt: <hash>,
// lte: <hash>,
// limit: 10,
// reverse: true
// }
// Get messages
var iter = orbit.channel(channelName, channelPwd).iterator(options); // Symbol.iterator
var next = iter.next(); // { value: <item>, done: false|true}
// Get messages
var iter = orbit.channel(channelName, channelPwd).iterator(options); // Symbol.iterator
var next = iter.next(); // { value: <item>, done: false|true}
// OR:
// for(let i of iter)
// console.log(i.hash, i.item.Data.seq);
// OR:
// for(let i of iter)
// console.log(i.hash, i.item.Data.seq);
// delete channel
var result = orbit.channel(channelName, channelPwd).delete(); // true | false
// delete channel
var result = orbit.channel(channelName, channelPwd).delete(); // true | false
})();
```

19
Timer.js Normal file
View File

@ -0,0 +1,19 @@
'use strict';
class Timer {
constructor(startImmediately) {
this.startTime = startImmediately ? new Date().getTime() : null;
this.endTime = null;
}
start() {
this.startTime = new Date().getTime();
}
stop() {
this.endTime = new Date().getTime();
return (this.endTime - this.startTime);
}
}
module.exports = Timer;

44
examples/readMessages.js Normal file
View File

@ -0,0 +1,44 @@
'use strict';
var async = require('asyncawait/async');
var OrbitClient = require('../OrbitClient');
var Timer = require('../Timer');
var host = 'localhost:3006';
var username = 'testrunner';
var password = '';
let run = (async(() => {
try {
var channel = 'hello-world-test1'
// Connect
var orbit = OrbitClient.connect(host, username, password);
var timer = new Timer(true);
// Iterator options
var options = {
limit: -1, // fetch all
reverse: false, // latest first
// gte: <ipfs-hash> // eg. 'QmQGa4Bsw3JxxydNNbbYnXfnW1BC54koTidHQ6Am2VM8Ep'
// lte: <ipfs-hash> // eg. 'QmQGa4Bsw3JxxydNNbbYnXfnW1BC54koTidHQ6Am2VM8Ep'
}
// Get all messages
var iter = orbit.channel(channel, '').iterator(options);
for(let i of iter) {
console.log(i.item.Data.seq, i.hash, "ts: " + i.item.Data.meta.ts);
}
console.log("Fetch messages took " + timer.stop() + "ms");
} catch(e) {
console.error("error:", e);
console.error(e.stack);
process.exit(1);
}
}))();
module.exports = run;

38
examples/writeMessages.js Normal file
View File

@ -0,0 +1,38 @@
'use strict';
var async = require('asyncawait/async');
var OrbitClient = require('../OrbitClient');
var Timer = require('../Timer');
var host = 'localhost:3006';
var username = 'testrunner';
var password = '';
let run = (async(() => {
try {
var channel = 'hello-world-test1'
// Connect
var orbit = OrbitClient.connect(host, username, password);
// Delete channel and its data
var result = orbit.channel(channel, '').delete();
var messages = 100;
var i = 0;
while(i < messages) {
var timer = new Timer(true);
// Send a message
var head = orbit.channel(channel, '').send('hello');
console.log(i, head, timer.stop() + "ms");
i ++;
}
} catch(e) {
console.error("error:", e);
console.error(e.stack);
process.exit(1);
}
}))();
module.exports = run;