Files: bfbfdb33ca0f1e7090cdffc8673079db4cf0679e / sbot-api.js
2163 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-lite') |
19 | //var createClient = require('ssb-client') |
20 | var createConfig = require('ssb-config/inject') |
21 | //var createClient = require('./lite') |
22 | var createFeed = require('ssb-feed') |
23 | var keys = require('./keys') |
24 | |
25 | |
26 | module.exports = function () { |
27 | var opts = createConfig() |
28 | var sbot = null |
29 | |
30 | var rec = Reconnect(function (isConn) { |
31 | createClient(keys, opts, function (err, _sbot) { |
32 | if(err) {console.error(err.stack); isConn(err)} |
33 | sbot = _sbot |
34 | sbot.on('closed', function () { |
35 | sbot = null |
36 | isConn(new Error('closed')) |
37 | }) |
38 | isConn() |
39 | }) |
40 | }) |
41 | |
42 | var internal = { |
43 | getLatest: rec.async(function (id, cb) { |
44 | sbot.getLatest(id, cb) |
45 | }), |
46 | add: rec.async(function (msg, cb) { |
47 | sbot.add(msg, cb) |
48 | }) |
49 | } |
50 | |
51 | var feed = createFeed(internal, keys) |
52 | |
53 | return { |
54 | sbot_blobs_add: rec.sink(function (cb) { |
55 | return pull( |
56 | Hash(cb), |
57 | sbot.blobs.add() |
58 | ) |
59 | }), |
60 | sbot_links: rec.source(function (query) { |
61 | return sbot.links(query) |
62 | }), |
63 | sbot_links2: rec.source(function (query) { |
64 | return sbot.links2.read(query) |
65 | }), |
66 | sbot_query: rec.source(function (query) { |
67 | return sbot.query.read(query) |
68 | }), |
69 | sbot_log: rec.source(function (opts) { |
70 | return sbot.createLogStream(opts) |
71 | }), |
72 | sbot_user_feed: rec.source(function (opts) { |
73 | return sbot.createUserStream(opts) |
74 | }), |
75 | sbot_get: rec.async(function (key, cb) { |
76 | sbot.get(key, cb) |
77 | }), |
78 | sbot_publish: rec.async(function (msg, cb) { |
79 | feed.add(msg, function (err, msg) { |
80 | cb(err, msg) |
81 | }) |
82 | }), |
83 | sbot_whoami: rec.async(function (cb) { |
84 | sbot.whoami(cb) |
85 | }) |
86 | } |
87 | } |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 |
Built with git-ssb-web