@leofcoin/peernet 0.10.2 → 0.10.5
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 +659 -628
- package/dist/commonjs/codec-format-interface.js +20 -4
- package/dist/commonjs/peernet.js +30 -144
- package/dist/module/peernet.js +47 -146
- package/package.json +3 -4
- package/rollup.config.js +7 -4
- package/src/codec/codec-format-interface.js +20 -4
- package/src/messages/data.js +7 -3
- package/src/peernet.js +21 -17
|
@@ -16,7 +16,7 @@ var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
|
|
|
16
16
|
|
|
17
17
|
class FormatInterface {
|
|
18
18
|
/**
|
|
19
|
-
* @param {Buffer|String|Object} buffer -
|
|
19
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
20
20
|
* @param {Object} proto - {encode, decode}
|
|
21
21
|
* @param {Object} options - {hashFormat, name}
|
|
22
22
|
*/
|
|
@@ -25,7 +25,9 @@ class FormatInterface {
|
|
|
25
25
|
this.protoDecode = proto.decode;
|
|
26
26
|
if (options.name) this.name = options.name;
|
|
27
27
|
this.hashFormat = options.hashFormat || 'bs32';
|
|
28
|
-
if (
|
|
28
|
+
if (buffer.name === options.name) {
|
|
29
|
+
return buffer
|
|
30
|
+
} else if (Buffer.isBuffer(buffer)) {
|
|
29
31
|
const codec$1 = new codec.PeernetCodec(buffer);
|
|
30
32
|
if (codec$1.name) {
|
|
31
33
|
this.fromEncoded(buffer);
|
|
@@ -143,8 +145,22 @@ class FormatInterface {
|
|
|
143
145
|
* @param {Object} data
|
|
144
146
|
*/
|
|
145
147
|
create(data) {
|
|
146
|
-
|
|
147
|
-
this.
|
|
148
|
+
const decoded = {};
|
|
149
|
+
if (this.keys?.length > 0) {
|
|
150
|
+
for (const key of this.keys) {
|
|
151
|
+
Object.defineProperties(decoded, {
|
|
152
|
+
[key]: {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
configurable: true,
|
|
155
|
+
set: (val) => value = data[key],
|
|
156
|
+
get: () => data[key]
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.decoded = decoded;
|
|
162
|
+
this.encode();
|
|
163
|
+
}
|
|
148
164
|
}
|
|
149
165
|
}
|
|
150
166
|
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
var PubSub = require('@vandeurenglenn/little-pubsub');
|
|
4
4
|
var sha256 = require('crypto-js/sha256');
|
|
5
5
|
var P2P = require('p2pt');
|
|
6
|
+
var LeofcoinStorage$1 = require('@leofcoin/storage');
|
|
6
7
|
var websocket = require('websocket');
|
|
7
8
|
var http$1 = require('http');
|
|
8
9
|
var Koa = require('koa');
|
|
@@ -28,6 +29,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
28
29
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
29
30
|
var sha256__default = /*#__PURE__*/_interopDefaultLegacy(sha256);
|
|
30
31
|
var P2P__default = /*#__PURE__*/_interopDefaultLegacy(P2P);
|
|
32
|
+
var LeofcoinStorage__default = /*#__PURE__*/_interopDefaultLegacy(LeofcoinStorage$1);
|
|
31
33
|
var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
|
|
32
34
|
var protons__default = /*#__PURE__*/_interopDefaultLegacy(protons);
|
|
33
35
|
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch$1);
|
|
@@ -170,129 +172,7 @@ class PeernetClient {
|
|
|
170
172
|
}
|
|
171
173
|
}
|
|
172
174
|
|
|
173
|
-
|
|
174
|
-
const LevelStore = require('datastore-level');
|
|
175
|
-
const { homedir } = require('os');
|
|
176
|
-
const { join } = require('path');
|
|
177
|
-
const Key = require('interface-datastore').Key;
|
|
178
|
-
const {readdirSync, mkdirSync} = require('fs');
|
|
179
|
-
|
|
180
|
-
class LeofcoinStorage$1 {
|
|
181
|
-
|
|
182
|
-
constructor(path, root = '.leofcoin', home = true) {
|
|
183
|
-
if (!home) this.root = root;
|
|
184
|
-
else this.root = join(homedir(), root);
|
|
185
|
-
if (readdirSync) try {
|
|
186
|
-
readdirSync(this.root);
|
|
187
|
-
} catch (e) {
|
|
188
|
-
let _path = home ? homedir() : root;
|
|
189
|
-
const parts = root.split('/');
|
|
190
|
-
if (e.code === 'ENOENT') {
|
|
191
|
-
|
|
192
|
-
if (parts.length > 0) {
|
|
193
|
-
for (const path of parts) {
|
|
194
|
-
_path = join(_path, path);
|
|
195
|
-
try {
|
|
196
|
-
readdirSync(_path);
|
|
197
|
-
} catch (e) {
|
|
198
|
-
if (e.code === 'ENOENT') mkdirSync(_path);
|
|
199
|
-
else throw e
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
} else {
|
|
203
|
-
mkdirSync(this.root);
|
|
204
|
-
}
|
|
205
|
-
} else throw e
|
|
206
|
-
}
|
|
207
|
-
this.db = new LevelStore(join(this.root, path));
|
|
208
|
-
// this.db = level(path, { prefix: 'lfc-'})
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
toBuffer(value) {
|
|
212
|
-
if (Buffer.isBuffer(value)) return value;
|
|
213
|
-
if (typeof value === 'object' ||
|
|
214
|
-
typeof value === 'boolean' ||
|
|
215
|
-
!isNaN(value)) value = JSON.stringify(value);
|
|
216
|
-
|
|
217
|
-
return Buffer.from(value)
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async many(type, _value) {
|
|
221
|
-
const jobs = [];
|
|
222
|
-
|
|
223
|
-
for (const key of Object.keys(_value)) {
|
|
224
|
-
const value = this.toBuffer(_value[key]);
|
|
225
|
-
|
|
226
|
-
jobs.push(this[type](key, value));
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return Promise.all(jobs)
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
async put(key, value) {
|
|
233
|
-
if (typeof key === 'object') return this.many('put', key);
|
|
234
|
-
value = this.toBuffer(value);
|
|
235
|
-
|
|
236
|
-
return this.db.put(new Key(String(key)), value);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
async query() {
|
|
240
|
-
const object = {};
|
|
241
|
-
|
|
242
|
-
for await (let query of this.db.query({})) {
|
|
243
|
-
const key = query.key.baseNamespace();
|
|
244
|
-
object[key] = this.possibleJSON(query.value);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return object
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
async get(key) {
|
|
251
|
-
if (!key) return this.query()
|
|
252
|
-
if (typeof key === 'object') return this.many('get', key);
|
|
253
|
-
let data = await this.db.get(new Key(String(key)));
|
|
254
|
-
if (!data) return undefined
|
|
255
|
-
return this.possibleJSON(data)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
async has(key) {
|
|
259
|
-
if (typeof key === 'object') return this.many('has', key);
|
|
260
|
-
|
|
261
|
-
try {
|
|
262
|
-
await this.db.get(new Key(String(key)));
|
|
263
|
-
return true;
|
|
264
|
-
} catch (e) {
|
|
265
|
-
return false
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
async delete(key) {
|
|
270
|
-
return this.db.delete(new Key(String(key)))
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
async size() {
|
|
274
|
-
const object = await this.query();
|
|
275
|
-
return Object.keys(object).length
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// TODO: deprecate usage possibleJSON
|
|
279
|
-
// make possibleJSON optional
|
|
280
|
-
// or release as its own package
|
|
281
|
-
possibleJSON(data) {
|
|
282
|
-
let string = data.toString();
|
|
283
|
-
if (string.charAt(0) === '{' && string.charAt(string.length - 1) === '}' ||
|
|
284
|
-
string.charAt(0) === '[' && string.charAt(string.length - 1) === ']' ||
|
|
285
|
-
string === 'true' ||
|
|
286
|
-
string === 'false' ||
|
|
287
|
-
!isNaN(string))
|
|
288
|
-
return JSON.parse(string);
|
|
289
|
-
|
|
290
|
-
return data
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
var version = "0.10.1";
|
|
175
|
+
var version = "0.10.4";
|
|
296
176
|
|
|
297
177
|
var api$1 = {
|
|
298
178
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
@@ -955,14 +835,18 @@ message PeernetDataMessage {
|
|
|
955
835
|
}
|
|
956
836
|
`;
|
|
957
837
|
|
|
838
|
+
/**
|
|
839
|
+
* @extends {CodecFormat}
|
|
840
|
+
*/
|
|
958
841
|
class DataMessage extends codecFormatInterface {
|
|
959
842
|
get keys() {
|
|
960
843
|
return ['hash', 'store']
|
|
961
844
|
}
|
|
962
|
-
|
|
845
|
+
/**
|
|
846
|
+
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
847
|
+
*/
|
|
963
848
|
constructor(data) {
|
|
964
|
-
|
|
965
|
-
super(data, protons__default['default'](proto$5).PeernetDataMessage, {name});
|
|
849
|
+
super(data, protons__default['default'](proto$5).PeernetDataMessage, {name: 'peernet-data'});
|
|
966
850
|
}
|
|
967
851
|
}
|
|
968
852
|
|
|
@@ -1453,7 +1337,7 @@ class Peernet {
|
|
|
1453
1337
|
globalThis.peernet = this;
|
|
1454
1338
|
this.bw = {
|
|
1455
1339
|
up: 0,
|
|
1456
|
-
down: 0
|
|
1340
|
+
down: 0,
|
|
1457
1341
|
};
|
|
1458
1342
|
return this._init(options)
|
|
1459
1343
|
}
|
|
@@ -1478,7 +1362,7 @@ class Peernet {
|
|
|
1478
1362
|
if (this.hasDaemon) {
|
|
1479
1363
|
Storage = LeofcoinStorageClient;
|
|
1480
1364
|
} else {
|
|
1481
|
-
Storage =
|
|
1365
|
+
Storage = LeofcoinStorage__default['default'];
|
|
1482
1366
|
}
|
|
1483
1367
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
1484
1368
|
await new Storage(`${prefix}-${name}`, root);
|
|
@@ -1667,7 +1551,7 @@ class Peernet {
|
|
|
1667
1551
|
*/
|
|
1668
1552
|
async _protoHandler(message, peer) {
|
|
1669
1553
|
const {id, proto} = message;
|
|
1670
|
-
this.bw.down +=
|
|
1554
|
+
this.bw.down += proto.encoded.length;
|
|
1671
1555
|
if (proto.name === 'peernet-peer') {
|
|
1672
1556
|
const from = proto.decoded.id;
|
|
1673
1557
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -1735,22 +1619,22 @@ class Peernet {
|
|
|
1735
1619
|
let data;
|
|
1736
1620
|
|
|
1737
1621
|
if (!store) {
|
|
1738
|
-
|
|
1622
|
+
store = await this.whichStore([...this.stores], hash);
|
|
1739
1623
|
} else {
|
|
1740
|
-
store = globalThis[`${store}Store`];
|
|
1741
|
-
if (store.private) {
|
|
1742
|
-
// TODO: ban
|
|
1743
|
-
return
|
|
1744
|
-
} else data = await store.get(hash);
|
|
1624
|
+
store = globalThis.stores[`${store}Store`];
|
|
1745
1625
|
}
|
|
1626
|
+
if (store && !store.private) {
|
|
1627
|
+
data = await store.get(hash);
|
|
1746
1628
|
|
|
1747
|
-
|
|
1748
|
-
|
|
1629
|
+
if (data) {
|
|
1630
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
1749
1631
|
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1632
|
+
const node = await this.prepareMessage(from, data.encoded);
|
|
1633
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
1634
|
+
this.bw.up += node.encoded.length;
|
|
1635
|
+
}
|
|
1753
1636
|
}
|
|
1637
|
+
|
|
1754
1638
|
} else if (proto.name === 'peernet-peer') {
|
|
1755
1639
|
const from = proto.decoded.id;
|
|
1756
1640
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -1853,7 +1737,7 @@ class Peernet {
|
|
|
1853
1737
|
get: async (hash) => {
|
|
1854
1738
|
const data = await blockStore.has(hash);
|
|
1855
1739
|
if (data) return await blockStore.get(hash)
|
|
1856
|
-
return this.requestData(hash)
|
|
1740
|
+
return this.requestData(hash, 'block')
|
|
1857
1741
|
},
|
|
1858
1742
|
put: async (hash, data) => {
|
|
1859
1743
|
if (await blockStore.has(hash)) return
|
|
@@ -1989,7 +1873,8 @@ class Peernet {
|
|
|
1989
1873
|
/**
|
|
1990
1874
|
* Get content for given hash
|
|
1991
1875
|
*
|
|
1992
|
-
* @param {String} hash
|
|
1876
|
+
* @param {String} hash - the hash of the wanted data
|
|
1877
|
+
* @param {String} store - storeName to access
|
|
1993
1878
|
*/
|
|
1994
1879
|
async get(hash, store) {
|
|
1995
1880
|
debug(`get ${hash}`);
|
|
@@ -1999,7 +1884,7 @@ class Peernet {
|
|
|
1999
1884
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2000
1885
|
if (data) return data
|
|
2001
1886
|
|
|
2002
|
-
return this.requestData(hash,
|
|
1887
|
+
return this.requestData(hash, store)
|
|
2003
1888
|
}
|
|
2004
1889
|
|
|
2005
1890
|
/**
|
|
@@ -2007,10 +1892,11 @@ class Peernet {
|
|
|
2007
1892
|
*
|
|
2008
1893
|
* @param {String} hash
|
|
2009
1894
|
* @param {Buffer} data
|
|
1895
|
+
* @param {String} store - storeName to access
|
|
2010
1896
|
*/
|
|
2011
1897
|
async put(hash, data, store = 'data') {
|
|
2012
1898
|
store = globalThis[`${store}Store`];
|
|
2013
|
-
return
|
|
1899
|
+
return store.put(hash, data)
|
|
2014
1900
|
}
|
|
2015
1901
|
|
|
2016
1902
|
/**
|
package/dist/module/peernet.js
CHANGED
|
@@ -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
|
-
|
|
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.1";
|
|
154
|
+
var version = "0.10.4";
|
|
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 (
|
|
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
|
-
|
|
1398
|
-
this.
|
|
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
|
-
|
|
1477
|
-
super(data, protons(proto$7).PeernetDataMessage, {name});
|
|
1376
|
+
super(data, protons(proto$7).PeernetDataMessage, {name: 'peernet-data'});
|
|
1478
1377
|
}
|
|
1479
1378
|
}
|
|
1480
1379
|
|
|
@@ -2215,7 +2114,7 @@ class Peernet {
|
|
|
2215
2114
|
*/
|
|
2216
2115
|
async _protoHandler(message, peer) {
|
|
2217
2116
|
const {id, proto} = message;
|
|
2218
|
-
this.bw.down +=
|
|
2117
|
+
this.bw.down += proto.encoded.length;
|
|
2219
2118
|
if (proto.name === 'peernet-peer') {
|
|
2220
2119
|
const from = proto.decoded.id;
|
|
2221
2120
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -2283,22 +2182,22 @@ class Peernet {
|
|
|
2283
2182
|
let data;
|
|
2284
2183
|
|
|
2285
2184
|
if (!store) {
|
|
2286
|
-
|
|
2185
|
+
store = await this.whichStore([...this.stores], hash);
|
|
2287
2186
|
} else {
|
|
2288
|
-
store = globalThis[`${store}Store`];
|
|
2289
|
-
if (store.private) {
|
|
2290
|
-
// TODO: ban
|
|
2291
|
-
return
|
|
2292
|
-
} else data = await store.get(hash);
|
|
2187
|
+
store = globalThis.stores[`${store}Store`];
|
|
2293
2188
|
}
|
|
2189
|
+
if (store && !store.private) {
|
|
2190
|
+
data = await store.get(hash);
|
|
2294
2191
|
|
|
2295
|
-
|
|
2296
|
-
|
|
2192
|
+
if (data) {
|
|
2193
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
2297
2194
|
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2195
|
+
const node = await this.prepareMessage(from, data.encoded);
|
|
2196
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2197
|
+
this.bw.up += node.encoded.length;
|
|
2198
|
+
}
|
|
2301
2199
|
}
|
|
2200
|
+
|
|
2302
2201
|
} else if (proto.name === 'peernet-peer') {
|
|
2303
2202
|
const from = proto.decoded.id;
|
|
2304
2203
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -2401,7 +2300,7 @@ class Peernet {
|
|
|
2401
2300
|
get: async (hash) => {
|
|
2402
2301
|
const data = await blockStore.has(hash);
|
|
2403
2302
|
if (data) return await blockStore.get(hash)
|
|
2404
|
-
return this.requestData(hash)
|
|
2303
|
+
return this.requestData(hash, 'block')
|
|
2405
2304
|
},
|
|
2406
2305
|
put: async (hash, data) => {
|
|
2407
2306
|
if (await blockStore.has(hash)) return
|
|
@@ -2537,7 +2436,8 @@ class Peernet {
|
|
|
2537
2436
|
/**
|
|
2538
2437
|
* Get content for given hash
|
|
2539
2438
|
*
|
|
2540
|
-
* @param {String} hash
|
|
2439
|
+
* @param {String} hash - the hash of the wanted data
|
|
2440
|
+
* @param {String} store - storeName to access
|
|
2541
2441
|
*/
|
|
2542
2442
|
async get(hash, store) {
|
|
2543
2443
|
debug(`get ${hash}`);
|
|
@@ -2547,7 +2447,7 @@ class Peernet {
|
|
|
2547
2447
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2548
2448
|
if (data) return data
|
|
2549
2449
|
|
|
2550
|
-
return this.requestData(hash,
|
|
2450
|
+
return this.requestData(hash, store)
|
|
2551
2451
|
}
|
|
2552
2452
|
|
|
2553
2453
|
/**
|
|
@@ -2555,10 +2455,11 @@ class Peernet {
|
|
|
2555
2455
|
*
|
|
2556
2456
|
* @param {String} hash
|
|
2557
2457
|
* @param {Buffer} data
|
|
2458
|
+
* @param {String} store - storeName to access
|
|
2558
2459
|
*/
|
|
2559
2460
|
async put(hash, data, store = 'data') {
|
|
2560
2461
|
store = globalThis[`${store}Store`];
|
|
2561
|
-
return
|
|
2462
|
+
return store.put(hash, data)
|
|
2562
2463
|
}
|
|
2563
2464
|
|
|
2564
2465
|
/**
|
package/package.json
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.5",
|
|
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": "
|
|
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",
|
|
@@ -24,6 +22,7 @@
|
|
|
24
22
|
"@leofcoin/generate-account": "^1.0.2",
|
|
25
23
|
"@leofcoin/multi-wallet": "^2.0.6",
|
|
26
24
|
"@leofcoin/storage": "^2.1.0",
|
|
25
|
+
"@vandeurenglenn/little-pubsub": "^1.3.1",
|
|
27
26
|
"bs32": "^0.1.6",
|
|
28
27
|
"bs58": "^4.0.1",
|
|
29
28
|
"bs58check": "^2.1.2",
|
package/rollup.config.js
CHANGED
|
@@ -27,10 +27,13 @@ export default [{
|
|
|
27
27
|
modify({
|
|
28
28
|
"import fetch from 'node-fetch'": ''
|
|
29
29
|
}),
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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 (
|
|
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
|
-
|
|
137
|
-
this.
|
|
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
|
}
|