@leofcoin/peernet 1.1.2 → 1.1.4
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-111c93a3.js +612 -0
- package/exports/browser/{index-3c5482a7.js → index-3d3f56ca.js} +1 -1
- package/exports/browser/{messages-c9724c0b.js → messages-af41e873.js} +1 -1
- package/exports/browser/{peernet-5b33f983.js → peernet-f349ddaf.js} +8 -4
- package/exports/browser/peernet.js +1 -1
- package/exports/browser/{client-4b8a9fee.js → simple-peer-743c19fe.js} +2880 -655
- package/package.json +2 -1
- package/rollup.config.js +1 -0
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
import { L as LittlePubSub } from './peernet-f349ddaf.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
|
+
id;
|
|
234
|
+
#peerId;
|
|
235
|
+
#channelName;
|
|
236
|
+
#chunkSize = 16 * 1024; // 16384
|
|
237
|
+
#queRunning = false;
|
|
238
|
+
#MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024;
|
|
239
|
+
initiator = false;
|
|
240
|
+
state;
|
|
241
|
+
#makingOffer = false;
|
|
242
|
+
get connection() {
|
|
243
|
+
return this.#connection;
|
|
244
|
+
}
|
|
245
|
+
get connected() {
|
|
246
|
+
return this.#connected;
|
|
247
|
+
}
|
|
248
|
+
get readyState() {
|
|
249
|
+
return this.#channel?.readyState;
|
|
250
|
+
}
|
|
251
|
+
get channelName() {
|
|
252
|
+
return this.#channelName;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* @params {Object} options
|
|
256
|
+
* @params {string} options.channelName - this peerid : otherpeer id
|
|
257
|
+
*/
|
|
258
|
+
constructor(options = {}) {
|
|
259
|
+
this._in = this._in.bind(this);
|
|
260
|
+
this.offerOptions = options.offerOptions;
|
|
261
|
+
this.initiator = options.initiator;
|
|
262
|
+
this.streams = options.streams;
|
|
263
|
+
this.socketClient = options.socketClient;
|
|
264
|
+
this.id = options.id;
|
|
265
|
+
this.to = options.to;
|
|
266
|
+
this.bw = {
|
|
267
|
+
up: 0,
|
|
268
|
+
down: 0
|
|
269
|
+
};
|
|
270
|
+
this.#channelName = options.channelName;
|
|
271
|
+
this.#peerId = options.peerId;
|
|
272
|
+
this.options = options;
|
|
273
|
+
return this.#init();
|
|
274
|
+
}
|
|
275
|
+
get peerId() {
|
|
276
|
+
return this.#peerId;
|
|
277
|
+
}
|
|
278
|
+
set socketClient(value) {
|
|
279
|
+
// this.socketClient?.pubsub.unsubscribe('signal', this._in)
|
|
280
|
+
this._socketClient = value;
|
|
281
|
+
this._socketClient.pubsub.subscribe('signal', this._in);
|
|
282
|
+
}
|
|
283
|
+
get socketClient() {
|
|
284
|
+
return this._socketClient;
|
|
285
|
+
}
|
|
286
|
+
splitMessage(message) {
|
|
287
|
+
const chunks = [];
|
|
288
|
+
message = pako.deflate(message);
|
|
289
|
+
const size = message.byteLength || message.length;
|
|
290
|
+
let offset = 0;
|
|
291
|
+
return new Promise((resolve, reject) => {
|
|
292
|
+
const splitMessage = () => {
|
|
293
|
+
const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
|
|
294
|
+
offset += this.#chunkSize;
|
|
295
|
+
chunks.push(chunk);
|
|
296
|
+
if (offset < size)
|
|
297
|
+
return splitMessage();
|
|
298
|
+
else
|
|
299
|
+
resolve({ chunks, size });
|
|
300
|
+
};
|
|
301
|
+
splitMessage();
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
async #runQue() {
|
|
305
|
+
this.#queRunning = true;
|
|
306
|
+
if (this.#messageQue.length > 0 && this.#channel?.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
|
|
307
|
+
const message = this.#messageQue.shift();
|
|
308
|
+
await this.#connection.send(message);
|
|
309
|
+
if (this.#messageQue.length > 0)
|
|
310
|
+
return this.#runQue();
|
|
311
|
+
// switch (this.#channel?.readyState) {
|
|
312
|
+
// case 'open':
|
|
313
|
+
// await this.#channel.send(message);
|
|
314
|
+
// if (this.#messageQue.length > 0) return this.#runQue()
|
|
315
|
+
// else this.#queRunning = false
|
|
316
|
+
// break;
|
|
317
|
+
// case 'closed':
|
|
318
|
+
// case 'closing':
|
|
319
|
+
// this.#messageQue = []
|
|
320
|
+
// this.#queRunning = false
|
|
321
|
+
// debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
322
|
+
// break;
|
|
323
|
+
// case undefined:
|
|
324
|
+
// this.#messageQue = []
|
|
325
|
+
// this.#queRunning = false
|
|
326
|
+
// debug(`trying to send before a channel is created`);
|
|
327
|
+
// break;
|
|
328
|
+
// }
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
return setTimeout(() => this.#runQue(), 50);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
#trySend({ size, id, chunks }) {
|
|
335
|
+
let offset = 0;
|
|
336
|
+
for (const chunk of chunks) {
|
|
337
|
+
const start = offset;
|
|
338
|
+
const end = offset + chunk.length;
|
|
339
|
+
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
340
|
+
this.#messageQue.push(message);
|
|
341
|
+
}
|
|
342
|
+
if (!this.queRunning)
|
|
343
|
+
return this.#runQue();
|
|
344
|
+
}
|
|
345
|
+
async send(message, id) {
|
|
346
|
+
const { chunks, size } = await this.splitMessage(message);
|
|
347
|
+
return this.#trySend({ size, id, chunks });
|
|
348
|
+
}
|
|
349
|
+
request(data) {
|
|
350
|
+
return new Promise((resolve, reject) => {
|
|
351
|
+
const id = Math.random().toString(36).slice(-12);
|
|
352
|
+
const _onData = message => {
|
|
353
|
+
if (message.id === id) {
|
|
354
|
+
resolve(message.data);
|
|
355
|
+
pubsub.unsubscribe(`peer:data`, _onData);
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
pubsub.subscribe(`peer:data`, _onData);
|
|
359
|
+
// cleanup subscriptions
|
|
360
|
+
// setTimeout(() => {
|
|
361
|
+
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
362
|
+
// }, 5000);
|
|
363
|
+
this.send(data, id);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
async #init() {
|
|
367
|
+
try {
|
|
368
|
+
if (!globalThis.pako) {
|
|
369
|
+
const importee = await import('./pako.esm-aa674ebf.js');
|
|
370
|
+
globalThis.pako = importee.default;
|
|
371
|
+
}
|
|
372
|
+
const iceServers = [{
|
|
373
|
+
urls: 'stun:stun.l.google.com:19302' // Google's public STUN server
|
|
374
|
+
}, {
|
|
375
|
+
urls: "stun:openrelay.metered.ca:80",
|
|
376
|
+
}, {
|
|
377
|
+
urls: "turn:openrelay.metered.ca:443",
|
|
378
|
+
username: "openrelayproject",
|
|
379
|
+
credential: "openrelayproject",
|
|
380
|
+
}, {
|
|
381
|
+
urls: "turn:openrelay.metered.ca:443?transport=tcp",
|
|
382
|
+
username: "openrelayproject",
|
|
383
|
+
credential: "openrelayproject",
|
|
384
|
+
}];
|
|
385
|
+
const importee = await import('./simple-peer-743c19fe.js');
|
|
386
|
+
const SimplePeer = importee.default;
|
|
387
|
+
this.#connection = new SimplePeer({
|
|
388
|
+
channelName: this.channelName,
|
|
389
|
+
initiator: this.initiator,
|
|
390
|
+
peerId: this.peerId,
|
|
391
|
+
wrtc: globalThis.wrtc,
|
|
392
|
+
config: {
|
|
393
|
+
iceServers
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
this.#connection.on('signal', signal => {
|
|
397
|
+
this._sendMessage({ signal });
|
|
398
|
+
});
|
|
399
|
+
this.#connection.on('connected', () => {
|
|
400
|
+
this.#connected = true;
|
|
401
|
+
pubsub.publish('peer:connected', this);
|
|
402
|
+
});
|
|
403
|
+
this.#connection.on('close', () => {
|
|
404
|
+
this.close();
|
|
405
|
+
});
|
|
406
|
+
this.#connection.on('data', data => {
|
|
407
|
+
this._handleMessage(data);
|
|
408
|
+
});
|
|
409
|
+
this.#connection.on('error', (e) => {
|
|
410
|
+
pubsub.publish('connection closed', this);
|
|
411
|
+
console.log(e);
|
|
412
|
+
this.close();
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
catch (e) {
|
|
416
|
+
console.log(e);
|
|
417
|
+
}
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
_handleMessage(message) {
|
|
421
|
+
console.log({ message });
|
|
422
|
+
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
423
|
+
// allow sharding (multiple peers share data)
|
|
424
|
+
pubsub.publish('peernet:shard', message);
|
|
425
|
+
const { id } = message;
|
|
426
|
+
if (!this.#chunksQue[id])
|
|
427
|
+
this.#chunksQue[id] = [];
|
|
428
|
+
if (message.size > this.#chunksQue[id].length || message.size === this.#chunksQue[id].length) {
|
|
429
|
+
for (const value of Object.values(message.chunk)) {
|
|
430
|
+
this.#chunksQue[id].push(value);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (message.size === this.#chunksQue[id].length) {
|
|
434
|
+
let data = new Uint8Array(Object.values(this.#chunksQue[id]));
|
|
435
|
+
delete this.#chunksQue[id];
|
|
436
|
+
data = pako.inflate(data);
|
|
437
|
+
pubsub.publish('peer:data', { id, data, from: this.peerId });
|
|
438
|
+
}
|
|
439
|
+
this.bw.down += message.byteLength || message.length;
|
|
440
|
+
}
|
|
441
|
+
_sendMessage(message) {
|
|
442
|
+
this.socketClient.send({ url: 'signal', params: {
|
|
443
|
+
to: this.to,
|
|
444
|
+
from: this.id,
|
|
445
|
+
channelName: this.channelName,
|
|
446
|
+
...message
|
|
447
|
+
} });
|
|
448
|
+
}
|
|
449
|
+
async _in(message, data) {
|
|
450
|
+
if (message.signal)
|
|
451
|
+
return this.#connection.signal(message.signal);
|
|
452
|
+
}
|
|
453
|
+
close() {
|
|
454
|
+
// debug(`closing ${this.peerId}`)
|
|
455
|
+
this.#connected = false;
|
|
456
|
+
// this.#channel?.close()
|
|
457
|
+
// this.#connection?.exit()
|
|
458
|
+
this.socketClient.pubsub.unsubscribe('signal', this._in);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
class Client {
|
|
463
|
+
#peerConnection;
|
|
464
|
+
#connections = {};
|
|
465
|
+
#stars = {};
|
|
466
|
+
id;
|
|
467
|
+
networkVersion;
|
|
468
|
+
starsConfig;
|
|
469
|
+
socketClient;
|
|
470
|
+
get connections() {
|
|
471
|
+
return { ...this.#connections };
|
|
472
|
+
}
|
|
473
|
+
get peers() {
|
|
474
|
+
return Object.entries(this.#connections);
|
|
475
|
+
}
|
|
476
|
+
constructor(id, networkVersion = 'peach', stars = ['wss://peach.leofcoin.org']) {
|
|
477
|
+
this.id = id || Math.random().toString(36).slice(-12);
|
|
478
|
+
this.peerJoined = this.peerJoined.bind(this);
|
|
479
|
+
this.peerLeft = this.peerLeft.bind(this);
|
|
480
|
+
this.starLeft = this.starLeft.bind(this);
|
|
481
|
+
this.starJoined = this.starJoined.bind(this);
|
|
482
|
+
this.networkVersion = networkVersion;
|
|
483
|
+
this._init(stars);
|
|
484
|
+
}
|
|
485
|
+
async _init(stars = []) {
|
|
486
|
+
this.starsConfig = stars;
|
|
487
|
+
// reconnectJob()
|
|
488
|
+
if (!globalThis.RTCPeerConnection)
|
|
489
|
+
globalThis.wrtc = (await import('./browser-10ffabe1.js').then(function (n) { return n.b; })).default;
|
|
490
|
+
else
|
|
491
|
+
globalThis.wrtc = {
|
|
492
|
+
RTCPeerConnection,
|
|
493
|
+
RTCSessionDescription,
|
|
494
|
+
RTCIceCandidate
|
|
495
|
+
};
|
|
496
|
+
for (const star of stars) {
|
|
497
|
+
try {
|
|
498
|
+
this.socketClient = await socketRequestClient(star, this.networkVersion);
|
|
499
|
+
const id = await this.socketClient.request({ url: 'id', params: { from: this.id } });
|
|
500
|
+
this.socketClient.peerId = id;
|
|
501
|
+
this.#stars[id] = this.socketClient;
|
|
502
|
+
}
|
|
503
|
+
catch (e) {
|
|
504
|
+
if (stars.indexOf(star) === stars.length - 1 && !this.socketClient)
|
|
505
|
+
throw new Error(`No star available to connect`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
this.setupListeners();
|
|
509
|
+
const peers = await this.socketClient.peernet.join({ id: this.id });
|
|
510
|
+
for (const id of peers) {
|
|
511
|
+
if (id !== this.id && !this.#connections[id])
|
|
512
|
+
this.#connections[id] = await new Peer({ channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id });
|
|
513
|
+
}
|
|
514
|
+
pubsub.subscribe('connection closed', (peer) => {
|
|
515
|
+
this.removePeer(peer.peerId);
|
|
516
|
+
setTimeout(() => {
|
|
517
|
+
this.peerJoined(peer.peerId);
|
|
518
|
+
}, 1000);
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
setupListeners() {
|
|
522
|
+
this.socketClient.subscribe('peer:joined', this.peerJoined);
|
|
523
|
+
this.socketClient.subscribe('peer:left', this.peerLeft);
|
|
524
|
+
this.socketClient.subscribe('star:left', this.starLeft);
|
|
525
|
+
}
|
|
526
|
+
starJoined(id) {
|
|
527
|
+
if (this.#stars[id]) {
|
|
528
|
+
this.#stars[id].close();
|
|
529
|
+
delete this.#stars[id];
|
|
530
|
+
}
|
|
531
|
+
console.log(`star ${id} joined`);
|
|
532
|
+
}
|
|
533
|
+
async starLeft(id) {
|
|
534
|
+
if (this.#stars[id]) {
|
|
535
|
+
this.#stars[id].close();
|
|
536
|
+
delete this.#stars[id];
|
|
537
|
+
}
|
|
538
|
+
if (this.socketClient?.peerId === id) {
|
|
539
|
+
this.socketClient.unsubscribe('peer:joined', this.peerJoined);
|
|
540
|
+
this.socketClient.unsubscribe('peer:left', this.peerLeft);
|
|
541
|
+
this.socketClient.unsubscribe('star:left', this.starLeft);
|
|
542
|
+
this.socketClient.close();
|
|
543
|
+
this.socketClient = undefined;
|
|
544
|
+
for (const star of this.starsConfig) {
|
|
545
|
+
try {
|
|
546
|
+
this.socketClient = await socketRequestClient(star, this.networkVersion);
|
|
547
|
+
if (!this.socketClient?.client?._connection.connected)
|
|
548
|
+
return;
|
|
549
|
+
const id = await this.socketClient.request({ url: 'id', params: { from: this.id } });
|
|
550
|
+
this.#stars[id] = this.socketClient;
|
|
551
|
+
this.socketClient.peerId = id;
|
|
552
|
+
const peers = await this.socketClient.peernet.join({ id: this.id });
|
|
553
|
+
this.setupListeners();
|
|
554
|
+
for (const id of peers) {
|
|
555
|
+
if (id !== this.id) {
|
|
556
|
+
if (!this.#connections[id]) {
|
|
557
|
+
if (id !== this.id)
|
|
558
|
+
this.#connections[id] = await new Peer({ channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id });
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
catch (e) {
|
|
564
|
+
console.log(e);
|
|
565
|
+
if (this.starsConfig.indexOf(star) === this.starsConfig.length - 1 && !this.socketClient)
|
|
566
|
+
throw new Error(`No star available to connect`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
globalThis.debug(`star ${id} left`);
|
|
571
|
+
}
|
|
572
|
+
peerLeft(peer) {
|
|
573
|
+
const id = peer.peerId || peer;
|
|
574
|
+
if (this.#connections[id]) {
|
|
575
|
+
this.#connections[id].close();
|
|
576
|
+
delete this.#connections[id];
|
|
577
|
+
}
|
|
578
|
+
globalThis.debug(`peer ${id} left`);
|
|
579
|
+
}
|
|
580
|
+
async peerJoined(peer, signal) {
|
|
581
|
+
const id = peer.peerId || peer;
|
|
582
|
+
if (this.#connections[id]) {
|
|
583
|
+
if (this.#connections[id].connected)
|
|
584
|
+
this.#connections[id].close();
|
|
585
|
+
delete this.#connections[id];
|
|
586
|
+
}
|
|
587
|
+
// RTCPeerConnection
|
|
588
|
+
this.#connections[id] = await new Peer({ initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id });
|
|
589
|
+
globalThis.debug(`peer ${id} joined`);
|
|
590
|
+
}
|
|
591
|
+
removePeer(peer) {
|
|
592
|
+
const id = peer.peerId || peer;
|
|
593
|
+
if (this.#connections[id]) {
|
|
594
|
+
this.#connections[id].connected && this.#connections[id].close();
|
|
595
|
+
delete this.#connections[id];
|
|
596
|
+
}
|
|
597
|
+
globalThis.debug(`peer ${id} removed`);
|
|
598
|
+
}
|
|
599
|
+
async close() {
|
|
600
|
+
this.socketClient.unsubscribe('peer:joined', this.peerJoined);
|
|
601
|
+
this.socketClient.unsubscribe('peer:left', this.peerLeft);
|
|
602
|
+
this.socketClient.unsubscribe('star:left', this.starLeft);
|
|
603
|
+
const promises = [
|
|
604
|
+
Object.values(this.#connections).map(connection => connection.close()),
|
|
605
|
+
Object.values(this.#stars).map(connection => connection.close()),
|
|
606
|
+
this.socketClient.close()
|
|
607
|
+
];
|
|
608
|
+
return Promise.allSettled(promises);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
export { Client as default };
|
|
@@ -2600,6 +2600,10 @@ var index = {
|
|
|
2600
2600
|
|
|
2601
2601
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
2602
2602
|
|
|
2603
|
+
function getDefaultExportFromCjs (x) {
|
|
2604
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
2605
|
+
}
|
|
2606
|
+
|
|
2603
2607
|
function getAugmentedNamespace(n) {
|
|
2604
2608
|
if (n.__esModule) return n;
|
|
2605
2609
|
var f = n.default;
|
|
@@ -16082,7 +16086,7 @@ class Identity {
|
|
|
16082
16086
|
globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
|
|
16083
16087
|
}
|
|
16084
16088
|
else {
|
|
16085
|
-
const importee = await import(/* webpackChunkName: "generate-account" */ './index-
|
|
16089
|
+
const importee = await import(/* webpackChunkName: "generate-account" */ './index-3d3f56ca.js');
|
|
16086
16090
|
const { identity, accounts } = await importee.default(password, this.network);
|
|
16087
16091
|
await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
|
|
16088
16092
|
await globalThis.walletStore.put('version', String(1));
|
|
@@ -16252,7 +16256,7 @@ class Peernet {
|
|
|
16252
16256
|
this.root = options.root;
|
|
16253
16257
|
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
|
|
16254
16258
|
// FolderMessageResponse
|
|
16255
|
-
} = await import(/* webpackChunkName: "messages" */ './messages-
|
|
16259
|
+
} = await import(/* webpackChunkName: "messages" */ './messages-af41e873.js');
|
|
16256
16260
|
/**
|
|
16257
16261
|
* proto Object containing protos
|
|
16258
16262
|
* @type {Object}
|
|
@@ -16319,7 +16323,7 @@ class Peernet {
|
|
|
16319
16323
|
if (this.#starting || this.#started)
|
|
16320
16324
|
return;
|
|
16321
16325
|
this.#starting = true;
|
|
16322
|
-
const importee = await import('./client-
|
|
16326
|
+
const importee = await import('./client-111c93a3.js');
|
|
16323
16327
|
/**
|
|
16324
16328
|
* @access public
|
|
16325
16329
|
* @type {PeernetClient}
|
|
@@ -16754,4 +16758,4 @@ class Peernet {
|
|
|
16754
16758
|
}
|
|
16755
16759
|
globalThis.Peernet = Peernet;
|
|
16756
16760
|
|
|
16757
|
-
export { FormatInterface as F, LittlePubSub as L, MultiWallet as M, Peernet as P, base58$1 as b, commonjsGlobal as c, encrypt as e, inherits_browserExports as i, require$$3 as r };
|
|
16761
|
+
export { FormatInterface as F, LittlePubSub as L, MultiWallet as M, Peernet as P, base58$1 as b, commonjsGlobal as c, encrypt as e, getDefaultExportFromCjs as g, inherits_browserExports as i, require$$3 as r };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { P as default } from './peernet-
|
|
1
|
+
export { P as default } from './peernet-f349ddaf.js';
|
|
2
2
|
import './value-157ab062.js';
|