git ssb

16+

Dominic / patchbay



Tree: 1d7dbb5c6e686bd2ced063869d27d82cd1111a1f

Files: 1d7dbb5c6e686bd2ced063869d27d82cd1111a1f / sbot-api.js

2210 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(keys, opts, function (err, _sbot) {
35 if(err) {console.error(err.stack); isConn(err)}
36 sbot = _sbot
37 sbot.on('closed', function () {
38 sbot = null
39 isConn(new Error('closed'))
40 })
41 isConn()
42 })
43 })
44
45 var internal = {
46 getLatest: rec.async(function (id, cb) {
47 sbot.getLatest(id, cb)
48 }),
49 add: rec.async(function (msg, cb) {
50 sbot.add(msg, cb)
51 })
52 }
53
54 var feed = createFeed(internal, keys)
55
56 return {
57 sbot_blobs_add: rec.sink(function (cb) {
58 return pull(
59 Hash(cb),
60 sbot.blobs.add()
61 )
62 }),
63 sbot_links: rec.source(function (query) {
64 return sbot.links(query)
65 }),
66 sbot_links2: rec.source(function (query) {
67 return sbot.links2.read(query)
68 }),
69 sbot_query: rec.source(function (query) {
70 return sbot.query.read(query)
71 }),
72 sbot_log: rec.source(function (opts) {
73 return sbot.createLogStream(opts)
74 }),
75 sbot_user_feed: rec.source(function (opts) {
76 return sbot.createUserStream(opts)
77 }),
78 sbot_get: rec.async(function (key, cb) {
79 sbot.get(key, cb)
80 }),
81 sbot_publish: rec.async(function (msg, cb) {
82 feed.add(msg, function (err, msg) {
83 cb(err, msg)
84 })
85 }),
86 sbot_whoami: rec.async(function (cb) {
87 sbot.whoami(cb)
88 })
89 }
90}
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

Built with git-ssb-web