@leofcoin/peernet 1.1.50 → 1.1.51
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/browser-store.js +58 -141
- package/exports/browser/client-3221a5d7.js +17580 -0
- package/exports/browser/dht/dht.d.ts +29 -7
- package/exports/browser/identity.d.ts +15 -0
- package/exports/browser/{index-6311966e.js → index-36ed3c80.js} +2 -2
- package/exports/browser/{messages-54aa3cad.js → messages-b7f50660.js} +2 -2
- package/exports/browser/{peernet-a4156bfd.js → peernet-1b30b405.js} +432 -531
- package/exports/browser/peernet.d.ts +25 -32
- package/exports/browser/peernet.js +2 -2
- package/exports/browser/types.d.ts +16 -0
- package/exports/browser/value-4e80eeeb.js +73 -0
- package/exports/peernet.js +35 -34
- package/exports/store.js +117 -193
- package/exports/types/dht/dht.d.ts +29 -7
- package/exports/types/identity.d.ts +15 -0
- package/exports/types/peernet.d.ts +25 -32
- package/exports/types/types.d.ts +16 -0
- package/package.json +2 -2
- package/src/dht/dht.ts +36 -16
- package/src/peernet.ts +47 -60
- package/src/types.ts +28 -0
- package/exports/browser/client-6072af1a.js +0 -41092
- package/exports/browser/value-157ab062.js +0 -64
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
import '@vandeurenglenn/debug';
|
|
3
|
-
import
|
|
3
|
+
import PeerDiscovery from './discovery/peer-discovery.js';
|
|
4
4
|
import DHT from './dht/dht.js';
|
|
5
5
|
import MessageHandler from './handlers/message.js';
|
|
6
6
|
import LeofcoinStorageClass from '@leofcoin/storage';
|
|
7
7
|
import Identity from './identity.js';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var pubsub: PubSub;
|
|
11
|
-
var peernet: Peernet;
|
|
12
|
-
var LeofcoinStorage: typeof LeofcoinStorageClass;
|
|
13
|
-
var LeofcoinStorageClient: any;
|
|
14
|
-
}
|
|
8
|
+
import swarm from '@netpeer/p2pt-swarm';
|
|
9
|
+
import P2PTPeer from '@netpeer/p2pt-swarm/peer';
|
|
15
10
|
/**
|
|
16
11
|
* @access public
|
|
17
12
|
* @example
|
|
@@ -19,20 +14,18 @@ declare global {
|
|
|
19
14
|
*/
|
|
20
15
|
export default class Peernet {
|
|
21
16
|
#private;
|
|
17
|
+
storePrefix: string;
|
|
18
|
+
root: string;
|
|
22
19
|
identity: Identity;
|
|
23
20
|
stores: string[];
|
|
21
|
+
peerId: string;
|
|
24
22
|
/**
|
|
25
23
|
* @type {Object}
|
|
26
24
|
* @property {Object} peer Instance of Peer
|
|
27
25
|
*/
|
|
28
26
|
dht: DHT;
|
|
29
27
|
/** @leofcoin/peernet-swarm/client */
|
|
30
|
-
client:
|
|
31
|
-
connections: [];
|
|
32
|
-
id: string;
|
|
33
|
-
removePeer: (peer: string) => {};
|
|
34
|
-
close: () => {};
|
|
35
|
-
};
|
|
28
|
+
client: swarm;
|
|
36
29
|
network: string;
|
|
37
30
|
stars: string[];
|
|
38
31
|
networkVersion: string;
|
|
@@ -44,6 +37,7 @@ export default class Peernet {
|
|
|
44
37
|
autoStart: boolean;
|
|
45
38
|
requestProtos: {};
|
|
46
39
|
_messageHandler: MessageHandler;
|
|
40
|
+
_peerHandler: PeerDiscovery;
|
|
47
41
|
protos: {};
|
|
48
42
|
/**
|
|
49
43
|
* @access public
|
|
@@ -63,7 +57,7 @@ export default class Peernet {
|
|
|
63
57
|
get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]>;
|
|
64
58
|
get defaultStores(): string[];
|
|
65
59
|
addProto(name: any, proto: any): void;
|
|
66
|
-
addCodec(codec: any):
|
|
60
|
+
addCodec(codec: any): any;
|
|
67
61
|
addStore(name: any, prefix: any, root: any, isPrivate?: boolean): Promise<void>;
|
|
68
62
|
/**
|
|
69
63
|
* @see MessageHandler
|
|
@@ -74,13 +68,13 @@ export default class Peernet {
|
|
|
74
68
|
*
|
|
75
69
|
* @return {Array} peerId
|
|
76
70
|
*/
|
|
77
|
-
get peers(): [string,
|
|
78
|
-
get connections():
|
|
79
|
-
get peerEntries():
|
|
71
|
+
get peers(): [string, P2PTPeer][];
|
|
72
|
+
get connections(): P2PTPeer[];
|
|
73
|
+
get peerEntries(): P2PTPeer[];
|
|
80
74
|
/**
|
|
81
75
|
* @return {String} id - peerId
|
|
82
76
|
*/
|
|
83
|
-
getConnection(id: any):
|
|
77
|
+
getConnection(id: any): any;
|
|
84
78
|
/**
|
|
85
79
|
* @private
|
|
86
80
|
*
|
|
@@ -89,7 +83,7 @@ export default class Peernet {
|
|
|
89
83
|
*
|
|
90
84
|
* @return {Promise} instance of Peernet
|
|
91
85
|
*/
|
|
92
|
-
_init(options: any, password: any): Peernet
|
|
86
|
+
_init(options: any, password: any): Promise<Peernet>;
|
|
93
87
|
start(): Promise<void>;
|
|
94
88
|
addRequestHandler(name: any, method: any): void;
|
|
95
89
|
sendMessage(peer: any, id: any, data: any): Promise<void>;
|
|
@@ -114,16 +108,16 @@ export default class Peernet {
|
|
|
114
108
|
*
|
|
115
109
|
* @param {String} hash
|
|
116
110
|
*/
|
|
117
|
-
providersFor(hash: string, store?: undefined): Promise<
|
|
111
|
+
providersFor(hash: string, store?: undefined): Promise<import("./dht/dht.js").DHTProviderMapValue>;
|
|
118
112
|
get block(): {
|
|
119
|
-
get: (hash:
|
|
120
|
-
put: (hash:
|
|
121
|
-
has: (hash:
|
|
113
|
+
get: (hash: string) => Promise<any>;
|
|
114
|
+
put: (hash: string, data: Uint8Array) => Promise<any>;
|
|
115
|
+
has: (hash: string) => Promise<any>;
|
|
122
116
|
};
|
|
123
117
|
get transaction(): {
|
|
124
|
-
get: (hash:
|
|
125
|
-
put: (hash:
|
|
126
|
-
has: (hash:
|
|
118
|
+
get: (hash: string) => Promise<any>;
|
|
119
|
+
put: (hash: string, data: Uint8Array) => Promise<any>;
|
|
120
|
+
has: (hash: string) => Promise<any>;
|
|
127
121
|
};
|
|
128
122
|
requestData(hash: any, store: any): any;
|
|
129
123
|
get message(): {
|
|
@@ -207,9 +201,9 @@ export default class Peernet {
|
|
|
207
201
|
*
|
|
208
202
|
* @param {String} hash
|
|
209
203
|
* @param {Buffer} data
|
|
210
|
-
* @param {String}
|
|
204
|
+
* @param {String} storeName - storeName to access
|
|
211
205
|
*/
|
|
212
|
-
put(hash:
|
|
206
|
+
put(hash: string, data: Uint8Array, storeName?: string | LeofcoinStorageClass): Promise<any>;
|
|
213
207
|
/**
|
|
214
208
|
* @param {String} hash
|
|
215
209
|
* @return {Boolean}
|
|
@@ -221,13 +215,12 @@ export default class Peernet {
|
|
|
221
215
|
* @param {String|Object|Array|Boolean|Buffer} data
|
|
222
216
|
*/
|
|
223
217
|
publish(topic: any, data: any): Promise<void>;
|
|
224
|
-
createHash(data: any, name: any): any;
|
|
225
218
|
/**
|
|
226
219
|
*
|
|
227
220
|
* @param {String} topic
|
|
228
221
|
* @param {Method} cb
|
|
229
222
|
*/
|
|
230
|
-
subscribe(topic:
|
|
223
|
+
subscribe(topic: string, callback: Function): Promise<void>;
|
|
231
224
|
removePeer(peer: any): Promise<any>;
|
|
232
225
|
get Buffer(): BufferConstructor;
|
|
233
226
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { P as default } from './peernet-
|
|
2
|
-
import './value-
|
|
1
|
+
export { P as default } from './peernet-1b30b405.js';
|
|
2
|
+
import './value-4e80eeeb.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import '@vandeurenglenn/debug';
|
|
2
|
+
import PubSub from '@vandeurenglenn/little-pubsub';
|
|
3
|
+
import Peernet from './peernet.js';
|
|
4
|
+
import { LeofcoinStorage as LeofcoinStorageClass } from '@leofcoin/storage';
|
|
5
|
+
declare global {
|
|
6
|
+
var debug: (message: any) => string;
|
|
7
|
+
var globalSub: PubSub;
|
|
8
|
+
var pubsub: PubSub;
|
|
9
|
+
var peernet: Peernet;
|
|
10
|
+
var LeofcoinStorage: typeof LeofcoinStorageClass;
|
|
11
|
+
var LeofcoinStorageClient: any;
|
|
12
|
+
var messageStore: typeof LeofcoinStorage;
|
|
13
|
+
var dataStore: typeof LeofcoinStorage;
|
|
14
|
+
var transactionStore: typeof LeofcoinStorage;
|
|
15
|
+
var blockStore: typeof LeofcoinStorage;
|
|
16
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const encode = (string) => {
|
|
2
|
+
if (typeof string === 'string')
|
|
3
|
+
return new TextEncoder().encode(string);
|
|
4
|
+
throw Error(`expected typeof String instead got ${string}`);
|
|
5
|
+
};
|
|
6
|
+
const decode = (uint8Array) => {
|
|
7
|
+
if (uint8Array instanceof Uint8Array)
|
|
8
|
+
return new TextDecoder().decode(uint8Array);
|
|
9
|
+
throw Error(`expected typeof uint8Array instead got ${uint8Array}`);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const pathSepS = '/';
|
|
13
|
+
class KeyPath {
|
|
14
|
+
uint8Array;
|
|
15
|
+
constructor(input) {
|
|
16
|
+
if (typeof input === 'string') {
|
|
17
|
+
this.uint8Array = encode(input);
|
|
18
|
+
}
|
|
19
|
+
else if (input instanceof Uint8Array) {
|
|
20
|
+
this.uint8Array = input;
|
|
21
|
+
}
|
|
22
|
+
else if (input instanceof KeyPath) {
|
|
23
|
+
this.uint8Array = input.uint8Array;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
isKeyPath() {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
toString() {
|
|
33
|
+
return decode(this.uint8Array);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the `list` representation of this path.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```js
|
|
40
|
+
* new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
|
|
41
|
+
* // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
list() {
|
|
45
|
+
return this.toString().split(pathSepS).slice(1);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class KeyValue {
|
|
50
|
+
uint8Array;
|
|
51
|
+
constructor(input) {
|
|
52
|
+
if (typeof input === 'string') {
|
|
53
|
+
this.uint8Array = encode(input);
|
|
54
|
+
}
|
|
55
|
+
else if (input instanceof Uint8Array) {
|
|
56
|
+
this.uint8Array = input;
|
|
57
|
+
}
|
|
58
|
+
else if (input instanceof KeyValue) {
|
|
59
|
+
this.uint8Array = input.uint8Array;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
isKeyValue() {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
toString() {
|
|
69
|
+
return decode(this.uint8Array);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { KeyPath as K, KeyValue as a };
|
package/exports/peernet.js
CHANGED
|
@@ -197,8 +197,7 @@ class DhtEarth {
|
|
|
197
197
|
if (!fetchedCoordinates[address]) {
|
|
198
198
|
const request = `https://whereis.leofcoin.org/?ip=${address}`;
|
|
199
199
|
let response = await fetch(request);
|
|
200
|
-
|
|
201
|
-
const { lat, lon } = response;
|
|
200
|
+
const { lat, lon } = await response.json();
|
|
202
201
|
fetchedCoordinates[address] = { latitude: lat, longitude: lon };
|
|
203
202
|
}
|
|
204
203
|
return fetchedCoordinates[address];
|
|
@@ -222,6 +221,7 @@ class DhtEarth {
|
|
|
222
221
|
else
|
|
223
222
|
all.push(this.getDistance(peerLoc, provider));
|
|
224
223
|
}
|
|
224
|
+
// todo queue
|
|
225
225
|
all = await Promise.all(all);
|
|
226
226
|
all = all.sort((previous, current) => previous.distance - current.distance);
|
|
227
227
|
return all[0].provider;
|
|
@@ -235,22 +235,20 @@ class DhtEarth {
|
|
|
235
235
|
providers = this.providerMap.get(hash);
|
|
236
236
|
return providers;
|
|
237
237
|
}
|
|
238
|
-
addProvider(
|
|
239
|
-
let providers
|
|
238
|
+
addProvider(provider, hash) {
|
|
239
|
+
let providers;
|
|
240
240
|
if (this.providerMap.has(hash)) {
|
|
241
241
|
providers = this.providerMap.get(hash);
|
|
242
242
|
}
|
|
243
|
-
providers.
|
|
243
|
+
providers[provider.address] = provider;
|
|
244
244
|
this.providerMap.set(hash, providers);
|
|
245
245
|
}
|
|
246
246
|
removeProvider(address, hash) {
|
|
247
|
-
let deleted = undefined;
|
|
248
247
|
if (this.providerMap.has(hash)) {
|
|
249
248
|
const providers = this.providerMap.get(hash);
|
|
250
|
-
|
|
251
|
-
|
|
249
|
+
delete providers[address];
|
|
250
|
+
this.providerMap.set(hash, providers);
|
|
252
251
|
}
|
|
253
|
-
return deleted;
|
|
254
252
|
}
|
|
255
253
|
}
|
|
256
254
|
|
|
@@ -391,8 +389,11 @@ globalThis.globalSub = globalThis.globalSub || new PubSub();
|
|
|
391
389
|
* const peernet = new Peernet();
|
|
392
390
|
*/
|
|
393
391
|
class Peernet {
|
|
392
|
+
storePrefix;
|
|
393
|
+
root;
|
|
394
394
|
identity;
|
|
395
395
|
stores = [];
|
|
396
|
+
peerId;
|
|
396
397
|
/**
|
|
397
398
|
* @type {Object}
|
|
398
399
|
* @property {Object} peer Instance of Peer
|
|
@@ -411,6 +412,7 @@ class Peernet {
|
|
|
411
412
|
#connections = {};
|
|
412
413
|
requestProtos = {};
|
|
413
414
|
_messageHandler;
|
|
415
|
+
_peerHandler;
|
|
414
416
|
protos;
|
|
415
417
|
/**
|
|
416
418
|
* @access public
|
|
@@ -446,6 +448,7 @@ class Peernet {
|
|
|
446
448
|
up: 0,
|
|
447
449
|
down: 0,
|
|
448
450
|
};
|
|
451
|
+
// @ts-ignore
|
|
449
452
|
return this._init(options, password);
|
|
450
453
|
}
|
|
451
454
|
get id() {
|
|
@@ -584,7 +587,13 @@ class Peernet {
|
|
|
584
587
|
else {
|
|
585
588
|
process.on('SIGTERM', async () => {
|
|
586
589
|
process.stdin.resume();
|
|
587
|
-
|
|
590
|
+
try {
|
|
591
|
+
await this.client.destroy();
|
|
592
|
+
}
|
|
593
|
+
catch (error) {
|
|
594
|
+
// @ts-ignore
|
|
595
|
+
await this.client.close();
|
|
596
|
+
}
|
|
588
597
|
process.exit();
|
|
589
598
|
});
|
|
590
599
|
}
|
|
@@ -724,21 +733,13 @@ class Peernet {
|
|
|
724
733
|
* @param {String} hash
|
|
725
734
|
*/
|
|
726
735
|
async providersFor(hash, store) {
|
|
727
|
-
let providers =
|
|
736
|
+
let providers = this.dht.providersFor(hash);
|
|
728
737
|
// walk the network to find a provider
|
|
729
|
-
|
|
738
|
+
let tries = 0;
|
|
739
|
+
while (Object.keys(providers).length === 0 && tries < 3) {
|
|
740
|
+
tries += 1;
|
|
730
741
|
await this.walk(hash);
|
|
731
|
-
providers =
|
|
732
|
-
// second walk the network to find a provider
|
|
733
|
-
if (!providers || providers.length === 0) {
|
|
734
|
-
await this.walk(hash);
|
|
735
|
-
providers = await this.dht.providersFor(hash);
|
|
736
|
-
}
|
|
737
|
-
// last walk
|
|
738
|
-
if (!providers || providers.length === 0) {
|
|
739
|
-
await this.walk(hash);
|
|
740
|
-
providers = await this.dht.providersFor(hash);
|
|
741
|
-
}
|
|
742
|
+
providers = this.dht.providersFor(hash);
|
|
742
743
|
}
|
|
743
744
|
// undefined if no providers given
|
|
744
745
|
return providers;
|
|
@@ -777,11 +778,11 @@ class Peernet {
|
|
|
777
778
|
}
|
|
778
779
|
async requestData(hash, store) {
|
|
779
780
|
const providers = await this.providersFor(hash);
|
|
780
|
-
if (!providers || providers.
|
|
781
|
+
if (!providers || Object.keys(providers).length === 0)
|
|
781
782
|
throw nothingFoundError(hash);
|
|
782
783
|
debug(`found ${providers.size} provider(s) for ${hash}`);
|
|
783
784
|
// get closest peer on earth
|
|
784
|
-
const closestPeer =
|
|
785
|
+
const closestPeer = Object.values(providers)[0];
|
|
785
786
|
// get peer instance by id
|
|
786
787
|
if (!closestPeer || !closestPeer.id)
|
|
787
788
|
return this.requestData(hash, store?.name || store);
|
|
@@ -796,7 +797,7 @@ class Peernet {
|
|
|
796
797
|
// fallback and try every provider found
|
|
797
798
|
const promises = [];
|
|
798
799
|
const providers = await this.providersFor(hash, store);
|
|
799
|
-
for (const provider of providers) {
|
|
800
|
+
for (const provider of Object.values(providers)) {
|
|
800
801
|
const peer = this.#connections[provider.id];
|
|
801
802
|
if (peer)
|
|
802
803
|
promises.push(peer.request(node.encoded));
|
|
@@ -982,10 +983,10 @@ class Peernet {
|
|
|
982
983
|
*
|
|
983
984
|
* @param {String} hash
|
|
984
985
|
* @param {Buffer} data
|
|
985
|
-
* @param {String}
|
|
986
|
+
* @param {String} storeName - storeName to access
|
|
986
987
|
*/
|
|
987
|
-
async put(hash, data,
|
|
988
|
-
store = globalThis[`${
|
|
988
|
+
async put(hash, data, storeName = 'data') {
|
|
989
|
+
const store = globalThis[`${storeName}Store`];
|
|
989
990
|
return store.put(hash, data);
|
|
990
991
|
}
|
|
991
992
|
/**
|
|
@@ -1016,9 +1017,9 @@ class Peernet {
|
|
|
1016
1017
|
// TODO: if peer subscribed
|
|
1017
1018
|
}
|
|
1018
1019
|
}
|
|
1019
|
-
createHash(data, name) {
|
|
1020
|
-
|
|
1021
|
-
}
|
|
1020
|
+
// createHash(data, name) {
|
|
1021
|
+
// return new CodeHash(data, {name})
|
|
1022
|
+
// }
|
|
1022
1023
|
/**
|
|
1023
1024
|
*
|
|
1024
1025
|
* @param {String} topic
|
|
@@ -1032,10 +1033,10 @@ class Peernet {
|
|
|
1032
1033
|
console.log('removepeer', peer.id);
|
|
1033
1034
|
const id = peer.id;
|
|
1034
1035
|
await this.client._removePeer(peer);
|
|
1035
|
-
console.log(this.client.peers[id]);
|
|
1036
1036
|
if (this.client.peers[id]) {
|
|
1037
1037
|
for (const connection of Object.keys(this.client.peers[id])) {
|
|
1038
|
-
// if (this.client.peers[id][connection].connected === false) delete this.client.peers[id][connection]
|
|
1038
|
+
// if (this.client.peers[id][connection].connected === false) delete this.client.peers[id][connection]
|
|
1039
|
+
// @ts-ignore
|
|
1039
1040
|
if (this.client.peers[id][connection].connected)
|
|
1040
1041
|
return this.client.emit('peerconnect', connection);
|
|
1041
1042
|
}
|