@leofcoin/peernet 0.11.12 → 0.11.15
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 +40436 -75323
- package/dist/commonjs/{client-1a1f75e6.js → client-5633ba04.js} +1 -4
- package/dist/commonjs/codec-format-interface.js +7 -7
- package/dist/commonjs/{http-dc146709.js → http-f3ec9531.js} +1 -1
- package/dist/commonjs/peernet.js +187 -62
- package/dist/module/peernet.js +191 -67
- package/package.json +3 -10
- package/rollup.config.js +10 -20
- package/src/codec/codec-format-interface.js +7 -7
- package/src/peernet.js +26 -19
- package/webpack.config.js +11 -2
package/src/peernet.js
CHANGED
|
@@ -89,7 +89,7 @@ export default class Peernet {
|
|
|
89
89
|
Storage = globalThis.LeofcoinStorage?.default ? globalThis.LeofcoinStorage.default : LeofcoinStorage
|
|
90
90
|
}
|
|
91
91
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
92
|
-
await new Storage(
|
|
92
|
+
await new Storage(name, root)
|
|
93
93
|
|
|
94
94
|
globalThis[`${name}Store`].private = isPrivate
|
|
95
95
|
if (!isPrivate) this.stores.push(name)
|
|
@@ -120,6 +120,13 @@ export default class Peernet {
|
|
|
120
120
|
return Object.entries(this.client.connections)
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
/**
|
|
124
|
+
* @return {String} id - peerId
|
|
125
|
+
*/
|
|
126
|
+
getConnection(id) {
|
|
127
|
+
return this.client.connections[id]
|
|
128
|
+
}
|
|
129
|
+
|
|
123
130
|
/**
|
|
124
131
|
* @private
|
|
125
132
|
*
|
|
@@ -242,6 +249,16 @@ export default class Peernet {
|
|
|
242
249
|
this.requestProtos[name] = method
|
|
243
250
|
}
|
|
244
251
|
|
|
252
|
+
sendMessage(peer, id, data) {
|
|
253
|
+
if (peer.readyState === 'open') {
|
|
254
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data})))
|
|
255
|
+
this.bw.up += data.length
|
|
256
|
+
} else if (peer.readyState === 'closed') {
|
|
257
|
+
this.removePeer(peer)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
}
|
|
261
|
+
|
|
245
262
|
/**
|
|
246
263
|
* @private
|
|
247
264
|
*
|
|
@@ -266,8 +283,7 @@ export default class Peernet {
|
|
|
266
283
|
const data = new DHTMessageResponse({hash, has})
|
|
267
284
|
const node = await this.prepareMessage(from, data.encoded)
|
|
268
285
|
|
|
269
|
-
peer
|
|
270
|
-
this.bw.up += node.encoded.length
|
|
286
|
+
sendMessage(peer, id, node.encoded)
|
|
271
287
|
} else if (proto.name === 'peernet-data') {
|
|
272
288
|
let { hash, store } = proto.decoded
|
|
273
289
|
let data
|
|
@@ -280,26 +296,21 @@ export default class Peernet {
|
|
|
280
296
|
data = await store.get(hash)
|
|
281
297
|
|
|
282
298
|
if (data) {
|
|
283
|
-
data = new DataMessageResponse({hash, data
|
|
299
|
+
data = new DataMessageResponse({hash, data});
|
|
284
300
|
|
|
285
301
|
const node = await this.prepareMessage(from, data.encoded)
|
|
286
|
-
peer
|
|
287
|
-
this.bw.up += node.encoded.length
|
|
302
|
+
sendMessage(peer, id, node.encoded)
|
|
288
303
|
}
|
|
289
304
|
} else {
|
|
290
305
|
// ban (trying to access private st)
|
|
291
306
|
}
|
|
292
307
|
|
|
293
308
|
} else if (proto.name === 'peernet-request') {
|
|
294
|
-
// TODO: make dynamic
|
|
295
|
-
// exposeddevapi[proto.decoded.request](proto.decoded.params)
|
|
296
309
|
const method = this.requestProtos[proto.decoded.request]
|
|
297
310
|
if (method) {
|
|
298
311
|
const data = await method()
|
|
299
312
|
const node = await this.prepareMessage(from, data.encoded)
|
|
300
|
-
peer
|
|
301
|
-
|
|
302
|
-
this.bw.up += node.encoded.length
|
|
313
|
+
sendMessage(peer, id, node.encoded)
|
|
303
314
|
}
|
|
304
315
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
305
316
|
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data)
|
|
@@ -570,13 +581,9 @@ export default class Peernet {
|
|
|
570
581
|
const id = Math.random().toString(36).slice(-12)
|
|
571
582
|
data = new PsMessage({data, topic})
|
|
572
583
|
for (const peer of this.connections) {
|
|
573
|
-
if (peer.
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
577
|
-
}
|
|
578
|
-
} else {
|
|
579
|
-
this.removePeer(peer)
|
|
584
|
+
if (peer.peerId !== this.peerId) {
|
|
585
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded)
|
|
586
|
+
sendMessage(peer, id, node.encoded)
|
|
580
587
|
}
|
|
581
588
|
// TODO: if peer subscribed
|
|
582
589
|
}
|
|
@@ -597,7 +604,7 @@ export default class Peernet {
|
|
|
597
604
|
}
|
|
598
605
|
|
|
599
606
|
async removePeer(peer) {
|
|
600
|
-
|
|
607
|
+
return this.client.removePeer(peer)
|
|
601
608
|
}
|
|
602
609
|
|
|
603
610
|
get Buffer() {
|
package/webpack.config.js
CHANGED
|
@@ -2,6 +2,7 @@ const path = require('path');
|
|
|
2
2
|
const webpack = require('webpack');
|
|
3
3
|
module.exports = [{
|
|
4
4
|
entry: './src/peernet.js',
|
|
5
|
+
mode: 'production',
|
|
5
6
|
plugins: [
|
|
6
7
|
// Work around for Buffer is undefined:
|
|
7
8
|
// https://github.com/webpack/changelog-v5/issues/10
|
|
@@ -14,7 +15,13 @@ module.exports = [{
|
|
|
14
15
|
new webpack.DefinePlugin({
|
|
15
16
|
HTTP_IMPORT: '',
|
|
16
17
|
"import fetch from 'node-fetch'": ''
|
|
17
|
-
})
|
|
18
|
+
}),
|
|
19
|
+
new webpack.IgnorePlugin({
|
|
20
|
+
checkResource(resource, context) {
|
|
21
|
+
if (resource.includes('./wordlists') && !resource.includes('english')) return true
|
|
22
|
+
// console.log(resource, context);
|
|
23
|
+
}
|
|
24
|
+
}) // bi39 wordlist (mnemonic) only english
|
|
18
25
|
],
|
|
19
26
|
optimization: {
|
|
20
27
|
minimize: false
|
|
@@ -27,7 +34,9 @@ resolve: {
|
|
|
27
34
|
"stream": require.resolve("stream-browserify"),
|
|
28
35
|
"buffer": require.resolve("buffer"),
|
|
29
36
|
"path": require.resolve("path-browserify"),
|
|
30
|
-
"os": require.resolve("os-browserify")
|
|
37
|
+
"os": require.resolve("os-browserify"),
|
|
38
|
+
"crypto": require.resolve("crypto-browserify"),
|
|
39
|
+
"vm": require.resolve("vm-browserify")
|
|
31
40
|
}
|
|
32
41
|
},
|
|
33
42
|
output: {
|