git ssb

0+

dangerousbeans / patchbay-bootstrap



Tree: 6a04c1500ae5d062c3a688cf9677d5229a8ca9b1

Files: 6a04c1500ae5d062c3a688cf9677d5229a8ca9b1 / sbot-api.js

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

Built with git-ssb-web