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