Files: cb26626721ed310376d6b1cfac574cc58e616db4 / sbot-api.js
1722 bytesRaw
1 | var pull = require('pull-stream') |
2 | var crypto = require('crypto') |
3 | var Reconnect = require('pull-reconnect') |
4 | |
5 | function Hash (onHash) { |
6 | var hash = crypto.createHash('sha256') |
7 | return pull.through(function (data) { |
8 | hash.update( |
9 | 'string' === typeof data |
10 | ? new Buffer(data, 'utf8') |
11 | : data |
12 | ) |
13 | }, function (err) { |
14 | if(err && !onHash) throw err |
15 | onHash && onHash(err, '&'+hash.digest('base64')+'.sha256') |
16 | }) |
17 | } |
18 | var createClient = require('ssb-client') |
19 | |
20 | module.exports = function () { |
21 | var sbot = null |
22 | var rec = Reconnect(function (isConn) { |
23 | console.log("RECONNECT", isConn) |
24 | createClient(function (err, _sbot) { |
25 | if(err) {console.error(err.stack); isConn(err)} |
26 | sbot = _sbot |
27 | sbot.on('closed', function () { |
28 | console.log("DISCONNECT") |
29 | sbot = null |
30 | isConn(new Error('closed')) |
31 | }) |
32 | isConn() |
33 | }) |
34 | }) |
35 | |
36 | return { |
37 | sbot_blobs_add: rec.sink(function (cb) { |
38 | return pull( |
39 | Hash(cb), |
40 | sbot.blobs.add() |
41 | ) |
42 | }), |
43 | sbot_links: rec.source(function (query) { |
44 | return sbot.links(query) |
45 | }), |
46 | sbot_links2: rec.source(function (query) { |
47 | return sbot.links2.read(query) |
48 | }), |
49 | sbot_query: rec.source(function (query) { |
50 | return sbot.query.read(query) |
51 | }), |
52 | sbot_log: rec.source(function (opts) { |
53 | return sbot.createLogStream(opts) |
54 | }), |
55 | sbot_user_feed: rec.source(function (opts) { |
56 | return sbot.createUserStream(opts) |
57 | }), |
58 | sbot_get: rec.async(function (key, cb) { |
59 | sbot.get(key, cb) |
60 | }), |
61 | sbot_publish: rec.async(function (msg, cb) { |
62 | sbot.publish(msg, cb) |
63 | }), |
64 | sbot_whoami: rec.async(function (cb) { |
65 | sbot.whoami(cb) |
66 | }) |
67 | } |
68 | } |
69 | |
70 | |
71 |
Built with git-ssb-web