git ssb

0+

dangerousbeans / patchbay-bootstrap



Tree: 59211949b2fd8d2c1c3b1584a297219e9b85a907

Files: 59211949b2fd8d2c1c3b1584a297219e9b85a907 / sbot-api.js

2728 bytesRaw
1var pull = require('pull-stream')
2var crypto = require('crypto')
3var ref = require('ssb-ref')
4var Reconnect = require('pull-reconnect')
5
6
7function 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')
23var createClient = require('ssb-client')
24
25var createConfig = require('ssb-config/inject')
26
27var createFeed = require('ssb-feed')
28var keys = require('./keys')
29var ssbKeys = require('ssb-keys')
30
31module.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(cb),
73 sbot.blobs.add()
74 )
75 }),
76 sbot_links: rec.source(function (query) {
77 return sbot.links(query)
78 }),
79 sbot_links2: rec.source(function (query) {
80 return sbot.links2.read(query)
81 }),
82 sbot_query: rec.source(function (query) {
83 return sbot.query.read(query)
84 }),
85 sbot_log: rec.source(function (opts) {
86 return sbot.createLogStream(opts)
87 }),
88 sbot_user_feed: rec.source(function (opts) {
89 return sbot.createUserStream(opts)
90 }),
91 sbot_get: rec.async(function (key, cb) {
92 sbot.get(key, cb)
93 }),
94 sbot_publish: rec.async(function (content, cb) {
95 if(content.recps)
96 content = ssbKeys.box(content, content.recps.map(function (e) {
97 return ref.isFeed(e) ? e : e.link
98 }))
99
100 feed.add(content, function (err, msg) {
101 if(err) console.error(err)
102 else if(!cb) console.log(msg)
103 cb && cb(err, msg)
104 })
105 }),
106 sbot_whoami: rec.async(function (cb) {
107 sbot.whoami(cb)
108 })
109 }
110}
111
112

Built with git-ssb-web