@leofcoin/peernet 0.11.9 → 0.11.12
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 +6 -18
- package/dist/commonjs/{http-63196be9.js → http-dc146709.js} +1 -1
- package/dist/commonjs/peernet.js +6 -19
- package/dist/module/peernet.js +5 -18
- package/package.json +1 -1
- package/src/peernet.js +3 -11
- package/src/utils/utils.js +0 -4
- package/src/client.js +0 -75
- package/src/peer.js +0 -67
package/dist/browser/peernet.js
CHANGED
|
@@ -83727,10 +83727,6 @@ const expected = (expected, actual) => {
|
|
|
83727
83727
|
${entries.join('\n\t')}`;
|
|
83728
83728
|
}
|
|
83729
83729
|
|
|
83730
|
-
const utils_debug = (log) => {
|
|
83731
|
-
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;')
|
|
83732
|
-
}
|
|
83733
|
-
|
|
83734
83730
|
const protoFor = (data) => {
|
|
83735
83731
|
if (!utils_Buffer.isBuffer(data)) data = utils_Buffer.from(data)
|
|
83736
83732
|
const codec = new PeernetCodec(data)
|
|
@@ -84530,6 +84526,7 @@ const nothingFoundError = (hash) => {
|
|
|
84530
84526
|
|
|
84531
84527
|
|
|
84532
84528
|
|
|
84529
|
+
|
|
84533
84530
|
|
|
84534
84531
|
|
|
84535
84532
|
globalThis.leofcoin = globalThis.leofcoin || {}
|
|
@@ -84648,7 +84645,6 @@ class Peernet {
|
|
|
84648
84645
|
* @type {Map}
|
|
84649
84646
|
* @property {Object} peer Instance of Peer
|
|
84650
84647
|
*/
|
|
84651
|
-
this.peerMap = new Map()
|
|
84652
84648
|
this.stores = []
|
|
84653
84649
|
this.requestProtos = {}
|
|
84654
84650
|
this.storePrefix = options.storePrefix
|
|
@@ -84748,14 +84744,6 @@ class Peernet {
|
|
|
84748
84744
|
return this
|
|
84749
84745
|
}
|
|
84750
84746
|
|
|
84751
|
-
_getPeerId(id) {
|
|
84752
|
-
for (const entry of [...this.peerMap.entries()]) {
|
|
84753
|
-
for (const _id of entry[1]) {
|
|
84754
|
-
if (_id === id) return entry[0]
|
|
84755
|
-
}
|
|
84756
|
-
}
|
|
84757
|
-
}
|
|
84758
|
-
|
|
84759
84747
|
addRequestHandler(name, method) {
|
|
84760
84748
|
this.requestProtos[name] = method
|
|
84761
84749
|
}
|
|
@@ -84820,7 +84808,7 @@ class Peernet {
|
|
|
84820
84808
|
this.bw.up += node.encoded.length
|
|
84821
84809
|
}
|
|
84822
84810
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
84823
|
-
globalSub.publish(proto.decoded.topic
|
|
84811
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data)
|
|
84824
84812
|
}
|
|
84825
84813
|
// }
|
|
84826
84814
|
}
|
|
@@ -84928,7 +84916,7 @@ class Peernet {
|
|
|
84928
84916
|
async requestData(hash, store) {
|
|
84929
84917
|
const providers = await this.providersFor(hash)
|
|
84930
84918
|
if (!providers || providers.size === 0) throw nothingFoundError(hash)
|
|
84931
|
-
|
|
84919
|
+
debug(`found ${providers.size} provider(s) for ${hash}`)
|
|
84932
84920
|
// get closest peer on earth
|
|
84933
84921
|
const closestPeer = await this.dht.closestPeer(providers)
|
|
84934
84922
|
// get peer instance by id
|
|
@@ -84970,7 +84958,7 @@ class Peernet {
|
|
|
84970
84958
|
* @param {String} hash
|
|
84971
84959
|
*/
|
|
84972
84960
|
get: async (hash) => {
|
|
84973
|
-
|
|
84961
|
+
debug(`get message ${hash}`)
|
|
84974
84962
|
const message = await messageStore.has(hash)
|
|
84975
84963
|
if (message) return await messageStore.get(hash)
|
|
84976
84964
|
return this.requestData(hash, 'message')
|
|
@@ -84998,7 +84986,7 @@ class Peernet {
|
|
|
84998
84986
|
* @param {String} hash
|
|
84999
84987
|
*/
|
|
85000
84988
|
get: async (hash) => {
|
|
85001
|
-
|
|
84989
|
+
debug(`get data ${hash}`)
|
|
85002
84990
|
const data = await dataStore.has(hash)
|
|
85003
84991
|
if (data) return await dataStore.get(hash)
|
|
85004
84992
|
return this.requestData(hash, 'data')
|
|
@@ -85041,7 +85029,7 @@ class Peernet {
|
|
|
85041
85029
|
* @param {String} store - storeName to access
|
|
85042
85030
|
*/
|
|
85043
85031
|
async get(hash, store) {
|
|
85044
|
-
|
|
85032
|
+
debug(`get ${hash}`)
|
|
85045
85033
|
let data
|
|
85046
85034
|
if (store) store = globalThis[`${store}Store`]
|
|
85047
85035
|
if (!store) store = await this.whichStore([...this.stores], hash)
|
|
@@ -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.11";
|
|
14
14
|
|
|
15
15
|
var api$1 = {
|
|
16
16
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -776,10 +776,6 @@ class ChatMessage extends codecFormatInterface {
|
|
|
776
776
|
}
|
|
777
777
|
}
|
|
778
778
|
|
|
779
|
-
const debug$1 = (log) => {
|
|
780
|
-
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
781
|
-
};
|
|
782
|
-
|
|
783
779
|
const protoFor = (data) => {
|
|
784
780
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
785
781
|
const codec$1 = new codec.PeernetCodec(data);
|
|
@@ -1595,7 +1591,6 @@ class Peernet {
|
|
|
1595
1591
|
* @type {Map}
|
|
1596
1592
|
* @property {Object} peer Instance of Peer
|
|
1597
1593
|
*/
|
|
1598
|
-
this.peerMap = new Map();
|
|
1599
1594
|
this.stores = [];
|
|
1600
1595
|
this.requestProtos = {};
|
|
1601
1596
|
this.storePrefix = options.storePrefix;
|
|
@@ -1638,7 +1633,7 @@ class Peernet {
|
|
|
1638
1633
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1639
1634
|
});
|
|
1640
1635
|
} else {
|
|
1641
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1636
|
+
const http = await Promise.resolve().then(function () { return require('./http-dc146709.js'); });
|
|
1642
1637
|
if (environment !== 'browser') http.default(options);
|
|
1643
1638
|
}
|
|
1644
1639
|
|
|
@@ -1702,14 +1697,6 @@ class Peernet {
|
|
|
1702
1697
|
return this
|
|
1703
1698
|
}
|
|
1704
1699
|
|
|
1705
|
-
_getPeerId(id) {
|
|
1706
|
-
for (const entry of [...this.peerMap.entries()]) {
|
|
1707
|
-
for (const _id of entry[1]) {
|
|
1708
|
-
if (_id === id) return entry[0]
|
|
1709
|
-
}
|
|
1710
|
-
}
|
|
1711
|
-
}
|
|
1712
|
-
|
|
1713
1700
|
addRequestHandler(name, method) {
|
|
1714
1701
|
this.requestProtos[name] = method;
|
|
1715
1702
|
}
|
|
@@ -1772,7 +1759,7 @@ class Peernet {
|
|
|
1772
1759
|
this.bw.up += node.encoded.length;
|
|
1773
1760
|
}
|
|
1774
1761
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
1775
|
-
globalSub.publish(proto.decoded.topic
|
|
1762
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data);
|
|
1776
1763
|
}
|
|
1777
1764
|
// }
|
|
1778
1765
|
}
|
|
@@ -1880,7 +1867,7 @@ class Peernet {
|
|
|
1880
1867
|
async requestData(hash, store) {
|
|
1881
1868
|
const providers = await this.providersFor(hash);
|
|
1882
1869
|
if (!providers || providers.size === 0) throw nothingFoundError(hash)
|
|
1883
|
-
debug
|
|
1870
|
+
debug(`found ${providers.size} provider(s) for ${hash}`);
|
|
1884
1871
|
// get closest peer on earth
|
|
1885
1872
|
const closestPeer = await this.dht.closestPeer(providers);
|
|
1886
1873
|
// get peer instance by id
|
|
@@ -1922,7 +1909,7 @@ class Peernet {
|
|
|
1922
1909
|
* @param {String} hash
|
|
1923
1910
|
*/
|
|
1924
1911
|
get: async (hash) => {
|
|
1925
|
-
debug
|
|
1912
|
+
debug(`get message ${hash}`);
|
|
1926
1913
|
const message = await messageStore.has(hash);
|
|
1927
1914
|
if (message) return await messageStore.get(hash)
|
|
1928
1915
|
return this.requestData(hash, 'message')
|
|
@@ -1950,7 +1937,7 @@ class Peernet {
|
|
|
1950
1937
|
* @param {String} hash
|
|
1951
1938
|
*/
|
|
1952
1939
|
get: async (hash) => {
|
|
1953
|
-
debug
|
|
1940
|
+
debug(`get data ${hash}`);
|
|
1954
1941
|
const data = await dataStore.has(hash);
|
|
1955
1942
|
if (data) return await dataStore.get(hash)
|
|
1956
1943
|
return this.requestData(hash, 'data')
|
|
@@ -1992,7 +1979,7 @@ class Peernet {
|
|
|
1992
1979
|
* @param {String} store - storeName to access
|
|
1993
1980
|
*/
|
|
1994
1981
|
async get(hash, store) {
|
|
1995
|
-
debug
|
|
1982
|
+
debug(`get ${hash}`);
|
|
1996
1983
|
let data;
|
|
1997
1984
|
if (store) store = globalThis[`${store}Store`];
|
|
1998
1985
|
if (!store) store = await this.whichStore([...this.stores], hash);
|
package/dist/module/peernet.js
CHANGED
|
@@ -1373,10 +1373,6 @@ class ChatMessage extends FormatInterface {
|
|
|
1373
1373
|
}
|
|
1374
1374
|
}
|
|
1375
1375
|
|
|
1376
|
-
const debug$1 = (log) => {
|
|
1377
|
-
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
1378
|
-
};
|
|
1379
|
-
|
|
1380
1376
|
const protoFor = (data) => {
|
|
1381
1377
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
1382
1378
|
const codec = new PeernetCodec(data);
|
|
@@ -2192,7 +2188,6 @@ class Peernet {
|
|
|
2192
2188
|
* @type {Map}
|
|
2193
2189
|
* @property {Object} peer Instance of Peer
|
|
2194
2190
|
*/
|
|
2195
|
-
this.peerMap = new Map();
|
|
2196
2191
|
this.stores = [];
|
|
2197
2192
|
this.requestProtos = {};
|
|
2198
2193
|
this.storePrefix = options.storePrefix;
|
|
@@ -2291,14 +2286,6 @@ class Peernet {
|
|
|
2291
2286
|
return this
|
|
2292
2287
|
}
|
|
2293
2288
|
|
|
2294
|
-
_getPeerId(id) {
|
|
2295
|
-
for (const entry of [...this.peerMap.entries()]) {
|
|
2296
|
-
for (const _id of entry[1]) {
|
|
2297
|
-
if (_id === id) return entry[0]
|
|
2298
|
-
}
|
|
2299
|
-
}
|
|
2300
|
-
}
|
|
2301
|
-
|
|
2302
2289
|
addRequestHandler(name, method) {
|
|
2303
2290
|
this.requestProtos[name] = method;
|
|
2304
2291
|
}
|
|
@@ -2361,7 +2348,7 @@ class Peernet {
|
|
|
2361
2348
|
this.bw.up += node.encoded.length;
|
|
2362
2349
|
}
|
|
2363
2350
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
2364
|
-
globalSub.publish(proto.decoded.topic
|
|
2351
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data);
|
|
2365
2352
|
}
|
|
2366
2353
|
// }
|
|
2367
2354
|
}
|
|
@@ -2469,7 +2456,7 @@ class Peernet {
|
|
|
2469
2456
|
async requestData(hash, store) {
|
|
2470
2457
|
const providers = await this.providersFor(hash);
|
|
2471
2458
|
if (!providers || providers.size === 0) throw nothingFoundError(hash)
|
|
2472
|
-
debug
|
|
2459
|
+
debug(`found ${providers.size} provider(s) for ${hash}`);
|
|
2473
2460
|
// get closest peer on earth
|
|
2474
2461
|
const closestPeer = await this.dht.closestPeer(providers);
|
|
2475
2462
|
// get peer instance by id
|
|
@@ -2511,7 +2498,7 @@ class Peernet {
|
|
|
2511
2498
|
* @param {String} hash
|
|
2512
2499
|
*/
|
|
2513
2500
|
get: async (hash) => {
|
|
2514
|
-
debug
|
|
2501
|
+
debug(`get message ${hash}`);
|
|
2515
2502
|
const message = await messageStore.has(hash);
|
|
2516
2503
|
if (message) return await messageStore.get(hash)
|
|
2517
2504
|
return this.requestData(hash, 'message')
|
|
@@ -2539,7 +2526,7 @@ class Peernet {
|
|
|
2539
2526
|
* @param {String} hash
|
|
2540
2527
|
*/
|
|
2541
2528
|
get: async (hash) => {
|
|
2542
|
-
debug
|
|
2529
|
+
debug(`get data ${hash}`);
|
|
2543
2530
|
const data = await dataStore.has(hash);
|
|
2544
2531
|
if (data) return await dataStore.get(hash)
|
|
2545
2532
|
return this.requestData(hash, 'data')
|
|
@@ -2581,7 +2568,7 @@ class Peernet {
|
|
|
2581
2568
|
* @param {String} store - storeName to access
|
|
2582
2569
|
*/
|
|
2583
2570
|
async get(hash, store) {
|
|
2584
|
-
debug
|
|
2571
|
+
debug(`get ${hash}`);
|
|
2585
2572
|
let data;
|
|
2586
2573
|
if (store) store = globalThis[`${store}Store`];
|
|
2587
2574
|
if (!store) store = await this.whichStore([...this.stores], hash);
|
package/package.json
CHANGED
package/src/peernet.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import '@vandeurenglenn/debug'
|
|
1
2
|
import Client from './../node_modules/@leofcoin/peernet-swarm/dist/es/client.js'
|
|
2
3
|
import LeofcoinStorage from '@leofcoin/storage'
|
|
3
4
|
import LeofcoinStorageClient from './http/client/storage.js'
|
|
@@ -16,7 +17,7 @@ import PeerDiscovery from './discovery/peer-discovery'
|
|
|
16
17
|
import DHT from './dht/dht.js'
|
|
17
18
|
import Hash from './hash/hash'
|
|
18
19
|
import codecs from './codec/codecs'
|
|
19
|
-
import {
|
|
20
|
+
import { protoFor, target } from './utils/utils.js'
|
|
20
21
|
import generateAccount from '@leofcoin/generate-account'
|
|
21
22
|
import MessageHandler from './handlers/message.js'
|
|
22
23
|
import { encapsulatedError, dhtError,
|
|
@@ -138,7 +139,6 @@ export default class Peernet {
|
|
|
138
139
|
* @type {Map}
|
|
139
140
|
* @property {Object} peer Instance of Peer
|
|
140
141
|
*/
|
|
141
|
-
this.peerMap = new Map()
|
|
142
142
|
this.stores = []
|
|
143
143
|
this.requestProtos = {}
|
|
144
144
|
this.storePrefix = options.storePrefix
|
|
@@ -238,14 +238,6 @@ export default class Peernet {
|
|
|
238
238
|
return this
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
-
_getPeerId(id) {
|
|
242
|
-
for (const entry of [...this.peerMap.entries()]) {
|
|
243
|
-
for (const _id of entry[1]) {
|
|
244
|
-
if (_id === id) return entry[0]
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
241
|
addRequestHandler(name, method) {
|
|
250
242
|
this.requestProtos[name] = method
|
|
251
243
|
}
|
|
@@ -310,7 +302,7 @@ export default class Peernet {
|
|
|
310
302
|
this.bw.up += node.encoded.length
|
|
311
303
|
}
|
|
312
304
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
313
|
-
globalSub.publish(proto.decoded.topic
|
|
305
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data)
|
|
314
306
|
}
|
|
315
307
|
// }
|
|
316
308
|
}
|
package/src/utils/utils.js
CHANGED
|
@@ -16,10 +16,6 @@ export const expected = (expected, actual) => {
|
|
|
16
16
|
${entries.join('\n\t')}`;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export const debug = (log) => {
|
|
20
|
-
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;')
|
|
21
|
-
}
|
|
22
|
-
|
|
23
19
|
export const protoFor = (data) => {
|
|
24
20
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data)
|
|
25
21
|
const codec = new Codec(data)
|
package/src/client.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import Pubsub from '@vandeurenglenn/little-pubsub'
|
|
2
|
-
import PeernetPeer from './peer.js'
|
|
3
|
-
import sha256 from 'crypto-js/sha256'
|
|
4
|
-
|
|
5
|
-
import P2P from 'p2pt'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Array of peers
|
|
10
|
-
* @type {Array}
|
|
11
|
-
* @property {PeernetPeer} peer Instance of Peer
|
|
12
|
-
*/
|
|
13
|
-
globalThis.connections = new Map()
|
|
14
|
-
globalThis.recentConnections = new Map()
|
|
15
|
-
globalThis.pubsub = globalThis.pubsub || new Pubsub({verbose: false})
|
|
16
|
-
|
|
17
|
-
export default class PeernetClient {
|
|
18
|
-
constructor(options = {}) {
|
|
19
|
-
if (!options.id) options.id = Buffer.from('00000000000000000000000000000000')
|
|
20
|
-
if (!options.networkVersion) options.networkVersion = 'v0.1.0'
|
|
21
|
-
if (!options.networkName) options.networkName = 'peernet'
|
|
22
|
-
this.id = options.id
|
|
23
|
-
|
|
24
|
-
this.topic = Buffer.from(sha256(`${options.networkName}-${options.networkVersion}`).toString())
|
|
25
|
-
|
|
26
|
-
const trackers = [
|
|
27
|
-
'wss://star.leofcoin.org',
|
|
28
|
-
'wss://tracker.openwebtorrent.com',
|
|
29
|
-
// 'wss://tracker.sloppyta.co:443/announce',
|
|
30
|
-
]
|
|
31
|
-
this.p2p = new P2P(trackers, this.topic.slice(0, 20))
|
|
32
|
-
this.p2p.on('peerconnect', (peer) => {
|
|
33
|
-
peer = new PeernetPeer(peer.id, peer)
|
|
34
|
-
connections.set(peer.id, peer)
|
|
35
|
-
pubsub.publish('peer:discovered', peer)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
this.p2p.on('peerclose', (peer) => {
|
|
39
|
-
// TODO: close peernetPeer
|
|
40
|
-
const peernetPeer = connections.get(peer.id)
|
|
41
|
-
if (peernetPeer) {
|
|
42
|
-
peernetPeer.close()
|
|
43
|
-
}
|
|
44
|
-
connections.delete(peer.id)
|
|
45
|
-
pubsub.publish('peer:disconnected', peer)
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
this.p2p.start()
|
|
49
|
-
|
|
50
|
-
if (globalThis.process) {
|
|
51
|
-
process.on('SIGINT', async () => {
|
|
52
|
-
console.log('Caught interrupt signal')
|
|
53
|
-
this.close()
|
|
54
|
-
setTimeout(async () => {
|
|
55
|
-
process.exit();
|
|
56
|
-
}, 100);
|
|
57
|
-
})
|
|
58
|
-
} else {
|
|
59
|
-
globalThis.onbeforeunload = () => {
|
|
60
|
-
this.close()
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
//
|
|
64
|
-
// this.sw.on('close', () => {
|
|
65
|
-
// })
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
close() {
|
|
69
|
-
return this.p2p.destroy()
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
_peers() {
|
|
73
|
-
return this.p2p.getPeers()
|
|
74
|
-
}
|
|
75
|
-
}
|
package/src/peer.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
export default class PeernetPeer {
|
|
2
|
-
constructor(id, connection) {
|
|
3
|
-
this._events = {}
|
|
4
|
-
this.bw = {
|
|
5
|
-
up: 0,
|
|
6
|
-
down: 0,
|
|
7
|
-
}
|
|
8
|
-
this.id = id
|
|
9
|
-
this.connection = connection
|
|
10
|
-
|
|
11
|
-
this.connection.on('data', (message) => {
|
|
12
|
-
this.bw.down += message.length
|
|
13
|
-
pubsub.publish('peernet.data', JSON.parse(message.toString()))
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
request(data) {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
const id = Math.random().toString(36).slice(-12)
|
|
20
|
-
data = Buffer.from(JSON.stringify({id, data}))
|
|
21
|
-
const _onData = (message) => {
|
|
22
|
-
if (message.id !== id) return
|
|
23
|
-
|
|
24
|
-
resolve(message.data)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
pubsub.subscribe('peernet.data', _onData)
|
|
28
|
-
|
|
29
|
-
// cleanup subscriptions
|
|
30
|
-
setTimeout(() => {
|
|
31
|
-
pubsub.unsubscribe('peernet.data', _onData)
|
|
32
|
-
}, 5000);
|
|
33
|
-
|
|
34
|
-
this.write(data)
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
write(data) {
|
|
39
|
-
if (!Buffer.isBuffer(data)) data = Buffer.from(data)
|
|
40
|
-
|
|
41
|
-
this.bw.up += data.length
|
|
42
|
-
this.connection.write(data)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
on(event = 'peernet.data', cb) {
|
|
46
|
-
this._events[event] = cb
|
|
47
|
-
pubsub.subscribe(event, cb)
|
|
48
|
-
// this.connection.on(event, cb)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
removeListener(event = 'data', cb) {
|
|
52
|
-
delete this._events[event]
|
|
53
|
-
pubsub.unsubscribe(event, cb)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
close() {
|
|
57
|
-
for (const event of Object.keys(this._events)) {
|
|
58
|
-
pubsub.unsubscribe(event, this._events[event])
|
|
59
|
-
}
|
|
60
|
-
this._events = []
|
|
61
|
-
|
|
62
|
-
for (const event of this.connection._events.data) {
|
|
63
|
-
this.connection.removeListener('data', event)
|
|
64
|
-
}
|
|
65
|
-
this.connection.destroy()
|
|
66
|
-
}
|
|
67
|
-
}
|