Commit ec7786dfd8f6383e239dd576da6806e1b6bfe5d8
Merge pull request #1 from dominictarr/master
simplify usagePaul Frazee committed on 3/6/2015, 7:58:23 PM
Parent: 255aa84c011080723da25d6b83d454648e972fb3
Parent: 6cb576f11d7d777e55c0e841c33e0c636b11d101
Files changed
README.md | changed |
index.js | changed |
package.json | changed |
example.js | added |
README.md | |||
---|---|---|---|
@@ -2,44 +2,31 @@ | |||
2 | 2 … | ||
3 | 3 … | [scuttlebot](https://github.com/ssbc/scuttlebot) client | |
4 | 4 … | ||
5 | 5 … | ```js | |
6 | -var pull = require('pull-stream') | ||
7 | -var ssbkeys = require('ssb-keys') | ||
8 | -var ssbclient = require('ssb-client') | ||
9 | 6 … | ||
10 | -// generate a new keypair | ||
11 | -var keys = ssbkeys.generate() | ||
7 … | +var Client = require('./') | ||
12 | 8 … | ||
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) | ||
15 | 12 … | ||
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() | ||
19 | 26 … | }) | |
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 | -}) | ||
34 | 27 … | ||
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 | -} | ||
41 | 28 … | ``` | |
42 | 29 … | ||
43 | 30 … | ## License | |
44 | 31 … | ||
45 | -MIT, Copyright 2015 Paul Frazee and Dominic Tarr | ||
32 … | +MIT, Copyright 2015 Paul Frazee and Dominic Tarr |
index.js | ||
---|---|---|
@@ -3,57 +3,78 @@ | ||
3 | 3 … | var address = require('ssb-address') |
4 | 4 … | var ws = require('pull-ws-server') |
5 | 5 … | var Serializer = require('pull-serializer') |
6 | 6 … | var ssbKeys = require('ssb-keys') |
7 | -var manifest = require('ssb-manifest') | |
7 … | +var loadManf = require('ssb-manifest/load') | |
8 | 8 … | var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults')) |
9 | 9 … | |
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)() | |
12 | 23 … | client.keys = keys |
13 | 24 … | |
14 | 25 … | var wsStream |
15 | 26 … | var rpcStream |
16 | 27 … | |
17 | 28 … | client.connect = function(addr, cb) { |
18 | - addr = address(addr) | |
29 … | + if(isFunction(addr)) | |
30 … | + cb = addr, addr = null | |
31 … | + | |
32 … | + addr = address(config || addr) | |
19 | 33 … | if (wsStream) { |
20 | - wsStream.socket.close() | |
34 … | + wsStream.close() | |
21 | 35 … | client._emit('reconnecting') |
22 | 36 … | } |
23 | 37 … | |
38 … | + var called = false | |
39 … | + | |
24 | 40 … | 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 … | + | |
26 | 67 … | rpcStream = client.createStream() |
27 | 68 … | pull(wsStream, rpcStream, wsStream) |
28 | 69 … | |
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 | |
49 | 71 … | } |
50 | 72 … | |
51 | 73 … | 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 | |
56 | 77 … | } |
57 | 78 … | |
58 | 79 … | client.reconnect = function(opts) { |
59 | 80 … | opts = opts || {} |
@@ -62,8 +83,9 @@ | ||
62 | 83 … | setTimeout(client.connect.bind(client, client.addr), opts.wait) |
63 | 84 … | else |
64 | 85 … | client.connect(client.addr) |
65 | 86 … | }) |
87 … | + return client | |
66 | 88 … | } |
67 | 89 … | |
68 | 90 … | client.publish = function (content, cb) { |
69 | 91 … | client.getLatest(client.keys.id, function (err, prev) { |
@@ -82,8 +104,9 @@ | ||
82 | 104 … | var msg = createMsg(client.keys, null, content, prev||null) |
83 | 105 … | client.add(msg, cb) |
84 | 106 … | } |
85 | 107 … | }) |
108 … | + return client | |
86 | 109 … | } |
87 | 110 … | |
88 | 111 … | var auth_ = client.auth |
89 | 112 … | client.auth = function (cb) { |
@@ -92,13 +115,13 @@ | ||
92 | 115 … | ts: Date.now(), |
93 | 116 … | public: client.keys.public |
94 | 117 … | }) |
95 | 118 … | auth_.call(client, authReq, cb) |
119 … | + return client | |
96 | 120 … | } |
97 | 121 … | |
98 | - client.connect(addr, readyCb) | |
99 | 122 … | return client |
100 | 123 … | } |
101 | 124 … | |
102 | 125 … | function serialize (stream) { |
103 | 126 … | return Serializer(stream, JSON, {split: '\n\n'}) |
104 | -} | |
127 … | +} |
package.json | ||
---|---|---|
@@ -3,9 +3,9 @@ | ||
3 | 3 … | "version": "1.0.0", |
4 | 4 … | "description": "scuttlebot client", |
5 | 5 … | "main": "index.js", |
6 | 6 … | "scripts": { |
7 | - "test": "echo \"Error: no test specified\" && exit 1" | |
7 … | + "test": "set -e; for t in test/*.js; do node $t; done" | |
8 | 8 … | }, |
9 | 9 … | "repository": { |
10 | 10 … | "type": "git", |
11 | 11 … | "url": "https://github.com/ssbc/ssb-client.git" |
@@ -13,24 +13,22 @@ | ||
13 | 13 … | "dependencies": { |
14 | 14 … | "muxrpc": "~3.3.1", |
15 | 15 … | "pull-stream": "~2.26.0", |
16 | 16 … | "pull-serializer": "~0.3.2", |
17 | - "pull-ws-server": "~1.1.2", | |
17 … | + "pull-ws-server": "~1.3.0", | |
18 | 18 … | "secure-scuttlebutt": "~9.0.1", |
19 | 19 … | "ssb-address": "~1.0.0", |
20 | 20 … | "ssb-keys": "~0.4.1", |
21 | - "ssb-manifest": "~1.0.0" | |
21 … | + "ssb-manifest": "~1.0.0", | |
22 … | + "ssb-config": "~1.0.0" | |
22 | 23 … | }, |
23 | 24 … | "devDependencies": { |
24 | 25 … | "levelup": "~0.19.0", |
25 | 26 … | "level-sublevel": "~6.4.6", |
26 | 27 … | "memdown": "~1.0.0", |
27 | 28 … | "scuttlebot": "~3.0.0", |
28 | 29 … | "tape": "~2.12.3" |
29 | 30 … | }, |
30 | - "scripts": { | |
31 | - "test": "set -e; for t in test/*.js; do node $t; done" | |
32 | - }, | |
33 | 31 … | "author": "Paul Frazee <pfrazee@gmail.com>", |
34 | 32 … | "license": "MIT", |
35 | 33 … | "bugs": { |
36 | 34 … | "url": "https://github.com/ssbc/ssb-client/issues" |
example.js | ||
---|---|---|
@@ -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