@leofcoin/peernet 1.1.1 → 1.1.2
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.
- package/exports/browser/client-4b8a9fee.js +5631 -0
- package/exports/browser/{index-c0652eae.js → index-3c5482a7.js} +1 -1
- package/exports/browser/{messages-0fcc3d4e.js → messages-c9724c0b.js} +1 -1
- package/exports/browser/{peernet-4aaa825c.js → peernet-5b33f983.js} +9 -9
- package/exports/browser/peernet.js +1 -1
- package/package.json +1 -1
- package/exports/browser/client-ff436f6a.js +0 -682
|
@@ -1,682 +0,0 @@
|
|
|
1
|
-
import { L as LittlePubSub } from './peernet-4aaa825c.js';
|
|
2
|
-
import './value-157ab062.js';
|
|
3
|
-
|
|
4
|
-
var clientApi = _pubsub => {
|
|
5
|
-
|
|
6
|
-
const subscribe = (topic, cb) => {
|
|
7
|
-
_pubsub.subscribe(topic, cb);
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const unsubscribe = (topic, cb) => {
|
|
11
|
-
_pubsub.unsubscribe(topic, cb);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const publish = (topic, value) => {
|
|
15
|
-
_pubsub.publish(topic, value);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const connectionState = (state) => {
|
|
19
|
-
switch (state) {
|
|
20
|
-
case 0:
|
|
21
|
-
return 'connecting'
|
|
22
|
-
case 1:
|
|
23
|
-
return 'open'
|
|
24
|
-
case 2:
|
|
25
|
-
return 'closing'
|
|
26
|
-
case 3:
|
|
27
|
-
return 'closed'
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* @param {string} type
|
|
32
|
-
* @param {string} name
|
|
33
|
-
* @param {object} params
|
|
34
|
-
*/
|
|
35
|
-
const request = (client, request) => {
|
|
36
|
-
return new Promise((resolve, reject) => {
|
|
37
|
-
|
|
38
|
-
const state = connectionState(client.readyState);
|
|
39
|
-
if (state !== 'open') return reject(`coudn't send request to ${client.id}, no open connection found.`)
|
|
40
|
-
|
|
41
|
-
request.id = Math.random().toString(36).slice(-12);
|
|
42
|
-
const handler = result => {
|
|
43
|
-
if (result && result.error) return reject(result.error)
|
|
44
|
-
resolve({result, id: request.id, handler});
|
|
45
|
-
unsubscribe(request.id, handler);
|
|
46
|
-
};
|
|
47
|
-
subscribe(request.id, handler);
|
|
48
|
-
send(client, request);
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const send = async (client, request) => {
|
|
53
|
-
return client.send(JSON.stringify(request))
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const pubsub = client => {
|
|
57
|
-
return {
|
|
58
|
-
publish: (topic = 'pubsub', value) => {
|
|
59
|
-
return send(client, {url: 'pubsub', params: { topic, value }})
|
|
60
|
-
},
|
|
61
|
-
subscribe: (topic = 'pubsub', cb) => {
|
|
62
|
-
subscribe(topic, cb);
|
|
63
|
-
return send(client, {url: 'pubsub', params: { topic, subscribe: true }})
|
|
64
|
-
},
|
|
65
|
-
unsubscribe: (topic = 'pubsub', cb) => {
|
|
66
|
-
unsubscribe(topic, cb);
|
|
67
|
-
return send(client, {url: 'pubsub', params: { topic, unsubscribe: true }})
|
|
68
|
-
},
|
|
69
|
-
subscribers: _pubsub.subscribers
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const server = (client) => {
|
|
74
|
-
return {
|
|
75
|
-
uptime: async () => {
|
|
76
|
-
try {
|
|
77
|
-
const { result, id, handler } = await request(client, {url: 'uptime'});
|
|
78
|
-
unsubscribe(id, handler);
|
|
79
|
-
return result
|
|
80
|
-
} catch (e) {
|
|
81
|
-
throw e
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
ping: async () => {
|
|
85
|
-
try {
|
|
86
|
-
const now = new Date().getTime();
|
|
87
|
-
const { result, id, handler } = await request(client, {url: 'ping'});
|
|
88
|
-
unsubscribe(id, handler);
|
|
89
|
-
return (Number(result) - now)
|
|
90
|
-
} catch (e) {
|
|
91
|
-
throw e
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const peernet = (client) => {
|
|
98
|
-
return {
|
|
99
|
-
join: async (params) => {
|
|
100
|
-
try {
|
|
101
|
-
params.join = true;
|
|
102
|
-
const requested = { url: 'peernet', params };
|
|
103
|
-
const { result, id, handler } = await request(client, requested);
|
|
104
|
-
unsubscribe(id, handler);
|
|
105
|
-
return result
|
|
106
|
-
} catch (e) {
|
|
107
|
-
throw e
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
leave: async (params) => {
|
|
111
|
-
try {
|
|
112
|
-
params.join = false;
|
|
113
|
-
const requested = { url: 'peernet', params };
|
|
114
|
-
const { result, id, handler } = await request(client, requested);
|
|
115
|
-
unsubscribe(id, handler);
|
|
116
|
-
return result
|
|
117
|
-
} catch (e) {
|
|
118
|
-
throw e
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
return { send, request, pubsub, server, subscribe, unsubscribe, publish, peernet, connectionState }
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
if (!globalThis.PubSub) globalThis.PubSub = LittlePubSub;
|
|
128
|
-
if (!globalThis.pubsub) globalThis.pubsub = new LittlePubSub({verbose: false});
|
|
129
|
-
|
|
130
|
-
const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry: true, timeout: 10_000, times: 10 }) => {
|
|
131
|
-
let { retry, timeout, times } = options;
|
|
132
|
-
if (retry === undefined) retry = true;
|
|
133
|
-
if (timeout === undefined) timeout = 10_000;
|
|
134
|
-
if (times === undefined) times = 10;
|
|
135
|
-
|
|
136
|
-
const api = clientApi(pubsub);
|
|
137
|
-
|
|
138
|
-
let tries = 0;
|
|
139
|
-
|
|
140
|
-
const onerror = error => {
|
|
141
|
-
if (pubsub.subscribers['error']) {
|
|
142
|
-
pubsub.publish('error', error);
|
|
143
|
-
} else {
|
|
144
|
-
console.error(error);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const onmessage = message => {
|
|
149
|
-
const {value, url, status, id} = JSON.parse(message.data.toString());
|
|
150
|
-
const publisher = id ? id : url;
|
|
151
|
-
if (status === 200) {
|
|
152
|
-
pubsub.publish(publisher, value);
|
|
153
|
-
} else {
|
|
154
|
-
pubsub.publish(publisher, {error: value});
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const clientConnection = client => {
|
|
159
|
-
const startTime = new Date().getTime();
|
|
160
|
-
return {
|
|
161
|
-
client,
|
|
162
|
-
request: async req => {
|
|
163
|
-
const { result, id, handler } = await api.request(client, req);
|
|
164
|
-
pubsub.unsubscribe(id, handler);
|
|
165
|
-
return result
|
|
166
|
-
},
|
|
167
|
-
send: req => api.send(client, req),
|
|
168
|
-
subscribe: api.subscribe,
|
|
169
|
-
unsubscribe: api.unsubscribe,
|
|
170
|
-
subscribers: api.subscribers,
|
|
171
|
-
publish: api.publish,
|
|
172
|
-
pubsub: api.pubsub(client),
|
|
173
|
-
uptime: () => {
|
|
174
|
-
const now = new Date().getTime();
|
|
175
|
-
return (now - startTime)
|
|
176
|
-
},
|
|
177
|
-
peernet: api.peernet(client),
|
|
178
|
-
server: api.server(client),
|
|
179
|
-
connectionState: () => api.connectionState(client.readyState),
|
|
180
|
-
close: exit => {
|
|
181
|
-
// client.onclose = message => {
|
|
182
|
-
// if (exit) process.exit()
|
|
183
|
-
// }
|
|
184
|
-
client.close();
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
return new Promise(async (resolve, reject) => {
|
|
190
|
-
const init = async () => {
|
|
191
|
-
let ws;
|
|
192
|
-
if (typeof process === 'object' && !globalThis.WebSocket) {
|
|
193
|
-
ws = (await import('./browser-e1cd4e67.js').then(function (n) { return n.b; })).default;
|
|
194
|
-
ws = ws.w3cwebsocket;
|
|
195
|
-
} else {
|
|
196
|
-
ws = WebSocket;
|
|
197
|
-
}
|
|
198
|
-
const client = new ws(url, protocols);
|
|
199
|
-
|
|
200
|
-
client.onmessage = onmessage;
|
|
201
|
-
client.onerror = onerror;
|
|
202
|
-
|
|
203
|
-
client.onopen = () => {
|
|
204
|
-
tries = 0;
|
|
205
|
-
resolve(clientConnection(client));
|
|
206
|
-
};
|
|
207
|
-
client.onclose = message => {
|
|
208
|
-
tries++;
|
|
209
|
-
if (!retry) return reject(options)
|
|
210
|
-
if (tries > times) {
|
|
211
|
-
console.log(`${protocols} Client Closed`);
|
|
212
|
-
console.error(`could not connect to - ${url}/`);
|
|
213
|
-
return resolve(clientConnection(client))
|
|
214
|
-
}
|
|
215
|
-
if (message.code === 1006) {
|
|
216
|
-
console.log(`Retrying in ${timeout} ms`);
|
|
217
|
-
setTimeout(() => {
|
|
218
|
-
return init();
|
|
219
|
-
}, timeout);
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
return init();
|
|
224
|
-
});
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
class Peer {
|
|
228
|
-
#connection;
|
|
229
|
-
#connected = false;
|
|
230
|
-
#messageQue = [];
|
|
231
|
-
#chunksQue = {};
|
|
232
|
-
#channel;
|
|
233
|
-
#peerId;
|
|
234
|
-
#channelName;
|
|
235
|
-
#chunkSize = 16 * 1024; // 16384
|
|
236
|
-
#queRunning = false;
|
|
237
|
-
#MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024;
|
|
238
|
-
initiator = false;
|
|
239
|
-
state;
|
|
240
|
-
#makingOffer = false;
|
|
241
|
-
get connection() {
|
|
242
|
-
return this.#connection;
|
|
243
|
-
}
|
|
244
|
-
get connected() {
|
|
245
|
-
return this.#connected;
|
|
246
|
-
}
|
|
247
|
-
get readyState() {
|
|
248
|
-
return this.#channel?.readyState;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* @params {Object} options
|
|
252
|
-
* @params {string} options.channelName - this peerid : otherpeer id
|
|
253
|
-
*/
|
|
254
|
-
constructor(options = {}) {
|
|
255
|
-
this._in = this._in.bind(this);
|
|
256
|
-
this.offerOptions = options.offerOptions;
|
|
257
|
-
this.initiator = options.initiator;
|
|
258
|
-
this.streams = options.streams;
|
|
259
|
-
this.socketClient = options.socketClient;
|
|
260
|
-
this.id = options.id;
|
|
261
|
-
this.to = options.to;
|
|
262
|
-
this.bw = {
|
|
263
|
-
up: 0,
|
|
264
|
-
down: 0
|
|
265
|
-
};
|
|
266
|
-
this.#channelName = options.channelName;
|
|
267
|
-
this.#peerId = options.peerId;
|
|
268
|
-
this.options = options;
|
|
269
|
-
return this.#init();
|
|
270
|
-
}
|
|
271
|
-
get peerId() {
|
|
272
|
-
return this.#peerId;
|
|
273
|
-
}
|
|
274
|
-
set socketClient(value) {
|
|
275
|
-
// this.socketClient?.pubsub.unsubscribe('signal', this._in)
|
|
276
|
-
this._socketClient = value;
|
|
277
|
-
this._socketClient.pubsub.subscribe('signal', this._in);
|
|
278
|
-
}
|
|
279
|
-
get socketClient() {
|
|
280
|
-
return this._socketClient;
|
|
281
|
-
}
|
|
282
|
-
splitMessage(message) {
|
|
283
|
-
const chunks = [];
|
|
284
|
-
message = pako.deflate(message);
|
|
285
|
-
const size = message.byteLength || message.length;
|
|
286
|
-
let offset = 0;
|
|
287
|
-
return new Promise((resolve, reject) => {
|
|
288
|
-
const splitMessage = () => {
|
|
289
|
-
const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
|
|
290
|
-
offset += this.#chunkSize;
|
|
291
|
-
chunks.push(chunk);
|
|
292
|
-
if (offset < size)
|
|
293
|
-
return splitMessage();
|
|
294
|
-
else
|
|
295
|
-
resolve({ chunks, size });
|
|
296
|
-
};
|
|
297
|
-
splitMessage();
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
async #runQue() {
|
|
301
|
-
this.#queRunning = true;
|
|
302
|
-
if (this.#messageQue.length > 0 && this.#channel?.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
|
|
303
|
-
const message = this.#messageQue.shift();
|
|
304
|
-
switch (this.#channel?.readyState) {
|
|
305
|
-
case 'open':
|
|
306
|
-
await this.#channel.send(message);
|
|
307
|
-
if (this.#messageQue.length > 0)
|
|
308
|
-
return this.#runQue();
|
|
309
|
-
else
|
|
310
|
-
this.#queRunning = false;
|
|
311
|
-
break;
|
|
312
|
-
case 'closed':
|
|
313
|
-
case 'closing':
|
|
314
|
-
this.#messageQue = [];
|
|
315
|
-
this.#queRunning = false;
|
|
316
|
-
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
317
|
-
break;
|
|
318
|
-
case undefined:
|
|
319
|
-
this.#messageQue = [];
|
|
320
|
-
this.#queRunning = false;
|
|
321
|
-
debug(`trying to send before a channel is created`);
|
|
322
|
-
break;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
else {
|
|
326
|
-
return setTimeout(() => this.#runQue(), 50);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
#trySend({ size, id, chunks }) {
|
|
330
|
-
let offset = 0;
|
|
331
|
-
for (const chunk of chunks) {
|
|
332
|
-
const start = offset;
|
|
333
|
-
const end = offset + chunk.length;
|
|
334
|
-
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
335
|
-
this.#messageQue.push(message);
|
|
336
|
-
}
|
|
337
|
-
if (!this.queRunning)
|
|
338
|
-
return this.#runQue();
|
|
339
|
-
}
|
|
340
|
-
async send(message, id) {
|
|
341
|
-
const { chunks, size } = await this.splitMessage(message);
|
|
342
|
-
return this.#trySend({ size, id, chunks });
|
|
343
|
-
}
|
|
344
|
-
request(data) {
|
|
345
|
-
return new Promise((resolve, reject) => {
|
|
346
|
-
const id = Math.random().toString(36).slice(-12);
|
|
347
|
-
const _onData = message => {
|
|
348
|
-
if (message.id === id) {
|
|
349
|
-
resolve(message.data);
|
|
350
|
-
pubsub.unsubscribe(`peer:data`, _onData);
|
|
351
|
-
}
|
|
352
|
-
};
|
|
353
|
-
pubsub.subscribe(`peer:data`, _onData);
|
|
354
|
-
// cleanup subscriptions
|
|
355
|
-
// setTimeout(() => {
|
|
356
|
-
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
357
|
-
// }, 5000);
|
|
358
|
-
this.send(data, id);
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
async #init() {
|
|
362
|
-
try {
|
|
363
|
-
if (!globalThis.pako) {
|
|
364
|
-
const importee = await import('./pako.esm-aa674ebf.js');
|
|
365
|
-
globalThis.pako = importee.default;
|
|
366
|
-
}
|
|
367
|
-
const iceServers = [{
|
|
368
|
-
urls: 'stun:stun.l.google.com:19302' // Google's public STUN server
|
|
369
|
-
}, {
|
|
370
|
-
urls: "stun:openrelay.metered.ca:80",
|
|
371
|
-
}, {
|
|
372
|
-
urls: "turn:openrelay.metered.ca:443",
|
|
373
|
-
username: "openrelayproject",
|
|
374
|
-
credential: "openrelayproject",
|
|
375
|
-
}, {
|
|
376
|
-
urls: "turn:openrelay.metered.ca:443?transport=tcp",
|
|
377
|
-
username: "openrelayproject",
|
|
378
|
-
credential: "openrelayproject",
|
|
379
|
-
}];
|
|
380
|
-
this.#connection = new wrtc.RTCPeerConnection({ iceServers });
|
|
381
|
-
this.#connection.onnegotiationneeded = async () => {
|
|
382
|
-
try {
|
|
383
|
-
this.#makingOffer = true;
|
|
384
|
-
await this.#connection.setLocalDescription();
|
|
385
|
-
this._sendMessage({ description: this.#connection.localDescription });
|
|
386
|
-
this.#makingOffer = false;
|
|
387
|
-
}
|
|
388
|
-
catch (err) {
|
|
389
|
-
console.error(err);
|
|
390
|
-
}
|
|
391
|
-
};
|
|
392
|
-
this.#connection.oniceconnectionstatechange = () => {
|
|
393
|
-
if (this.#connection.iceConnectionState === "failed") {
|
|
394
|
-
this.#connection.restartIce();
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
this.#connection.onconnectionstatechange = () => {
|
|
398
|
-
switch (this.#connection.connectionState) {
|
|
399
|
-
case "new":
|
|
400
|
-
case "checking":
|
|
401
|
-
this.state = "connecting";
|
|
402
|
-
break;
|
|
403
|
-
case "connected":
|
|
404
|
-
this.state = 'connected';
|
|
405
|
-
break;
|
|
406
|
-
case "closed":
|
|
407
|
-
case "disconnected":
|
|
408
|
-
this.state = "disconnected";
|
|
409
|
-
break;
|
|
410
|
-
case "failed":
|
|
411
|
-
this.state = 'failed';
|
|
412
|
-
break;
|
|
413
|
-
default:
|
|
414
|
-
this.state = this.#connection.connectionState;
|
|
415
|
-
break;
|
|
416
|
-
}
|
|
417
|
-
if (this.state === 'connected') {
|
|
418
|
-
this.#connection.ondatachannel = (message) => {
|
|
419
|
-
message.channel.onopen = () => {
|
|
420
|
-
this.#connected = true;
|
|
421
|
-
// debug(`peer:connected ${this}`)
|
|
422
|
-
pubsub.publish('peer:connected', this);
|
|
423
|
-
};
|
|
424
|
-
message.channel.onclose = () => this.close.bind(this);
|
|
425
|
-
message.channel.onmessage = (message) => {
|
|
426
|
-
this._handleMessage(this.id, message);
|
|
427
|
-
};
|
|
428
|
-
this.#channel = message.channel;
|
|
429
|
-
};
|
|
430
|
-
if (this.initiator) {
|
|
431
|
-
this.#channel = this.#connection.createDataChannel('messageChannel');
|
|
432
|
-
this.#channel.onopen = () => {
|
|
433
|
-
this.#connected = true;
|
|
434
|
-
pubsub.publish('peer:connected', this);
|
|
435
|
-
// this.#channel.send('hi')
|
|
436
|
-
};
|
|
437
|
-
this.#channel.onclose = () => this.close.bind(this);
|
|
438
|
-
this.#channel.onmessage = (message) => {
|
|
439
|
-
this._handleMessage(this.peerId, message);
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
this.#connection.onicecandidate = ({ candidate }) => {
|
|
445
|
-
if (candidate) {
|
|
446
|
-
this.address = candidate.address;
|
|
447
|
-
this.port = candidate.port;
|
|
448
|
-
this.protocol = candidate.protocol;
|
|
449
|
-
this.ipFamily = this.address.includes('::') ? 'ipv6' : 'ipv4';
|
|
450
|
-
this._sendMessage({ candidate });
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
// if (this.initiator) this.#connection.onnegotiationneeded = () => {
|
|
454
|
-
// console.log('create offer');
|
|
455
|
-
}
|
|
456
|
-
catch (e) {
|
|
457
|
-
console.log(e);
|
|
458
|
-
}
|
|
459
|
-
return this;
|
|
460
|
-
}
|
|
461
|
-
_handleMessage(peerId, message) {
|
|
462
|
-
// debug(`incoming message from ${peerId}`)
|
|
463
|
-
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
464
|
-
// allow sharding (multiple peers share data)
|
|
465
|
-
pubsub.publish('peernet:shard', message);
|
|
466
|
-
const { id } = message;
|
|
467
|
-
if (!this.#chunksQue[id])
|
|
468
|
-
this.#chunksQue[id] = [];
|
|
469
|
-
if (message.size > this.#chunksQue[id].length || message.size === this.#chunksQue[id].length) {
|
|
470
|
-
for (const value of Object.values(message.chunk)) {
|
|
471
|
-
this.#chunksQue[id].push(value);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
if (message.size === this.#chunksQue[id].length) {
|
|
475
|
-
let data = new Uint8Array(Object.values(this.#chunksQue[id]));
|
|
476
|
-
delete this.#chunksQue[id];
|
|
477
|
-
data = pako.inflate(data);
|
|
478
|
-
pubsub.publish('peer:data', { id, data, from: this.peerId });
|
|
479
|
-
}
|
|
480
|
-
this.bw.down += message.byteLength || message.length;
|
|
481
|
-
}
|
|
482
|
-
_sendMessage(message) {
|
|
483
|
-
this.socketClient.send({ url: 'signal', params: {
|
|
484
|
-
to: this.to,
|
|
485
|
-
from: this.id,
|
|
486
|
-
channelName: this.options.channelName,
|
|
487
|
-
...message
|
|
488
|
-
} });
|
|
489
|
-
}
|
|
490
|
-
async _in(message, data) {
|
|
491
|
-
let ignoreOffer = false;
|
|
492
|
-
try {
|
|
493
|
-
if (message.description) {
|
|
494
|
-
const offerCollision = message.description.type === "offer" &&
|
|
495
|
-
(this.#makingOffer || this.#connection.signalingState !== "stable");
|
|
496
|
-
ignoreOffer = this.initiator && offerCollision;
|
|
497
|
-
if (ignoreOffer) {
|
|
498
|
-
return;
|
|
499
|
-
}
|
|
500
|
-
await this.#connection.setRemoteDescription(message.description);
|
|
501
|
-
if (message.description.type === "offer") {
|
|
502
|
-
await this.#connection.setLocalDescription();
|
|
503
|
-
this._sendMessage({ description: this.#connection.localDescription });
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
else if (message.candidate) {
|
|
507
|
-
try {
|
|
508
|
-
await this.#connection.addIceCandidate(message.candidate);
|
|
509
|
-
}
|
|
510
|
-
catch (err) {
|
|
511
|
-
if (!ignoreOffer) {
|
|
512
|
-
throw err;
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
catch (e) {
|
|
518
|
-
pubsub.publish('connection closed', this);
|
|
519
|
-
console.log(e);
|
|
520
|
-
this.close();
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
close() {
|
|
524
|
-
// debug(`closing ${this.peerId}`)
|
|
525
|
-
this.#connected = false;
|
|
526
|
-
this.#channel?.close();
|
|
527
|
-
this.#connection?.close();
|
|
528
|
-
this.socketClient.pubsub.unsubscribe('signal', this._in);
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
class Client {
|
|
533
|
-
#peerConnection;
|
|
534
|
-
#connections = {};
|
|
535
|
-
#stars = {};
|
|
536
|
-
id;
|
|
537
|
-
networkVersion;
|
|
538
|
-
starsConfig;
|
|
539
|
-
socketClient;
|
|
540
|
-
get connections() {
|
|
541
|
-
return { ...this.#connections };
|
|
542
|
-
}
|
|
543
|
-
get peers() {
|
|
544
|
-
return Object.entries(this.#connections);
|
|
545
|
-
}
|
|
546
|
-
constructor(id, networkVersion = 'peach', stars = ['wss://peach.leofcoin.org']) {
|
|
547
|
-
this.id = id || Math.random().toString(36).slice(-12);
|
|
548
|
-
this.peerJoined = this.peerJoined.bind(this);
|
|
549
|
-
this.peerLeft = this.peerLeft.bind(this);
|
|
550
|
-
this.starLeft = this.starLeft.bind(this);
|
|
551
|
-
this.starJoined = this.starJoined.bind(this);
|
|
552
|
-
this.networkVersion = networkVersion;
|
|
553
|
-
this._init(stars);
|
|
554
|
-
}
|
|
555
|
-
async _init(stars = []) {
|
|
556
|
-
this.starsConfig = stars;
|
|
557
|
-
// reconnectJob()
|
|
558
|
-
if (!globalThis.RTCPeerConnection)
|
|
559
|
-
globalThis.wrtc = (await import('./browser-10ffabe1.js').then(function (n) { return n.b; })).default;
|
|
560
|
-
else
|
|
561
|
-
globalThis.wrtc = {
|
|
562
|
-
RTCPeerConnection,
|
|
563
|
-
RTCSessionDescription,
|
|
564
|
-
RTCIceCandidate
|
|
565
|
-
};
|
|
566
|
-
for (const star of stars) {
|
|
567
|
-
try {
|
|
568
|
-
this.socketClient = await socketRequestClient(star, this.networkVersion);
|
|
569
|
-
const id = await this.socketClient.request({ url: 'id', params: { from: this.id } });
|
|
570
|
-
this.socketClient.peerId = id;
|
|
571
|
-
this.#stars[id] = this.socketClient;
|
|
572
|
-
}
|
|
573
|
-
catch (e) {
|
|
574
|
-
if (stars.indexOf(star) === stars.length - 1 && !this.socketClient)
|
|
575
|
-
throw new Error(`No star available to connect`);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
this.setupListeners();
|
|
579
|
-
const peers = await this.socketClient.peernet.join({ id: this.id });
|
|
580
|
-
for (const id of peers) {
|
|
581
|
-
if (id !== this.id && !this.#connections[id])
|
|
582
|
-
this.#connections[id] = await new Peer({ channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id });
|
|
583
|
-
}
|
|
584
|
-
pubsub.subscribe('connection closed', (peer) => {
|
|
585
|
-
this.removePeer(peer.peerId);
|
|
586
|
-
setTimeout(() => {
|
|
587
|
-
this.peerJoined(peer.peerId);
|
|
588
|
-
}, 1000);
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
setupListeners() {
|
|
592
|
-
this.socketClient.subscribe('peer:joined', this.peerJoined);
|
|
593
|
-
this.socketClient.subscribe('peer:left', this.peerLeft);
|
|
594
|
-
this.socketClient.subscribe('star:left', this.starLeft);
|
|
595
|
-
}
|
|
596
|
-
starJoined(id) {
|
|
597
|
-
if (this.#stars[id]) {
|
|
598
|
-
this.#stars[id].close();
|
|
599
|
-
delete this.#stars[id];
|
|
600
|
-
}
|
|
601
|
-
console.log(`star ${id} joined`);
|
|
602
|
-
}
|
|
603
|
-
async starLeft(id) {
|
|
604
|
-
if (this.#stars[id]) {
|
|
605
|
-
this.#stars[id].close();
|
|
606
|
-
delete this.#stars[id];
|
|
607
|
-
}
|
|
608
|
-
if (this.socketClient?.peerId === id) {
|
|
609
|
-
this.socketClient.unsubscribe('peer:joined', this.peerJoined);
|
|
610
|
-
this.socketClient.unsubscribe('peer:left', this.peerLeft);
|
|
611
|
-
this.socketClient.unsubscribe('star:left', this.starLeft);
|
|
612
|
-
this.socketClient.close();
|
|
613
|
-
this.socketClient = undefined;
|
|
614
|
-
for (const star of this.starsConfig) {
|
|
615
|
-
try {
|
|
616
|
-
this.socketClient = await socketRequestClient(star, this.networkVersion);
|
|
617
|
-
if (!this.socketClient?.client?._connection.connected)
|
|
618
|
-
return;
|
|
619
|
-
const id = await this.socketClient.request({ url: 'id', params: { from: this.id } });
|
|
620
|
-
this.#stars[id] = this.socketClient;
|
|
621
|
-
this.socketClient.peerId = id;
|
|
622
|
-
const peers = await this.socketClient.peernet.join({ id: this.id });
|
|
623
|
-
this.setupListeners();
|
|
624
|
-
for (const id of peers) {
|
|
625
|
-
if (id !== this.id) {
|
|
626
|
-
if (!this.#connections[id]) {
|
|
627
|
-
if (id !== this.id)
|
|
628
|
-
this.#connections[id] = await new Peer({ channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id });
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
catch (e) {
|
|
634
|
-
console.log(e);
|
|
635
|
-
if (this.starsConfig.indexOf(star) === this.starsConfig.length - 1 && !this.socketClient)
|
|
636
|
-
throw new Error(`No star available to connect`);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
globalThis.debug(`star ${id} left`);
|
|
641
|
-
}
|
|
642
|
-
peerLeft(peer) {
|
|
643
|
-
const id = peer.peerId || peer;
|
|
644
|
-
if (this.#connections[id]) {
|
|
645
|
-
this.#connections[id].close();
|
|
646
|
-
delete this.#connections[id];
|
|
647
|
-
}
|
|
648
|
-
globalThis.debug(`peer ${id} left`);
|
|
649
|
-
}
|
|
650
|
-
async peerJoined(peer, signal) {
|
|
651
|
-
const id = peer.peerId || peer;
|
|
652
|
-
if (this.#connections[id]) {
|
|
653
|
-
if (this.#connections[id].connected)
|
|
654
|
-
this.#connections[id].close();
|
|
655
|
-
delete this.#connections[id];
|
|
656
|
-
}
|
|
657
|
-
// RTCPeerConnection
|
|
658
|
-
this.#connections[id] = await new Peer({ initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id });
|
|
659
|
-
globalThis.debug(`peer ${id} joined`);
|
|
660
|
-
}
|
|
661
|
-
removePeer(peer) {
|
|
662
|
-
const id = peer.peerId || peer;
|
|
663
|
-
if (this.#connections[id]) {
|
|
664
|
-
this.#connections[id].connected && this.#connections[id].close();
|
|
665
|
-
delete this.#connections[id];
|
|
666
|
-
}
|
|
667
|
-
globalThis.debug(`peer ${id} removed`);
|
|
668
|
-
}
|
|
669
|
-
async close() {
|
|
670
|
-
this.socketClient.unsubscribe('peer:joined', this.peerJoined);
|
|
671
|
-
this.socketClient.unsubscribe('peer:left', this.peerLeft);
|
|
672
|
-
this.socketClient.unsubscribe('star:left', this.starLeft);
|
|
673
|
-
const promises = [
|
|
674
|
-
Object.values(this.#connections).map(connection => connection.close()),
|
|
675
|
-
Object.values(this.#stars).map(connection => connection.close()),
|
|
676
|
-
this.socketClient.close()
|
|
677
|
-
];
|
|
678
|
-
return Promise.allSettled(promises);
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
export { Client as default };
|