Files: 79c10b336a5d261d0c19aedbef677d2a5d332ff3 / sbot-api.js
1539 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 | createClient(function (err, _sbot) { |
24 | if(err) return isConn(err) |
25 | sbot = _sbot |
26 | sbot.on('closed', function () { |
27 | sbot = null |
28 | isConn(new Error('closed')) |
29 | }) |
30 | isConn() |
31 | }) |
32 | }) |
33 | |
34 | return { |
35 | sbot_blobs_add: rec.sink(function (cb) { |
36 | return pull( |
37 | Hash(cb), |
38 | sbot.blobs.add() |
39 | ) |
40 | }), |
41 | sbot_links: rec.source(function (query) { |
42 | return sbot.links(query) |
43 | }), |
44 | sbot_links2: rec.source(function (query) { |
45 | return sbot.links2.read(query) |
46 | }), |
47 | sbot_log: rec.source(function (opts) { |
48 | return sbot.createLogStream(opts) |
49 | }), |
50 | sbot_user_feed: rec.source(function (opts) { |
51 | return sbot.createUserStream(opts) |
52 | }), |
53 | sbot_get: rec.async(function (key, cb) { |
54 | sbot.get(key, cb) |
55 | }), |
56 | sbot_publish: rec.async(function (msg, cb) { |
57 | sbot.publish(msg, cb) |
58 | }), |
59 | sbot_whoami: rec.async(function (cb) { |
60 | sbot.whoami(cb) |
61 | }) |
62 | } |
63 | } |
64 | |
65 |
Built with git-ssb-web