@leofcoin/peernet 0.14.18 → 0.14.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,827 @@
1
+ (self["webpackChunk_leofcoin_peernet"] = self["webpackChunk_leofcoin_peernet"] || []).push([[629],{
2
+
3
+ /***/ 96:
4
+ /***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
5
+
6
+ // ESM COMPAT FLAG
7
+ __webpack_require__.r(__webpack_exports__);
8
+
9
+ // EXPORTS
10
+ __webpack_require__.d(__webpack_exports__, {
11
+ "default": function() { return /* binding */ Client; }
12
+ });
13
+
14
+ ;// CONCATENATED MODULE: ./node_modules/socket-request-client/dist/es/index.js
15
+ /* provided dependency */ var process = __webpack_require__(155);
16
+ /* socket-request-client version 1.6.3 */
17
+ const ENVIRONMENT = {version: '1.6.3', production: true};
18
+
19
+ class LittlePubSub {
20
+ constructor(verbose = true) {
21
+ this.subscribers = {};
22
+ this.verbose = verbose;
23
+ }
24
+ subscribe(event, handler, context) {
25
+ if (typeof context === 'undefined') {
26
+ context = handler;
27
+ }
28
+ this.subscribers[event] = this.subscribers[event] || { handlers: [], value: null};
29
+ this.subscribers[event].handlers.push(handler.bind(context));
30
+ }
31
+ unsubscribe(event, handler, context) {
32
+ if (typeof context === 'undefined') {
33
+ context = handler;
34
+ }
35
+ if (this.subscribers[event]) {
36
+ const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
37
+ this.subscribers[event].handlers.splice(index);
38
+ if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
39
+ }
40
+ }
41
+ publish(event, change) {
42
+ if (this.subscribers[event]) {
43
+ if (this.verbose || this.subscribers[event].value !== change) {
44
+ this.subscribers[event].value = change;
45
+ this.subscribers[event].handlers.forEach(handler => {
46
+ handler(change, this.subscribers[event].value);
47
+ });
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ var clientApi = _pubsub => {
54
+ const subscribe = (topic, cb) => {
55
+ _pubsub.subscribe(topic, cb);
56
+ };
57
+ const unsubscribe = (topic, cb) => {
58
+ _pubsub.unsubscribe(topic, cb);
59
+ };
60
+ const publish = (topic, value) => {
61
+ _pubsub.publish(topic, value);
62
+ };
63
+ const _connectionState = (state) => {
64
+ switch (state) {
65
+ case 0:
66
+ return 'connecting'
67
+ case 1:
68
+ return 'open'
69
+ case 2:
70
+ return 'closing'
71
+ case 3:
72
+ return 'closed'
73
+ }
74
+ };
75
+ const request = (client, request) => {
76
+ return new Promise((resolve, reject) => {
77
+ const state = _connectionState(client.readyState);
78
+ if (state !== 'open') return reject(`coudn't send request to ${client.id}, no open connection found.`)
79
+ request.id = Math.random().toString(36).slice(-12);
80
+ const handler = result => {
81
+ if (result && result.error) return reject(result.error)
82
+ resolve({result, id: request.id, handler});
83
+ unsubscribe(request.id, handler);
84
+ };
85
+ subscribe(request.id, handler);
86
+ send(client, request);
87
+ });
88
+ };
89
+ const send = async (client, request) => {
90
+ return client.send(JSON.stringify(request))
91
+ };
92
+ const pubsub = client => {
93
+ return {
94
+ publish: (topic = 'pubsub', value) => {
95
+ return send(client, {url: 'pubsub', params: { topic, value }})
96
+ },
97
+ subscribe: (topic = 'pubsub', cb) => {
98
+ subscribe(topic, cb);
99
+ return send(client, {url: 'pubsub', params: { topic, subscribe: true }})
100
+ },
101
+ unsubscribe: (topic = 'pubsub', cb) => {
102
+ unsubscribe(topic, cb);
103
+ return send(client, {url: 'pubsub', params: { topic, unsubscribe: true }})
104
+ },
105
+ subscribers: _pubsub.subscribers
106
+ }
107
+ };
108
+ const server = (client) => {
109
+ return {
110
+ uptime: async () => {
111
+ try {
112
+ const { result, id, handler } = await request(client, {url: 'uptime'});
113
+ unsubscribe(id, handler);
114
+ return result
115
+ } catch (e) {
116
+ throw e
117
+ }
118
+ },
119
+ ping: async () => {
120
+ try {
121
+ const now = new Date().getTime();
122
+ const { result, id, handler } = await request(client, {url: 'ping'});
123
+ unsubscribe(id, handler);
124
+ return (Number(result) - now)
125
+ } catch (e) {
126
+ throw e
127
+ }
128
+ }
129
+ }
130
+ };
131
+ const peernet = (client) => {
132
+ return {
133
+ join: async (params) => {
134
+ try {
135
+ params.join = true;
136
+ const requested = { url: 'peernet', params };
137
+ const { result, id, handler } = await request(client, requested);
138
+ unsubscribe(id, handler);
139
+ return result
140
+ } catch (e) {
141
+ throw e
142
+ }
143
+ },
144
+ leave: async (params) => {
145
+ try {
146
+ params.join = false;
147
+ const requested = { url: 'peernet', params };
148
+ const { result, id, handler } = await request(client, requested);
149
+ unsubscribe(id, handler);
150
+ return result
151
+ } catch (e) {
152
+ throw e
153
+ }
154
+ }
155
+ }
156
+ };
157
+ return { send, request, pubsub, server, subscribe, unsubscribe, publish, peernet }
158
+ };
159
+
160
+ if (!globalThis.PubSub) globalThis.PubSub = LittlePubSub;
161
+ if (!globalThis.pubsub) globalThis.pubsub = new LittlePubSub({verbose: false});
162
+ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry: false }) => {
163
+ const { retry } = options;
164
+ const api = clientApi(pubsub);
165
+ let tries = 0;
166
+ const onerror = error => {
167
+ if (pubsub.subscribers['error']) {
168
+ pubsub.publish('error', error);
169
+ } else {
170
+ console.error(error);
171
+ }
172
+ };
173
+ const onmessage = message => {
174
+ const {value, url, status, id} = JSON.parse(message.data.toString());
175
+ const publisher = id ? id : url;
176
+ if (status === 200) {
177
+ pubsub.publish(publisher, value);
178
+ } else {
179
+ pubsub.publish(publisher, {error: value});
180
+ }
181
+ };
182
+ const clientConnection = client => {
183
+ const startTime = new Date().getTime();
184
+ return {
185
+ client,
186
+ request: async req => {
187
+ const { result, id, handler } = await api.request(client, req);
188
+ pubsub.unsubscribe(id, handler);
189
+ return result
190
+ },
191
+ send: req => api.send(client, req),
192
+ subscribe: api.subscribe,
193
+ unsubscribe: api.unsubscribe,
194
+ subscribers: api.subscribers,
195
+ publish: api.publish,
196
+ pubsub: api.pubsub(client),
197
+ uptime: () => {
198
+ const now = new Date().getTime();
199
+ return (now - startTime)
200
+ },
201
+ peernet: api.peernet(client),
202
+ server: api.server(client),
203
+ close: exit => {
204
+ client.close();
205
+ }
206
+ }
207
+ };
208
+ return new Promise((resolve, reject) => {
209
+ const init = () => {
210
+ let ws;
211
+ if (typeof process === 'object' && !globalThis.WebSocket) {
212
+ ws = (__webpack_require__(840).w3cwebsocket);
213
+ } else {
214
+ ws = WebSocket;
215
+ }
216
+ const client = new ws(url, protocols);
217
+ client.onmessage = onmessage;
218
+ client.onerror = onerror;
219
+ client.onopen = () => {
220
+ tries = 0;
221
+ resolve(clientConnection(client));
222
+ };
223
+ client.onclose = message => {
224
+ tries++;
225
+ if (!retry) return reject(options)
226
+ if (tries > 5) {
227
+ console.log(`${protocols} Client Closed`);
228
+ console.error(`could not connect to - ${url}/`);
229
+ return resolve(clientConnection(client))
230
+ }
231
+ if (message.code === 1006) {
232
+ console.log('Retrying in 10 seconds');
233
+ setTimeout(() => {
234
+ return init();
235
+ }, retry);
236
+ }
237
+ };
238
+ };
239
+ return init();
240
+ });
241
+ };
242
+
243
+
244
+
245
+ // EXTERNAL MODULE: ./node_modules/@vandeurenglenn/debug/debug.js
246
+ var debug_debug = __webpack_require__(307);
247
+ ;// CONCATENATED MODULE: ./node_modules/@leofcoin/peernet-swarm/src/client/peer.js
248
+
249
+
250
+ class Peer {
251
+ #connection
252
+ #connected = false
253
+ #messageQue = []
254
+ #chunksQue = {}
255
+ #channel
256
+ #peerId
257
+ #channelName
258
+ #chunkSize = 16 * 1024 // 16384
259
+ #queRunning = false
260
+ #MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
261
+
262
+ get connection() {
263
+ return this.#connection
264
+ }
265
+
266
+ get connected() {
267
+ return this.#connected
268
+ }
269
+
270
+ get readyState() {
271
+ return this.#channel?.readyState
272
+ }
273
+
274
+ /**
275
+ * @params {Object} options
276
+ * @params {string} options.channelName - this peerid : otherpeer id
277
+ */
278
+ constructor(options = {}) {
279
+ this._in = this._in.bind(this);
280
+ this.offerOptions = options.offerOptions
281
+ this.initiator = options.initiator
282
+ this.streams = options.streams
283
+ this.socketClient = options.socketClient
284
+ this.id = options.id
285
+ this.to = options.to
286
+ this.bw = {
287
+ up: 0,
288
+ down: 0
289
+ }
290
+
291
+ this.#channelName = options.channelName
292
+
293
+ this.#peerId = options.peerId
294
+ this.options = options
295
+ return this.#init()
296
+ }
297
+
298
+ get peerId() {
299
+ return this.#peerId
300
+ }
301
+
302
+ set socketClient(value) {
303
+ // this.socketClient?.pubsub.unsubscribe('signal', this._in)
304
+ this._socketClient = value
305
+ this._socketClient.pubsub.subscribe('signal', this._in)
306
+ }
307
+
308
+ get socketClient() {
309
+ return this._socketClient
310
+ }
311
+
312
+ splitMessage(message) {
313
+ const chunks = []
314
+ message = pako.deflate(message)
315
+ const size = message.byteLength || message.length
316
+ let offset = 0
317
+ return new Promise((resolve, reject) => {
318
+ const splitMessage = () => {
319
+ const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize)
320
+ offset += this.#chunkSize
321
+ chunks.push(chunk)
322
+ if (offset < size) return splitMessage()
323
+ else resolve({chunks, size})
324
+ }
325
+
326
+ splitMessage()
327
+ })
328
+ }
329
+
330
+ async #runQue() {
331
+ this.#queRunning = true
332
+ if (this.#messageQue.length > 0 && this.#channel?.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
333
+ const message = this.#messageQue.shift()
334
+
335
+ switch (this.#channel?.readyState) {
336
+ case 'open':
337
+ await this.#channel.send(message);
338
+ if (this.#messageQue.length > 0) return this.#runQue()
339
+ else this.#queRunning = false
340
+ break;
341
+ case 'closed':
342
+ case 'closing':
343
+ this.#messageQue = []
344
+ this.#queRunning = false
345
+ debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
346
+ break;
347
+ case undefined:
348
+ this.#messageQue = []
349
+ this.#queRunning = false
350
+ debug(`trying to send before a channel is created`);
351
+ break;
352
+ }
353
+
354
+
355
+ } else {
356
+ return setTimeout(() => this.#runQue(), 50)
357
+ }
358
+ }
359
+
360
+ #trySend({ size, id, chunks }) {
361
+ let offset = 0
362
+
363
+ for (const chunk of chunks) {
364
+ const start = offset
365
+ const end = offset + chunk.length
366
+
367
+ const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
368
+ this.#messageQue.push(message)
369
+ }
370
+
371
+ if (!this.queRunning) return this.#runQue()
372
+ }
373
+
374
+ async send(message, id) {
375
+ const { chunks, size } = await this.splitMessage(message)
376
+ return this.#trySend({ size, id, chunks })
377
+ }
378
+
379
+ request(data) {
380
+ return new Promise((resolve, reject) => {
381
+ const id = Math.random().toString(36).slice(-12)
382
+
383
+ const _onData = message => {
384
+ if (message.id === id) {
385
+ resolve(message.data)
386
+ pubsub.unsubscribe(`peer:data`, _onData)
387
+ }
388
+ }
389
+
390
+ pubsub.subscribe(`peer:data`, _onData)
391
+
392
+ // cleanup subscriptions
393
+ // setTimeout(() => {
394
+ // pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
395
+ // }, 5000);
396
+
397
+ this.send(data, id)
398
+ })
399
+ }
400
+
401
+ async #init() {
402
+ try {
403
+
404
+ if (!globalThis.pako) {
405
+ const importee = await __webpack_require__.e(/* import() | pako */ 682).then(__webpack_require__.bind(__webpack_require__, 885))
406
+ globalThis.pako = importee.default
407
+ }
408
+
409
+ const iceServers = [{
410
+ urls: 'stun:stun.l.google.com:19302' // Google's public STUN server
411
+ }, {
412
+ urls: "stun:openrelay.metered.ca:80",
413
+ }, {
414
+ urls: "turn:openrelay.metered.ca:443",
415
+ username: "openrelayproject",
416
+ credential: "openrelayproject",
417
+ }, {
418
+ urls: "turn:openrelay.metered.ca:443?transport=tcp",
419
+ username: "openrelayproject",
420
+ credential: "openrelayproject",
421
+ }]
422
+
423
+ this.#connection = new wrtc.RTCPeerConnection({iceServers});
424
+
425
+ this.#connection.onicecandidate = ({ candidate }) => {
426
+ if (candidate) {
427
+ this.address = candidate.address
428
+ this.port = candidate.port
429
+ this.protocol = candidate.protocol
430
+ this.ipFamily = this.address.includes('::') ? 'ipv6': 'ipv4'
431
+ this._sendMessage({candidate})
432
+ }
433
+ }
434
+ // if (this.initiator) this.#connection.onnegotiationneeded = () => {
435
+ // console.log('create offer');
436
+ this.#connection.ondatachannel = (message) => {
437
+ message.channel.onopen = () => {
438
+ this.#connected = true
439
+ pubsub.publish('peer:connected', this)
440
+ }
441
+ message.channel.onclose = () => this.close.bind(this)
442
+
443
+ message.channel.onmessage = (message) => {
444
+ this._handleMessage(this.id, message)
445
+ }
446
+ this.#channel = message.channel
447
+ }
448
+ if (this.initiator) {
449
+
450
+ this.#channel = this.#connection.createDataChannel('messageChannel')
451
+ this.#channel.onopen = () => {
452
+ this.#connected = true
453
+ pubsub.publish('peer:connected', this)
454
+ // this.#channel.send('hi')
455
+ }
456
+ this.#channel.onclose = () => this.close.bind(this)
457
+
458
+ this.#channel.onmessage = (message) => {
459
+ this._handleMessage(this.peerId, message)
460
+ }
461
+
462
+ const offer = await this.#connection.createOffer()
463
+ await this.#connection.setLocalDescription(offer)
464
+
465
+ this._sendMessage({'sdp': this.#connection.localDescription})
466
+ }
467
+ } catch (e) {
468
+ console.log(e);
469
+ }
470
+
471
+ return this
472
+ }
473
+
474
+ _handleMessage(peerId, message) {
475
+ debug(`incoming message from ${peerId}`)
476
+
477
+ message = JSON.parse(new TextDecoder().decode(message.data))
478
+ // allow sharding (multiple peers share data)
479
+ pubsub.publish('peernet:shard', message)
480
+ const { id } = message
481
+
482
+ if (!this.#chunksQue[id]) this.#chunksQue[id] = []
483
+
484
+ if (message.size > this.#chunksQue[id].length || message.size === this.#chunksQue[id].length) {
485
+ for (const value of Object.values(message.chunk)) {
486
+ this.#chunksQue[id].push(value)
487
+ }
488
+ }
489
+
490
+ if (message.size === this.#chunksQue[id].length) {
491
+ let data = new Uint8Array(Object.values(this.#chunksQue[id]))
492
+ delete this.#chunksQue[id]
493
+ data = pako.inflate(data)
494
+ pubsub.publish('peer:data', { id, data, from: this.peerId })
495
+ }
496
+ this.bw.down += message.byteLength || message.length
497
+ }
498
+
499
+ _sendMessage(message) {
500
+ this.socketClient.send({url: 'signal', params: {
501
+ to: this.to,
502
+ from: this.id,
503
+ channelName: this.options.channelName,
504
+ ...message
505
+ }})
506
+ }
507
+
508
+ async _in(message, data) {
509
+ // message = JSON.parse(message);
510
+ if (message.to !== this.id) return
511
+ // if (data.videocall) return this._startStream(true, false); // start video and audio stream
512
+ // if (data.call) return this._startStream(true, true); // start audio stream
513
+ const signalinState = this.#connection.signalinState
514
+ if (signalinState === 'stable' && this.#connection.remoteDescription !== null && this.#connection.localDescription !== null) return
515
+
516
+ if (message.candidate) {
517
+ debug(`incoming candidate ${this.#channelName}`)
518
+ // debug(message.candidate.candidate)
519
+ this.remoteAddress = message.candidate.address
520
+ this.remotePort = message.candidate.port
521
+ this.remoteProtocol = message.candidate.protocol
522
+ this.remoteIpFamily = this.remoteAddress?.includes('::') ? 'ipv6': 'ipv4'
523
+ return this.#connection.addIceCandidate(new wrtc.RTCIceCandidate(message.candidate));
524
+ }
525
+ try {
526
+ if (message.sdp) {
527
+ if (message.sdp.type === 'offer') {
528
+ debug(`incoming offer ${this.#channelName}`)
529
+ await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp))
530
+ const answer = await this.#connection.createAnswer();
531
+ await this.#connection.setLocalDescription(answer)
532
+ this._sendMessage({'sdp': this.#connection.localDescription})
533
+ }
534
+ if (message.sdp.type === 'answer') {
535
+ debug(`incoming answer ${this.#channelName}`)
536
+ await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp))
537
+ }
538
+ }
539
+ } catch (e) {
540
+ console.log(e);
541
+ }
542
+ }
543
+
544
+ close() {
545
+ debug(`closing ${this.peerId}`)
546
+ this.#connected = false
547
+ this.#channel?.close()
548
+ this.#connection?.close()
549
+
550
+ this.socketClient.pubsub.unsubscribe('signal', this._in)
551
+ }
552
+ }
553
+
554
+ ;// CONCATENATED MODULE: ./node_modules/@leofcoin/peernet-swarm/src/client/client.js
555
+
556
+
557
+
558
+
559
+ class Client {
560
+ #peerConnection
561
+ #connections = {}
562
+ #stars = {}
563
+
564
+ get connections() {
565
+ return { ...this.#connections }
566
+ }
567
+
568
+ get peers() {
569
+ return Object.entries(this.#connections)
570
+ }
571
+
572
+ constructor(id, identifiers = ['peernet-v0.1.0'], stars = []) {
573
+ this.id = id || Math.random().toString(36).slice(-12);
574
+ if (!Array.isArray(identifiers)) identifiers = [identifiers]
575
+ this.peerJoined = this.peerJoined.bind(this)
576
+ this.peerLeft = this.peerLeft.bind(this)
577
+ this.starLeft = this.starLeft.bind(this)
578
+ this.starJoined = this.starJoined.bind(this)
579
+
580
+ this._init(identifiers, stars)
581
+ }
582
+
583
+ async _init(identifiers, stars = []) {
584
+ if (stars.length === 0) {
585
+ stars.push('wss://star.leofcoin.org')
586
+ }
587
+ this.identifiers = identifiers
588
+ this.starsConfig = stars
589
+ // reconnectJob()
590
+
591
+ if (!globalThis.RTCPeerConnection) globalThis.wrtc = await __webpack_require__.e(/* import() | wrtc */ 228).then(__webpack_require__.t.bind(__webpack_require__, 347, 19))
592
+ else globalThis.wrtc = {
593
+ RTCPeerConnection,
594
+ RTCSessionDescription,
595
+ RTCIceCandidate
596
+ }
597
+
598
+ for (const star of stars) {
599
+ try {
600
+ this.socketClient = await socketRequestClient(star, identifiers[0])
601
+ const id = await this.socketClient.request({url: 'id', params: {from: this.id}})
602
+ this.socketClient.peerId = id
603
+ this.#stars[id] = this.socketClient
604
+ } catch (e) {
605
+ if (stars.indexOf(star) === stars.length -1 && !this.socketClient) throw new Error(`No star available to connect`);
606
+ }
607
+ }
608
+ const peers = await this.socketClient.peernet.join({id: this.id})
609
+ for (const id of peers) {
610
+ if (id !== this.id && !this.#connections[id]) this.#connections[id] = await new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id})
611
+ }
612
+ this.setupListeners()
613
+ }
614
+
615
+ setupListeners() {
616
+ this.socketClient.subscribe('peer:joined', this.peerJoined)
617
+ this.socketClient.subscribe('peer:left', this.peerLeft)
618
+ this.socketClient.subscribe('star:left', this.starLeft)
619
+ }
620
+
621
+ starJoined(id) {
622
+ if (this.#stars[id]) {
623
+ this.#stars[id].close()
624
+ delete this.#stars[id]
625
+ }
626
+ console.log(`star ${id} joined`);
627
+ }
628
+
629
+ async starLeft(id) {
630
+ if (this.#stars[id]) {
631
+ this.#stars[id].close()
632
+ delete this.#stars[id]
633
+ }
634
+ if (this.socketClient?.peerId === id) {
635
+
636
+ this.socketClient.unsubscribe('peer:joined', this.peerJoined)
637
+ this.socketClient.unsubscribe('peer:left', this.peerLeft)
638
+ this.socketClient.unsubscribe('star:left', this.starLeft)
639
+ this.socketClient.close()
640
+ this.socketClient = undefined
641
+
642
+ for (const star of this.starsConfig) {
643
+ try {
644
+ this.socketClient = await socketRequestClient(star, this.identifiers[0])
645
+ if (!this.socketClient?.client?._connection.connected) return
646
+ const id = await this.socketClient.request({url: 'id', params: {from: this.id}})
647
+ this.#stars[id] = this.socketClient
648
+
649
+ this.socketClient.peerId = id
650
+
651
+ const peers = await this.socketClient.peernet.join({id: this.id})
652
+ this.setupListeners()
653
+ for (const id of peers) {
654
+ if (id !== this.id) {
655
+ // close connection
656
+ if (this.#connections[id]) {
657
+ if (this.#connections[id].connected) await this.#connections[id].close()
658
+ delete this.#connections[id]
659
+ }
660
+ // reconnect
661
+ if (id !== this.id) this.#connections[id] = await new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id})
662
+ }
663
+
664
+ }
665
+ } catch (e) {
666
+ console.log(e);
667
+ if (this.starsConfig.indexOf(star) === this.starsConfig.length -1 && !this.socketClient) throw new Error(`No star available to connect`);
668
+ }
669
+ }
670
+ }
671
+ debug(`star ${id} left`);
672
+ }
673
+
674
+ peerLeft(peer) {
675
+ const id = peer.peerId || peer
676
+ if (this.#connections[id]) {
677
+ this.#connections[id].close()
678
+ delete this.#connections[id]
679
+ }
680
+ debug(`peer ${id} left`)
681
+ }
682
+
683
+ async peerJoined(peer, signal) {
684
+ const id = peer.peerId || peer
685
+ if (this.#connections[id]) {
686
+ if (this.#connections[id].connected) this.#connections[id].close()
687
+ delete this.#connections[id]
688
+ }
689
+ // RTCPeerConnection
690
+ this.#connections[id] = await new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id})
691
+ debug(`peer ${id} joined`)
692
+ }
693
+
694
+ removePeer(peer) {
695
+ const id = peer.peerId || peer
696
+ if (this.#connections[id]) {
697
+ this.#connections[id].connected && this.#connections[id].close()
698
+ delete this.#connections[id]
699
+ }
700
+ debug(`peer ${id} removed`)
701
+ }
702
+
703
+
704
+ }
705
+
706
+
707
+ /***/ }),
708
+
709
+ /***/ 284:
710
+ /***/ (function(module) {
711
+
712
+ var naiveFallback = function () {
713
+ if (typeof self === "object" && self) return self;
714
+ if (typeof window === "object" && window) return window;
715
+ throw new Error("Unable to resolve global `this`");
716
+ };
717
+
718
+ module.exports = (function () {
719
+ if (this) return this;
720
+
721
+ // Unexpected strict mode (may happen if e.g. bundled into ESM module)
722
+
723
+ // Fallback to standard globalThis if available
724
+ if (typeof globalThis === "object" && globalThis) return globalThis;
725
+
726
+ // Thanks @mathiasbynens -> https://mathiasbynens.be/notes/globalthis
727
+ // In all ES5+ engines global object inherits from Object.prototype
728
+ // (if you approached one that doesn't please report)
729
+ try {
730
+ Object.defineProperty(Object.prototype, "__global__", {
731
+ get: function () { return this; },
732
+ configurable: true
733
+ });
734
+ } catch (error) {
735
+ // Unfortunate case of updates to Object.prototype being restricted
736
+ // via preventExtensions, seal or freeze
737
+ return naiveFallback();
738
+ }
739
+ try {
740
+ // Safari case (window.__global__ works, but __global__ does not)
741
+ if (!__global__) return naiveFallback();
742
+ return __global__;
743
+ } finally {
744
+ delete Object.prototype.__global__;
745
+ }
746
+ })();
747
+
748
+
749
+ /***/ }),
750
+
751
+ /***/ 840:
752
+ /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
753
+
754
+ var _globalThis;
755
+ if (typeof globalThis === 'object') {
756
+ _globalThis = globalThis;
757
+ } else {
758
+ try {
759
+ _globalThis = __webpack_require__(284);
760
+ } catch (error) {
761
+ } finally {
762
+ if (!_globalThis && typeof window !== 'undefined') { _globalThis = window; }
763
+ if (!_globalThis) { throw new Error('Could not determine global this'); }
764
+ }
765
+ }
766
+
767
+ var NativeWebSocket = _globalThis.WebSocket || _globalThis.MozWebSocket;
768
+ var websocket_version = __webpack_require__(387);
769
+
770
+
771
+ /**
772
+ * Expose a W3C WebSocket class with just one or two arguments.
773
+ */
774
+ function W3CWebSocket(uri, protocols) {
775
+ var native_instance;
776
+
777
+ if (protocols) {
778
+ native_instance = new NativeWebSocket(uri, protocols);
779
+ }
780
+ else {
781
+ native_instance = new NativeWebSocket(uri);
782
+ }
783
+
784
+ /**
785
+ * 'native_instance' is an instance of nativeWebSocket (the browser's WebSocket
786
+ * class). Since it is an Object it will be returned as it is when creating an
787
+ * instance of W3CWebSocket via 'new W3CWebSocket()'.
788
+ *
789
+ * ECMAScript 5: http://bclary.com/2004/11/07/#a-13.2.2
790
+ */
791
+ return native_instance;
792
+ }
793
+ if (NativeWebSocket) {
794
+ ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'].forEach(function(prop) {
795
+ Object.defineProperty(W3CWebSocket, prop, {
796
+ get: function() { return NativeWebSocket[prop]; }
797
+ });
798
+ });
799
+ }
800
+
801
+ /**
802
+ * Module exports.
803
+ */
804
+ module.exports = {
805
+ 'w3cwebsocket' : NativeWebSocket ? W3CWebSocket : null,
806
+ 'version' : websocket_version
807
+ };
808
+
809
+
810
+ /***/ }),
811
+
812
+ /***/ 387:
813
+ /***/ (function(module, __unused_webpack_exports, __webpack_require__) {
814
+
815
+ module.exports = __webpack_require__(794).version;
816
+
817
+
818
+ /***/ }),
819
+
820
+ /***/ 794:
821
+ /***/ (function(module) {
822
+
823
+ module.exports = {"version":"1.0.34"};
824
+
825
+ /***/ })
826
+
827
+ }])