@leofcoin/peernet 1.2.15 → 1.2.17

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.
Files changed (36) hide show
  1. package/exports/browser/{client-Dl1IxbL-.js → client-otmI2TFk.js} +1 -1
  2. package/exports/browser/{messages-BHDRCKN5.js → messages-B1x_wCLa.js} +1 -1
  3. package/exports/browser/{peernet-dd6ESbyH.js → peernet-DoO_MDV_.js} +35 -40
  4. package/exports/browser/peernet.js +1 -1
  5. package/exports/peernet.js +33 -38
  6. package/exports/types/dht/dht.d.ts +1 -1
  7. package/exports/types/messages/chat.d.ts +5 -0
  8. package/exports/types/messages/data-response.d.ts +5 -0
  9. package/exports/types/messages/data.d.ts +4 -3
  10. package/exports/types/messages/dht-response.d.ts +5 -0
  11. package/exports/types/messages/dht.d.ts +13 -0
  12. package/exports/types/messages/file-link.d.ts +4 -3
  13. package/exports/types/messages/file.d.ts +4 -3
  14. package/exports/types/messages/peer-response.d.ts +5 -0
  15. package/exports/types/messages/peer.d.ts +5 -0
  16. package/exports/types/messages/peernet.d.ts +5 -0
  17. package/exports/types/messages/ps.d.ts +5 -0
  18. package/exports/types/messages/request.d.ts +5 -0
  19. package/exports/types/messages/response.d.ts +5 -0
  20. package/exports/types/peernet.d.ts +1 -1
  21. package/package.json +67 -3
  22. package/src/dht/dht.ts +6 -4
  23. package/src/messages/{chat.js → chat.ts} +2 -2
  24. package/src/messages/{data-response.js → data-response.ts} +2 -2
  25. package/src/messages/{data.js → data.ts} +2 -2
  26. package/src/messages/{dht-response.js → dht-response.ts} +2 -2
  27. package/src/messages/{dht.js → dht.ts} +2 -2
  28. package/src/messages/{file-link.js → file-link.ts} +2 -2
  29. package/src/messages/{file.js → file.ts} +2 -2
  30. package/src/messages/{peer-response.js → peer-response.ts} +3 -3
  31. package/src/messages/{peer.js → peer.ts} +3 -3
  32. package/src/messages/{peernet.js → peernet.ts} +2 -2
  33. package/src/messages/{ps.js → ps.ts} +2 -2
  34. package/src/messages/{request.js → request.ts} +2 -2
  35. package/src/messages/{response.js → response.ts} +2 -2
  36. package/src/peernet.ts +27 -33
@@ -1,4 +1,4 @@
1
- import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-dd6ESbyH.js';
1
+ import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-DoO_MDV_.js';
2
2
  import './identity-CqSnKXWH.js';
3
3
  import './value-C3vAp-wb.js';
4
4
 
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './peernet-dd6ESbyH.js';
1
+ import { F as FormatInterface } from './peernet-DoO_MDV_.js';
2
2
  import './identity-CqSnKXWH.js';
3
3
  import './value-C3vAp-wb.js';
4
4
 
@@ -8121,7 +8121,7 @@ class DhtEarth {
8121
8121
  const address = await getAddress();
8122
8122
  const peerLoc = await this.getCoordinates(address);
8123
8123
  for (const provider of providers) {
8124
- if (provider.address === '127.0.0.1' || provider.address === '::1')
8124
+ if (!provider.address || provider.address === '127.0.0.1' || provider.address === '::1')
8125
8125
  all.push({ provider, distance: 0 });
8126
8126
  else
8127
8127
  all.push(this.getDistance(peerLoc, provider));
@@ -8141,17 +8141,19 @@ class DhtEarth {
8141
8141
  return providers;
8142
8142
  }
8143
8143
  addProvider(provider, hash) {
8144
+ if (!provider.address)
8145
+ provider.address = '127.0.0.1';
8144
8146
  let providers = {};
8145
8147
  if (this.providerMap.has(hash)) {
8146
8148
  providers = this.providerMap.get(hash);
8147
8149
  }
8148
- providers[provider.address] = provider;
8150
+ providers[provider.id] = provider;
8149
8151
  this.providerMap.set(hash, providers);
8150
8152
  }
8151
- removeProvider(address, hash) {
8153
+ removeProvider(id, hash) {
8152
8154
  if (this.providerMap.has(hash)) {
8153
8155
  const providers = this.providerMap.get(hash);
8154
- delete providers[address];
8156
+ delete providers[id];
8155
8157
  this.providerMap.set(hash, providers);
8156
8158
  }
8157
8159
  }
@@ -8473,7 +8475,7 @@ class Peernet {
8473
8475
  await getAddress();
8474
8476
  this.storePrefix = options.storePrefix;
8475
8477
  this.root = options.root;
8476
- const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-BHDRCKN5.js');
8478
+ const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-B1x_wCLa.js');
8477
8479
  /**
8478
8480
  * proto Object containing protos
8479
8481
  * @type {Object}
@@ -8520,7 +8522,7 @@ class Peernet {
8520
8522
  if (this.#starting || this.#started)
8521
8523
  return;
8522
8524
  this.#starting = true;
8523
- const importee = await import('./client-Dl1IxbL-.js');
8525
+ const importee = await import('./client-otmI2TFk.js');
8524
8526
  /**
8525
8527
  * @access public
8526
8528
  * @type {PeernetClient}
@@ -8841,7 +8843,7 @@ class Peernet {
8841
8843
  closestPeer = Object.values(providers)[0];
8842
8844
  debug(`closest peer for ${hash} is ${closestPeer?.address}`);
8843
8845
  // get peer instance by id
8844
- if (!closestPeer || !closestPeer.id || !closestPeer.address)
8846
+ if (!closestPeer || !closestPeer.id)
8845
8847
  return undefined;
8846
8848
  const id = closestPeer.id;
8847
8849
  const peer = this.connections[id];
@@ -9021,22 +9023,7 @@ class Peernet {
9021
9023
  *
9022
9024
  * @param {Array} files
9023
9025
  */
9024
- putFolder: async (files) => {
9025
- const links = [];
9026
- for (const file of files) {
9027
- const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
9028
- const hash = await fileNode.hash();
9029
- await dataStore.put(hash, fileNode.encoded);
9030
- links.push({ hash, path: file.path });
9031
- }
9032
- const node = await new globalThis.peernet.protos['peernet-file']({
9033
- path: '/',
9034
- links
9035
- });
9036
- const hash = await node.hash();
9037
- await dataStore.put(hash, node.encoded);
9038
- return hash;
9039
- }
9026
+ putFolder: async (files) => await this.addFolder(files)
9040
9027
  };
9041
9028
  }
9042
9029
  /**
@@ -9048,13 +9035,13 @@ class Peernet {
9048
9035
  return await shareStore.keys();
9049
9036
  }
9050
9037
  async addFolder(files) {
9051
- const links = [];
9052
- for (const file of files) {
9038
+ const processFile = async (file) => {
9053
9039
  const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
9054
9040
  const hash = await fileNode.hash();
9055
9041
  await dataStore.put(hash, fileNode.encoded);
9056
- links.push({ hash, path: file.path });
9057
- }
9042
+ return { hash, path: file.path };
9043
+ };
9044
+ const links = await Promise.all(files.map(processFile));
9058
9045
  const node = await new globalThis.peernet.protos['peernet-file']({
9059
9046
  path: '/',
9060
9047
  links
@@ -9067,9 +9054,10 @@ class Peernet {
9067
9054
  let data;
9068
9055
  const has = await dataStore.has(hash);
9069
9056
  data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
9070
- const node = await new peernet.protos['peernet-file'](data);
9057
+ if (!data)
9058
+ throw nothingFoundError(hash);
9059
+ const node = await new globalThis.peernet.protos['peernet-file'](data);
9071
9060
  await node.decode();
9072
- console.log(data);
9073
9061
  const paths = [];
9074
9062
  if (node.decoded?.links.length === 0)
9075
9063
  throw new Error(`${hash} is a file`);
@@ -9084,7 +9072,10 @@ class Peernet {
9084
9072
  let data;
9085
9073
  const has = await dataStore.has(hash);
9086
9074
  data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
9087
- const node = await new peernet.protos['peernet-file'](data);
9075
+ if (!data)
9076
+ throw nothingFoundError(hash);
9077
+ const node = await new globalThis.peernet.protos['peernet-file'](data);
9078
+ await node.decode();
9088
9079
  if (node.decoded?.links.length > 0)
9089
9080
  throw new Error(`${hash} is a directory`);
9090
9081
  if (options?.pin)
@@ -9097,17 +9088,21 @@ class Peernet {
9097
9088
  * @param {string} hash
9098
9089
  */
9099
9090
  async whichStore(stores, hash) {
9100
- let store = stores.pop();
9101
- store = globalThis[`${store}Store`];
9102
- if (store) {
9103
- const has = await store.has(hash);
9104
- if (has)
9105
- return store;
9106
- if (stores.length > 0)
9107
- return this.whichStore(stores, hash);
9091
+ const checkStore = async (name) => {
9092
+ const store = globalThis[`${name}Store`];
9093
+ if (store) {
9094
+ const has = await store.has(hash);
9095
+ if (has)
9096
+ return store;
9097
+ }
9098
+ throw new Error('Not found');
9099
+ };
9100
+ try {
9101
+ return await Promise.any(stores.map(checkStore));
9102
+ }
9103
+ catch {
9104
+ return undefined;
9108
9105
  }
9109
- else
9110
- return;
9111
9106
  }
9112
9107
  /**
9113
9108
  * Get content for given hash
@@ -1,3 +1,3 @@
1
- export { P as default } from './peernet-dd6ESbyH.js';
1
+ export { P as default } from './peernet-DoO_MDV_.js';
2
2
  import './identity-CqSnKXWH.js';
3
3
  import './value-C3vAp-wb.js';
@@ -209,7 +209,7 @@ class DhtEarth {
209
209
  const address = await getAddress();
210
210
  const peerLoc = await this.getCoordinates(address);
211
211
  for (const provider of providers) {
212
- if (provider.address === '127.0.0.1' || provider.address === '::1')
212
+ if (!provider.address || provider.address === '127.0.0.1' || provider.address === '::1')
213
213
  all.push({ provider, distance: 0 });
214
214
  else
215
215
  all.push(this.getDistance(peerLoc, provider));
@@ -229,17 +229,19 @@ class DhtEarth {
229
229
  return providers;
230
230
  }
231
231
  addProvider(provider, hash) {
232
+ if (!provider.address)
233
+ provider.address = '127.0.0.1';
232
234
  let providers = {};
233
235
  if (this.providerMap.has(hash)) {
234
236
  providers = this.providerMap.get(hash);
235
237
  }
236
- providers[provider.address] = provider;
238
+ providers[provider.id] = provider;
237
239
  this.providerMap.set(hash, providers);
238
240
  }
239
- removeProvider(address, hash) {
241
+ removeProvider(id, hash) {
240
242
  if (this.providerMap.has(hash)) {
241
243
  const providers = this.providerMap.get(hash);
242
- delete providers[address];
244
+ delete providers[id];
243
245
  this.providerMap.set(hash, providers);
244
246
  }
245
247
  }
@@ -819,7 +821,7 @@ class Peernet {
819
821
  closestPeer = Object.values(providers)[0];
820
822
  debug(`closest peer for ${hash} is ${closestPeer?.address}`);
821
823
  // get peer instance by id
822
- if (!closestPeer || !closestPeer.id || !closestPeer.address)
824
+ if (!closestPeer || !closestPeer.id)
823
825
  return undefined;
824
826
  const id = closestPeer.id;
825
827
  const peer = this.connections[id];
@@ -999,22 +1001,7 @@ class Peernet {
999
1001
  *
1000
1002
  * @param {Array} files
1001
1003
  */
1002
- putFolder: async (files) => {
1003
- const links = [];
1004
- for (const file of files) {
1005
- const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
1006
- const hash = await fileNode.hash();
1007
- await dataStore.put(hash, fileNode.encoded);
1008
- links.push({ hash, path: file.path });
1009
- }
1010
- const node = await new globalThis.peernet.protos['peernet-file']({
1011
- path: '/',
1012
- links
1013
- });
1014
- const hash = await node.hash();
1015
- await dataStore.put(hash, node.encoded);
1016
- return hash;
1017
- }
1004
+ putFolder: async (files) => await this.addFolder(files)
1018
1005
  };
1019
1006
  }
1020
1007
  /**
@@ -1026,13 +1013,13 @@ class Peernet {
1026
1013
  return await shareStore.keys();
1027
1014
  }
1028
1015
  async addFolder(files) {
1029
- const links = [];
1030
- for (const file of files) {
1016
+ const processFile = async (file) => {
1031
1017
  const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
1032
1018
  const hash = await fileNode.hash();
1033
1019
  await dataStore.put(hash, fileNode.encoded);
1034
- links.push({ hash, path: file.path });
1035
- }
1020
+ return { hash, path: file.path };
1021
+ };
1022
+ const links = await Promise.all(files.map(processFile));
1036
1023
  const node = await new globalThis.peernet.protos['peernet-file']({
1037
1024
  path: '/',
1038
1025
  links
@@ -1045,9 +1032,10 @@ class Peernet {
1045
1032
  let data;
1046
1033
  const has = await dataStore.has(hash);
1047
1034
  data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
1048
- const node = await new peernet.protos['peernet-file'](data);
1035
+ if (!data)
1036
+ throw nothingFoundError(hash);
1037
+ const node = await new globalThis.peernet.protos['peernet-file'](data);
1049
1038
  await node.decode();
1050
- console.log(data);
1051
1039
  const paths = [];
1052
1040
  if (node.decoded?.links.length === 0)
1053
1041
  throw new Error(`${hash} is a file`);
@@ -1062,7 +1050,10 @@ class Peernet {
1062
1050
  let data;
1063
1051
  const has = await dataStore.has(hash);
1064
1052
  data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
1065
- const node = await new peernet.protos['peernet-file'](data);
1053
+ if (!data)
1054
+ throw nothingFoundError(hash);
1055
+ const node = await new globalThis.peernet.protos['peernet-file'](data);
1056
+ await node.decode();
1066
1057
  if (node.decoded?.links.length > 0)
1067
1058
  throw new Error(`${hash} is a directory`);
1068
1059
  if (options?.pin)
@@ -1075,17 +1066,21 @@ class Peernet {
1075
1066
  * @param {string} hash
1076
1067
  */
1077
1068
  async whichStore(stores, hash) {
1078
- let store = stores.pop();
1079
- store = globalThis[`${store}Store`];
1080
- if (store) {
1081
- const has = await store.has(hash);
1082
- if (has)
1083
- return store;
1084
- if (stores.length > 0)
1085
- return this.whichStore(stores, hash);
1069
+ const checkStore = async (name) => {
1070
+ const store = globalThis[`${name}Store`];
1071
+ if (store) {
1072
+ const has = await store.has(hash);
1073
+ if (has)
1074
+ return store;
1075
+ }
1076
+ throw new Error('Not found');
1077
+ };
1078
+ try {
1079
+ return await Promise.any(stores.map(checkStore));
1080
+ }
1081
+ catch {
1082
+ return undefined;
1086
1083
  }
1087
- else
1088
- return;
1089
1084
  }
1090
1085
  /**
1091
1086
  * Get content for given hash
@@ -33,6 +33,6 @@ export default class DhtEarth {
33
33
  hasProvider(hash: string): boolean;
34
34
  providersFor(hash: string): DHTProviderMapValue;
35
35
  addProvider(provider: DHTProvider, hash: string): void;
36
- removeProvider(address: string, hash: string): void;
36
+ removeProvider(id: string, hash: string): void;
37
37
  }
38
38
  export {};
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class ChatMessage extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(buffer: any);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class DataMessageResponse extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(data: any);
5
+ }
@@ -1,10 +1,11 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
1
2
  /**
2
3
  * @extends {CodecFormat}
3
4
  */
4
- export default class DataMessage {
5
+ export default class DataMessage extends FormatInterface {
6
+ get messageName(): string;
5
7
  /**
6
8
  * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
7
9
  */
8
- constructor(data: Buffer | string | any | DataMessage);
9
- get messageName(): string;
10
+ constructor(data: any);
10
11
  }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class DHTMessageResponse extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(data: any);
5
+ }
@@ -0,0 +1,13 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ /**
3
+ * @example `
4
+ new DHTMessage(hash, store)
5
+ // store = optional if not set, peernet checks every store
6
+ let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
7
+ message = new DHTMessage('hashmvbs124xcfd...', 'block')
8
+ `
9
+ */
10
+ export default class DHTMessage extends FormatInterface {
11
+ get messageName(): string;
12
+ constructor(data: any);
13
+ }
@@ -1,10 +1,11 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
1
2
  /**
2
3
  * @extends {CodecFormat}
3
4
  */
4
- export default class PeernetFileLink {
5
+ export default class PeernetFileLink extends FormatInterface {
6
+ get messageName(): string;
5
7
  /**
6
8
  * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
7
9
  */
8
- constructor(data: Buffer | string | any | DataMessage);
9
- get messageName(): string;
10
+ constructor(data: any);
10
11
  }
@@ -1,10 +1,11 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
1
2
  /**
2
3
  * @extends {CodecFormat}
3
4
  */
4
- export default class PeernetFile {
5
+ export default class PeernetFile extends FormatInterface {
6
+ get messageName(): string;
5
7
  /**
6
8
  * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
7
9
  */
8
- constructor(data: Buffer | string | any | DataMessage);
9
- get messageName(): string;
10
+ constructor(data: any);
10
11
  }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class PeerMessageResponse extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(data: any);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class PeerMessage extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(data: any);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class PeernetMessage extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(buffer: any);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class PsMessage extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(buffer: any);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class RequestMessage extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(data: any);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { FormatInterface } from '@leofcoin/codec-format-interface';
2
+ export default class ResponseMessage extends FormatInterface {
3
+ get messageName(): string;
4
+ constructor(data: any);
5
+ }
@@ -271,7 +271,7 @@ export default class Peernet {
271
271
  * @param {Array} stores
272
272
  * @param {string} hash
273
273
  */
274
- whichStore(stores: any, hash: any): any;
274
+ whichStore(stores: string[], hash: string): Promise<any>;
275
275
  /**
276
276
  * Get content for given hash
277
277
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.2.15",
3
+ "version": "1.2.17",
4
4
  "description": "",
5
5
  "browser": "./exports/browser/peernet.js",
6
6
  "exports": {
@@ -24,6 +24,22 @@
24
24
  "import": "././exports/chat-message.proto.js",
25
25
  "types": "./exports/types/proto/chat-message.proto.d.ts"
26
26
  },
27
+ "./chat": {
28
+ "import": "././exports/chat.js",
29
+ "types": "./exports/types/messages/chat.d.ts"
30
+ },
31
+ "./chat.js": {
32
+ "import": "././exports/chat.js",
33
+ "types": "./exports/types/messages/chat.d.ts"
34
+ },
35
+ "./data-response": {
36
+ "import": "././exports/data-response.js",
37
+ "types": "./exports/types/messages/data-response.d.ts"
38
+ },
39
+ "./data-response.js": {
40
+ "import": "././exports/data-response.js",
41
+ "types": "./exports/types/messages/data-response.d.ts"
42
+ },
27
43
  "./data-response.proto": {
28
44
  "import": "././exports/data-response.proto.js",
29
45
  "types": "./exports/types/proto/data-response.proto.d.ts"
@@ -48,6 +64,14 @@
48
64
  "import": "././exports/data.proto.js",
49
65
  "types": "./exports/types/proto/data.proto.d.ts"
50
66
  },
67
+ "./dht-response": {
68
+ "import": "././exports/dht-response.js",
69
+ "types": "./exports/types/messages/dht-response.d.ts"
70
+ },
71
+ "./dht-response.js": {
72
+ "import": "././exports/dht-response.js",
73
+ "types": "./exports/types/messages/dht-response.d.ts"
74
+ },
51
75
  "./dht-response.proto": {
52
76
  "import": "././exports/dht-response.proto.js",
53
77
  "types": "./exports/types/proto/dht-response.proto.d.ts"
@@ -160,6 +184,14 @@
160
184
  "import": "././exports/peer-info.js",
161
185
  "types": "./exports/types/peer-info.d.ts"
162
186
  },
187
+ "./peer-response": {
188
+ "import": "././exports/peer-response.js",
189
+ "types": "./exports/types/messages/peer-response.d.ts"
190
+ },
191
+ "./peer-response.js": {
192
+ "import": "././exports/peer-response.js",
193
+ "types": "./exports/types/messages/peer-response.d.ts"
194
+ },
163
195
  "./peer-response.proto": {
164
196
  "import": "././exports/peer-response.proto.js",
165
197
  "types": "./exports/types/proto/peer-response.proto.d.ts"
@@ -168,6 +200,14 @@
168
200
  "import": "././exports/peer-response.proto.js",
169
201
  "types": "./exports/types/proto/peer-response.proto.d.ts"
170
202
  },
203
+ "./peer": {
204
+ "import": "././exports/peer.js",
205
+ "types": "./exports/types/messages/peer.d.ts"
206
+ },
207
+ "./peer.js": {
208
+ "import": "././exports/peer.js",
209
+ "types": "./exports/types/messages/peer.d.ts"
210
+ },
171
211
  "./peer.proto": {
172
212
  "import": "././exports/peer.proto.js",
173
213
  "types": "./exports/types/proto/peer.proto.d.ts"
@@ -178,11 +218,11 @@
178
218
  },
179
219
  "./peernet": {
180
220
  "import": "././exports/peernet.js",
181
- "types": "./exports/types/peernet.d.ts"
221
+ "types": "./exports/types/messages/peernet.d.ts"
182
222
  },
183
223
  "./peernet.js": {
184
224
  "import": "././exports/peernet.js",
185
- "types": "./exports/types/peernet.d.ts"
225
+ "types": "./exports/types/messages/peernet.d.ts"
186
226
  },
187
227
  "./peernet.proto": {
188
228
  "import": "././exports/peernet.proto.js",
@@ -192,6 +232,14 @@
192
232
  "import": "././exports/peernet.proto.js",
193
233
  "types": "./exports/types/proto/peernet.proto.d.ts"
194
234
  },
235
+ "./ps": {
236
+ "import": "././exports/ps.js",
237
+ "types": "./exports/types/messages/ps.d.ts"
238
+ },
239
+ "./ps.js": {
240
+ "import": "././exports/ps.js",
241
+ "types": "./exports/types/messages/ps.d.ts"
242
+ },
195
243
  "./ps.proto": {
196
244
  "import": "././exports/ps.proto.js",
197
245
  "types": "./exports/types/proto/ps.proto.d.ts"
@@ -200,6 +248,14 @@
200
248
  "import": "././exports/ps.proto.js",
201
249
  "types": "./exports/types/proto/ps.proto.d.ts"
202
250
  },
251
+ "./request": {
252
+ "import": "././exports/request.js",
253
+ "types": "./exports/types/messages/request.d.ts"
254
+ },
255
+ "./request.js": {
256
+ "import": "././exports/request.js",
257
+ "types": "./exports/types/messages/request.d.ts"
258
+ },
203
259
  "./request.proto": {
204
260
  "import": "././exports/request.proto.js",
205
261
  "types": "./exports/types/proto/request.proto.d.ts"
@@ -208,6 +264,14 @@
208
264
  "import": "././exports/request.proto.js",
209
265
  "types": "./exports/types/proto/request.proto.d.ts"
210
266
  },
267
+ "./response": {
268
+ "import": "././exports/response.js",
269
+ "types": "./exports/types/messages/response.d.ts"
270
+ },
271
+ "./response.js": {
272
+ "import": "././exports/response.js",
273
+ "types": "./exports/types/messages/response.d.ts"
274
+ },
211
275
  "./response.proto": {
212
276
  "import": "././exports/response.proto.js",
213
277
  "types": "./exports/types/proto/response.proto.d.ts"
package/src/dht/dht.ts CHANGED
@@ -101,7 +101,8 @@ export default class DhtEarth {
101
101
  const peerLoc = await this.getCoordinates(address)
102
102
 
103
103
  for (const provider of providers) {
104
- if (provider.address === '127.0.0.1' || provider.address === '::1') all.push({ provider, distance: 0 })
104
+ if (!provider.address || provider.address === '127.0.0.1' || provider.address === '::1')
105
+ all.push({ provider, distance: 0 })
105
106
  else all.push(this.getDistance(peerLoc, provider))
106
107
  }
107
108
 
@@ -122,18 +123,19 @@ export default class DhtEarth {
122
123
  }
123
124
 
124
125
  addProvider(provider: DHTProvider, hash: string) {
126
+ if (!provider.address) provider.address = '127.0.0.1'
125
127
  let providers: DHTProviderMapValue = {}
126
128
  if (this.providerMap.has(hash)) {
127
129
  providers = this.providerMap.get(hash)
128
130
  }
129
- providers[provider.address] = provider
131
+ providers[provider.id] = provider
130
132
  this.providerMap.set(hash, providers)
131
133
  }
132
134
 
133
- removeProvider(address: string, hash: string) {
135
+ removeProvider(id: string, hash: string) {
134
136
  if (this.providerMap.has(hash)) {
135
137
  const providers = this.providerMap.get(hash)
136
- delete providers[address]
138
+ delete providers[id]
137
139
  this.providerMap.set(hash, providers)
138
140
  }
139
141
  }
@@ -2,11 +2,11 @@ import proto from './../proto/chat-message.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class ChatMessage extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'ChatMessage'
7
7
  }
8
8
 
9
- constructor(buffer) {
9
+ constructor(buffer: any) {
10
10
  const name = 'chat-message'
11
11
  super(buffer, proto, { name })
12
12
  }
@@ -2,11 +2,11 @@ import proto from './../proto/data-response.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class DataMessageResponse extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PeernetDataMessageResponse'
7
7
  }
8
8
 
9
- constructor(data) {
9
+ constructor(data: any) {
10
10
  const name = 'peernet-data-response'
11
11
  super(data, proto, { name })
12
12
  }
@@ -5,13 +5,13 @@ import { FormatInterface } from '@leofcoin/codec-format-interface'
5
5
  * @extends {CodecFormat}
6
6
  */
7
7
  export default class DataMessage extends FormatInterface {
8
- get messageName() {
8
+ get messageName(): string {
9
9
  return 'PeernetDataMessage'
10
10
  }
11
11
  /**
12
12
  * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
13
13
  */
14
- constructor(data) {
14
+ constructor(data: any) {
15
15
  super(data, proto, { name: 'peernet-data' })
16
16
  }
17
17
  }
@@ -2,11 +2,11 @@ import proto from './../proto/dht-response.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class DHTMessageResponse extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PeernetDHTMessageResponse'
7
7
  }
8
8
 
9
- constructor(data) {
9
+ constructor(data: any) {
10
10
  const name = 'peernet-dht-response'
11
11
  super(data, proto, { name })
12
12
  }
@@ -10,11 +10,11 @@ import { FormatInterface } from '@leofcoin/codec-format-interface'
10
10
  `
11
11
  */
12
12
  export default class DHTMessage extends FormatInterface {
13
- get messageName() {
13
+ get messageName(): string {
14
14
  return 'PeernetDHTMessage'
15
15
  }
16
16
 
17
- constructor(data) {
17
+ constructor(data: any) {
18
18
  const name = 'peernet-dht'
19
19
  super(data, proto, { name })
20
20
  }
@@ -5,13 +5,13 @@ import { FormatInterface } from '@leofcoin/codec-format-interface'
5
5
  * @extends {CodecFormat}
6
6
  */
7
7
  export default class PeernetFileLink extends FormatInterface {
8
- get messageName() {
8
+ get messageName(): string {
9
9
  return 'PeernetFileLink'
10
10
  }
11
11
  /**
12
12
  * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
13
13
  */
14
- constructor(data) {
14
+ constructor(data: any) {
15
15
  super(data, proto, { name: 'peernet-file-link' })
16
16
  }
17
17
  }
@@ -5,13 +5,13 @@ import { FormatInterface } from '@leofcoin/codec-format-interface'
5
5
  * @extends {CodecFormat}
6
6
  */
7
7
  export default class PeernetFile extends FormatInterface {
8
- get messageName() {
8
+ get messageName(): string {
9
9
  return 'PeernetFile'
10
10
  }
11
11
  /**
12
12
  * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
13
13
  */
14
- constructor(data) {
14
+ constructor(data: any) {
15
15
  super(data, proto, { name: 'peernet-file' })
16
16
  }
17
17
  }
@@ -1,12 +1,12 @@
1
- import proto from './../proto/peer-response.proto.js'
1
+ import proto from '../proto/peer-response.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class PeerMessageResponse extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PeernetPeerMessageResponse'
7
7
  }
8
8
 
9
- constructor(data) {
9
+ constructor(data: any) {
10
10
  const name = 'peernet-peer-response'
11
11
  super(data, proto, { name })
12
12
  }
@@ -1,12 +1,12 @@
1
- import proto from './../proto/peer.proto.js'
1
+ import proto from '../proto/peer.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class PeerMessage extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PeernetPeerMessage'
7
7
  }
8
8
 
9
- constructor(data) {
9
+ constructor(data: any) {
10
10
  const name = 'peernet-peer'
11
11
  super(data, proto, { name })
12
12
  }
@@ -2,11 +2,11 @@ import proto from './../proto/peernet.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class PeernetMessage extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PeernetMessage'
7
7
  }
8
8
 
9
- constructor(buffer) {
9
+ constructor(buffer: any) {
10
10
  const name = 'peernet-message'
11
11
  super(buffer, proto, { name })
12
12
  }
@@ -2,11 +2,11 @@ import proto from './../proto/ps.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class PsMessage extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PsMessage'
7
7
  }
8
8
 
9
- constructor(buffer) {
9
+ constructor(buffer: any) {
10
10
  const name = 'peernet-ps'
11
11
  super(buffer, proto, { name })
12
12
  }
@@ -2,11 +2,11 @@ import proto from './../proto/request.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class RequestMessage extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PeernetRequestMessage'
7
7
  }
8
8
 
9
- constructor(data) {
9
+ constructor(data: any) {
10
10
  const name = 'peernet-request'
11
11
  super(data, proto, { name })
12
12
  }
@@ -2,11 +2,11 @@ import proto from './../proto/response.proto.js'
2
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
3
3
 
4
4
  export default class ResponseMessage extends FormatInterface {
5
- get messageName() {
5
+ get messageName(): string {
6
6
  return 'PeernetResponseMessage'
7
7
  }
8
8
 
9
- constructor(data) {
9
+ constructor(data: any) {
10
10
  const name = 'peernet-response'
11
11
  super(data, proto, { name })
12
12
  }
package/src/peernet.ts CHANGED
@@ -605,7 +605,7 @@ export default class Peernet {
605
605
 
606
606
  debug(`closest peer for ${hash} is ${closestPeer?.address}`)
607
607
  // get peer instance by id
608
- if (!closestPeer || !closestPeer.id || !closestPeer.address) return undefined
608
+ if (!closestPeer || !closestPeer.id) return undefined
609
609
  const id = closestPeer.id
610
610
  const peer = this.connections[id]
611
611
 
@@ -785,23 +785,7 @@ export default class Peernet {
785
785
  *
786
786
  * @param {Array} files
787
787
  */
788
- putFolder: async (files) => {
789
- const links = []
790
- for (const file of files) {
791
- const fileNode = await new globalThis.peernet.protos['peernet-file'](file)
792
- const hash = await fileNode.hash()
793
- await dataStore.put(hash, fileNode.encoded)
794
- links.push({ hash, path: file.path })
795
- }
796
- const node = await new globalThis.peernet.protos['peernet-file']({
797
- path: '/',
798
- links
799
- })
800
- const hash = await node.hash()
801
- await dataStore.put(hash, node.encoded)
802
-
803
- return hash
804
- }
788
+ putFolder: async (files) => await this.addFolder(files)
805
789
  }
806
790
  }
807
791
 
@@ -815,13 +799,15 @@ export default class Peernet {
815
799
  }
816
800
 
817
801
  async addFolder(files) {
818
- const links = []
819
- for (const file of files) {
802
+ const processFile = async (file) => {
820
803
  const fileNode = await new globalThis.peernet.protos['peernet-file'](file)
821
804
  const hash = await fileNode.hash()
822
805
  await dataStore.put(hash, fileNode.encoded)
823
- links.push({ hash, path: file.path })
806
+ return { hash, path: file.path }
824
807
  }
808
+
809
+ const links = await Promise.all(files.map(processFile))
810
+
825
811
  const node = await new globalThis.peernet.protos['peernet-file']({
826
812
  path: '/',
827
813
  links
@@ -836,10 +822,10 @@ export default class Peernet {
836
822
  let data
837
823
  const has = await dataStore.has(hash)
838
824
  data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data')
825
+ if (!data) throw nothingFoundError(hash)
839
826
 
840
- const node = await new peernet.protos['peernet-file'](data)
827
+ const node = await new globalThis.peernet.protos['peernet-file'](data)
841
828
  await node.decode()
842
- console.log(data)
843
829
  const paths = []
844
830
  if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
845
831
  for (const { path, hash } of node.decoded.links) {
@@ -853,7 +839,9 @@ export default class Peernet {
853
839
  let data
854
840
  const has = await dataStore.has(hash)
855
841
  data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data')
856
- const node = await new peernet.protos['peernet-file'](data)
842
+ if (!data) throw nothingFoundError(hash)
843
+ const node = await new globalThis.peernet.protos['peernet-file'](data)
844
+ await node.decode()
857
845
 
858
846
  if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
859
847
  if (options?.pin) await dataStore.put(hash, node.encoded)
@@ -865,15 +853,21 @@ export default class Peernet {
865
853
  * @param {Array} stores
866
854
  * @param {string} hash
867
855
  */
868
- async whichStore(stores, hash) {
869
- let store = stores.pop()
870
- const name = store
871
- store = globalThis[`${store}Store`]
872
- if (store) {
873
- const has = await store.has(hash)
874
- if (has) return store
875
- if (stores.length > 0) return this.whichStore(stores, hash)
876
- } else return
856
+ async whichStore(stores: string[], hash: string) {
857
+ const checkStore = async (name) => {
858
+ const store = globalThis[`${name}Store`]
859
+ if (store) {
860
+ const has = await store.has(hash)
861
+ if (has) return store
862
+ }
863
+ throw new Error('Not found')
864
+ }
865
+
866
+ try {
867
+ return await Promise.any(stores.map(checkStore))
868
+ } catch {
869
+ return undefined
870
+ }
877
871
  }
878
872
 
879
873
  /**