@leofcoin/peernet 0.10.3 → 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 +656 -627
- package/dist/commonjs/codec-format-interface.js +20 -4
- package/dist/commonjs/peernet.js +27 -143
- package/dist/module/peernet.js +45 -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 +19 -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.2";
|
|
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
|
|
|
@@ -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);
|
|
@@ -1735,22 +1619,20 @@ class Peernet {
|
|
|
1735
1619
|
let data;
|
|
1736
1620
|
|
|
1737
1621
|
if (!store) {
|
|
1738
|
-
|
|
1739
|
-
} else {
|
|
1740
|
-
store = globalThis[`${store}Store`];
|
|
1741
|
-
if (store.private) {
|
|
1742
|
-
// TODO: ban
|
|
1743
|
-
return
|
|
1744
|
-
} else data = await store.get(hash);
|
|
1622
|
+
store = await this.whichStore([...this.stores], hash);
|
|
1745
1623
|
}
|
|
1624
|
+
if (store && !store.private) {
|
|
1625
|
+
data = await store.get(hash);
|
|
1746
1626
|
|
|
1747
|
-
|
|
1748
|
-
|
|
1627
|
+
if (data) {
|
|
1628
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
1749
1629
|
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
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
|
+
}
|
|
1753
1634
|
}
|
|
1635
|
+
|
|
1754
1636
|
} else if (proto.name === 'peernet-peer') {
|
|
1755
1637
|
const from = proto.decoded.id;
|
|
1756
1638
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -1853,7 +1735,7 @@ class Peernet {
|
|
|
1853
1735
|
get: async (hash) => {
|
|
1854
1736
|
const data = await blockStore.has(hash);
|
|
1855
1737
|
if (data) return await blockStore.get(hash)
|
|
1856
|
-
return this.requestData(hash)
|
|
1738
|
+
return this.requestData(hash, 'block')
|
|
1857
1739
|
},
|
|
1858
1740
|
put: async (hash, data) => {
|
|
1859
1741
|
if (await blockStore.has(hash)) return
|
|
@@ -1989,7 +1871,8 @@ class Peernet {
|
|
|
1989
1871
|
/**
|
|
1990
1872
|
* Get content for given hash
|
|
1991
1873
|
*
|
|
1992
|
-
* @param {String} hash
|
|
1874
|
+
* @param {String} hash - the hash of the wanted data
|
|
1875
|
+
* @param {String} store - storeName to access
|
|
1993
1876
|
*/
|
|
1994
1877
|
async get(hash, store) {
|
|
1995
1878
|
debug(`get ${hash}`);
|
|
@@ -1999,7 +1882,7 @@ class Peernet {
|
|
|
1999
1882
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2000
1883
|
if (data) return data
|
|
2001
1884
|
|
|
2002
|
-
return this.requestData(hash,
|
|
1885
|
+
return this.requestData(hash, store)
|
|
2003
1886
|
}
|
|
2004
1887
|
|
|
2005
1888
|
/**
|
|
@@ -2007,10 +1890,11 @@ class Peernet {
|
|
|
2007
1890
|
*
|
|
2008
1891
|
* @param {String} hash
|
|
2009
1892
|
* @param {Buffer} data
|
|
1893
|
+
* @param {String} store - storeName to access
|
|
2010
1894
|
*/
|
|
2011
1895
|
async put(hash, data, store = 'data') {
|
|
2012
1896
|
store = globalThis[`${store}Store`];
|
|
2013
|
-
return
|
|
1897
|
+
return store.put(hash, data)
|
|
2014
1898
|
}
|
|
2015
1899
|
|
|
2016
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.2";
|
|
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
|
|
|
@@ -2283,22 +2182,20 @@ class Peernet {
|
|
|
2283
2182
|
let data;
|
|
2284
2183
|
|
|
2285
2184
|
if (!store) {
|
|
2286
|
-
|
|
2287
|
-
} else {
|
|
2288
|
-
store = globalThis[`${store}Store`];
|
|
2289
|
-
if (store.private) {
|
|
2290
|
-
// TODO: ban
|
|
2291
|
-
return
|
|
2292
|
-
} else data = await store.get(hash);
|
|
2185
|
+
store = await this.whichStore([...this.stores], hash);
|
|
2293
2186
|
}
|
|
2187
|
+
if (store && !store.private) {
|
|
2188
|
+
data = await store.get(hash);
|
|
2294
2189
|
|
|
2295
|
-
|
|
2296
|
-
|
|
2190
|
+
if (data) {
|
|
2191
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
2297
2192
|
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
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
|
+
}
|
|
2301
2197
|
}
|
|
2198
|
+
|
|
2302
2199
|
} else if (proto.name === 'peernet-peer') {
|
|
2303
2200
|
const from = proto.decoded.id;
|
|
2304
2201
|
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
|
|
@@ -2401,7 +2298,7 @@ class Peernet {
|
|
|
2401
2298
|
get: async (hash) => {
|
|
2402
2299
|
const data = await blockStore.has(hash);
|
|
2403
2300
|
if (data) return await blockStore.get(hash)
|
|
2404
|
-
return this.requestData(hash)
|
|
2301
|
+
return this.requestData(hash, 'block')
|
|
2405
2302
|
},
|
|
2406
2303
|
put: async (hash, data) => {
|
|
2407
2304
|
if (await blockStore.has(hash)) return
|
|
@@ -2537,7 +2434,8 @@ class Peernet {
|
|
|
2537
2434
|
/**
|
|
2538
2435
|
* Get content for given hash
|
|
2539
2436
|
*
|
|
2540
|
-
* @param {String} hash
|
|
2437
|
+
* @param {String} hash - the hash of the wanted data
|
|
2438
|
+
* @param {String} store - storeName to access
|
|
2541
2439
|
*/
|
|
2542
2440
|
async get(hash, store) {
|
|
2543
2441
|
debug(`get ${hash}`);
|
|
@@ -2547,7 +2445,7 @@ class Peernet {
|
|
|
2547
2445
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2548
2446
|
if (data) return data
|
|
2549
2447
|
|
|
2550
|
-
return this.requestData(hash,
|
|
2448
|
+
return this.requestData(hash, store)
|
|
2551
2449
|
}
|
|
2552
2450
|
|
|
2553
2451
|
/**
|
|
@@ -2555,10 +2453,11 @@ class Peernet {
|
|
|
2555
2453
|
*
|
|
2556
2454
|
* @param {String} hash
|
|
2557
2455
|
* @param {Buffer} data
|
|
2456
|
+
* @param {String} store - storeName to access
|
|
2558
2457
|
*/
|
|
2559
2458
|
async put(hash, data, store = 'data') {
|
|
2560
2459
|
store = globalThis[`${store}Store`];
|
|
2561
|
-
return
|
|
2460
|
+
return store.put(hash, data)
|
|
2562
2461
|
}
|
|
2563
2462
|
|
|
2564
2463
|
/**
|
package/package.json
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.4",
|
|
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
|
}
|
package/src/messages/data.js
CHANGED
|
@@ -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
|
-
|
|
12
|
-
super(data, protons(proto).PeernetDataMessage, {name})
|
|
16
|
+
super(data, protons(proto).PeernetDataMessage, {name: 'peernet-data'})
|
|
13
17
|
}
|
|
14
18
|
}
|