UNPKG

1.59 kBJavaScriptView Raw
1var Documentstore, Envelope, bs, defer, ecc, _;
2
3_ = require('lodash');
4
5bs = require('bs58check');
6
7ecc = require('ecc-tools');
8
9defer = require('when');
10
11Envelope = require('ecc-envelope');
12
13Documentstore = (function() {
14 function Documentstore(_store) {
15 this._store = _store;
16 if (!(this instanceof Documentstore)) {
17 return new Documentstore(this._store);
18 }
19 }
20
21 Documentstore.prototype.put = function(envelope, encoding) {
22 return defer(envelope).then((function(_this) {
23 return function(envelope) {
24 envelope = Envelope({
25 decode: envelope,
26 as: encoding
27 });
28 return envelope.open().then(function(data) {
29 var key;
30 if (data) {
31 key = data.from;
32 return envelope.encode(_this._store.encoding).then(function(value) {
33 return _this._store.put(key, value);
34 }).then(function() {
35 return key;
36 });
37 } else {
38 return false;
39 }
40 });
41 };
42 })(this));
43 };
44
45 Documentstore.prototype.get = function(key, encoding) {
46 return defer(key).then((function(_this) {
47 return function(key) {
48 if (typeof key === 'string') {
49 key = bs.decode(key);
50 }
51 return _this._store.get(key);
52 };
53 })(this)).then((function(_this) {
54 return function(data) {
55 return Envelope({
56 decode: data,
57 as: _this._store.encoding
58 }).encode(encoding);
59 };
60 })(this));
61 };
62
63 return Documentstore;
64
65})();
66
67module.exports = Documentstore;