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