git ssb

2+

Dominic / ssb-client



Commit 231808394d23c64c26293d6f914c30beb963f78c

pull out .auth and .publish, add .createFeed

Paul Frazee committed on 3/6/2015, 9:23:22 PM
Parent: e7b4beb56321323338362cee9e112fa749bc385c

Files changed

index.jschanged
test/index.jschanged
index.jsView
@@ -1,12 +1,13 @@
1-var pull = require('pull-stream')
2-var muxrpc = require('muxrpc')
3-var address = require('ssb-address')
4-var ws = require('pull-ws-server')
5-var Serializer = require('pull-serializer')
6-var ssbKeys = require('ssb-keys')
7-var loadManf = require('ssb-manifest/load')
8-var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults'))
1 +var pull = require('pull-stream')
2 +var muxrpc = require('muxrpc')
3 +var address = require('ssb-address')
4 +var ws = require('pull-ws-server')
5 +var Serializer = require('pull-serializer')
6 +var ssbKeys = require('ssb-keys')
7 +var loadManf = require('ssb-manifest/load')
8 +var ssbFeed = require('secure-scuttlebutt/feed')
9 +var ssbDefaults = require('secure-scuttlebutt/defaults')
910
1011 function isFunction (f) {
1112 return 'function' === typeof f
1213 }
@@ -36,39 +37,26 @@
3637 if(isFunction(addr))
3738 cb = addr, addr = null
3839
3940 addr = address(addr || config)
41 + client.addr = addr
4042 if (wsStream) {
4143 wsStream.close()
4244 client._emit('reconnecting')
4345 }
4446
4547 var called = false
46-
47- client.addr = addr
48-
49- //if auth is not the first method called,
50- //then the other methods will get auth errors.
51- //since rpc calls are queued, we can just do it here.
52- client.auth(function (err, authed) {
53- if (err)
54- client._emit('error', err)
55- else
56- client._emit('authed', authed)
57- if(called) return
58- called = true; cb && cb(err, authed)
59- })
60-
6148 wsStream = ws.connect(addr, {
6249 onOpen: function() {
6350 client._emit('connect')
64- //cb is called after auth, just above
51 + if(called) return
52 + called = true; cb && cb()
6553 },
6654 onClose: function() {
6755 client._emit('close')
6856 //rpcStream will detect close on it's own.
6957 if(called) return
70- called = true; cb && cb(err, authed)
58 + called = true; cb && cb()
7159 }
7260 })
7361
7462 rpcStream = client.createStream()
@@ -93,40 +81,12 @@
9381 })
9482 return client
9583 }
9684
97- client.publish = function (content, cb) {
98- client.getLatest(client.keys.id, function (err, prev) {
99- if (!prev) {
100- var init = createMsg(client.keys, null, { type: 'init', public: client.keys.public }, null)
101- client.add(init, function (err, res) {
102- if (err)
103- return cb(err)
104- prev = res.value
105- next()
106- })
107- } else
108- next()
109-
110- function next () {
111- var msg = createMsg(client.keys, null, content, prev||null)
112- client.add(msg, cb)
113- }
114- })
115- return client
85 + client.createFeed = function (keys) {
86 + return ssbFeed(this, keys, ssbDefaults)
11687 }
11788
118- var auth_ = client.auth
119- client.auth = function (cb) {
120- var authReq = ssbKeys.signObj(client.keys, {
121- role: 'client',
122- ts: Date.now(),
123- public: client.keys.public
124- })
125- auth_.call(client, authReq, cb)
126- return client
127- }
128-
12989 return client
13090 }
13191
13292 function serialize (stream) {
test/index.jsView
@@ -10,18 +10,23 @@
1010 var server = scuttlebot({ port: 45451, host: 'localhost' }, ssb, ssb.createFeed()).use(require('scuttlebot/plugins/logging'))
1111
1212 var keys = ssbkeys.generate()
1313 var client = ssbclient(keys)
14- client.connect({ port: 45451, host: 'localhost' }, function (err) {
15- if (err)
16- throw err
17- })
18- client.publish({type: 'post', text: 'hello'}, function (err, data) {
19- if(err) throw err
14 + client.connect({ port: 45451, host: 'localhost' }, iferr)
15 + client.auth(ssbkeys.createAuth(keys), iferr)
16 +
17 + var feed = client.createFeed(keys)
18 + feed.add({type: 'post', text: 'hello'}, function (err, data) {
19 + iferr(err)
2020 t.equal(data.value.content.text, 'hello')
2121 console.log(data)
2222 client.close(function() {
2323 server.close()
2424 t.end()
2525 })
2626 })
27 +
28 + function iferr (err) {
29 + if (err)
30 + throw err
31 + }
2732 })

Built with git-ssb-web