This repository was archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathindex.js
208 lines (148 loc) · 5.06 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
'use strict'
const debug = require('debug')
const log = debug('libp2p:webrtc-star')
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const Id = require('peer-id')
const withIs = require('class-is')
const EE = require('events').EventEmitter
const assert = require('assert')
const SimplePeer = require('simple-peer')
const webrtcSupport = require('webrtcsupport')
const Connection = require('interface-connection').Connection
const toPull = require('stream-to-pull-stream')
const setImmediate = require('async/setImmediate')
const once = require('once')
const noop = once(() => {})
const {cleanMultiaddr} = require('./utils')
class WebRTCStar {
constructor (options) {
options = options || {}
if (options.wrtc) {
this.wrtc = options.wrtc
}
assert(options.exchange, 'Exchange missing!')
this.exchange = options.exchange
}
dial (ma, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
ma = cleanMultiaddr(String(ma))
callback = callback ? once(callback) : noop
let peerId = multiaddr(ma).getPeerId()
log('dialing %s (id=%s)', ma, peerId)
if (!peerId) {
return callback(new Error('Cannot dial peer: No Id provided!'))
}
const spOptions = { initiator: true, trickle: false }
// Use custom WebRTC implementation
if (this.wrtc) { spOptions.wrtc = this.wrtc }
const channel = new SimplePeer(spOptions)
const conn = new Connection(toPull.duplex(channel))
let connected = false
channel.on('signal', (signal) => {
log('dial#%s got signal', ma)
this.exchange.request(Id.createFromB58String(peerId), 'webrtc', Buffer.from(JSON.stringify({signal})), (err, result) => {
if (err) {
log('dial#%s exchange failed %s', ma, err)
return callback(err)
}
let offer
try {
offer = JSON.parse(String(result))
} catch (err) {
log('dial#%s malformed response %s', ma, err)
return callback(err)
}
channel.once('connect', () => {
log('dial#%s connected', ma)
connected = true
conn.destroy = channel.destroy.bind(channel)
channel.once('close', () => conn.destroy())
conn.getObservedAddrs = (callback) => callback(null, [ma])
callback(null, conn)
})
channel.signal(offer.signal)
})
})
channel.once('timeout', () => callback(new Error('timeout')))
channel.once('error', (err) => {
if (!connected) { callback(err) }
})
return conn
}
createListener (options, handler) {
if (typeof options === 'function') {
handler = options
options = {}
}
const listener = new EE()
listener.listen = (ma, callback) => {
callback = callback ? once(callback) : noop
if (!webrtcSupport.support && !this.wrtc) {
const err = new Error('No WebRTC support')
listener.emit('error', err)
return setImmediate(() => callback(err))
}
log('listening on %s', ma)
const ns = listener.ns = 'webrtc' // TODO: should this be ma.toString() ?
listener.ma = ma
this.exchange.handle(ns, (from, request, cb) => {
let offer
try {
offer = JSON.parse(String(request))
} catch (err) {
log('got malformed offer', err)
return cb(err)
}
const spOptions = { trickle: false }
// Use custom WebRTC implementation
if (this.wrtc) { spOptions.wrtc = this.wrtc }
const channel = new SimplePeer(spOptions)
const conn = new Connection(toPull.duplex(channel))
const remoteMa = '/p2p-webrtc-star/ipfs/' + from.toB58String()
log('incoming connection %s', remoteMa)
channel.once('connect', () => {
log('%s: connected', remoteMa)
conn.getObservedAddrs = (callback) => {
return callback(null, [multiaddr(remoteMa)])
}
listener.emit('connection', conn)
handler(conn)
})
channel.once('signal', (signal) => {
log('%s: sending back signal', remoteMa)
cb(null, Buffer.from(JSON.stringify({signal})))
})
// TODO: add error response?
channel.signal(offer.signal)
})
listener.emit('listening')
setImmediate(() => callback())
}
listener.close = (callback) => {
callback = callback ? once(callback) : noop
this.exchange.unhandle(listener.ns)
listener.emit('close')
setImmediate(callback)
}
listener.getAddrs = (callback) => {
setImmediate(() => callback(null, listener.ma ? [listener.ma] : []))
}
return listener
}
filter (multiaddrs) {
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
return multiaddrs.filter((ma) => {
if (ma.protoNames().indexOf('p2p-circuit') > -1) {
return false
}
return mafmt.WebRTCStar.matches(ma)
})
}
}
module.exports = withIs(WebRTCStar, { className: 'WebRTCStar', symbolName: '@libp2p/js-libp2p-webrtc-star/webrtcstar' })