Files: 0d9b44b8f8b24b2c8bce341d3b10f76dbd2cb75b / sbot-api.js
2930 bytesRaw
1 | var pull = require('pull-stream') |
2 | var crypto = require('crypto') |
3 | var ref = require('ssb-ref') |
4 | var Reconnect = require('pull-reconnect') |
5 | |
6 | |
7 | function Hash (onHash) { |
8 | var hash = crypto.createHash('sha256') |
9 | return pull.through(function (data) { |
10 | hash.update( |
11 | 'string' === typeof data |
12 | ? new Buffer(data, 'utf8') |
13 | : data |
14 | ) |
15 | }, function (err) { |
16 | if(err && !onHash) throw err |
17 | onHash && onHash(err, '&'+hash.digest('base64')+'.sha256') |
18 | }) |
19 | } |
20 | //uncomment this to use from browser... |
21 | //also depends on having ssb-ws installed. |
22 | //var createClient = require('ssb-lite') |
23 | var createClient = require('ssb-client') |
24 | |
25 | var createConfig = require('ssb-config/inject') |
26 | |
27 | var createFeed = require('ssb-feed') |
28 | var keys = require('./keys') |
29 | var ssbKeys = require('ssb-keys') |
30 | |
31 | module.exports = function () { |
32 | var opts = createConfig() |
33 | var sbot = null |
34 | var connection_status = [] |
35 | |
36 | |
37 | var rec = Reconnect(function (isConn) { |
38 | // var remote |
39 | // if('undefined' !== typeof localStorage) |
40 | // remote = localStorage.remote |
41 | |
42 | function notify (value) { |
43 | console.log('connection_status', value, connection_status) |
44 | isConn(value); connection_status.forEach(function (fn) { fn(value) }) |
45 | } |
46 | |
47 | createClient(keys, { |
48 | manifest: require('./manifest.json'), |
49 | remote: require('./config')().remote |
50 | }, function (err, _sbot) { |
51 | if(err) |
52 | return notify(err) |
53 | |
54 | sbot = _sbot |
55 | sbot.on('closed', function () { |
56 | sbot = null |
57 | notify(new Error('closed')) |
58 | }) |
59 | |
60 | notify() |
61 | }) |
62 | }) |
63 | |
64 | var internal = { |
65 | getLatest: rec.async(function (id, cb) { |
66 | sbot.getLatest(id, cb) |
67 | }), |
68 | add: rec.async(function (msg, cb) { |
69 | sbot.add(msg, cb) |
70 | }) |
71 | } |
72 | |
73 | var feed = createFeed(internal, keys) |
74 | |
75 | return { |
76 | connection_status: connection_status, |
77 | sbot_blobs_add: rec.sink(function (cb) { |
78 | return pull( |
79 | Hash(cb), |
80 | sbot.blobs.add() |
81 | ) |
82 | }), |
83 | sbot_links: rec.source(function (query) { |
84 | return sbot.links(query) |
85 | }), |
86 | sbot_links2: rec.source(function (query) { |
87 | return sbot.links2.read(query) |
88 | }), |
89 | sbot_query: rec.source(function (query) { |
90 | return sbot.query.read(query) |
91 | }), |
92 | sbot_log: rec.source(function (opts) { |
93 | return sbot.createLogStream(opts) |
94 | }), |
95 | sbot_user_feed: rec.source(function (opts) { |
96 | return sbot.createUserStream(opts) |
97 | }), |
98 | sbot_get: rec.async(function (key, cb) { |
99 | sbot.get(key, cb) |
100 | }), |
101 | sbot_publish: rec.async(function (content, cb) { |
102 | if(content.recps) |
103 | content = ssbKeys.box(content, content.recps.map(function (e) { |
104 | return ref.isFeed(e) ? e : e.link |
105 | })) |
106 | |
107 | feed.add(content, function (err, msg) { |
108 | if(err) console.error(err) |
109 | else if(!cb) console.log(msg) |
110 | cb && cb(err, msg) |
111 | }) |
112 | }), |
113 | sbot_whoami: rec.async(function (cb) { |
114 | sbot.whoami(cb) |
115 | }) |
116 | } |
117 | } |
118 | |
119 | |
120 |
Built with git-ssb-web