@leofcoin/peernet 0.10.1 → 0.10.4
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 +667 -627
- package/dist/commonjs/codec-format-interface.js +20 -4
- package/dist/commonjs/peernet.js +38 -143
- package/dist/module/peernet.js +56 -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 +30 -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.0";
|
|
175
|
+
var version = "0.10.3";
|
|
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
|
|
|
@@ -1451,6 +1335,10 @@ class Peernet {
|
|
|
1451
1335
|
else options.root = `.${this.network}/peernet`;
|
|
1452
1336
|
}
|
|
1453
1337
|
globalThis.peernet = this;
|
|
1338
|
+
this.bw = {
|
|
1339
|
+
up: 0,
|
|
1340
|
+
down: 0,
|
|
1341
|
+
};
|
|
1454
1342
|
return this._init(options)
|
|
1455
1343
|
}
|
|
1456
1344
|
|
|
@@ -1474,7 +1362,7 @@ class Peernet {
|
|
|
1474
1362
|
if (this.hasDaemon) {
|
|
1475
1363
|
Storage = LeofcoinStorageClient;
|
|
1476
1364
|
} else {
|
|
1477
|
-
Storage =
|
|
1365
|
+
Storage = LeofcoinStorage__default['default'];
|
|
1478
1366
|
}
|
|
1479
1367
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
1480
1368
|
await new Storage(`${prefix}-${name}`, root);
|
|
@@ -1663,6 +1551,7 @@ class Peernet {
|
|
|
1663
1551
|
*/
|
|
1664
1552
|
async _protoHandler(message, peer) {
|
|
1665
1553
|
const {id, proto} = message;
|
|
1554
|
+
this.bw.down += proto.encoded.length;
|
|
1666
1555
|
if (proto.name === 'peernet-peer') {
|
|
1667
1556
|
const from = proto.decoded.id;
|
|
1668
1557
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -1677,6 +1566,7 @@ class Peernet {
|
|
|
1677
1566
|
const node = await this.prepareMessage(from, data.encoded);
|
|
1678
1567
|
|
|
1679
1568
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
1569
|
+
this.bw.up += node.encoded.length;
|
|
1680
1570
|
} else if (proto.name === 'peernet-peer-response') {
|
|
1681
1571
|
const from = proto.decoded.id;
|
|
1682
1572
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -1692,9 +1582,10 @@ class Peernet {
|
|
|
1692
1582
|
if (!from) {
|
|
1693
1583
|
const data = new PeerMessage({id: this.id});
|
|
1694
1584
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
1695
|
-
|
|
1585
|
+
this.bw.up += node.encoded.length;
|
|
1696
1586
|
let response = await peer.request(node.encoded);
|
|
1697
1587
|
response = protoFor(response);
|
|
1588
|
+
|
|
1698
1589
|
response = new PeerMessageResponse(response.decoded.data);
|
|
1699
1590
|
|
|
1700
1591
|
from = response.decoded.id;
|
|
@@ -1722,26 +1613,26 @@ class Peernet {
|
|
|
1722
1613
|
const node = await this.prepareMessage(from, data.encoded);
|
|
1723
1614
|
|
|
1724
1615
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
1616
|
+
this.bw.up += node.encoded.length;
|
|
1725
1617
|
} else if (proto.name === 'peernet-data') {
|
|
1726
1618
|
let { hash, store } = proto.decoded;
|
|
1727
1619
|
let data;
|
|
1728
1620
|
|
|
1729
1621
|
if (!store) {
|
|
1730
|
-
|
|
1731
|
-
} else {
|
|
1732
|
-
store = globalThis[`${store}Store`];
|
|
1733
|
-
if (store.private) {
|
|
1734
|
-
// TODO: ban
|
|
1735
|
-
return
|
|
1736
|
-
} else data = await store.get(hash);
|
|
1622
|
+
store = await this.whichStore([...this.stores], hash);
|
|
1737
1623
|
}
|
|
1624
|
+
if (store && !store.private) {
|
|
1625
|
+
data = await store.get(hash);
|
|
1738
1626
|
|
|
1739
|
-
|
|
1740
|
-
|
|
1627
|
+
if (data) {
|
|
1628
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
1741
1629
|
|
|
1742
|
-
|
|
1743
|
-
|
|
1630
|
+
const node = await this.prepareMessage(from, data.encoded);
|
|
1631
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
1632
|
+
this.bw.up += node.encoded.length;
|
|
1633
|
+
}
|
|
1744
1634
|
}
|
|
1635
|
+
|
|
1745
1636
|
} else if (proto.name === 'peernet-peer') {
|
|
1746
1637
|
const from = proto.decoded.id;
|
|
1747
1638
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -1754,6 +1645,7 @@ class Peernet {
|
|
|
1754
1645
|
const node = await this.prepareMessage(from, data.encoded);
|
|
1755
1646
|
|
|
1756
1647
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
1648
|
+
this.bw.up += node.encoded.length;
|
|
1757
1649
|
} else if (proto.name === 'peernet-request') {
|
|
1758
1650
|
// TODO: make dynamic
|
|
1759
1651
|
// exposeddevapi[proto.decoded.request](proto.decoded.params)
|
|
@@ -1762,6 +1654,7 @@ class Peernet {
|
|
|
1762
1654
|
const data = await method();
|
|
1763
1655
|
const node = await this.prepareMessage(from, data.encoded);
|
|
1764
1656
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
1657
|
+
this.bw.up += node.encoded.length;
|
|
1765
1658
|
}
|
|
1766
1659
|
} else if (proto.name === 'peernet-ps' &&
|
|
1767
1660
|
this._getPeerId(peer.id) !== this.id.toString()) {
|
|
@@ -1842,7 +1735,7 @@ class Peernet {
|
|
|
1842
1735
|
get: async (hash) => {
|
|
1843
1736
|
const data = await blockStore.has(hash);
|
|
1844
1737
|
if (data) return await blockStore.get(hash)
|
|
1845
|
-
return this.requestData(hash)
|
|
1738
|
+
return this.requestData(hash, 'block')
|
|
1846
1739
|
},
|
|
1847
1740
|
put: async (hash, data) => {
|
|
1848
1741
|
if (await blockStore.has(hash)) return
|
|
@@ -1978,7 +1871,8 @@ class Peernet {
|
|
|
1978
1871
|
/**
|
|
1979
1872
|
* Get content for given hash
|
|
1980
1873
|
*
|
|
1981
|
-
* @param {String} hash
|
|
1874
|
+
* @param {String} hash - the hash of the wanted data
|
|
1875
|
+
* @param {String} store - storeName to access
|
|
1982
1876
|
*/
|
|
1983
1877
|
async get(hash, store) {
|
|
1984
1878
|
debug(`get ${hash}`);
|
|
@@ -1988,7 +1882,7 @@ class Peernet {
|
|
|
1988
1882
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
1989
1883
|
if (data) return data
|
|
1990
1884
|
|
|
1991
|
-
return this.requestData(hash,
|
|
1885
|
+
return this.requestData(hash, store)
|
|
1992
1886
|
}
|
|
1993
1887
|
|
|
1994
1888
|
/**
|
|
@@ -1996,10 +1890,11 @@ class Peernet {
|
|
|
1996
1890
|
*
|
|
1997
1891
|
* @param {String} hash
|
|
1998
1892
|
* @param {Buffer} data
|
|
1893
|
+
* @param {String} store - storeName to access
|
|
1999
1894
|
*/
|
|
2000
1895
|
async put(hash, data, store = 'data') {
|
|
2001
1896
|
store = globalThis[`${store}Store`];
|
|
2002
|
-
return
|
|
1897
|
+
return store.put(hash, data)
|
|
2003
1898
|
}
|
|
2004
1899
|
|
|
2005
1900
|
/**
|
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.0";
|
|
154
|
+
var version = "0.10.3";
|
|
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
|
|
|
@@ -1999,6 +1898,10 @@ class Peernet {
|
|
|
1999
1898
|
else options.root = `.${this.network}/peernet`;
|
|
2000
1899
|
}
|
|
2001
1900
|
globalThis.peernet = this;
|
|
1901
|
+
this.bw = {
|
|
1902
|
+
up: 0,
|
|
1903
|
+
down: 0,
|
|
1904
|
+
};
|
|
2002
1905
|
return this._init(options)
|
|
2003
1906
|
}
|
|
2004
1907
|
|
|
@@ -2211,6 +2114,7 @@ class Peernet {
|
|
|
2211
2114
|
*/
|
|
2212
2115
|
async _protoHandler(message, peer) {
|
|
2213
2116
|
const {id, proto} = message;
|
|
2117
|
+
this.bw.down += proto.encoded.length;
|
|
2214
2118
|
if (proto.name === 'peernet-peer') {
|
|
2215
2119
|
const from = proto.decoded.id;
|
|
2216
2120
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -2225,6 +2129,7 @@ class Peernet {
|
|
|
2225
2129
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2226
2130
|
|
|
2227
2131
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2132
|
+
this.bw.up += node.encoded.length;
|
|
2228
2133
|
} else if (proto.name === 'peernet-peer-response') {
|
|
2229
2134
|
const from = proto.decoded.id;
|
|
2230
2135
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -2240,9 +2145,10 @@ class Peernet {
|
|
|
2240
2145
|
if (!from) {
|
|
2241
2146
|
const data = new PeerMessage({id: this.id});
|
|
2242
2147
|
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2243
|
-
|
|
2148
|
+
this.bw.up += node.encoded.length;
|
|
2244
2149
|
let response = await peer.request(node.encoded);
|
|
2245
2150
|
response = protoFor(response);
|
|
2151
|
+
|
|
2246
2152
|
response = new PeerMessageResponse(response.decoded.data);
|
|
2247
2153
|
|
|
2248
2154
|
from = response.decoded.id;
|
|
@@ -2270,26 +2176,26 @@ class Peernet {
|
|
|
2270
2176
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2271
2177
|
|
|
2272
2178
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2179
|
+
this.bw.up += node.encoded.length;
|
|
2273
2180
|
} else if (proto.name === 'peernet-data') {
|
|
2274
2181
|
let { hash, store } = proto.decoded;
|
|
2275
2182
|
let data;
|
|
2276
2183
|
|
|
2277
2184
|
if (!store) {
|
|
2278
|
-
|
|
2279
|
-
} else {
|
|
2280
|
-
store = globalThis[`${store}Store`];
|
|
2281
|
-
if (store.private) {
|
|
2282
|
-
// TODO: ban
|
|
2283
|
-
return
|
|
2284
|
-
} else data = await store.get(hash);
|
|
2185
|
+
store = await this.whichStore([...this.stores], hash);
|
|
2285
2186
|
}
|
|
2187
|
+
if (store && !store.private) {
|
|
2188
|
+
data = await store.get(hash);
|
|
2286
2189
|
|
|
2287
|
-
|
|
2288
|
-
|
|
2190
|
+
if (data) {
|
|
2191
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
2289
2192
|
|
|
2290
|
-
|
|
2291
|
-
|
|
2193
|
+
const node = await this.prepareMessage(from, data.encoded);
|
|
2194
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2195
|
+
this.bw.up += node.encoded.length;
|
|
2196
|
+
}
|
|
2292
2197
|
}
|
|
2198
|
+
|
|
2293
2199
|
} else if (proto.name === 'peernet-peer') {
|
|
2294
2200
|
const from = proto.decoded.id;
|
|
2295
2201
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -2302,6 +2208,7 @@ class Peernet {
|
|
|
2302
2208
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2303
2209
|
|
|
2304
2210
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2211
|
+
this.bw.up += node.encoded.length;
|
|
2305
2212
|
} else if (proto.name === 'peernet-request') {
|
|
2306
2213
|
// TODO: make dynamic
|
|
2307
2214
|
// exposeddevapi[proto.decoded.request](proto.decoded.params)
|
|
@@ -2310,6 +2217,7 @@ class Peernet {
|
|
|
2310
2217
|
const data = await method();
|
|
2311
2218
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2312
2219
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2220
|
+
this.bw.up += node.encoded.length;
|
|
2313
2221
|
}
|
|
2314
2222
|
} else if (proto.name === 'peernet-ps' &&
|
|
2315
2223
|
this._getPeerId(peer.id) !== this.id.toString()) {
|
|
@@ -2390,7 +2298,7 @@ class Peernet {
|
|
|
2390
2298
|
get: async (hash) => {
|
|
2391
2299
|
const data = await blockStore.has(hash);
|
|
2392
2300
|
if (data) return await blockStore.get(hash)
|
|
2393
|
-
return this.requestData(hash)
|
|
2301
|
+
return this.requestData(hash, 'block')
|
|
2394
2302
|
},
|
|
2395
2303
|
put: async (hash, data) => {
|
|
2396
2304
|
if (await blockStore.has(hash)) return
|
|
@@ -2526,7 +2434,8 @@ class Peernet {
|
|
|
2526
2434
|
/**
|
|
2527
2435
|
* Get content for given hash
|
|
2528
2436
|
*
|
|
2529
|
-
* @param {String} hash
|
|
2437
|
+
* @param {String} hash - the hash of the wanted data
|
|
2438
|
+
* @param {String} store - storeName to access
|
|
2530
2439
|
*/
|
|
2531
2440
|
async get(hash, store) {
|
|
2532
2441
|
debug(`get ${hash}`);
|
|
@@ -2536,7 +2445,7 @@ class Peernet {
|
|
|
2536
2445
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2537
2446
|
if (data) return data
|
|
2538
2447
|
|
|
2539
|
-
return this.requestData(hash,
|
|
2448
|
+
return this.requestData(hash, store)
|
|
2540
2449
|
}
|
|
2541
2450
|
|
|
2542
2451
|
/**
|
|
@@ -2544,10 +2453,11 @@ class Peernet {
|
|
|
2544
2453
|
*
|
|
2545
2454
|
* @param {String} hash
|
|
2546
2455
|
* @param {Buffer} data
|
|
2456
|
+
* @param {String} store - storeName to access
|
|
2547
2457
|
*/
|
|
2548
2458
|
async put(hash, data, store = 'data') {
|
|
2549
2459
|
store = globalThis[`${store}Store`];
|
|
2550
|
-
return
|
|
2460
|
+
return store.put(hash, data)
|
|
2551
2461
|
}
|
|
2552
2462
|
|
|
2553
2463
|
/**
|