@leofcoin/peernet 0.11.21 → 0.11.24
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/dist/browser/peernet.js +7056 -227
- package/dist/commonjs/{http-62c5b155.js → http-b45d1330.js} +1 -1
- package/dist/commonjs/peernet.js +65 -32
- package/dist/module/peernet.js +63 -31
- package/package.json +2 -2
|
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
10
10
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
11
11
|
var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
|
|
12
12
|
|
|
13
|
-
var version = "0.11.
|
|
13
|
+
var version = "0.11.23";
|
|
14
14
|
|
|
15
15
|
var api$1 = {
|
|
16
16
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
require('@vandeurenglenn/debug');
|
|
4
|
+
var pako = require('pako');
|
|
4
5
|
var LeofcoinStorage = require('@leofcoin/storage');
|
|
5
6
|
var peernetMessage = require('./peernet-message.js');
|
|
6
7
|
var dht = require('./dht.js');
|
|
@@ -45,6 +46,7 @@ function _interopNamespace(e) {
|
|
|
45
46
|
return Object.freeze(n);
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
var pako__default = /*#__PURE__*/_interopDefaultLegacy(pako);
|
|
48
50
|
var LeofcoinStorage__default = /*#__PURE__*/_interopDefaultLegacy(LeofcoinStorage);
|
|
49
51
|
var protons__default = /*#__PURE__*/_interopDefaultLegacy(protons);
|
|
50
52
|
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
|
|
@@ -285,11 +287,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
285
287
|
});
|
|
286
288
|
};
|
|
287
289
|
|
|
288
|
-
const messageQue = {};
|
|
289
|
-
|
|
290
290
|
class Peer {
|
|
291
291
|
#connection
|
|
292
|
-
#ready = false
|
|
293
292
|
#connecting = false
|
|
294
293
|
#connected = false
|
|
295
294
|
#channelReady = false
|
|
@@ -302,10 +301,14 @@ class Peer {
|
|
|
302
301
|
#remoteStreams = []
|
|
303
302
|
#pendingCandidates = []
|
|
304
303
|
#senderMap = new Map()
|
|
304
|
+
#messageQue = []
|
|
305
|
+
#chunksQue = {}
|
|
305
306
|
#iceCompleteTimer
|
|
306
307
|
#channel
|
|
307
308
|
#peerId
|
|
308
|
-
#chunkSize = 16384
|
|
309
|
+
#chunkSize = 16 * 1024 // 16384
|
|
310
|
+
#queRunning = false
|
|
311
|
+
#MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
|
|
309
312
|
|
|
310
313
|
get connection() {
|
|
311
314
|
return this.#connection
|
|
@@ -359,12 +362,13 @@ class Peer {
|
|
|
359
362
|
|
|
360
363
|
splitMessage(message) {
|
|
361
364
|
const chunks = [];
|
|
365
|
+
message = pako__default["default"].deflate(message);
|
|
362
366
|
const size = message.byteLength || message.length;
|
|
363
367
|
let offset = 0;
|
|
364
368
|
return new Promise((resolve, reject) => {
|
|
365
369
|
const splitMessage = () => {
|
|
366
370
|
const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
|
|
367
|
-
offset +=
|
|
371
|
+
offset += this.#chunkSize;
|
|
368
372
|
chunks.push(chunk);
|
|
369
373
|
if (offset < size) return splitMessage()
|
|
370
374
|
else resolve({chunks, size});
|
|
@@ -374,27 +378,53 @@ class Peer {
|
|
|
374
378
|
})
|
|
375
379
|
}
|
|
376
380
|
|
|
377
|
-
async
|
|
378
|
-
|
|
381
|
+
async #runQue() {
|
|
382
|
+
this.#queRunning = true;
|
|
383
|
+
if (this.#messageQue.length > 0 && this.channel.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
|
|
384
|
+
const message = this.#messageQue.shift();
|
|
385
|
+
|
|
386
|
+
switch (this.channel?.readyState) {
|
|
387
|
+
case 'open':
|
|
388
|
+
await this.channel.send(message);
|
|
389
|
+
if (this.#messageQue.length > 0) return this.#runQue()
|
|
390
|
+
else this.#queRunning = false;
|
|
391
|
+
break;
|
|
392
|
+
case 'closed':
|
|
393
|
+
case 'closing':
|
|
394
|
+
this.#messageQue = [];
|
|
395
|
+
this.#queRunning = false;
|
|
396
|
+
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
397
|
+
break;
|
|
398
|
+
case undefined:
|
|
399
|
+
this.#messageQue = [];
|
|
400
|
+
this.#queRunning = false;
|
|
401
|
+
debug(`trying to send before a channel is created`);
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
} else {
|
|
407
|
+
return setTimeout(() => this.#runQue(), 50)
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#trySend({ size, id, chunks }) {
|
|
379
412
|
let offset = 0;
|
|
413
|
+
|
|
380
414
|
for (const chunk of chunks) {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
debug(`trying to send before a channel is created`);
|
|
395
|
-
break;
|
|
396
|
-
}
|
|
397
|
-
}
|
|
415
|
+
const start = offset;
|
|
416
|
+
const end = offset + chunk.length;
|
|
417
|
+
|
|
418
|
+
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
419
|
+
this.#messageQue.push(message);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (!this.queRunning) return this.#runQue()
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async send(message, id) {
|
|
426
|
+
const { chunks, size } = await this.splitMessage(message);
|
|
427
|
+
return this.#trySend({ size, id, chunks })
|
|
398
428
|
}
|
|
399
429
|
|
|
400
430
|
request(data) {
|
|
@@ -480,20 +510,23 @@ class Peer {
|
|
|
480
510
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
481
511
|
// allow sharding (multiple peers share data)
|
|
482
512
|
pubsub.publish('peernet:shard', message);
|
|
483
|
-
|
|
513
|
+
const { id } = message;
|
|
484
514
|
|
|
485
|
-
if (
|
|
515
|
+
if (!this.#chunksQue[id]) this.#chunksQue[id] = [];
|
|
516
|
+
|
|
517
|
+
if (message.size > this.#chunksQue[id].length || message.size === this.#chunksQue[id].length) {
|
|
486
518
|
for (const value of Object.values(message.chunk)) {
|
|
487
|
-
|
|
519
|
+
this.#chunksQue[id].push(value);
|
|
488
520
|
}
|
|
489
521
|
}
|
|
490
522
|
|
|
491
|
-
if (message.size ===
|
|
492
|
-
|
|
493
|
-
delete
|
|
523
|
+
if (message.size === this.#chunksQue[id].length) {
|
|
524
|
+
let data = new Uint8Array(Object.values(this.#chunksQue[id]));
|
|
525
|
+
delete this.#chunksQue[id];
|
|
526
|
+
data = pako__default["default"].inflate(data);
|
|
527
|
+
pubsub.publish('peer:data', { id, data });
|
|
494
528
|
}
|
|
495
529
|
this.bw.down += message.byteLength || message.length;
|
|
496
|
-
|
|
497
530
|
}
|
|
498
531
|
|
|
499
532
|
_sendMessage(message) {
|
|
@@ -1850,7 +1883,7 @@ class Peernet {
|
|
|
1850
1883
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1851
1884
|
});
|
|
1852
1885
|
} else {
|
|
1853
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1886
|
+
const http = await Promise.resolve().then(function () { return require('./http-b45d1330.js'); });
|
|
1854
1887
|
if (environment !== 'browser') http.default(options);
|
|
1855
1888
|
}
|
|
1856
1889
|
|
package/dist/module/peernet.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug';
|
|
2
|
+
import pako from 'pako';
|
|
2
3
|
import LeofcoinStorage from '@leofcoin/storage';
|
|
3
4
|
import protons from 'protons';
|
|
4
5
|
import bs32 from '@vandeurenglenn/base32';
|
|
@@ -241,11 +242,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
241
242
|
});
|
|
242
243
|
};
|
|
243
244
|
|
|
244
|
-
const messageQue = {};
|
|
245
|
-
|
|
246
245
|
class Peer {
|
|
247
246
|
#connection
|
|
248
|
-
#ready = false
|
|
249
247
|
#connecting = false
|
|
250
248
|
#connected = false
|
|
251
249
|
#channelReady = false
|
|
@@ -258,10 +256,14 @@ class Peer {
|
|
|
258
256
|
#remoteStreams = []
|
|
259
257
|
#pendingCandidates = []
|
|
260
258
|
#senderMap = new Map()
|
|
259
|
+
#messageQue = []
|
|
260
|
+
#chunksQue = {}
|
|
261
261
|
#iceCompleteTimer
|
|
262
262
|
#channel
|
|
263
263
|
#peerId
|
|
264
|
-
#chunkSize = 16384
|
|
264
|
+
#chunkSize = 16 * 1024 // 16384
|
|
265
|
+
#queRunning = false
|
|
266
|
+
#MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
|
|
265
267
|
|
|
266
268
|
get connection() {
|
|
267
269
|
return this.#connection
|
|
@@ -315,12 +317,13 @@ class Peer {
|
|
|
315
317
|
|
|
316
318
|
splitMessage(message) {
|
|
317
319
|
const chunks = [];
|
|
320
|
+
message = pako.deflate(message);
|
|
318
321
|
const size = message.byteLength || message.length;
|
|
319
322
|
let offset = 0;
|
|
320
323
|
return new Promise((resolve, reject) => {
|
|
321
324
|
const splitMessage = () => {
|
|
322
325
|
const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
|
|
323
|
-
offset +=
|
|
326
|
+
offset += this.#chunkSize;
|
|
324
327
|
chunks.push(chunk);
|
|
325
328
|
if (offset < size) return splitMessage()
|
|
326
329
|
else resolve({chunks, size});
|
|
@@ -330,27 +333,53 @@ class Peer {
|
|
|
330
333
|
})
|
|
331
334
|
}
|
|
332
335
|
|
|
333
|
-
async
|
|
334
|
-
|
|
336
|
+
async #runQue() {
|
|
337
|
+
this.#queRunning = true;
|
|
338
|
+
if (this.#messageQue.length > 0 && this.channel.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
|
|
339
|
+
const message = this.#messageQue.shift();
|
|
340
|
+
|
|
341
|
+
switch (this.channel?.readyState) {
|
|
342
|
+
case 'open':
|
|
343
|
+
await this.channel.send(message);
|
|
344
|
+
if (this.#messageQue.length > 0) return this.#runQue()
|
|
345
|
+
else this.#queRunning = false;
|
|
346
|
+
break;
|
|
347
|
+
case 'closed':
|
|
348
|
+
case 'closing':
|
|
349
|
+
this.#messageQue = [];
|
|
350
|
+
this.#queRunning = false;
|
|
351
|
+
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
352
|
+
break;
|
|
353
|
+
case undefined:
|
|
354
|
+
this.#messageQue = [];
|
|
355
|
+
this.#queRunning = false;
|
|
356
|
+
debug(`trying to send before a channel is created`);
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
} else {
|
|
362
|
+
return setTimeout(() => this.#runQue(), 50)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
#trySend({ size, id, chunks }) {
|
|
335
367
|
let offset = 0;
|
|
368
|
+
|
|
336
369
|
for (const chunk of chunks) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
debug(`trying to send before a channel is created`);
|
|
351
|
-
break;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
370
|
+
const start = offset;
|
|
371
|
+
const end = offset + chunk.length;
|
|
372
|
+
|
|
373
|
+
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
374
|
+
this.#messageQue.push(message);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (!this.queRunning) return this.#runQue()
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async send(message, id) {
|
|
381
|
+
const { chunks, size } = await this.splitMessage(message);
|
|
382
|
+
return this.#trySend({ size, id, chunks })
|
|
354
383
|
}
|
|
355
384
|
|
|
356
385
|
request(data) {
|
|
@@ -436,20 +465,23 @@ class Peer {
|
|
|
436
465
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
437
466
|
// allow sharding (multiple peers share data)
|
|
438
467
|
pubsub.publish('peernet:shard', message);
|
|
439
|
-
|
|
468
|
+
const { id } = message;
|
|
440
469
|
|
|
441
|
-
if (
|
|
470
|
+
if (!this.#chunksQue[id]) this.#chunksQue[id] = [];
|
|
471
|
+
|
|
472
|
+
if (message.size > this.#chunksQue[id].length || message.size === this.#chunksQue[id].length) {
|
|
442
473
|
for (const value of Object.values(message.chunk)) {
|
|
443
|
-
|
|
474
|
+
this.#chunksQue[id].push(value);
|
|
444
475
|
}
|
|
445
476
|
}
|
|
446
477
|
|
|
447
|
-
if (message.size ===
|
|
448
|
-
|
|
449
|
-
delete
|
|
478
|
+
if (message.size === this.#chunksQue[id].length) {
|
|
479
|
+
let data = new Uint8Array(Object.values(this.#chunksQue[id]));
|
|
480
|
+
delete this.#chunksQue[id];
|
|
481
|
+
data = pako.inflate(data);
|
|
482
|
+
pubsub.publish('peer:data', { id, data });
|
|
450
483
|
}
|
|
451
484
|
this.bw.down += message.byteLength || message.length;
|
|
452
|
-
|
|
453
485
|
}
|
|
454
486
|
|
|
455
487
|
_sendMessage(message) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.24",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
27
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
|
-
"@leofcoin/peernet-swarm": "^0.
|
|
28
|
+
"@leofcoin/peernet-swarm": "^0.3.0",
|
|
29
29
|
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|