@leofcoin/peernet 0.10.8 → 0.11.2

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.
Files changed (78) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +49 -49
  3. package/coverage/lcov-report/block-navigation.js +8 -0
  4. package/coverage/lcov-report/codec-format-interface.js.html +224 -120
  5. package/coverage/lcov-report/dht-response.js.html +44 -39
  6. package/coverage/lcov-report/index.html +39 -64
  7. package/coverage/lcov-report/sorter.js +26 -0
  8. package/coverage/lcov.info +164 -424
  9. package/dist/browser/326.peernet.js +29 -0
  10. package/dist/browser/peernet.js +84258 -95769
  11. package/dist/commonjs/client-1a1f75e6.js +324 -0
  12. package/dist/commonjs/{codec-6367213c.js → codec-8c8c652f.js} +198 -188
  13. package/dist/commonjs/codec-format-interface.js +169 -152
  14. package/dist/commonjs/codec.js +4 -4
  15. package/dist/commonjs/dht-response.js +13 -13
  16. package/dist/commonjs/dht.js +24 -24
  17. package/dist/commonjs/hash.js +151 -141
  18. package/dist/commonjs/{http-a94c5a81.js → http-4bc6caeb.js} +19 -15
  19. package/dist/commonjs/peernet-message.js +15 -15
  20. package/dist/commonjs/peernet.js +1901 -1794
  21. package/dist/commonjs/request.js +13 -13
  22. package/dist/commonjs/response.js +13 -13
  23. package/dist/module/peernet.js +2462 -2348
  24. package/index.html +5 -7
  25. package/package.json +22 -14
  26. package/rollup.config.js +33 -5
  27. package/rollup0.config.js +7 -0
  28. package/src/client.js +75 -75
  29. package/src/codec/codec-format-interface.js +172 -155
  30. package/src/codec/codec.js +124 -114
  31. package/src/codec/codecs.js +79 -79
  32. package/src/dht/dht.js +121 -121
  33. package/src/discovery/peer-discovery.js +75 -75
  34. package/src/handlers/message.js +50 -52
  35. package/src/hash/hash.js +155 -145
  36. package/src/http/client/http-client.js +44 -44
  37. package/src/messages/chat-message.js +14 -14
  38. package/src/messages/data-response.js +14 -14
  39. package/src/messages/data.js +18 -18
  40. package/src/messages/dht-response.js +14 -15
  41. package/src/messages/dht.js +25 -25
  42. package/src/messages/peer-response.js +14 -14
  43. package/src/messages/peer.js +14 -14
  44. package/src/messages/peernet-message.js +14 -14
  45. package/src/messages/ps.js +14 -14
  46. package/src/messages/request.js +14 -14
  47. package/src/messages/response.js +14 -14
  48. package/src/peer.js +67 -67
  49. package/src/peernet.js +614 -697
  50. package/src/proto/chat-message.proto.js +7 -7
  51. package/src/proto/peernet.proto.js +2 -2
  52. package/src/utils/utils.js +78 -78
  53. package/test/codec.js +3 -2
  54. package/test/messages.js +7 -4
  55. package/test.js +11 -4
  56. package/webpack.config.js +41 -0
  57. package/coverage/lcov-report/codec.js.html +0 -677
  58. package/coverage/lcov-report/hash.js.html +0 -551
  59. package/debug.log +0 -3
  60. package/dist/browser/peernet.js.tmp-browserify-14074318104595318069 +0 -0
  61. package/dist/browser/peernet.js.tmp-browserify-45407634493269122267 +0 -0
  62. package/dist/browser/peernet.js.tmp-browserify-53722389064799025427 +0 -0
  63. package/dist/browser/peernet.js.tmp-browserify-96323030449218949300 +0 -0
  64. package/dist/codec/codec-format-interface.js +0 -433
  65. package/dist/codec/codec.js +0 -199
  66. package/dist/commonjs/codec-73adfc0f.js +0 -205
  67. package/dist/commonjs/http-2c603501.js +0 -324
  68. package/dist/commonjs/http-42a6e555.js +0 -324
  69. package/dist/commonjs/http-43f4fafe.js +0 -324
  70. package/dist/commonjs/peernet-message-b6925673.js +0 -32
  71. package/dist/hash/hash.js +0 -340
  72. package/dist/messages/dht-response.js +0 -454
  73. package/dist/messages/dht.js +0 -453
  74. package/dist/messages/peernet.js +0 -456
  75. package/dist/module/http-273664bd.js +0 -317
  76. package/dist/module/http-8fe3c0d7.js +0 -317
  77. package/dist/module/http-c780c991.js +0 -317
  78. package/dist/module/http-f13e0d77.js +0 -317
@@ -2,10 +2,10 @@
2
2
 
3
3
  var createKeccakHash = require('keccak');
4
4
  var varint = require('varint');
5
- var bs32 = require('bs32');
6
- var bs58 = require('bs58');
7
- var isHex = require('is-hex');
8
- var codec = require('./codec-6367213c.js');
5
+ var bs32 = require('@vandeurenglenn/base32');
6
+ var bs58 = require('@vandeurenglenn/base58');
7
+ var isHex = require('@vandeurenglenn/is-hex');
8
+ var codec = require('./codec-8c8c652f.js');
9
9
 
10
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
11
 
@@ -15,143 +15,153 @@ var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
15
15
  var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
16
16
  var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
17
17
 
18
- class PeernetHash {
19
- constructor(buffer, options = {}) {
20
- if (options.name) this.name = options.name;
21
- else this.name = 'disco-hash';
22
- if (options.codecs) this.codecs = options.codecs;
23
- if (buffer) {
24
- if (Buffer.isBuffer(buffer)) {
25
- this.discoCodec = new codec.PeernetCodec(buffer, this.codecs);
26
- const name = this.discoCodec.name;
27
-
28
- if (name) {
29
- this.name = name;
30
- this.decode(buffer);
31
- } else {
32
- this.encode(buffer);
33
- }
34
- }
35
-
36
- if (typeof buffer === 'string') {
37
- if (isHex__default["default"](buffer)) this.fromHex(buffer);
38
- if (bs32__default["default"].test(buffer)) this.fromBs32(buffer);
39
- else this.fromBs58(buffer);
40
- } else if (typeof buffer === 'object') this.fromJSON(buffer);
41
- }
42
- }
43
-
44
- get prefix() {
45
- return Buffer.concat([this.discoCodec.codecBuffer, this.length])
46
- }
47
-
48
- get length() {
49
- return Buffer.from(varint__default["default"].encode(this.size))
50
- }
51
-
52
- get buffer() {
53
- return this.hash
54
- }
55
-
56
- toHex() {
57
- return this.hash.toString('hex')
58
- }
59
-
60
- fromHex(hex) {
61
- return this.decode(Buffer.from(hex, 'hex'))
62
- }
63
-
64
- fromJSON(json) {
65
- return this.encode(Buffer.from(JSON.stringify(json)))
66
- }
67
-
68
- toBs32() {
69
- return bs32__default["default"].encode(this.hash)
70
- }
71
-
72
- fromBs32(bs) {
73
- return this.decode(bs32__default["default"].decode(bs))
74
- }
75
-
76
- toBs58() {
77
- return bs58__default["default"].encode(this.hash)
78
- }
79
-
80
- fromBs58(bs) {
81
- return this.decode(bs58__default["default"].decode(bs))
82
- }
83
-
84
- toString(encoding = 'utf8') {
85
- return this.hash.toString(encoding)
86
- }
87
-
88
- encode(buffer, name) {
89
- if (!this.name && name) this.name = name;
90
- if (!buffer) buffer = this.buffer;
91
- this.discoCodec = new codec.PeernetCodec(this.name, this.codecs);
92
- this.discoCodec.fromName(this.name);
93
- let hashAlg = this.discoCodec.hashAlg;
94
- if (hashAlg.includes('dbl')) {
95
- hashAlg = hashAlg.replace('dbl-', '');
96
- buffer = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
97
- }
98
- this.digest = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
99
- this.size = this.digest.length;
100
-
101
- this.codec = this.discoCodec.encode();
102
- this.codec = this.discoCodec.codecBuffer;
103
- this.hash = Buffer.concat([
104
- this.prefix,
105
- this.digest,
106
- ]);
107
-
108
- return this.hash
109
- }
110
-
111
- validate(buffer) {
112
- if (Buffer.isBuffer(buffer)) {
113
- const codec = varint__default["default"].decode(buffer);
114
- if (this.codecs[codec]) {
115
- this.decode(buffer);
116
- } else {
117
- this.encode(buffer);
118
- }
119
- }
120
- if (typeof buffer === 'string') {
121
- if (isHex__default["default"](buffer)) this.fromHex(buffer);
122
- if (bs32__default["default"].test(buffer)) this.fromBs32(buffer);
123
- }
124
- if (typeof buffer === 'object') this.fromJSON(buffer);
125
- }
126
-
127
- decode(buffer) {
128
- this.hash = buffer;
129
- const codec$1 = varint__default["default"].decode(buffer);
130
-
131
- this.discoCodec = new codec.PeernetCodec(codec$1, this.codecs);
132
- // TODO: validate codec
133
- buffer = buffer.slice(varint__default["default"].decode.bytes);
134
- this.size = varint__default["default"].decode(buffer);
135
- this.digest = buffer.slice(varint__default["default"].decode.bytes);
136
- if (this.digest.length !== this.size) {
137
- throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
138
- }
139
-
140
- // const discoCodec = new Codec(codec, this.codecs)
141
-
142
- this.name = this.discoCodec.name;
143
-
144
-
145
- this.size = this.digest.length;
146
-
147
- return {
148
- codec: this.codec,
149
- name: this.name,
150
- size: this.size,
151
- length: this.length,
152
- digest: this.digest,
153
- }
154
- }
18
+ class PeernetHash {
19
+ constructor(buffer, options = {}) {
20
+ if (options.name) this.name = options.name;
21
+ else this.name = 'disco-hash';
22
+ if (options.codecs) this.codecs = options.codecs;
23
+ if (buffer) {
24
+ if (Buffer.isBuffer(buffer)) {
25
+ this.discoCodec = new codec.PeernetCodec(buffer, this.codecs);
26
+ const name = this.discoCodec.name;
27
+
28
+ if (name) {
29
+ this.name = name;
30
+ this.decode(buffer);
31
+ } else {
32
+ this.encode(buffer);
33
+ }
34
+ }
35
+
36
+ if (typeof buffer === 'string') {
37
+ if (isHex__default["default"](buffer)) this.fromHex(buffer);
38
+ if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
39
+ else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
40
+ else throw new Error(`unsupported string ${buffer}`)
41
+ } else if (typeof buffer === 'object') this.fromJSON(buffer);
42
+ }
43
+ }
44
+
45
+ get prefix() {
46
+ const length = this.length;
47
+ const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
48
+ for (let i = 0; i < this.discoCodec.codecBuffer.length; i++) {
49
+ uint8Array[i] = this.discoCodec.codecBuffer[i];
50
+ }
51
+
52
+ for (let i = uint8Array.length - 1; i < length.length; i++) {
53
+ uint8Array[i] = length[i];
54
+ }
55
+ return uint8Array
56
+ }
57
+
58
+ get length() {
59
+ return varint__default["default"].encode(this.size)
60
+ }
61
+
62
+ get buffer() {
63
+ return this.hash
64
+ }
65
+
66
+ toHex() {
67
+ return this.hash.toString('hex')
68
+ }
69
+
70
+ fromHex(hex) {
71
+ return this.decode(Buffer.from(hex, 'hex'))
72
+ }
73
+
74
+ fromJSON(json) {
75
+ return this.encode(Buffer.from(JSON.stringify(json)))
76
+ }
77
+
78
+ toBs32() {
79
+ return bs32__default["default"].encode(this.hash)
80
+ }
81
+
82
+ fromBs32(bs) {
83
+ return this.decode(bs32__default["default"].decode(bs))
84
+ }
85
+
86
+ toBs58() {
87
+ return bs58__default["default"].encode(this.hash)
88
+ }
89
+
90
+ fromBs58(bs) {
91
+ return this.decode(bs58__default["default"].decode(bs))
92
+ }
93
+
94
+ toString(encoding = 'utf8') {
95
+ return this.hash.toString(encoding)
96
+ }
97
+
98
+ encode(buffer, name) {
99
+ if (!this.name && name) this.name = name;
100
+ if (!buffer) buffer = this.buffer;
101
+ this.discoCodec = new codec.PeernetCodec(this.name, this.codecs);
102
+ this.discoCodec.fromName(this.name);
103
+ let hashAlg = this.discoCodec.hashAlg;
104
+ if (hashAlg.includes('dbl')) {
105
+ hashAlg = hashAlg.replace('dbl-', '');
106
+ buffer = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
107
+ }
108
+ this.digest = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
109
+ this.size = this.digest.length;
110
+
111
+ this.codec = this.discoCodec.encode();
112
+ this.codec = this.discoCodec.codecBuffer;
113
+ this.hash = Buffer.concat([
114
+ this.prefix,
115
+ this.digest,
116
+ ]);
117
+
118
+ return this.hash
119
+ }
120
+
121
+ validate(buffer) {
122
+ if (Buffer.isBuffer(buffer)) {
123
+ const codec = varint__default["default"].decode(buffer);
124
+ if (this.codecs[codec]) {
125
+ this.decode(buffer);
126
+ } else {
127
+ this.encode(buffer);
128
+ }
129
+ }
130
+ if (typeof buffer === 'string') {
131
+ if (isHex__default["default"](buffer)) this.fromHex(buffer);
132
+ if (bs32__default["default"].test(buffer)) this.fromBs32(buffer);
133
+ }
134
+ if (typeof buffer === 'object') this.fromJSON(buffer);
135
+ }
136
+
137
+ decode(buffer) {
138
+ this.hash = buffer;
139
+ const codec$1 = varint__default["default"].decode(buffer);
140
+
141
+ this.discoCodec = new codec.PeernetCodec(codec$1, this.codecs);
142
+ // TODO: validate codec
143
+ buffer = buffer.slice(varint__default["default"].decode.bytes);
144
+ this.size = varint__default["default"].decode(buffer);
145
+ this.digest = buffer.slice(varint__default["default"].decode.bytes);
146
+ if (this.digest.length !== this.size) {
147
+ throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
148
+ }
149
+
150
+ // const discoCodec = new Codec(codec, this.codecs)
151
+
152
+ this.name = this.discoCodec.name;
153
+
154
+
155
+ this.size = this.digest.length;
156
+
157
+ return {
158
+ codec: this.codec,
159
+ name: this.name,
160
+ size: this.size,
161
+ length: this.length,
162
+ digest: this.digest,
163
+ }
164
+ }
155
165
  }
156
166
 
157
167
  module.exports = PeernetHash;
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
 
3
- var Pubsub = require('@vandeurenglenn/little-pubsub');
4
3
  var websocket = require('websocket');
4
+ var PubSub = require('@vandeurenglenn/little-pubsub');
5
5
  var http$1 = require('http');
6
6
  var Koa = require('koa');
7
7
 
8
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
9
 
10
- var Pubsub__default = /*#__PURE__*/_interopDefaultLegacy(Pubsub);
10
+ var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
11
11
  var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
12
12
 
13
- var version = "0.7.18";
13
+ var version = "0.11.1";
14
14
 
15
15
  var api$1 = {
16
16
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -173,12 +173,15 @@ var socketResponse = (connection, url, id, customEvent) => {
173
173
  }
174
174
  };
175
175
 
176
+ if (!globalThis.PubSub) globalThis.PubSub = PubSub__default["default"];
177
+ if (!globalThis.pubsub) globalThis.pubsub = new PubSub__default["default"]();
178
+
176
179
  const socketRequestServer = (options, routes = {}) => {
177
180
  // if (!routes && !routes.port && options) routes = options;
178
181
  // else if (!options && !routes) return console.error('no routes defined');
179
182
 
180
183
  let {httpServer, httpsServer, port, protocol, credentials, origin, pubsub } = options;
181
- if (!pubsub) pubsub = new Pubsub__default['default']({verbose: false});
184
+ if (!pubsub) pubsub = new PubSub__default["default"]({verbose: false});
182
185
  if (!port) port = 6000;
183
186
  const connections = [];
184
187
  let connection;
@@ -195,12 +198,12 @@ const socketRequestServer = (options, routes = {}) => {
195
198
  response = params;
196
199
  params = {};
197
200
  }
198
-
201
+
199
202
  if (!params.topic) params.topic = 'pubsub';
200
-
203
+
201
204
  const topic = params.topic;
202
205
  delete params.topic;
203
-
206
+
204
207
  if (params.subscribe) {
205
208
  pubsub.subscribe(topic, message => {
206
209
  response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
@@ -225,7 +228,7 @@ const socketRequestServer = (options, routes = {}) => {
225
228
  pubsub.publish(topic, params.value);
226
229
  response.send('ok', 200);
227
230
  };
228
-
231
+
229
232
  }
230
233
  globalThis.peerMap = new Map();
231
234
  if (!routes.peernet) {
@@ -282,10 +285,10 @@ const socketRequestServer = (options, routes = {}) => {
282
285
  // ignore api when customEvent is defined
283
286
  if (customEvent) return;
284
287
  if (routes[url]) {
285
- if (!params) return routes[url](socketResponse(connection, url, id));
286
- return routes[url](params, socketResponse(connection, url, id));
288
+ if (!params) return routes[url](socketResponse(connection, url, id), connections);
289
+ return routes[url](params, socketResponse(connection, url, id), connections);
287
290
  }
288
- else return socketResponse(connection, url, id).error(`nop handler found for '${message.url}'`);
291
+ else return socketResponse(connection, url, id).error(`no handler found for '${message.url}'`);
289
292
  };
290
293
 
291
294
  connection.on('message', routeHandler);
@@ -295,7 +298,8 @@ const socketRequestServer = (options, routes = {}) => {
295
298
  close: () => socketServer.shutDown(),
296
299
  connections
297
300
  };
298
- };
301
+ };
302
+ var server = socketRequestServer;
299
303
 
300
304
  var http = (config = {}) => {
301
305
  if (typeof config !== 'object') config = {};
@@ -303,7 +307,7 @@ var http = (config = {}) => {
303
307
  if (!config.port) config.port = 2000;
304
308
  if (!config.host) config.host = '127.0.0.1';
305
309
 
306
- const app = new Koa__default['default']();
310
+ const app = new Koa__default["default"]();
307
311
 
308
312
  app.use(async (ctx) => {
309
313
  const url = ctx.url.split('/api/')[1];
@@ -318,7 +322,7 @@ var http = (config = {}) => {
318
322
  console.log(`listening on ${config.port}`);
319
323
  });
320
324
 
321
- return socketRequestServer(config, api$1)
325
+ return server(config, api$1)
322
326
  };
323
327
 
324
- exports.default = http;
328
+ exports["default"] = http;
@@ -2,10 +2,10 @@
2
2
 
3
3
  var protons = require('protons');
4
4
  var codecFormatInterface = require('./codec-format-interface.js');
5
- require('bs32');
6
- require('bs58');
7
- require('is-hex');
8
- require('./codec-6367213c.js');
5
+ require('@vandeurenglenn/base32');
6
+ require('@vandeurenglenn/base58');
7
+ require('@vandeurenglenn/is-hex');
8
+ require('./codec-8c8c652f.js');
9
9
  require('varint');
10
10
  require('./hash.js');
11
11
  require('keccak');
@@ -19,20 +19,20 @@ var proto = `
19
19
  message PeernetMessage {
20
20
  required bytes data = 1;
21
21
  required bytes signature = 2;
22
- optional bytes from = 3;
23
- optional bytes to = 4;
22
+ optional string from = 3;
23
+ optional string to = 4;
24
24
  optional string id = 5;
25
25
  }`;
26
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
- }
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
36
  }
37
37
 
38
38
  module.exports = PeernetMessage;