git ssb

2+

Dominic / ssb-client



Commit ec7786dfd8f6383e239dd576da6806e1b6bfe5d8

Merge pull request #1 from dominictarr/master

simplify usage
Paul Frazee committed on 3/6/2015, 7:58:23 PM
Parent: 255aa84c011080723da25d6b83d454648e972fb3
Parent: 6cb576f11d7d777e55c0e841c33e0c636b11d101

Files changed

README.mdchanged
index.jschanged
package.jsonchanged
example.jsadded
README.mdView
@@ -2,44 +2,31 @@
22
33 [scuttlebot](https://github.com/ssbc/scuttlebot) client
44
55 ```js
6-var pull = require('pull-stream')
7-var ssbkeys = require('ssb-keys')
8-var ssbclient = require('ssb-client')
96
10-// generate a new keypair
11-var keys = ssbkeys.generate()
7 +var Client = require('./')
128
13-// connect to the local scuttlebot
14-var client = ssbclient(keys, 'localhost', doWork)
9 +var config = require('ssb-config')
10 +var ssbKeys = require('ssb-keys')
11 +var keys = ssbKeys.loadOrCreateSync(config)
1512
16-// listen to connection events
17-client.on('connect', function () {
18- console.log('connection established, authenticating...')
13 +function abortIf (err) {
14 + if(err) throw err
15 +}
16 +
17 +var client = Client(keys, config)
18 + .connect(abortIf) //auth is automatic
19 +
20 +client.publish({
21 + type: 'post', text: 'hello, world!'
22 +}, function (err, msg) {
23 + abortIf(err)
24 + console.log(msg)
25 + client.close()
1926 })
20-client.on('authed', function () {
21- console.log('authed and ready to go')
22-})
23-client.on('error', function (err) {
24- console.log('authentication error', err)
25- client.reconnect({ wait: 5000 })
26-})
27-client.on('close', function (err) {
28- console.log('connection closed')
29- client.reconnect({ wait: 5000 })
30-})
31-client.on('reconnecting', function () {
32- console.log('attempting reconnect...')
33-})
3427
35-// make calls to the scuttlebot api
36-function doWork() {
37- // authed and ready to go
38- pull(client.createFeedStream(), pull.drain(console.log))
39- client.publish({ type: 'post', text: 'hello, world!' }, console.log)
40-}
4128 ```
4229
4330 ## License
4431
45-MIT, Copyright 2015 Paul Frazee and Dominic Tarr
32 +MIT, Copyright 2015 Paul Frazee and Dominic Tarr
index.jsView
@@ -3,57 +3,78 @@
33 var address = require('ssb-address')
44 var ws = require('pull-ws-server')
55 var Serializer = require('pull-serializer')
66 var ssbKeys = require('ssb-keys')
7-var manifest = require('ssb-manifest')
7 +var loadManf = require('ssb-manifest/load')
88 var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults'))
99
10-module.exports = function (keys, addr, readyCb) {
11- var client = muxrpc(manifest, false, serialize)()
10 +function isFunction (f) {
11 + return 'function' === typeof f
12 +}
13 +
14 +function throwIfError(err) {
15 + if(err) throw err
16 +}
17 +
18 +module.exports = function (keys, config) {
19 + var manifest
20 + //if we are in the browser
21 + config.host = config.host || 'localhost'
22 + var client = muxrpc(loadManf(config), false, serialize)()
1223 client.keys = keys
1324
1425 var wsStream
1526 var rpcStream
1627
1728 client.connect = function(addr, cb) {
18- addr = address(addr)
29 + if(isFunction(addr))
30 + cb = addr, addr = null
31 +
32 + addr = address(config || addr)
1933 if (wsStream) {
20- wsStream.socket.close()
34 + wsStream.close()
2135 client._emit('reconnecting')
2236 }
2337
38 + var called = false
39 +
2440 client.addr = addr
25- wsStream = ws.connect(addr)
41 +
42 + //if auth is not the first method called,
43 + //then the other methods will get auth errors.
44 + //since rpc calls are queued, we can just do it here.
45 + client.auth(function (err, authed) {
46 + if (err)
47 + client._emit('error', err)
48 + else
49 + client._emit('authed', authed)
50 + if(called) return
51 + called = true; cb && cb(err, authed)
52 + })
53 +
54 + wsStream = ws.connect(addr, {
55 + onOpen: function() {
56 + client._emit('connect')
57 + //cb is called after auth, just above
58 + },
59 + onClose: function() {
60 + client._emit('close')
61 + //rpcStream will detect close on it's own.
62 + if(called) return
63 + called = true; cb && cb(err, authed)
64 + }
65 + })
66 +
2667 rpcStream = client.createStream()
2768 pull(wsStream, rpcStream, wsStream)
2869
29- var onopen_ = wsStream.socket.onopen
30- wsStream.socket.onopen = function() {
31- onopen_()
32- client._emit('connect')
33-
34- client.auth(function (err, authed) {
35- if (err)
36- client._emit('error', err)
37- else
38- client._emit('authed', authed)
39- cb && cb(err, authed)
40- })
41- }
42-
43- var onclose_ = wsStream.socket.onclose
44- wsStream.socket.onclose = function() {
45- onclose_ && onclose_()
46- rpcStream.close(function(){})
47- client._emit('close')
48- }
70 + return client
4971 }
5072
5173 client.close = function(cb) {
52- wsStream.socket.close()
53- rpcStream.close(function () {
54- cb && cb()
55- })
74 + wsStream.close()
75 + rpcStream.close(cb ? cb : throwIfError)
76 + return client
5677 }
5778
5879 client.reconnect = function(opts) {
5980 opts = opts || {}
@@ -62,8 +83,9 @@
6283 setTimeout(client.connect.bind(client, client.addr), opts.wait)
6384 else
6485 client.connect(client.addr)
6586 })
87 + return client
6688 }
6789
6890 client.publish = function (content, cb) {
6991 client.getLatest(client.keys.id, function (err, prev) {
@@ -82,8 +104,9 @@
82104 var msg = createMsg(client.keys, null, content, prev||null)
83105 client.add(msg, cb)
84106 }
85107 })
108 + return client
86109 }
87110
88111 var auth_ = client.auth
89112 client.auth = function (cb) {
@@ -92,13 +115,13 @@
92115 ts: Date.now(),
93116 public: client.keys.public
94117 })
95118 auth_.call(client, authReq, cb)
119 + return client
96120 }
97121
98- client.connect(addr, readyCb)
99122 return client
100123 }
101124
102125 function serialize (stream) {
103126 return Serializer(stream, JSON, {split: '\n\n'})
104-}
127 +}
package.jsonView
@@ -3,9 +3,9 @@
33 "version": "1.0.0",
44 "description": "scuttlebot client",
55 "main": "index.js",
66 "scripts": {
7- "test": "echo \"Error: no test specified\" && exit 1"
7 + "test": "set -e; for t in test/*.js; do node $t; done"
88 },
99 "repository": {
1010 "type": "git",
1111 "url": "https://github.com/ssbc/ssb-client.git"
@@ -13,24 +13,22 @@
1313 "dependencies": {
1414 "muxrpc": "~3.3.1",
1515 "pull-stream": "~2.26.0",
1616 "pull-serializer": "~0.3.2",
17- "pull-ws-server": "~1.1.2",
17 + "pull-ws-server": "~1.3.0",
1818 "secure-scuttlebutt": "~9.0.1",
1919 "ssb-address": "~1.0.0",
2020 "ssb-keys": "~0.4.1",
21- "ssb-manifest": "~1.0.0"
21 + "ssb-manifest": "~1.0.0",
22 + "ssb-config": "~1.0.0"
2223 },
2324 "devDependencies": {
2425 "levelup": "~0.19.0",
2526 "level-sublevel": "~6.4.6",
2627 "memdown": "~1.0.0",
2728 "scuttlebot": "~3.0.0",
2829 "tape": "~2.12.3"
2930 },
30- "scripts": {
31- "test": "set -e; for t in test/*.js; do node $t; done"
32- },
3331 "author": "Paul Frazee <pfrazee@gmail.com>",
3432 "license": "MIT",
3533 "bugs": {
3634 "url": "https://github.com/ssbc/ssb-client/issues"
example.jsView
@@ -1,0 +1,19 @@
1 +
2 +var Client = require('./')
3 +
4 +var config = require('ssb-config')
5 +var ssbKeys = require('ssb-keys')
6 +var keys = ssbKeys.loadOrCreateSync(config)
7 +
8 +function abortIf (err) {
9 + if(err) throw err
10 +}
11 +
12 +var client = Client(keys, config)
13 + .connect(abortIf)
14 +
15 +client.whoami(function (err, whoami) {
16 + abortIf(err)
17 + console.log(whoami)
18 + client.close()
19 +})

Built with git-ssb-web