git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 1d9d3847f73308f375aa4bf88926240ec24d4266

Files: 1d9d3847f73308f375aa4bf88926240ec24d4266 / sbot-api.js

3499 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(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 else if(content.mentions)
113 content.mentions.forEach(function (mention) {
114 if(ref.isBlob(mention.link)) {
115 sbot.blobs.push(mention.link, function (err) {
116 if(err) console.error(err)
117 })
118 }
119 })
120
121 feed.add(content, function (err, msg) {
122 if(err) console.error(err)
123 else if(!cb) console.log(msg)
124 cb && cb(err, msg)
125 })
126 }),
127 sbot_whoami: rec.async(function (cb) {
128 sbot.whoami(cb)
129 })
130 }
131}
132
133
134
135

Built with git-ssb-web