@leofcoin/peernet 0.10.3 → 0.10.6

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.
@@ -1,6 +1,7 @@
1
1
  import PubSub from '@vandeurenglenn/little-pubsub';
2
2
  import sha256 from 'crypto-js/sha256';
3
3
  import P2P from 'p2pt';
4
+ import LeofcoinStorage$1 from '@leofcoin/storage';
4
5
  import { server } from 'websocket';
5
6
  import { createServer } from 'http';
6
7
  import Koa from 'koa';
@@ -150,129 +151,7 @@ class PeernetClient {
150
151
  }
151
152
  }
152
153
 
153
- // const level = require('level');
154
- const LevelStore = require('datastore-level');
155
- const { homedir } = require('os');
156
- const { join } = require('path');
157
- const Key = require('interface-datastore').Key;
158
- const {readdirSync, mkdirSync} = require('fs');
159
-
160
- class LeofcoinStorage$1 {
161
-
162
- constructor(path, root = '.leofcoin', home = true) {
163
- if (!home) this.root = root;
164
- else this.root = join(homedir(), root);
165
- if (readdirSync) try {
166
- readdirSync(this.root);
167
- } catch (e) {
168
- let _path = home ? homedir() : root;
169
- const parts = root.split('/');
170
- if (e.code === 'ENOENT') {
171
-
172
- if (parts.length > 0) {
173
- for (const path of parts) {
174
- _path = join(_path, path);
175
- try {
176
- readdirSync(_path);
177
- } catch (e) {
178
- if (e.code === 'ENOENT') mkdirSync(_path);
179
- else throw e
180
- }
181
- }
182
- } else {
183
- mkdirSync(this.root);
184
- }
185
- } else throw e
186
- }
187
- this.db = new LevelStore(join(this.root, path));
188
- // this.db = level(path, { prefix: 'lfc-'})
189
- }
190
-
191
- toBuffer(value) {
192
- if (Buffer.isBuffer(value)) return value;
193
- if (typeof value === 'object' ||
194
- typeof value === 'boolean' ||
195
- !isNaN(value)) value = JSON.stringify(value);
196
-
197
- return Buffer.from(value)
198
- }
199
-
200
- async many(type, _value) {
201
- const jobs = [];
202
-
203
- for (const key of Object.keys(_value)) {
204
- const value = this.toBuffer(_value[key]);
205
-
206
- jobs.push(this[type](key, value));
207
- }
208
-
209
- return Promise.all(jobs)
210
- }
211
-
212
- async put(key, value) {
213
- if (typeof key === 'object') return this.many('put', key);
214
- value = this.toBuffer(value);
215
-
216
- return this.db.put(new Key(String(key)), value);
217
- }
218
-
219
- async query() {
220
- const object = {};
221
-
222
- for await (let query of this.db.query({})) {
223
- const key = query.key.baseNamespace();
224
- object[key] = this.possibleJSON(query.value);
225
- }
226
-
227
- return object
228
- }
229
-
230
- async get(key) {
231
- if (!key) return this.query()
232
- if (typeof key === 'object') return this.many('get', key);
233
- let data = await this.db.get(new Key(String(key)));
234
- if (!data) return undefined
235
- return this.possibleJSON(data)
236
- }
237
-
238
- async has(key) {
239
- if (typeof key === 'object') return this.many('has', key);
240
-
241
- try {
242
- await this.db.get(new Key(String(key)));
243
- return true;
244
- } catch (e) {
245
- return false
246
- }
247
- }
248
-
249
- async delete(key) {
250
- return this.db.delete(new Key(String(key)))
251
- }
252
-
253
- async size() {
254
- const object = await this.query();
255
- return Object.keys(object).length
256
- }
257
-
258
- // TODO: deprecate usage possibleJSON
259
- // make possibleJSON optional
260
- // or release as its own package
261
- possibleJSON(data) {
262
- let string = data.toString();
263
- if (string.charAt(0) === '{' && string.charAt(string.length - 1) === '}' ||
264
- string.charAt(0) === '[' && string.charAt(string.length - 1) === ']' ||
265
- string === 'true' ||
266
- string === 'false' ||
267
- !isNaN(string))
268
- return JSON.parse(string);
269
-
270
- return data
271
- }
272
-
273
- }
274
-
275
- var version = "0.10.2";
154
+ var version = "0.10.5";
276
155
 
277
156
  var api$1 = {
278
157
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1267,7 +1146,7 @@ class PeernetHash {
1267
1146
 
1268
1147
  class FormatInterface {
1269
1148
  /**
1270
- * @param {Buffer|String|Object} buffer -
1149
+ * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
1271
1150
  * @param {Object} proto - {encode, decode}
1272
1151
  * @param {Object} options - {hashFormat, name}
1273
1152
  */
@@ -1276,7 +1155,9 @@ class FormatInterface {
1276
1155
  this.protoDecode = proto.decode;
1277
1156
  if (options.name) this.name = options.name;
1278
1157
  this.hashFormat = options.hashFormat || 'bs32';
1279
- if (Buffer.isBuffer(buffer)) {
1158
+ if (buffer.name === options.name) {
1159
+ return buffer
1160
+ } else if (Buffer.isBuffer(buffer)) {
1280
1161
  const codec = new PeernetCodec(buffer);
1281
1162
  if (codec.name) {
1282
1163
  this.fromEncoded(buffer);
@@ -1394,8 +1275,22 @@ class FormatInterface {
1394
1275
  * @param {Object} data
1395
1276
  */
1396
1277
  create(data) {
1397
- this.decoded = data;
1398
- this.encode(data);
1278
+ const decoded = {};
1279
+ if (this.keys?.length > 0) {
1280
+ for (const key of this.keys) {
1281
+ Object.defineProperties(decoded, {
1282
+ [key]: {
1283
+ enumerable: true,
1284
+ configurable: true,
1285
+ set: (val) => value = data[key],
1286
+ get: () => data[key]
1287
+ }
1288
+ });
1289
+ }
1290
+
1291
+ this.decoded = decoded;
1292
+ this.encode();
1293
+ }
1399
1294
  }
1400
1295
  }
1401
1296
 
@@ -1467,14 +1362,18 @@ message PeernetDataMessage {
1467
1362
  }
1468
1363
  `;
1469
1364
 
1365
+ /**
1366
+ * @extends {CodecFormat}
1367
+ */
1470
1368
  class DataMessage extends FormatInterface {
1471
1369
  get keys() {
1472
1370
  return ['hash', 'store']
1473
1371
  }
1474
-
1372
+ /**
1373
+ * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
1374
+ */
1475
1375
  constructor(data) {
1476
- const name = 'peernet-data';
1477
- super(data, protons(proto$7).PeernetDataMessage, {name});
1376
+ super(data, protons(proto$7).PeernetDataMessage, {name: 'peernet-data'});
1478
1377
  }
1479
1378
  }
1480
1379
 
@@ -2281,24 +2180,23 @@ class Peernet {
2281
2180
  } else if (proto.name === 'peernet-data') {
2282
2181
  let { hash, store } = proto.decoded;
2283
2182
  let data;
2284
-
2285
2183
  if (!store) {
2286
- data = await this.get(hash);
2184
+ store = await this.whichStore([...this.stores], hash);
2287
2185
  } else {
2288
- store = globalThis[`${store}Store`];
2289
- if (store.private) {
2290
- // TODO: ban
2291
- return
2292
- } else data = await store.get(hash);
2186
+ store = globalThis.stores[`${store}Store`];
2293
2187
  }
2188
+ if (store && !store.private) {
2189
+ data = await store.get(hash);
2294
2190
 
2295
- if (data) {
2296
- data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
2191
+ if (data) {
2192
+ data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
2297
2193
 
2298
- const node = await this.prepareMessage(from, data.encoded);
2299
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2300
- this.bw.up += node.encoded.length;
2194
+ const node = await this.prepareMessage(from, data.encoded);
2195
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2196
+ this.bw.up += node.encoded.length;
2197
+ }
2301
2198
  }
2199
+
2302
2200
  } else if (proto.name === 'peernet-peer') {
2303
2201
  const from = proto.decoded.id;
2304
2202
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
@@ -2401,7 +2299,7 @@ class Peernet {
2401
2299
  get: async (hash) => {
2402
2300
  const data = await blockStore.has(hash);
2403
2301
  if (data) return await blockStore.get(hash)
2404
- return this.requestData(hash)
2302
+ return this.requestData(hash, 'block')
2405
2303
  },
2406
2304
  put: async (hash, data) => {
2407
2305
  if (await blockStore.has(hash)) return
@@ -2433,7 +2331,7 @@ class Peernet {
2433
2331
  // get closest peer on earth
2434
2332
  const closestPeer = await this.dht.closestPeer(providers);
2435
2333
  // get peer instance by id
2436
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store)
2334
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
2437
2335
 
2438
2336
  const id = closestPeer.id.toString();
2439
2337
  if (this.peers) {
@@ -2537,7 +2435,8 @@ class Peernet {
2537
2435
  /**
2538
2436
  * Get content for given hash
2539
2437
  *
2540
- * @param {String} hash
2438
+ * @param {String} hash - the hash of the wanted data
2439
+ * @param {String} store - storeName to access
2541
2440
  */
2542
2441
  async get(hash, store) {
2543
2442
  debug(`get ${hash}`);
@@ -2547,7 +2446,7 @@ class Peernet {
2547
2446
  if (store && await store.has(hash)) data = await store.get(hash);
2548
2447
  if (data) return data
2549
2448
 
2550
- return this.requestData(hash, 'data')
2449
+ return this.requestData(hash, store.name ? store.name : store)
2551
2450
  }
2552
2451
 
2553
2452
  /**
@@ -2555,10 +2454,11 @@ class Peernet {
2555
2454
  *
2556
2455
  * @param {String} hash
2557
2456
  * @param {Buffer} data
2457
+ * @param {String} store - storeName to access
2558
2458
  */
2559
2459
  async put(hash, data, store = 'data') {
2560
2460
  store = globalThis[`${store}Store`];
2561
- return await store.put(hash, data)
2461
+ return store.put(hash, data)
2562
2462
  }
2563
2463
 
2564
2464
  /**
@@ -2629,4 +2529,4 @@ class Peernet {
2629
2529
  // }
2630
2530
  }
2631
2531
 
2632
- export default Peernet;
2532
+ export { Peernet as default };
package/package.json CHANGED
@@ -1,15 +1,13 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.10.3",
3
+ "version": "0.10.6",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
- "module": "dist/module/peernet.js",
7
- "browser": "dist/browser/peernet.js",
8
6
  "scripts": {
9
7
  "test": "node test/index.js",
10
8
  "server": "discovery-swarm-webrtc --port=4000",
11
9
  "demo": "jsproject --serve ./ --port 6868",
12
- "doc": "./node_modules/.bin/esdoc",
10
+ "doc": "esdoc",
13
11
  "lint": "./node_modules/.bin/eslint src/**/**.js --fix",
14
12
  "coverage": "nyc --reporter=lcov npm run test",
15
13
  "coveralls": "cat ./coverage/lcov.info | coveralls",
@@ -23,7 +21,9 @@
23
21
  "dependencies": {
24
22
  "@leofcoin/generate-account": "^1.0.2",
25
23
  "@leofcoin/multi-wallet": "^2.0.6",
26
- "@leofcoin/storage": "^2.1.0",
24
+ "@leofcoin/storage": "^2.1.1",
25
+ "@vandeurenglenn/little-pubsub": "^1.3.1",
26
+ "async_hooks": "^1.0.0",
27
27
  "bs32": "^0.1.6",
28
28
  "bs58": "^4.0.1",
29
29
  "bs58check": "^2.1.2",
@@ -31,6 +31,7 @@
31
31
  "keccak": "^3.0.1",
32
32
  "koa": "^2.13.1",
33
33
  "node-fetch": "^2.6.1",
34
+ "on-finished": "^2.4.1",
34
35
  "p2pt": "^1.5.0",
35
36
  "protons": "^2.0.1",
36
37
  "socket-request-client": "^1.5.0",
@@ -42,7 +43,7 @@
42
43
  "@rollup/plugin-eslint": "^8.0.1",
43
44
  "@rollup/plugin-json": "^4.1.0",
44
45
  "@rollup/plugin-node-resolve": "^13.0.4",
45
- "browserify": "^16.5.2",
46
+ "browserify": "^16.5.0",
46
47
  "coveralls": "^3.1.1",
47
48
  "esdoc": "^1.1.0",
48
49
  "esdoc-ecmascript-proposal-plugin": "^1.0.0",
package/rollup.config.js CHANGED
@@ -27,10 +27,13 @@ export default [{
27
27
  modify({
28
28
  "import fetch from 'node-fetch'": ''
29
29
  }),
30
- lint({
31
- fix: true,
32
- exclude: ['package.json', "package-lock.json"]
33
- })
30
+ cjs(),
31
+ // resolve(),
32
+ // lint({
33
+ // fix: true,
34
+ // exclude: ['package.json', "package-lock.json"]
35
+ // })
36
+
34
37
 
35
38
  ]
36
39
  }, {
@@ -6,7 +6,7 @@ import Hash from './../hash/hash'
6
6
 
7
7
  export default class FormatInterface {
8
8
  /**
9
- * @param {Buffer|String|Object} buffer -
9
+ * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
10
10
  * @param {Object} proto - {encode, decode}
11
11
  * @param {Object} options - {hashFormat, name}
12
12
  */
@@ -15,7 +15,9 @@ export default class FormatInterface {
15
15
  this.protoDecode = proto.decode
16
16
  if (options.name) this.name = options.name
17
17
  this.hashFormat = options.hashFormat || 'bs32'
18
- if (Buffer.isBuffer(buffer)) {
18
+ if (buffer.name === options.name) {
19
+ return buffer
20
+ } else if (Buffer.isBuffer(buffer)) {
19
21
  const codec = new Codec(buffer)
20
22
  if (codec.name) {
21
23
  this.fromEncoded(buffer)
@@ -133,7 +135,21 @@ export default class FormatInterface {
133
135
  * @param {Object} data
134
136
  */
135
137
  create(data) {
136
- this.decoded = data
137
- this.encode(data)
138
+ const decoded = {}
139
+ if (this.keys?.length > 0) {
140
+ for (const key of this.keys) {
141
+ Object.defineProperties(decoded, {
142
+ [key]: {
143
+ enumerable: true,
144
+ configurable: true,
145
+ set: (val) => value = data[key],
146
+ get: () => data[key]
147
+ }
148
+ })
149
+ }
150
+
151
+ this.decoded = decoded
152
+ this.encode()
153
+ }
138
154
  }
139
155
  }
@@ -2,13 +2,17 @@ import protons from 'protons'
2
2
  import proto from './../proto/data.proto.js'
3
3
  import CodecFormat from './../codec/codec-format-interface.js'
4
4
 
5
+ /**
6
+ * @extends {CodecFormat}
7
+ */
5
8
  export default class DataMessage extends CodecFormat {
6
9
  get keys() {
7
10
  return ['hash', 'store']
8
11
  }
9
-
12
+ /**
13
+ * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
14
+ */
10
15
  constructor(data) {
11
- const name = 'peernet-data'
12
- super(data, protons(proto).PeernetDataMessage, {name})
16
+ super(data, protons(proto).PeernetDataMessage, {name: 'peernet-data'})
13
17
  }
14
18
  }
package/src/peernet.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import Pubsub from '@vandeurenglenn/little-pubsub'
2
2
  import Client from './client'
3
- import LeofcoinStorage from './../node_modules/@leofcoin/storage/src/level.js'
3
+ import LeofcoinStorage from '@leofcoin/storage'
4
4
  import http from './http/http.js'
5
5
  import httpClient from './http/client/client.js'
6
6
  import LeofcoinStorageClient from './http/client/storage.js'
@@ -344,24 +344,25 @@ export default class Peernet {
344
344
  } else if (proto.name === 'peernet-data') {
345
345
  let { hash, store } = proto.decoded
346
346
  let data
347
-
348
347
  if (!store) {
349
- data = await this.get(hash)
348
+ store = await this.whichStore([...this.stores], hash)
350
349
  } else {
351
- store = globalThis[`${store}Store`]
352
- if (store.private) {
353
- // TODO: ban
354
- return
355
- } else data = await store.get(hash)
350
+ store = globalThis.stores[`${store}Store`]
356
351
  }
352
+ if (store && !store.private) {
353
+ data = await store.get(hash)
357
354
 
358
- if (data) {
359
- data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
355
+ if (data) {
356
+ data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
360
357
 
361
- const node = await this.prepareMessage(from, data.encoded)
362
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
363
- this.bw.up += node.encoded.length
358
+ const node = await this.prepareMessage(from, data.encoded)
359
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
360
+ this.bw.up += node.encoded.length
361
+ }
362
+ } else {
363
+ // ban (trying to access private st)
364
364
  }
365
+
365
366
  } else if (proto.name === 'peernet-peer') {
366
367
  const from = proto.decoded.id
367
368
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
@@ -464,7 +465,7 @@ export default class Peernet {
464
465
  get: async (hash) => {
465
466
  const data = await blockStore.has(hash)
466
467
  if (data) return await blockStore.get(hash)
467
- return this.requestData(hash)
468
+ return this.requestData(hash, 'block')
468
469
  },
469
470
  put: async (hash, data) => {
470
471
  if (await blockStore.has(hash)) return
@@ -496,7 +497,7 @@ export default class Peernet {
496
497
  // get closest peer on earth
497
498
  const closestPeer = await this.dht.closestPeer(providers)
498
499
  // get peer instance by id
499
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store)
500
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
500
501
 
501
502
  const id = closestPeer.id.toString()
502
503
  if (this.peers) {
@@ -601,7 +602,8 @@ export default class Peernet {
601
602
  /**
602
603
  * Get content for given hash
603
604
  *
604
- * @param {String} hash
605
+ * @param {String} hash - the hash of the wanted data
606
+ * @param {String} store - storeName to access
605
607
  */
606
608
  async get(hash, store) {
607
609
  debug(`get ${hash}`)
@@ -611,7 +613,7 @@ export default class Peernet {
611
613
  if (store && await store.has(hash)) data = await store.get(hash)
612
614
  if (data) return data
613
615
 
614
- return this.requestData(hash, 'data')
616
+ return this.requestData(hash, store.name ? store.name : store)
615
617
  }
616
618
 
617
619
  /**
@@ -619,10 +621,11 @@ export default class Peernet {
619
621
  *
620
622
  * @param {String} hash
621
623
  * @param {Buffer} data
624
+ * @param {String} store - storeName to access
622
625
  */
623
626
  async put(hash, data, store = 'data') {
624
627
  store = globalThis[`${store}Store`]
625
- return await store.put(hash, data)
628
+ return store.put(hash, data)
626
629
  }
627
630
 
628
631
  /**