-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
225 lines (193 loc) · 5.92 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
/* global Y */
'use strict'
const log = require('debug')('y-ipfs-connector')
const EventEmitter = require('events')
const Room = require('ipfs-pubsub-room')
const Queue = require('async/queue')
const Buffer = require('safe-buffer').Buffer
const encode = require('./encode')
const decode = require('./decode')
function extend (Y) {
class YIpfsConnector extends Y.AbstractConnector {
constructor (y, options) {
if (options === undefined) {
throw new Error('Options must not be undefined!')
}
if (options.room == null) {
throw new Error('You must define a room name!')
}
if (!options.ipfs) {
throw new Error('You must define a started IPFS object inside options')
}
if (!options.role) { options.role = 'master' }
super(y, options)
this._yConnectorOptions = options
this.ipfs = options.ipfs
const topic = this.ipfsPubSubTopic = 'y-ipfs:rooms:' + options.room
this.roomEmitter = options.roomEmitter || new EventEmitter()
this.roomEmitter.peers = () => this._room.getPeers()
this.roomEmitter.id = () => topic
this._receiveQueue = Queue(this._processQueue.bind(this), 1)
this._room = Room(this.ipfs, topic)
this._room.setMaxListeners(Infinity)
this._room.on('error', (err) => {
console.error(err)
})
this._room.on('message', (msg) => {
if (this.ipfs._peerInfo && msg.from === this.ipfs._peerInfo.id.toB58String()) {
return
}
const processMessage = () => {
let message
if (this._yConnectorOptions.decode) {
message = this._yConnectorOptions.decode(msg.data)
} else {
message = decode(msg.data)
}
const proceed = () => {
const yMessage = decode(message.payload)
this.roomEmitter.emit('received message', msg.from, yMessage)
if (yMessage.type === null) {
return
}
this._queueReceiveMessage(msg.from, yMessage)
}
if (options.verifySignature) {
const sig = message.signature && Buffer.from(message.signature, 'base64')
options.verifySignature.call(
null,
msg.from,
Buffer.from(message.payload),
sig,
(err, valid) => {
if (err) {
console.error(
'Error verifying signature from peer ' + msg.from +
'. Discarding message.', err)
return
}
if (!valid) {
console.error(
'Invalid signature from peer ' + msg.from +
'. Discarding message.')
return
}
proceed()
}
)
} else {
proceed()
}
}
if (!this._room.hasPeer(msg.from)) {
const joinedListener = (peer) => {
if (peer === msg.from) {
this._room.removeListener('peer joined', joinedListener)
processMessage()
}
}
this._room.on('peer joined', joinedListener)
} else {
processMessage()
}
})
this._room.on('peer joined', (peer) => {
this.roomEmitter.emit('peer joined', peer)
this.userJoined(peer, 'master')
})
this._room.on('peer left', (peer) => {
this.roomEmitter.emit('peer left', peer)
this.userLeft(peer)
})
if (this.ipfs.isOnline()) {
this._start()
} else {
this.ipfs.once('ready', this._start.bind(this))
}
}
_queueReceiveMessage (from, message) {
this._receiveQueue.push({
from: from,
message: message
})
}
_processQueue (item, callback) {
const from = item.from
const message = item.message
if (from === this._ipfsUserId) {
// ignore message from self
callback()
} else if (this._room.hasPeer(from)) {
this.receiveMessage(from, message)
callback()
} else {
this._receiveQueue.unshift(item)
setTimeout(callback, 500)
}
}
_start () {
const id = this.ipfs._peerInfo.id.toB58String()
this._ipfsUserId = id
this.setUserId(id)
}
disconnect () {
log('disconnect')
this._room.leave()
super.disconnect()
}
send (peer, message) {
this._encodeMessage(message, (err, encodedMessage) => {
if (err) {
throw err
}
if (this._yConnectorOptions.encode) {
encodedMessage = this._yConnectorOptions.encode(encodedMessage)
}
this._room.sendTo(peer, encodedMessage)
this.roomEmitter.emit('sent message', peer, message)
})
}
broadcast (message) {
this._encodeMessage(message, (err, encodedMessage) => {
if (err) {
throw err
}
if (this._yConnectorOptions.encode) {
encodedMessage = this._yConnectorOptions.encode(encodedMessage)
}
this._room.broadcast(encodedMessage)
this.roomEmitter.emit('sent message', 'broadcast', message)
})
}
isDisconnected () {
return false
}
_encodeMessage (_message, callback) {
const message = localEncode(_message)
if (this._yConnectorOptions.sign) {
this._yConnectorOptions.sign(Buffer.from(message), (err, signature) => {
if (err) {
return callback(err)
}
const sig = signature.toString('base64')
callback(null, encode({
signature: sig,
payload: message
}))
})
} else {
callback(null, encode({
payload: message
}))
}
}
}
Y.extend('ipfs', YIpfsConnector)
}
module.exports = extend
if (typeof Y !== 'undefined') {
extend(Y)
}
function localEncode (m) {
return JSON.stringify(m)
}