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