Files: 1245ae4906c27c7046c345c5183022b5e4df1928 / scuttlebot.js
1525 bytesRaw
1 | var pull = require('pull-stream') |
2 | var ssbKeys = require('ssb-keys') |
3 | var ref = require('ssb-ref') |
4 | var reconnect = require('pull-reconnect') |
5 | |
6 | var config = require('./config')() |
7 | var createClient = require('ssb-client') |
8 | var keys = require('./keys') |
9 | |
10 | var CACHE = {} |
11 | |
12 | var rec = reconnect(function (isConn) { |
13 | function notify (value) { |
14 | isConn(value) |
15 | } |
16 | |
17 | createClient(keys, { |
18 | manifest: require('./manifest.json'), |
19 | remote: config.remote, |
20 | caps: config.caps |
21 | }, function (err, _sbot) { |
22 | if(err) |
23 | return notify(err) |
24 | |
25 | sbot = _sbot |
26 | sbot.on('closed', function () { |
27 | sbot = null |
28 | notify(new Error('closed')) |
29 | }) |
30 | |
31 | notify() |
32 | }) |
33 | }) |
34 | |
35 | module.exports = { |
36 | createLogStream: rec.source(function (opts) { |
37 | return pull( |
38 | sbot.createLogStream(opts), |
39 | pull.through(function (e) { |
40 | CACHE[e.key] = CACHE[e.key] || e.value |
41 | }) |
42 | ) |
43 | }), |
44 | userStream: rec.source(function (config) { |
45 | return pull( |
46 | sbot.createUserStream(config), |
47 | pull.through(function (e) { |
48 | CACHE[e.key] = CACHE[e.key] || e.value |
49 | }) |
50 | ) |
51 | }), |
52 | query: rec.source(function (query) { |
53 | return sbot.query.read(query) |
54 | }), |
55 | get: rec.async(function (key, cb) { |
56 | if('function' !== typeof cb) |
57 | throw new Error('cb must be function') |
58 | if(CACHE[key]) cb(null, CACHE[key]) |
59 | else sbot.get(key, function (err, value) { |
60 | if(err) return cb(err) |
61 | cb(null, CACHE[key] = value) |
62 | }) |
63 | }), |
64 | links: rec.source(function (query) { |
65 | return sbot.links(query) |
66 | }) |
67 | } |
68 | |
69 |
Built with git-ssb-web