@leofcoin/peernet 0.8.11 → 0.9.1
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
CHANGED
|
@@ -261,7 +261,7 @@ class LeofcoinStorage$1 {
|
|
|
261
261
|
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
var version = "0.
|
|
264
|
+
var version = "0.9.0";
|
|
265
265
|
|
|
266
266
|
var api$1 = {
|
|
267
267
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
@@ -999,6 +999,11 @@ var codecs = {
|
|
|
999
999
|
hashAlg: 'keccak-256', // ,
|
|
1000
1000
|
// testnet: 'olivia'
|
|
1001
1001
|
},
|
|
1002
|
+
// chat message
|
|
1003
|
+
'chat-message': {
|
|
1004
|
+
codec: '636d',
|
|
1005
|
+
hashAlg: 'dbl-keccak-512',
|
|
1006
|
+
},
|
|
1002
1007
|
};
|
|
1003
1008
|
|
|
1004
1009
|
class PeernetCodec {
|
|
@@ -1571,6 +1576,17 @@ class DataMessageResponse extends FormatInterface {
|
|
|
1571
1576
|
}
|
|
1572
1577
|
}
|
|
1573
1578
|
|
|
1579
|
+
class ChatMessage extends FormatInterface {
|
|
1580
|
+
get keys() {
|
|
1581
|
+
return ['author', 'value', 'timestamp', 'files']
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
constructor(buffer) {
|
|
1585
|
+
const name = 'chat-message';
|
|
1586
|
+
super(buffer, protons__default['default'](proto$9).ChatMessage, {name});
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1574
1590
|
const debug = (log) => {
|
|
1575
1591
|
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
1576
1592
|
};
|
|
@@ -1972,7 +1988,7 @@ class Peernet {
|
|
|
1972
1988
|
}
|
|
1973
1989
|
|
|
1974
1990
|
get defaultStores() {
|
|
1975
|
-
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data']
|
|
1991
|
+
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
|
|
1976
1992
|
}
|
|
1977
1993
|
|
|
1978
1994
|
addProto(name, proto) {
|
|
@@ -1985,7 +2001,7 @@ class Peernet {
|
|
|
1985
2001
|
|
|
1986
2002
|
async addStore(name, prefix, root, isPrivate = true) {
|
|
1987
2003
|
if (name === 'block' || name === 'transaction' || name === 'chain' ||
|
|
1988
|
-
name === 'data') isPrivate = false;
|
|
2004
|
+
name === 'data' || name === 'message') isPrivate = false;
|
|
1989
2005
|
|
|
1990
2006
|
let Storage;
|
|
1991
2007
|
if (this.hasDaemon) {
|
|
@@ -2008,6 +2024,11 @@ class Peernet {
|
|
|
2008
2024
|
return this._messageHandler.prepareMessage(this.id, to, data)
|
|
2009
2025
|
}
|
|
2010
2026
|
|
|
2027
|
+
/**
|
|
2028
|
+
* @access public
|
|
2029
|
+
*
|
|
2030
|
+
* @return {Array} peerId
|
|
2031
|
+
*/
|
|
2011
2032
|
get peers() {
|
|
2012
2033
|
return [...connections.values()]
|
|
2013
2034
|
}
|
|
@@ -2056,6 +2077,7 @@ class Peernet {
|
|
|
2056
2077
|
'peernet-data': DataMessage,
|
|
2057
2078
|
'peernet-data-response': DataMessageResponse,
|
|
2058
2079
|
'peernet-ps': PsMessage,
|
|
2080
|
+
'chat-message': ChatMessage
|
|
2059
2081
|
};
|
|
2060
2082
|
this.protos = globalThis.peernet.protos;
|
|
2061
2083
|
|
|
@@ -2408,6 +2430,36 @@ class Peernet {
|
|
|
2408
2430
|
return null
|
|
2409
2431
|
}
|
|
2410
2432
|
|
|
2433
|
+
|
|
2434
|
+
|
|
2435
|
+
get message() {
|
|
2436
|
+
return {
|
|
2437
|
+
/**
|
|
2438
|
+
* Get content for given message hash
|
|
2439
|
+
*
|
|
2440
|
+
* @param {String} hash
|
|
2441
|
+
*/
|
|
2442
|
+
get: async (hash) => {
|
|
2443
|
+
debug(`get message ${hash}`);
|
|
2444
|
+
const message = await messageStore.has(hash);
|
|
2445
|
+
if (message) return await messageStore.get(hash)
|
|
2446
|
+
return this.requestData(hash, 'message')
|
|
2447
|
+
},
|
|
2448
|
+
/**
|
|
2449
|
+
* put message content
|
|
2450
|
+
*
|
|
2451
|
+
* @param {String} hash
|
|
2452
|
+
* @param {Buffer} message
|
|
2453
|
+
*/
|
|
2454
|
+
put: async (hash, message) => await messageStore.put(hash, message),
|
|
2455
|
+
/**
|
|
2456
|
+
* @param {String} hash
|
|
2457
|
+
* @return {Boolean}
|
|
2458
|
+
*/
|
|
2459
|
+
has: async (hash) => await messageStore.has(hash),
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2411
2463
|
get data() {
|
|
2412
2464
|
return {
|
|
2413
2465
|
/**
|
|
@@ -2498,16 +2550,16 @@ class Peernet {
|
|
|
2498
2550
|
*/
|
|
2499
2551
|
async publish(topic, data) {
|
|
2500
2552
|
// globalSub.publish(topic, data)
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
2553
|
if (!Buffer.isBuffer(topic)) topic = Buffer.from(topic);
|
|
2504
2554
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
2505
2555
|
const id = Math.random().toString(36).slice(-12);
|
|
2506
2556
|
data = new PsMessage({data, topic});
|
|
2507
2557
|
for (const peer of this.peers) {
|
|
2508
|
-
if (peer.connection._connected
|
|
2509
|
-
|
|
2510
|
-
|
|
2558
|
+
if (peer.connection._connected) {
|
|
2559
|
+
if (peer.id.toString() !== this.peerId.toString()) {
|
|
2560
|
+
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2561
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2562
|
+
}
|
|
2511
2563
|
} else {
|
|
2512
2564
|
this.removePeer(peer);
|
|
2513
2565
|
}
|
|
@@ -2515,6 +2567,10 @@ class Peernet {
|
|
|
2515
2567
|
}
|
|
2516
2568
|
}
|
|
2517
2569
|
|
|
2570
|
+
createHash(data, name) {
|
|
2571
|
+
return new PeernetHash(data, {name})
|
|
2572
|
+
}
|
|
2573
|
+
|
|
2518
2574
|
/**
|
|
2519
2575
|
*
|
|
2520
2576
|
* @param {String} topic
|
package/dist/commonjs/codec.js
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var protons = require('protons');
|
|
4
|
+
var codecFormatInterface = require('./codec-format-interface.js');
|
|
5
|
+
|
|
6
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var protons__default = /*#__PURE__*/_interopDefaultLegacy(protons);
|
|
9
|
+
|
|
10
|
+
var proto = `
|
|
11
|
+
// PeernetMessage
|
|
12
|
+
message PeernetMessage {
|
|
13
|
+
required bytes data = 1;
|
|
14
|
+
required bytes signature = 2;
|
|
15
|
+
optional bytes from = 3;
|
|
16
|
+
optional bytes to = 4;
|
|
17
|
+
optional string id = 5;
|
|
18
|
+
}`;
|
|
19
|
+
|
|
20
|
+
class PeernetMessage extends codecFormatInterface {
|
|
21
|
+
get keys() {
|
|
22
|
+
return ['data', 'signature', 'from', 'to', 'id']
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
constructor(buffer) {
|
|
26
|
+
const name = 'peernet-message';
|
|
27
|
+
super(buffer, protons__default['default'](proto).PeernetMessage, {name});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.PeernetMessage = PeernetMessage;
|
|
32
|
+
exports.proto = proto;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var
|
|
3
|
+
require('protons');
|
|
4
|
+
var peernetMessage = require('./peernet-message-b6925673.js');
|
|
5
|
+
require('./codec-format-interface.js');
|
|
5
6
|
require('bs32');
|
|
6
7
|
require('bs58');
|
|
7
8
|
require('is-hex');
|
|
@@ -10,29 +11,6 @@ require('varint');
|
|
|
10
11
|
require('./hash.js');
|
|
11
12
|
require('keccak');
|
|
12
13
|
|
|
13
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
14
|
|
|
15
|
-
var protons__default = /*#__PURE__*/_interopDefaultLegacy(protons);
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
// PeernetMessage
|
|
19
|
-
message PeernetMessage {
|
|
20
|
-
required bytes data = 1;
|
|
21
|
-
required bytes signature = 2;
|
|
22
|
-
optional bytes from = 3;
|
|
23
|
-
optional bytes to = 4;
|
|
24
|
-
optional string id = 5;
|
|
25
|
-
}`;
|
|
26
|
-
|
|
27
|
-
class PeernetMessage extends codecFormatInterface {
|
|
28
|
-
get keys() {
|
|
29
|
-
return ['data', 'signature', 'from', 'to', 'id']
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
constructor(buffer) {
|
|
33
|
-
const name = 'peernet-message';
|
|
34
|
-
super(buffer, protons__default['default'](proto).PeernetMessage, {name});
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
module.exports = PeernetMessage;
|
|
16
|
+
module.exports = peernetMessage.PeernetMessage;
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -6,7 +6,7 @@ var P2P = require('p2pt');
|
|
|
6
6
|
var websocket = require('websocket');
|
|
7
7
|
var http$1 = require('http');
|
|
8
8
|
var Koa = require('koa');
|
|
9
|
-
var peernetMessage = require('./peernet-message.js');
|
|
9
|
+
var peernetMessage = require('./peernet-message-b6925673.js');
|
|
10
10
|
var dht = require('./dht.js');
|
|
11
11
|
var dhtResponse = require('./dht-response.js');
|
|
12
12
|
var protons = require('protons');
|
|
@@ -15,8 +15,8 @@ var request = require('./request.js');
|
|
|
15
15
|
var response = require('./response.js');
|
|
16
16
|
var fetch$1 = require('node-fetch');
|
|
17
17
|
var codec = require('./codec.js');
|
|
18
|
-
var MultiWallet = require('@leofcoin/multi-wallet');
|
|
19
18
|
var hash = require('./hash.js');
|
|
19
|
+
var MultiWallet = require('@leofcoin/multi-wallet');
|
|
20
20
|
require('bs32');
|
|
21
21
|
require('bs58');
|
|
22
22
|
require('is-hex');
|
|
@@ -264,7 +264,7 @@ class LeofcoinStorage$1 {
|
|
|
264
264
|
|
|
265
265
|
}
|
|
266
266
|
|
|
267
|
-
var version = "0.
|
|
267
|
+
var version = "0.9.0";
|
|
268
268
|
|
|
269
269
|
var api$1 = {
|
|
270
270
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
@@ -1011,6 +1011,17 @@ class DataMessageResponse extends codecFormatInterface {
|
|
|
1011
1011
|
}
|
|
1012
1012
|
}
|
|
1013
1013
|
|
|
1014
|
+
class ChatMessage extends codecFormatInterface {
|
|
1015
|
+
get keys() {
|
|
1016
|
+
return ['author', 'value', 'timestamp', 'files']
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
constructor(buffer) {
|
|
1020
|
+
const name = 'chat-message';
|
|
1021
|
+
super(buffer, protons__default['default'](peernetMessage.proto).ChatMessage, {name});
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1014
1025
|
const debug = (log) => {
|
|
1015
1026
|
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
1016
1027
|
};
|
|
@@ -1350,7 +1361,7 @@ class MessageHandler {
|
|
|
1350
1361
|
data,
|
|
1351
1362
|
};
|
|
1352
1363
|
const signature = await this.hashAndSignMessage(message);
|
|
1353
|
-
const node = new peernetMessage({
|
|
1364
|
+
const node = new peernetMessage.PeernetMessage({
|
|
1354
1365
|
...message,
|
|
1355
1366
|
signature,
|
|
1356
1367
|
});
|
|
@@ -1412,7 +1423,7 @@ class Peernet {
|
|
|
1412
1423
|
}
|
|
1413
1424
|
|
|
1414
1425
|
get defaultStores() {
|
|
1415
|
-
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data']
|
|
1426
|
+
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
|
|
1416
1427
|
}
|
|
1417
1428
|
|
|
1418
1429
|
addProto(name, proto) {
|
|
@@ -1425,7 +1436,7 @@ class Peernet {
|
|
|
1425
1436
|
|
|
1426
1437
|
async addStore(name, prefix, root, isPrivate = true) {
|
|
1427
1438
|
if (name === 'block' || name === 'transaction' || name === 'chain' ||
|
|
1428
|
-
name === 'data') isPrivate = false;
|
|
1439
|
+
name === 'data' || name === 'message') isPrivate = false;
|
|
1429
1440
|
|
|
1430
1441
|
let Storage;
|
|
1431
1442
|
if (this.hasDaemon) {
|
|
@@ -1448,6 +1459,11 @@ class Peernet {
|
|
|
1448
1459
|
return this._messageHandler.prepareMessage(this.id, to, data)
|
|
1449
1460
|
}
|
|
1450
1461
|
|
|
1462
|
+
/**
|
|
1463
|
+
* @access public
|
|
1464
|
+
*
|
|
1465
|
+
* @return {Array} peerId
|
|
1466
|
+
*/
|
|
1451
1467
|
get peers() {
|
|
1452
1468
|
return [...connections.values()]
|
|
1453
1469
|
}
|
|
@@ -1490,12 +1506,13 @@ class Peernet {
|
|
|
1490
1506
|
'peernet-response': response,
|
|
1491
1507
|
'peernet-peer': PeerMessage,
|
|
1492
1508
|
'peernet-peer-response': PeerMessageResponse,
|
|
1493
|
-
'peernet-message': peernetMessage,
|
|
1509
|
+
'peernet-message': peernetMessage.PeernetMessage,
|
|
1494
1510
|
'peernet-dht': dht,
|
|
1495
1511
|
'peernet-dht-response': dhtResponse,
|
|
1496
1512
|
'peernet-data': DataMessage,
|
|
1497
1513
|
'peernet-data-response': DataMessageResponse,
|
|
1498
1514
|
'peernet-ps': PsMessage,
|
|
1515
|
+
'chat-message': ChatMessage
|
|
1499
1516
|
};
|
|
1500
1517
|
this.protos = globalThis.peernet.protos;
|
|
1501
1518
|
|
|
@@ -1544,7 +1561,7 @@ class Peernet {
|
|
|
1544
1561
|
this._peerHandler.discover(peer);
|
|
1545
1562
|
peer.on('peernet.data', async (message) => {
|
|
1546
1563
|
const id = message.id;
|
|
1547
|
-
message = new peernetMessage(Buffer.from(message.data.data));
|
|
1564
|
+
message = new peernetMessage.PeernetMessage(Buffer.from(message.data.data));
|
|
1548
1565
|
const proto = protoFor(message.decoded.data);
|
|
1549
1566
|
await this._protoHandler({id, proto}, peer);
|
|
1550
1567
|
const fulldId = this._getPeerId(peer.id);
|
|
@@ -1848,6 +1865,36 @@ class Peernet {
|
|
|
1848
1865
|
return null
|
|
1849
1866
|
}
|
|
1850
1867
|
|
|
1868
|
+
|
|
1869
|
+
|
|
1870
|
+
get message() {
|
|
1871
|
+
return {
|
|
1872
|
+
/**
|
|
1873
|
+
* Get content for given message hash
|
|
1874
|
+
*
|
|
1875
|
+
* @param {String} hash
|
|
1876
|
+
*/
|
|
1877
|
+
get: async (hash) => {
|
|
1878
|
+
debug(`get message ${hash}`);
|
|
1879
|
+
const message = await messageStore.has(hash);
|
|
1880
|
+
if (message) return await messageStore.get(hash)
|
|
1881
|
+
return this.requestData(hash, 'message')
|
|
1882
|
+
},
|
|
1883
|
+
/**
|
|
1884
|
+
* put message content
|
|
1885
|
+
*
|
|
1886
|
+
* @param {String} hash
|
|
1887
|
+
* @param {Buffer} message
|
|
1888
|
+
*/
|
|
1889
|
+
put: async (hash, message) => await messageStore.put(hash, message),
|
|
1890
|
+
/**
|
|
1891
|
+
* @param {String} hash
|
|
1892
|
+
* @return {Boolean}
|
|
1893
|
+
*/
|
|
1894
|
+
has: async (hash) => await messageStore.has(hash),
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1851
1898
|
get data() {
|
|
1852
1899
|
return {
|
|
1853
1900
|
/**
|
|
@@ -1938,16 +1985,16 @@ class Peernet {
|
|
|
1938
1985
|
*/
|
|
1939
1986
|
async publish(topic, data) {
|
|
1940
1987
|
// globalSub.publish(topic, data)
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
1988
|
if (!Buffer.isBuffer(topic)) topic = Buffer.from(topic);
|
|
1944
1989
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
1945
1990
|
const id = Math.random().toString(36).slice(-12);
|
|
1946
1991
|
data = new PsMessage({data, topic});
|
|
1947
1992
|
for (const peer of this.peers) {
|
|
1948
|
-
if (peer.connection._connected
|
|
1949
|
-
|
|
1950
|
-
|
|
1993
|
+
if (peer.connection._connected) {
|
|
1994
|
+
if (peer.id.toString() !== this.peerId.toString()) {
|
|
1995
|
+
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
1996
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
1997
|
+
}
|
|
1951
1998
|
} else {
|
|
1952
1999
|
this.removePeer(peer);
|
|
1953
2000
|
}
|
|
@@ -1955,6 +2002,10 @@ class Peernet {
|
|
|
1955
2002
|
}
|
|
1956
2003
|
}
|
|
1957
2004
|
|
|
2005
|
+
createHash(data, name) {
|
|
2006
|
+
return new hash(data, {name})
|
|
2007
|
+
}
|
|
2008
|
+
|
|
1958
2009
|
/**
|
|
1959
2010
|
*
|
|
1960
2011
|
* @param {String} topic
|
package/dist/module/peernet.js
CHANGED
|
@@ -244,7 +244,7 @@ class LeofcoinStorage$1 {
|
|
|
244
244
|
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
-
var version = "0.
|
|
247
|
+
var version = "0.9.0";
|
|
248
248
|
|
|
249
249
|
var api$1 = {
|
|
250
250
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
@@ -982,6 +982,11 @@ var codecs = {
|
|
|
982
982
|
hashAlg: 'keccak-256', // ,
|
|
983
983
|
// testnet: 'olivia'
|
|
984
984
|
},
|
|
985
|
+
// chat message
|
|
986
|
+
'chat-message': {
|
|
987
|
+
codec: '636d',
|
|
988
|
+
hashAlg: 'dbl-keccak-512',
|
|
989
|
+
},
|
|
985
990
|
};
|
|
986
991
|
|
|
987
992
|
class PeernetCodec {
|
|
@@ -1554,6 +1559,17 @@ class DataMessageResponse extends FormatInterface {
|
|
|
1554
1559
|
}
|
|
1555
1560
|
}
|
|
1556
1561
|
|
|
1562
|
+
class ChatMessage extends FormatInterface {
|
|
1563
|
+
get keys() {
|
|
1564
|
+
return ['author', 'value', 'timestamp', 'files']
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
constructor(buffer) {
|
|
1568
|
+
const name = 'chat-message';
|
|
1569
|
+
super(buffer, protons(proto$9).ChatMessage, {name});
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1557
1573
|
const debug = (log) => {
|
|
1558
1574
|
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
1559
1575
|
};
|
|
@@ -1955,7 +1971,7 @@ class Peernet {
|
|
|
1955
1971
|
}
|
|
1956
1972
|
|
|
1957
1973
|
get defaultStores() {
|
|
1958
|
-
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data']
|
|
1974
|
+
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
|
|
1959
1975
|
}
|
|
1960
1976
|
|
|
1961
1977
|
addProto(name, proto) {
|
|
@@ -1968,7 +1984,7 @@ class Peernet {
|
|
|
1968
1984
|
|
|
1969
1985
|
async addStore(name, prefix, root, isPrivate = true) {
|
|
1970
1986
|
if (name === 'block' || name === 'transaction' || name === 'chain' ||
|
|
1971
|
-
name === 'data') isPrivate = false;
|
|
1987
|
+
name === 'data' || name === 'message') isPrivate = false;
|
|
1972
1988
|
|
|
1973
1989
|
let Storage;
|
|
1974
1990
|
if (this.hasDaemon) {
|
|
@@ -1991,6 +2007,11 @@ class Peernet {
|
|
|
1991
2007
|
return this._messageHandler.prepareMessage(this.id, to, data)
|
|
1992
2008
|
}
|
|
1993
2009
|
|
|
2010
|
+
/**
|
|
2011
|
+
* @access public
|
|
2012
|
+
*
|
|
2013
|
+
* @return {Array} peerId
|
|
2014
|
+
*/
|
|
1994
2015
|
get peers() {
|
|
1995
2016
|
return [...connections.values()]
|
|
1996
2017
|
}
|
|
@@ -2039,6 +2060,7 @@ class Peernet {
|
|
|
2039
2060
|
'peernet-data': DataMessage,
|
|
2040
2061
|
'peernet-data-response': DataMessageResponse,
|
|
2041
2062
|
'peernet-ps': PsMessage,
|
|
2063
|
+
'chat-message': ChatMessage,
|
|
2042
2064
|
};
|
|
2043
2065
|
this.protos = globalThis.peernet.protos;
|
|
2044
2066
|
|
|
@@ -2391,6 +2413,35 @@ class Peernet {
|
|
|
2391
2413
|
return null
|
|
2392
2414
|
}
|
|
2393
2415
|
|
|
2416
|
+
|
|
2417
|
+
get message() {
|
|
2418
|
+
return {
|
|
2419
|
+
/**
|
|
2420
|
+
* Get content for given message hash
|
|
2421
|
+
*
|
|
2422
|
+
* @param {String} hash
|
|
2423
|
+
*/
|
|
2424
|
+
get: async (hash) => {
|
|
2425
|
+
debug(`get message ${hash}`);
|
|
2426
|
+
const message = await messageStore.has(hash);
|
|
2427
|
+
if (message) return await messageStore.get(hash)
|
|
2428
|
+
return this.requestData(hash, 'message')
|
|
2429
|
+
},
|
|
2430
|
+
/**
|
|
2431
|
+
* put message content
|
|
2432
|
+
*
|
|
2433
|
+
* @param {String} hash
|
|
2434
|
+
* @param {Buffer} message
|
|
2435
|
+
*/
|
|
2436
|
+
put: async (hash, message) => await messageStore.put(hash, message),
|
|
2437
|
+
/**
|
|
2438
|
+
* @param {String} hash
|
|
2439
|
+
* @return {Boolean}
|
|
2440
|
+
*/
|
|
2441
|
+
has: async (hash) => await messageStore.has(hash),
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2394
2445
|
get data() {
|
|
2395
2446
|
return {
|
|
2396
2447
|
/**
|
|
@@ -2481,16 +2532,16 @@ class Peernet {
|
|
|
2481
2532
|
*/
|
|
2482
2533
|
async publish(topic, data) {
|
|
2483
2534
|
// globalSub.publish(topic, data)
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
2535
|
if (!Buffer.isBuffer(topic)) topic = Buffer.from(topic);
|
|
2487
2536
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
2488
2537
|
const id = Math.random().toString(36).slice(-12);
|
|
2489
2538
|
data = new PsMessage({data, topic});
|
|
2490
2539
|
for (const peer of this.peers) {
|
|
2491
|
-
if (peer.connection._connected
|
|
2492
|
-
|
|
2493
|
-
|
|
2540
|
+
if (peer.connection._connected) {
|
|
2541
|
+
if (peer.id.toString() !== this.peerId.toString()) {
|
|
2542
|
+
const node = await this.prepareMessage(peer.id, data.encoded);
|
|
2543
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
|
2544
|
+
}
|
|
2494
2545
|
} else {
|
|
2495
2546
|
this.removePeer(peer);
|
|
2496
2547
|
}
|
|
@@ -2498,6 +2549,10 @@ class Peernet {
|
|
|
2498
2549
|
}
|
|
2499
2550
|
}
|
|
2500
2551
|
|
|
2552
|
+
createHash(data, name) {
|
|
2553
|
+
return new PeernetHash(data, {name})
|
|
2554
|
+
}
|
|
2555
|
+
|
|
2501
2556
|
/**
|
|
2502
2557
|
*
|
|
2503
2558
|
* @param {String} topic
|