index.jsView |
---|
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') |
9 | 10 … | |
10 | 11 … | function isFunction (f) { |
11 | 12 … | return 'function' === typeof f |
12 | 13 … | } |
|
36 | 37 … | if(isFunction(addr)) |
37 | 38 … | cb = addr, addr = null |
38 | 39 … | |
39 | 40 … | addr = address(addr || config) |
| 41 … | + client.addr = addr |
40 | 42 … | if (wsStream) { |
41 | 43 … | wsStream.close() |
42 | 44 … | client._emit('reconnecting') |
43 | 45 … | } |
44 | 46 … | |
45 | 47 … | var called = false |
46 | | - |
47 | | - client.addr = addr |
48 | | - |
49 | | - |
50 | | - |
51 | | - |
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 | | - |
61 | 48 … | wsStream = ws.connect(addr, { |
62 | 49 … | onOpen: function() { |
63 | 50 … | client._emit('connect') |
64 | | - |
| 51 … | + if(called) return |
| 52 … | + called = true; cb && cb() |
65 | 53 … | }, |
66 | 54 … | onClose: function() { |
67 | 55 … | client._emit('close') |
68 | 56 … | |
69 | 57 … | if(called) return |
70 | | - called = true; cb && cb(err, authed) |
| 58 … | + called = true; cb && cb() |
71 | 59 … | } |
72 | 60 … | }) |
73 | 61 … | |
74 | 62 … | rpcStream = client.createStream() |
93 | 81 … | }) |
94 | 82 … | return client |
95 | 83 … | } |
96 | 84 … | |
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) |
116 | 87 … | } |
117 | 88 … | |
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 | | - |
129 | 89 … | return client |
130 | 90 … | } |
131 | 91 … | |
132 | 92 … | function serialize (stream) { |