Files: 9e6d7abeec492c4285f15bcf6ff689c60be4062b / sbot-api.js
3234 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 | |
35 | var rec = Reconnect(function (isConn) { |
36 | // var remote |
37 | // if('undefined' !== typeof localStorage) |
38 | // remote = localStorage.remote |
39 | |
40 | createClient(keys, { |
41 | manifest: require('./manifest.json'), |
42 | remote: require('./config')().remote |
43 | }, function (err, _sbot) { |
44 | if(err) { |
45 | console.error(err.stack) |
46 | isConn(err) |
47 | return |
48 | } |
49 | sbot = _sbot |
50 | sbot.on('closed', function () { |
51 | sbot = null |
52 | isConn(new Error('closed')) |
53 | }) |
54 | isConn() |
55 | }) |
56 | }) |
57 | |
58 | var internal = { |
59 | getLatest: rec.async(function (id, cb) { |
60 | sbot.getLatest(id, cb) |
61 | }), |
62 | add: rec.async(function (msg, cb) { |
63 | sbot.add(msg, cb) |
64 | }) |
65 | } |
66 | |
67 | var feed = createFeed(internal, keys) |
68 | |
69 | return { |
70 | sbot_blobs_add: rec.sink(function (cb) { |
71 | return pull( |
72 | Hash(function (err, id) { |
73 | if(err) return cb(err) |
74 | //completely UGLY hack to tell when the blob has been sucessfully written... |
75 | var start = Date.now(), n = 5 |
76 | ;(function next () { |
77 | setTimeout(function () { |
78 | sbot.blobs.has(id, function (err, has) { |
79 | if(has) return cb(null, id) |
80 | if(n--) next() |
81 | else cb(new Error('write failed')) |
82 | }) |
83 | }, Date.now() - start) |
84 | })() |
85 | }), |
86 | sbot.blobs.add() |
87 | ) |
88 | }), |
89 | sbot_links: rec.source(function (query) { |
90 | return sbot.links(query) |
91 | }), |
92 | sbot_links2: rec.source(function (query) { |
93 | return sbot.links2.read(query) |
94 | }), |
95 | sbot_query: rec.source(function (query) { |
96 | return sbot.query.read(query) |
97 | }), |
98 | sbot_log: rec.source(function (opts) { |
99 | return sbot.createLogStream(opts) |
100 | }), |
101 | sbot_user_feed: rec.source(function (opts) { |
102 | return sbot.createUserStream(opts) |
103 | }), |
104 | sbot_get: rec.async(function (key, cb) { |
105 | sbot.get(key, cb) |
106 | }), |
107 | sbot_publish: rec.async(function (content, cb) { |
108 | if(content.recps) |
109 | content = ssbKeys.box(content, content.recps.map(function (e) { |
110 | return ref.isFeed(e) ? e : e.link |
111 | })) |
112 | |
113 | feed.add(content, function (err, msg) { |
114 | if(err) console.error(err) |
115 | else if(!cb) console.log(msg) |
116 | cb && cb(err, msg) |
117 | }) |
118 | }), |
119 | sbot_whoami: rec.async(function (cb) { |
120 | sbot.whoami(cb) |
121 | }) |
122 | } |
123 | } |
124 | |
125 | |
126 | |
127 |
Built with git-ssb-web