@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
@@ -0,0 +1,324 @@
1
+ 'use strict';
2
+
3
+ /* socket-request-client version 1.6.1 */
4
+
5
+ class LittlePubSub {
6
+ constructor(verbose = true) {
7
+ this.subscribers = {};
8
+ this.verbose = verbose;
9
+ }
10
+ subscribe(event, handler, context) {
11
+ if (typeof context === 'undefined') {
12
+ context = handler;
13
+ }
14
+ this.subscribers[event] = this.subscribers[event] || { handlers: [], value: null};
15
+ this.subscribers[event].handlers.push(handler.bind(context));
16
+ }
17
+ unsubscribe(event, handler, context) {
18
+ if (typeof context === 'undefined') {
19
+ context = handler;
20
+ }
21
+ if (this.subscribers[event]) {
22
+ const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
23
+ this.subscribers[event].handlers.splice(index);
24
+ if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
25
+ }
26
+ }
27
+ publish(event, change) {
28
+ if (this.subscribers[event]) {
29
+ if (this.verbose || this.subscribers[event].value !== change) {
30
+ this.subscribers[event].value = change;
31
+ this.subscribers[event].handlers.forEach(handler => {
32
+ handler(change, this.subscribers[event].value);
33
+ });
34
+ }
35
+ }
36
+ }
37
+ }
38
+
39
+ var clientApi = _pubsub => {
40
+ const subscribe = (topic, cb) => {
41
+ _pubsub.subscribe(topic, cb);
42
+ };
43
+ const unsubscribe = (topic, cb) => {
44
+ _pubsub.unsubscribe(topic, cb);
45
+ };
46
+ const publish = (topic, value) => {
47
+ _pubsub.publish(topic, value);
48
+ };
49
+ const _connectionState = (state) => {
50
+ switch (state) {
51
+ case 0:
52
+ return 'connecting'
53
+ case 1:
54
+ return 'open'
55
+ case 2:
56
+ return 'closing'
57
+ case 3:
58
+ return 'closed'
59
+ }
60
+ };
61
+ const request = (client, request) => {
62
+ return new Promise((resolve, reject) => {
63
+ const state = _connectionState(client.readyState);
64
+ if (state !== 'open') return reject(`coudn't send request to ${client.id}, no open connection found.`)
65
+ request.id = Math.random().toString(36).slice(-12);
66
+ const handler = result => {
67
+ if (result && result.error) return reject(result.error)
68
+ resolve({result, id: request.id, handler});
69
+ unsubscribe(request.id, handler);
70
+ };
71
+ subscribe(request.id, handler);
72
+ send(client, request);
73
+ });
74
+ };
75
+ const send = async (client, request) => {
76
+ return client.send(JSON.stringify(request))
77
+ };
78
+ const pubsub = client => {
79
+ return {
80
+ publish: (topic = 'pubsub', value) => {
81
+ return send(client, {url: 'pubsub', params: { topic, value }})
82
+ },
83
+ subscribe: (topic = 'pubsub', cb) => {
84
+ subscribe(topic, cb);
85
+ return send(client, {url: 'pubsub', params: { topic, subscribe: true }})
86
+ },
87
+ unsubscribe: (topic = 'pubsub', cb) => {
88
+ unsubscribe(topic, cb);
89
+ return send(client, {url: 'pubsub', params: { topic, unsubscribe: true }})
90
+ },
91
+ subscribers: _pubsub.subscribers
92
+ }
93
+ };
94
+ const server = (client) => {
95
+ return {
96
+ uptime: async () => {
97
+ try {
98
+ const { result, id, handler } = await request(client, {url: 'uptime'});
99
+ unsubscribe(id, handler);
100
+ return result
101
+ } catch (e) {
102
+ throw e
103
+ }
104
+ },
105
+ ping: async () => {
106
+ try {
107
+ const now = new Date().getTime();
108
+ const { result, id, handler } = await request(client, {url: 'ping'});
109
+ unsubscribe(id, handler);
110
+ return (Number(result) - now)
111
+ } catch (e) {
112
+ throw e
113
+ }
114
+ }
115
+ }
116
+ };
117
+ const peernet = (client) => {
118
+ return {
119
+ join: async (params) => {
120
+ try {
121
+ params.join = true;
122
+ const requested = { url: 'peernet', params };
123
+ const { result, id, handler } = await request(client, requested);
124
+ unsubscribe(id, handler);
125
+ return result
126
+ } catch (e) {
127
+ throw e
128
+ }
129
+ },
130
+ leave: async (params) => {
131
+ try {
132
+ params.join = false;
133
+ const requested = { url: 'peernet', params };
134
+ const { result, id, handler } = await request(client, requested);
135
+ unsubscribe(id, handler);
136
+ return result
137
+ } catch (e) {
138
+ throw e
139
+ }
140
+ }
141
+ }
142
+ };
143
+ return { send, request, pubsub, server, subscribe, unsubscribe, publish, peernet }
144
+ };
145
+
146
+ if (!globalThis.PubSub) globalThis.PubSub = LittlePubSub;
147
+ if (!globalThis.pubsub) globalThis.pubsub = new LittlePubSub({verbose: false});
148
+ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry: false }) => {
149
+ const { retry } = options;
150
+ const api = clientApi(pubsub);
151
+ let tries = 0;
152
+ const onerror = error => {
153
+ if (pubsub.subscribers['error']) {
154
+ pubsub.publish('error', error);
155
+ } else {
156
+ console.error(error);
157
+ }
158
+ };
159
+ const onmessage = message => {
160
+ const {value, url, status, id} = JSON.parse(message.data.toString());
161
+ const publisher = id ? id : url;
162
+ if (status === 200) {
163
+ pubsub.publish(publisher, value);
164
+ } else {
165
+ pubsub.publish(publisher, {error: value});
166
+ }
167
+ };
168
+ const clientConnection = client => {
169
+ const startTime = new Date().getTime();
170
+ return {
171
+ client,
172
+ request: async req => {
173
+ const { result, id, handler } = await api.request(client, req);
174
+ pubsub.unsubscribe(id, handler);
175
+ return result
176
+ },
177
+ send: req => api.send(client, req),
178
+ subscribe: api.subscribe,
179
+ unsubscribe: api.unsubscribe,
180
+ subscribers: api.subscribers,
181
+ publish: api.publish,
182
+ pubsub: api.pubsub(client),
183
+ uptime: () => {
184
+ const now = new Date().getTime();
185
+ return (now - startTime)
186
+ },
187
+ peernet: api.peernet(client),
188
+ server: api.server(client),
189
+ close: exit => {
190
+ client.onclose = message => {
191
+ if (exit) process.exit();
192
+ };
193
+ client.close();
194
+ }
195
+ }
196
+ };
197
+ return new Promise((resolve, reject) => {
198
+ const init = () => {
199
+ let ws;
200
+ if (typeof process === 'object' && !globalThis.WebSocket) {
201
+ ws = require('websocket').w3cwebsocket;
202
+ } else {
203
+ ws = WebSocket;
204
+ }
205
+ const client = new ws(url, protocols);
206
+ client.onmessage = onmessage;
207
+ client.onerror = onerror;
208
+ client.onopen = () => {
209
+ tries = 0;
210
+ resolve(clientConnection(client));
211
+ };
212
+ client.onclose = message => {
213
+ tries++;
214
+ if (!retry) return reject(options)
215
+ if (tries > 5) {
216
+ console.log(`${protocols} Client Closed`);
217
+ console.error(`could not connect to - ${url}/`);
218
+ return resolve(clientConnection(client))
219
+ }
220
+ if (message.code === 1006) {
221
+ console.log('Retrying in 10 seconds');
222
+ setTimeout(() => {
223
+ return init();
224
+ }, retry);
225
+ }
226
+ };
227
+ };
228
+ return init();
229
+ });
230
+ };
231
+
232
+ class HttpClientApi$1 {
233
+ constructor(config = {}) {
234
+ if (!config.apiPath) config.apiPath = 'api';
235
+
236
+ const address = `ws://${config.host}:${config.port}`;
237
+
238
+ this.apiUrl = (url) => `${address}/${url}`;
239
+ return (async () => {
240
+ this.client = await socketRequestClient(address, config.protocol, {pubsub: config.pubsub, retry: 3000});
241
+ return this
242
+ })()
243
+ }
244
+
245
+ async get(url, obj) {
246
+ const headers = {};
247
+ let body = null;
248
+ let method = 'GET';
249
+ if (obj) {
250
+ method = 'POST';
251
+ headers['Content-Type'] = 'application/json';
252
+ body = JSON.stringify(obj);
253
+ }
254
+ let response = await this.client.request(url, {headers, body, method});
255
+ const type = response.headers.get('content-type').split(';')[0];
256
+ if (type==='application/json') response = await response.json();
257
+ return response
258
+ }
259
+
260
+ async put(url, obj) {
261
+ const headers = {};
262
+ let body = {};
263
+ if (obj) {
264
+ headers['Content-Type'] = 'application/json';
265
+ body = JSON.stringify(obj);
266
+ }
267
+
268
+ let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body});
269
+ const type = response.headers.get('content-type').split(';')[0];
270
+ if (type==='application/json') response = await response.json();
271
+ return response
272
+ }
273
+ }
274
+
275
+ class HttpClientApi extends HttpClientApi$1 {
276
+ constructor(config = {}) {
277
+ config.apiPath = 'api';
278
+ return (async () => {
279
+ await super(config);
280
+
281
+ this.properties = {
282
+ wallet: 'get',
283
+ version: 'get',
284
+ addresses: 'get',
285
+ config: 'get',
286
+ account: 'get',
287
+ accounts: 'get',
288
+ transaction: 'any',
289
+ transactions: 'get',
290
+ block: 'get',
291
+ blocks: 'get',
292
+ };
293
+ this.keys = Object.keys(this.properties);
294
+ return this
295
+ })()
296
+ }
297
+
298
+ async request(url, data) {
299
+ return await this.client.request({url, params: data})
300
+ }
301
+
302
+ async ready() {
303
+ return await this.request('ready')
304
+ }
305
+
306
+ async version() {
307
+ return await this.request('version')
308
+ }
309
+
310
+ async account(index) {
311
+ return await this.request('account', {index})
312
+ }
313
+ }
314
+
315
+ var client = (config = {}) => {
316
+ if (typeof config !== 'object') config = {};
317
+ if (!config.protocol) config.protocol = 'peernet-v0.1.0';
318
+ if (!config.port) config.port = 1000;
319
+ if (!config.host) config.host = '127.0.0.1';
320
+
321
+ return new HttpClientApi(config)
322
+ };
323
+
324
+ exports["default"] = client;