Commit cb94bd0d83fae7b007a0e33061df18c620bd6cb1
make sbot apis into a plugin
Dominic Tarr committed on 5/29/2016, 11:13:41 AMParent: 682c62ee009cae46f4f5328ce86d4c2b938b2eb2
Files changed
index.js | changed |
sbot-api.js | added |
index.js | ||
---|---|---|
@@ -3,24 +3,32 @@ | ||
3 | 3 | var pull = require('pull-stream') |
4 | 4 | var combine = require('depject') |
5 | 5 | var fs = require('fs') |
6 | 6 | var path = require('path') |
7 | +var SbotApi = require('./sbot-api') | |
7 | 8 | |
8 | 9 | document.head.appendChild(h('style', fs.readFileSync('./style.css', 'utf8'))) |
9 | 10 | |
10 | 11 | var modules = fs.readdirSync(path.join(__dirname, 'modules')) |
11 | 12 | .map(function (e) { return require('./modules/'+e) }) |
12 | 13 | |
13 | 14 | var renderers = [] |
14 | 15 | var app = [] |
15 | -modules.unshift({app: app}) | |
16 | 16 | |
17 | -combine(modules) | |
18 | 17 | |
19 | 18 | var u = require('./util') |
20 | 19 | |
21 | 20 | require('ssb-client')(function (err, sbot) { |
22 | 21 | if(err) throw err |
22 | + | |
23 | + modules.unshift(SbotApi(sbot)) | |
24 | + modules.unshift({app: app}) | |
25 | + combine(modules) | |
26 | + | |
23 | 27 | document.body.appendChild(u.decorate(app, sbot)) |
24 | 28 | }) |
25 | 29 | |
26 | 30 | |
31 | + | |
32 | + | |
33 | + | |
34 | + |
sbot-api.js | ||
---|---|---|
@@ -1,0 +1,39 @@ | ||
1 | +var pull = require('pull-stream') | |
2 | +var crypto = require('crypto') | |
3 | + | |
4 | +function Hash (onHash) { | |
5 | + var hash = crypto.createHash('sha256') | |
6 | + return pull.through(function (data) { | |
7 | + hash.update( | |
8 | + 'string' === typeof data | |
9 | + ? new Buffer(data, 'utf8') | |
10 | + : data | |
11 | + ) | |
12 | + }, function (err) { | |
13 | + if(err && !onHash) throw err | |
14 | + onHash && onHash(err, '&'+hash.digest('base64')+'.sha256') | |
15 | + }) | |
16 | +} | |
17 | + | |
18 | + | |
19 | +module.exports = function (sbot) { | |
20 | + | |
21 | + return { | |
22 | + sbot_blobs_add: function (cb) { | |
23 | + return pull( | |
24 | + Hash(cb), | |
25 | + sbot.blobs.add() | |
26 | + ) | |
27 | + }, | |
28 | + sbot_links: function (query) { | |
29 | + return sbot.links(query) | |
30 | + }, | |
31 | + sbot_get: function (key, cb) { | |
32 | + sbot.get(key, cb) | |
33 | + }, | |
34 | + sbot_publish: function (msg, cb) { | |
35 | + sbot.publish(msg, cb) | |
36 | + } | |
37 | + } | |
38 | +} | |
39 | + |
Built with git-ssb-web