git ssb

16+

Dominic / patchbay



Tree: 0eaf17392ff36c30135c6f4ef3eb9c8ee0c28faf

Files: 0eaf17392ff36c30135c6f4ef3eb9c8ee0c28faf / sbot-api.js

2238 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 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