-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
355 lines (298 loc) · 9.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const { NanoresourcePromise: Nanoresource } = require('nanoresource-promise/emitter')
const HypercoreProtocol = require('hypercore-protocol')
const hyperswarm = require('hyperswarm')
const codecs = require('codecs')
const pump = require('pump')
const maybe = require('call-me-maybe')
const STREAM_PEER = Symbol('networker-stream-peer')
class CorestoreNetworker extends Nanoresource {
constructor (corestore, opts = {}) {
super()
this.corestore = corestore
this.opts = opts
this.keyPair = opts.keyPair || HypercoreProtocol.keyPair()
this._replicationOpts = {
encrypt: true,
live: true,
keyPair: this.keyPair,
onauthenticate: opts.onauthenticate
}
this.streams = new Set()
this.peers = new Set()
this._joined = new Set()
this._flushed = new Set()
this._configurations = new Map()
this._extensions = new Set()
this._streamsProcessing = 0
this._streamsProcessed = 0
this.setMaxListeners(0)
}
_replicate (protocolStream) {
// The initiator parameter here is ignored, since we're passing in a stream.
this.corestore.replicate(false, {
...this._replicationOpts,
stream: protocolStream
})
}
async _flush (keyString, keyBuf) {
await new Promise((resolve, reject) => {
this.swarm.flush(err => {
if (err) reject(err)
else resolve()
})
})
if (!this._joined.has(keyString)) {
return
}
const processingAfterFlush = this._streamsProcessing
if (this._streamsProcessed >= processingAfterFlush) {
this._flushed.add(keyString)
this.emit('flushed', keyBuf)
} else {
// Wait until the stream processing has caught up.
const processedListener = () => {
if (!this._joined.has(keyString)) {
this.removeListener('stream-processed', processedListener)
return
}
if (this._streamsProcessed >= processingAfterFlush) {
this._flushed.add(keyString)
this.emit('flushed', keyBuf)
this.removeListener('stream-processed', processedListener)
}
}
this.on('stream-processed', processedListener)
}
}
async _join (discoveryKey, opts = {}) {
const keyString = toString(discoveryKey)
const keyBuf = (discoveryKey instanceof Buffer) ? discoveryKey : Buffer.from(discoveryKey, 'hex')
this._joined.add(keyString)
this.emit('joined', keyBuf)
this.swarm.join(keyBuf, {
announce: opts.announce,
lookup: opts.lookup
})
const flushedProm = this._flush(keyString, keyBuf)
if (opts.flush !== false) await flushedProm
else flushedProm.catch(() => {})
}
_leave (discoveryKey) {
const keyString = toString(discoveryKey)
const keyBuf = (discoveryKey instanceof Buffer) ? discoveryKey : Buffer.from(discoveryKey, 'hex')
this._joined.delete(keyString)
return new Promise((resolve, reject) => {
this.swarm.leave(keyBuf, err => {
if (err) return reject(err)
return resolve()
})
})
}
_registerAllExtensions (peer) {
for (const ext of this._extensions) {
ext._registerExtension(peer)
}
}
_unregisterAllExtensions (peer) {
for (const ext of this._extensions) {
ext._unregisterExtension(peer)
}
}
_addStream (stream) {
this._replicate(stream)
this.streams.add(stream)
const peer = intoPeer(stream)
this.peers.add(peer)
stream[STREAM_PEER] = peer
this._registerAllExtensions(peer)
this.emit('peer-add', peer)
this.emit('handshake', stream)
}
_removeStream (stream) {
this.streams.delete(stream)
if (stream[STREAM_PEER]) {
const peer = stream[STREAM_PEER]
this._unregisterAllExtensions(peer)
this.peers.delete(peer)
this.emit('peer-remove', peer)
}
}
_open () {
const self = this
if (this.swarm) return
this.swarm = this.opts.swarm || hyperswarm({
...this.opts,
announceLocalNetwork: true,
queue: { multiplex: true }
})
this.swarm.on('error', err => this.emit('error', err))
this.swarm.on('connection', (socket, info) => {
const isInitiator = !!info.client
if (socket.remoteAddress === '::ffff:127.0.0.1' || socket.remoteAddress === '127.0.0.1') return null
var finishedHandshake = false
var processed = false
const protocolStream = new HypercoreProtocol(isInitiator, { ...this._replicationOpts })
protocolStream.on('handshake', () => {
const deduped = info.deduplicate(protocolStream.publicKey, protocolStream.remotePublicKey)
if (!deduped) {
finishedHandshake = true
self._addStream(protocolStream)
}
if (!processed) {
processed = true
this._streamsProcessed++
this.emit('stream-processed')
}
})
protocolStream.on('close', () => {
this.emit('stream-closed', protocolStream, info, finishedHandshake)
if (!processed) {
processed = true
this._streamsProcessed++
this.emit('stream-processed')
}
})
pump(socket, protocolStream, socket, err => {
if (err) this.emit('replication-error', err)
this._removeStream(protocolStream)
})
this.emit('stream-opened', protocolStream, info)
this._streamsProcessing++
})
}
async _close () {
if (!this.swarm) return null
for (const ext of this._extensions) {
ext.destroy()
}
this._extensions.clear()
for (const stream of this.streams) {
stream.destroy()
}
return new Promise((resolve, reject) => {
this.swarm.destroy(err => {
if (err) return reject(err)
this.swarm = null
return resolve()
})
})
}
listen () {
return this.open()
}
status (discoveryKey) {
if (Buffer.isBuffer(discoveryKey)) discoveryKey = discoveryKey.toString('hex')
return this._configurations.get(discoveryKey)
}
allStatuses () {
return [...this._configurations].map(([k, v]) => {
return {
discoveryKey: Buffer.from(k, 'hex'),
...v
}
})
}
configure (discoveryKey, opts = {}, cb) {
return maybeOptional(cb, this._configure(discoveryKey, opts))
}
async _configure (discoveryKey, opts) {
if (!this.swarm) this.open()
if (this.swarm && this.swarm.destroyed) return
const config = {
announce: opts.announce !== false,
lookup: opts.lookup !== false
}
opts = { ...opts, ...config }
const keyString = toString(discoveryKey)
const prev = this._configurations.get(keyString)
const joining = config.announce || config.lookup
if (joining) this._configurations.set(keyString, opts)
else this._configurations.delete(keyString)
if (joining) {
if (opts.rejoin === false && prev && prev.lookup === config.lookup && prev.announce === config.announce) return
return this._join(discoveryKey, opts)
} else {
return this._leave(discoveryKey)
}
}
joined (discoveryKey) {
if (typeof discoveryKey !== 'string') discoveryKey = discoveryKey.toString('hex')
return this._joined.has(discoveryKey)
}
flushed (discoveryKey) {
if (typeof discoveryKey !== 'string') discoveryKey = discoveryKey.toString('hex')
return this._flushed.has(discoveryKey)
}
registerExtension (name, handlers) {
if (name && typeof name === 'object') return this.registerExtension(null, name)
const ext = new SwarmExtension(this, name || handlers.name, handlers)
this._extensions.add(ext)
for (const peer of this.peers) {
ext._registerExtension(peer)
}
return ext
}
}
module.exports = CorestoreNetworker
class SwarmExtension {
constructor (networker, name, opts) {
if (typeof opts === 'function') opts = opts(this)
this.networker = networker
this.name = name
this.encoding = codecs((opts && opts.encoding) || 'binary')
this._peerExtensions = new Map()
this.onmessage = opts.onmessage
this.onerror = opts.onerror
}
_registerExtension (peer) {
peer.stream.extensions.exclusive = false
const peerExt = peer.stream.registerExtension(this.name, {
encoding: this.encoding,
onmessage: this.onmessage && (message => this.onmessage(message, peer)),
onerror: this.onerror && (err => this.onerror(err))
})
this._peerExtensions.set(peer, peerExt)
}
_unregisterExtension (peer) {
if (!this._peerExtensions.has(peer)) return
const peerExt = this._peerExtensions.get(peer)
peerExt.destroy()
this._peerExtensions.delete(peer)
}
broadcast (message) {
for (const peerExt of this._peerExtensions.values()) {
peerExt.send(message)
}
}
send (message, peer) {
const peerExt = this._peerExtensions.get(peer)
if (!peer) throw new Error('Peer must be specified.')
if (!peerExt) throw new Error('Extension not registered for peer ' + peer.remotePublicKey.toString('hex'))
peerExt.send(message)
}
destroy () {
for (const peerExt of this._peerExtensions.values()) {
peerExt.destroy()
}
this._peerExtensions.clear()
this.onmessage = null
this.onerror = null
}
}
function intoPeer (stream) {
return {
remotePublicKey: stream.remotePublicKey,
remoteAddress: stream.remoteAddress,
type: stream.remoteType,
stream
}
}
function toString (dk) {
return typeof dk === 'string' ? dk : dk.toString('hex')
}
function noop () {}
function maybeOptional (cb, prom) {
if (cb) maybe(cb, prom)
else prom.catch(noop)
return prom
}