-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
213 lines (181 loc) · 6.13 KB
/
index.js
File metadata and controls
213 lines (181 loc) · 6.13 KB
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
const crypto = require('crypto')
const { EventEmitter } = require('events')
const { promisify } = require('util')
const dwebxEncoding = require('dwebx-encoding')
const ddatabaseProtocol = require('ddatabase-protocol')
const dwebswarm = require('dwebswarm')
const pump = require('pump')
const eos = require('end-of-stream')
const log = require('debug')('dwebstore:network')
const OUTER_STREAM = Symbol('dwebstore-outer-stream')
class SwarmNetworker extends EventEmitter {
constructor (dwebstore, opts = {}) {
super()
this.dwebstore = dwebstore
this.id = opts.id || crypto.randomBytes(32)
this.opts = opts
this.keyPair = opts.keyPair || ddatabaseProtocol.keyPair()
this._replicationOpts = {
id: this.id,
encrypt: true,
live: true,
keyPair: this.keyPair
}
this.streams = []
this._joined = new Set()
this._flushed = new Set()
this._streamsProcessing = 0
this._streamsProcessed = 0
// Set in listen
this.swarm = null
this.setMaxListeners(0)
}
_replicate (protocolStream) {
// The initiator parameter here is ignored, since we're passing in a stream.
this.dwebstore.replicate(false, {
...this._replicationOpts,
stream: protocolStream
})
}
listen () {
const self = this
if (this.swarm) return
this.swarm = dwebswarm({
...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
const peerInfo = info.peer
const discoveryKey = peerInfo && peerInfo.topic
var finishedHandshake = false
var processed = false
const protocolStream = new ddatabaseProtocol(isInitiator, { ...this._replicationOpts })
protocolStream.on('handshake', () => {
const deduped = info.deduplicate(protocolStream.publicKey, protocolStream.remotePublicKey)
if (!deduped) onhandshake()
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)
const idx = this.streams.indexOf(protocolStream)
if (idx === -1) return
this.streams.splice(idx, 1)
})
this.emit('stream-opened', protocolStream, info)
this._streamsProcessing++
function onhandshake () {
finishedHandshake = true
self._replicate(protocolStream)
self.streams.push(protocolStream)
self.emit('handshake', protocolStream, info)
}
})
}
status (discoveryKey) {
return this.swarm && this.swarm.status(discoveryKey)
}
async join (discoveryKey, opts = {}) {
if (this.swarm && this.swarm.destroyed) return null
if (!this.swarm) {
this.listen()
return this.join(discoveryKey, opts)
}
const self = this
const keyString = (typeof discoveryKey === 'string') ? discoveryKey : dwebxEncoding.encode(discoveryKey)
const keyBuf = (discoveryKey instanceof Buffer) ? discoveryKey : dwebxEncoding.decode(discoveryKey)
this._joined.add(keyString)
this.emit('joined', keyBuf)
this.swarm.join(keyBuf, {
announce: opts.announce !== false,
lookup: opts.lookup !== false,
})
if (opts.flush !== false) {
await promisify(this.swarm.flush.bind(this.swarm))()
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 leave (discoveryKey) {
if (this.swarm && this.swarm.destroyed) return
if (!this.swarm) {
this.listen()
return this.leave(discoveryKey)
}
const keyString = (typeof discoveryKey === 'string') ? discoveryKey : dwebxEncoding.encode(discoveryKey)
const keyBuf = (discoveryKey instanceof Buffer) ? discoveryKey : dwebxEncoding.decode(discoveryKey)
this._joined.delete(keyString)
await new Promise((resolve, reject) => {
this.swarm.leave(keyBuf, err => {
if (err) return reject(err)
return resolve()
})
})
for (let stream of this.streams) {
stream.close(keyBuf)
}
}
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)
}
async close () {
if (!this.swarm) return null
const leaving = [...this._joined].map(dkey => this.leave(dkey))
await Promise.all(leaving)
const closingStreams = this.streams.map(stream => {
return new Promise(resolve => {
stream.destroy()
eos(stream, () => resolve())
})
})
await Promise.all(closingStreams)
return new Promise((resolve, reject) => {
this.swarm.destroy(err => {
if (err) return reject(err)
this.swarm = null
return resolve()
})
})
}
}
module.exports = SwarmNetworker