@leofcoin/peernet 0.10.6 → 0.11.0

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 (75) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +49 -49
  3. package/dist/browser/peernet.js +101814 -92947
  4. package/dist/commonjs/client-bd0caeb7.js +324 -0
  5. package/dist/commonjs/{codec-6367213c.js → codec-4a768e5e.js} +99 -89
  6. package/dist/commonjs/codec-format-interface.js +41 -24
  7. package/dist/commonjs/codec.js +3 -3
  8. package/dist/commonjs/dht-response.js +3 -3
  9. package/dist/commonjs/dht.js +23 -23
  10. package/dist/commonjs/hash.js +17 -7
  11. package/dist/commonjs/{http-42a6e555.js → http-2b0735ef.js} +19 -15
  12. package/dist/commonjs/peernet-message.js +12 -12
  13. package/dist/commonjs/peernet.js +1164 -966
  14. package/dist/commonjs/request.js +12 -12
  15. package/dist/commonjs/response.js +12 -12
  16. package/dist/module/peernet.js +1325 -1120
  17. package/index.html +4 -6
  18. package/package.json +31 -7
  19. package/rollup.config.js +33 -5
  20. package/rollup0.config.js +7 -0
  21. package/src/codec/codec-format-interface.js +40 -23
  22. package/src/codec/codec.js +21 -11
  23. package/src/codec/codecs.js +79 -79
  24. package/src/handlers/message.js +52 -52
  25. package/src/hash/hash.js +16 -6
  26. package/src/http/client/http-client.js +1 -1
  27. package/src/messages/chat-message.js +14 -14
  28. package/src/messages/data-response.js +14 -14
  29. package/src/messages/data.js +18 -18
  30. package/src/messages/dht-response.js +0 -1
  31. package/src/messages/dht.js +25 -25
  32. package/src/messages/peer-response.js +14 -14
  33. package/src/messages/peer.js +14 -14
  34. package/src/messages/peernet-message.js +14 -14
  35. package/src/messages/ps.js +14 -14
  36. package/src/messages/request.js +14 -14
  37. package/src/messages/response.js +14 -14
  38. package/src/peernet.js +31 -116
  39. package/test/codec.js +3 -2
  40. package/test/messages.js +7 -4
  41. package/test.js +11 -4
  42. package/webpack.config.js +35 -0
  43. package/coverage/lcov-report/base.css +0 -224
  44. package/coverage/lcov-report/block-navigation.js +0 -79
  45. package/coverage/lcov-report/codec-format-interface.js.html +0 -533
  46. package/coverage/lcov-report/codec.js.html +0 -677
  47. package/coverage/lcov-report/dht-response.js.html +0 -188
  48. package/coverage/lcov-report/favicon.png +0 -0
  49. package/coverage/lcov-report/hash.js.html +0 -551
  50. package/coverage/lcov-report/index.html +0 -156
  51. package/coverage/lcov-report/prettify.css +0 -1
  52. package/coverage/lcov-report/prettify.js +0 -2
  53. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  54. package/coverage/lcov-report/sorter.js +0 -170
  55. package/coverage/lcov.info +0 -459
  56. package/debug.log +0 -3
  57. package/dist/browser/peernet.js.tmp-browserify-14074318104595318069 +0 -0
  58. package/dist/browser/peernet.js.tmp-browserify-45407634493269122267 +0 -0
  59. package/dist/browser/peernet.js.tmp-browserify-53722389064799025427 +0 -0
  60. package/dist/browser/peernet.js.tmp-browserify-96323030449218949300 +0 -0
  61. package/dist/codec/codec-format-interface.js +0 -433
  62. package/dist/codec/codec.js +0 -199
  63. package/dist/commonjs/codec-73adfc0f.js +0 -205
  64. package/dist/commonjs/http-2c603501.js +0 -324
  65. package/dist/commonjs/http-43f4fafe.js +0 -324
  66. package/dist/commonjs/http-a94c5a81.js +0 -324
  67. package/dist/commonjs/peernet-message-b6925673.js +0 -32
  68. package/dist/hash/hash.js +0 -340
  69. package/dist/messages/dht-response.js +0 -454
  70. package/dist/messages/dht.js +0 -453
  71. package/dist/messages/peernet.js +0 -456
  72. package/dist/module/http-273664bd.js +0 -317
  73. package/dist/module/http-8fe3c0d7.js +0 -317
  74. package/dist/module/http-c780c991.js +0 -317
  75. package/dist/module/http-f13e0d77.js +0 -317
@@ -1,317 +0,0 @@
1
- import Pubsub from '@vandeurenglenn/little-pubsub';
2
- import { server } from 'websocket';
3
- import { createServer } from 'http';
4
- import Koa from 'koa';
5
-
6
- var version = "0.7.19";
7
-
8
- var api$1 = {
9
- version: ({send}) => send({client: '@peernet/api/http', version}),
10
- ready: ({send}) => {
11
- if (globalThis.states.ready) send(true);
12
- else pubsub.subscribe('ready', () => send(true));
13
- },
14
- storage: async (params, {send, error}) => {
15
- console.log(params);
16
- const {name, root, key, value, method} = params;
17
- try {
18
- if (name && root) {
19
- globalThis[name] = globalThis[name] || await new LeofcoinStorage(name, root);
20
- } else {
21
- return error('Expected name & root')
22
- }
23
- if (method === 'put') {
24
- await globalThis[name].put(key, value);
25
- return send('ok')
26
- }
27
- if (method === 'remove') {
28
- await globalThis[name].remove(key, value);
29
- return send('ok')
30
- }
31
- value = await globalThis[name].get(key);
32
- return send(value)
33
- } catch (e) {
34
- return error(e)
35
- }
36
- },
37
- getConfig: async (params, {send, error}) => {
38
- try {
39
- const config = await api.getConfig(params);
40
- send(config);
41
- } catch (e) {
42
- error(e);
43
- }
44
- },
45
- setMinerConfig: async (params, {send, error}) => {
46
- try {
47
- await api.setMinerConfig(params);
48
- send('ok');
49
- } catch (e) {
50
- error(e);
51
- }
52
- },
53
- getMinerConfig: async ({send, error}) => {
54
- try {
55
- const config = await api.getMinerConfig();
56
- send(config);
57
- } catch (e) {
58
- error(e);
59
- }
60
- },
61
- wallet: async ({send}) => {
62
- const wallet = await walletStore.get();
63
- send(wallet);
64
- },
65
- addresses: async ({send, error}) => {
66
- try {
67
- const adresses = await api.addresses();
68
- send(adresses);
69
- } catch (e) {
70
- error(e);
71
- }
72
- },
73
- accountNames: async (params, {send, error}) => {
74
- try {
75
- const adresses = await api.accountNames(params.index);
76
- send(adresses);
77
- } catch (e) {
78
- error(e);
79
- }
80
- },
81
- accounts: async ({send}) => {
82
- const accounts = await accountStore.get();
83
- send(accounts);
84
- },
85
- account: async (params, {send}) => {
86
- const account = await accountStore.get(params);
87
- send(account);
88
- },
89
- balance: async (params, {send, error}) => {
90
- console.log('balance');
91
- try {
92
- console.log(await api.getBalanceForAddress(params.address));
93
- const value = await api.getBalanceForAddress(params.address);
94
- send(value);
95
- } catch (e) {
96
- console.log(e);
97
- error(e);
98
- }
99
- },
100
- balanceAfter: async (params, {send, error}) => {
101
- try {
102
- const value = await api.getBalanceForAddressAfter(params.address, params.index);
103
- send(value);
104
- } catch (e) {
105
- error(e);
106
- }
107
- },
108
- mine: async (params, {send, error}) => {
109
- api.mine(params);
110
- send('ok');
111
- },
112
- lastBlock: async ({send, error}) => {
113
- try {
114
- const value = await api.lastBlock();
115
- send(value);
116
- } catch (e) {
117
- error(e);
118
- }
119
- },
120
- };
121
-
122
- const fullLog = text => {
123
- return console.log(`${new Date()}: ${text}`)
124
- };
125
-
126
- const originIsAllowed = (requestOrigin, origin) => {
127
- // put logic here to detect whether the specified origin is allowed.
128
- if (origin && requestOrigin !== origin) return false;
129
- return true;
130
- };
131
-
132
- /**
133
- * @module socketResponse
134
- *
135
- * @param {object} connection socket connection
136
- * @param {string} route the route to handle
137
- */
138
- var socketConnection = (request, protocol, origin) => {
139
- if (origin && !originIsAllowed(request.origin, origin)) {
140
- // Make sure we only accept requests from an allowed origin
141
- request.reject();
142
- fullLog(`Connection from origin ${request.origin} rejected.`);
143
- return;
144
- }
145
- // console.log(request);
146
- const connection = request.accept(protocol, request.origin);
147
- fullLog(`Connection accepted @${protocol}`);
148
- return connection;
149
- };
150
-
151
- /**
152
- * @module socketResponse
153
- *
154
- * @param {object} connection socket connection
155
- * @param {string} url the request url
156
- */
157
- var socketResponse = (connection, url, id, customEvent) => {
158
- const send = (data = 'ok', status = 200) => connection.send(
159
- JSON.stringify({url, status, value: data, id, customEvent})
160
- );
161
- const error = data => connection.send(JSON.stringify({url, value: data}));
162
- return {
163
- connection,
164
- send,
165
- error
166
- }
167
- };
168
-
169
- const socketRequestServer = (options, routes = {}) => {
170
- // if (!routes && !routes.port && options) routes = options;
171
- // else if (!options && !routes) return console.error('no routes defined');
172
-
173
- let {httpServer, httpsServer, port, protocol, credentials, origin, pubsub } = options;
174
- if (!pubsub) pubsub = new Pubsub({verbose: false});
175
- if (!port) port = 6000;
176
- const connections = [];
177
- let connection;
178
- const startTime = new Date().getTime();
179
- // default routes
180
- if (!routes.ping) routes.ping = (response) => response.send(new Date().getTime());
181
- if (!routes.uptime) routes.uptime = (response) => {
182
- const now = new Date().getTime();
183
- response.send(now - startTime);
184
- };
185
- if (!routes.pubsub) {
186
- routes.pubsub = (params, response) => {
187
- if (!response) {
188
- response = params;
189
- params = {};
190
- }
191
-
192
- if (!params.topic) params.topic = 'pubsub';
193
-
194
- const topic = params.topic;
195
- delete params.topic;
196
-
197
- if (params.subscribe) {
198
- pubsub.subscribe(topic, message => {
199
- response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
200
- });
201
- response.send('ok', 200);
202
- } else if (params.unsubscribe) {
203
- pubsub.unsubscribe(topic, message => {
204
- response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
205
- });
206
- for (const connection of connections) {
207
- if (connection !== response.connection) connection.send(JSON.stringify({url: topic, status: 200, value: params}));
208
- }
209
- response.send('ok', 200);
210
- }
211
- else if (params.value !== undefined)
212
- // should only be send raw to stars
213
- // for (const connection of connections) {
214
- // if (connection !== response.connection) connection.send(JSON.stringify({
215
- // url: topic, status: 200, value: params
216
- // }));
217
- // }
218
- pubsub.publish(topic, params.value);
219
- response.send('ok', 200);
220
- };
221
-
222
- }
223
- globalThis.peerMap = new Map();
224
- if (!routes.peernet) {
225
- routes.peernet = (params, response) => {
226
- if (params.join) {
227
- peerMap.set(params.peerId, params.address);
228
- response.send([...peerMap.values()]);
229
- for (const connection of connections) {
230
- if (connection !== response.connection)
231
- socketResponse(connection, 'peernet', 'peernet').send({discovered: params.address});
232
- }
233
- return
234
- }
235
- if (!params.join) {
236
- peerMap.delete(params.peerId);
237
- return response.send()
238
- }
239
- };
240
- }
241
- // if (!protocol) protocol = 'echo-protocol';
242
- if (!httpServer && !httpsServer) {
243
- const { createServer } = credentials ? require('https') : require('http');
244
- if (credentials) httpServer = createServer(credentials);
245
- else httpServer = createServer();
246
-
247
- httpServer.listen(port, () => {
248
- console.log(`listening on ${port}`);
249
- });
250
- }
251
-
252
- const socketServer = new server({
253
- httpServer,
254
- autoAcceptConnections: false
255
- });
256
-
257
- socketServer.on('request', request => {
258
-
259
- connection = socketConnection(request, protocol, origin);
260
- connections.push(connection);
261
-
262
- const routeHandler = message => {
263
- let data;
264
- if (message.type) {
265
- switch (message.type) {
266
- case 'binary':
267
- data = message.binaryData.toString();
268
- break;
269
- case 'utf8':
270
- data = message.utf8Data;
271
- break;
272
- }
273
- }
274
- const { route, params, url, id, customEvent } = JSON.parse(data);
275
- // ignore api when customEvent is defined
276
- if (customEvent) return;
277
- if (routes[url]) {
278
- if (!params) return routes[url](socketResponse(connection, url, id));
279
- return routes[url](params, socketResponse(connection, url, id));
280
- }
281
- else return socketResponse(connection, url, id).error(`nop handler found for '${message.url}'`);
282
- };
283
-
284
- connection.on('message', routeHandler);
285
- });
286
-
287
- return {
288
- close: () => socketServer.shutDown(),
289
- connections
290
- };
291
- };
292
-
293
- var http = (config = {}) => {
294
- if (typeof config !== 'object') config = {};
295
- if (!config.protocol) config.protocol = 'peernet-v0.1.0';
296
- if (!config.port) config.port = 2000;
297
- if (!config.host) config.host = '127.0.0.1';
298
-
299
- const app = new Koa();
300
-
301
- app.use(async (ctx) => {
302
- const url = ctx.url.split('/api/')[1];
303
- if (url === 'version') ctx.body = {client: '@peernet/api/http', version};
304
- });
305
-
306
- const httpServer = createServer(app.callback());
307
-
308
- config.httpServer = httpServer;
309
-
310
- httpServer.listen(config.port, () => {
311
- console.log(`listening on ${config.port}`);
312
- });
313
-
314
- return socketRequestServer(config, api$1)
315
- };
316
-
317
- export default http;
@@ -1,317 +0,0 @@
1
- import Pubsub from '@vandeurenglenn/little-pubsub';
2
- import { server } from 'websocket';
3
- import { createServer } from 'http';
4
- import Koa from 'koa';
5
-
6
- var version = "0.7.16";
7
-
8
- var api$1 = {
9
- version: ({send}) => send({client: '@peernet/api/http', version}),
10
- ready: ({send}) => {
11
- if (globalThis.states.ready) send(true);
12
- else pubsub.subscribe('ready', () => send(true));
13
- },
14
- storage: async (params, {send, error}) => {
15
- console.log(params);
16
- const {name, root, key, value, method} = params;
17
- try {
18
- if (name && root) {
19
- globalThis[name] = globalThis[name] || await new LeofcoinStorage(name, root);
20
- } else {
21
- return error('Expected name & root')
22
- }
23
- if (method === 'put') {
24
- await globalThis[name].put(key, value);
25
- return send('ok')
26
- }
27
- if (method === 'remove') {
28
- await globalThis[name].remove(key, value);
29
- return send('ok')
30
- }
31
- value = await globalThis[name].get(key);
32
- return send(value)
33
- } catch (e) {
34
- return error(e)
35
- }
36
- },
37
- getConfig: async (params, {send, error}) => {
38
- try {
39
- const config = await api.getConfig(params);
40
- send(config);
41
- } catch (e) {
42
- error(e);
43
- }
44
- },
45
- setMinerConfig: async (params, {send, error}) => {
46
- try {
47
- await api.setMinerConfig(params);
48
- send('ok');
49
- } catch (e) {
50
- error(e);
51
- }
52
- },
53
- getMinerConfig: async ({send, error}) => {
54
- try {
55
- const config = await api.getMinerConfig();
56
- send(config);
57
- } catch (e) {
58
- error(e);
59
- }
60
- },
61
- wallet: async ({send}) => {
62
- const wallet = await walletStore.get();
63
- send(wallet);
64
- },
65
- addresses: async ({send, error}) => {
66
- try {
67
- const adresses = await api.addresses();
68
- send(adresses);
69
- } catch (e) {
70
- error(e);
71
- }
72
- },
73
- accountNames: async (params, {send, error}) => {
74
- try {
75
- const adresses = await api.accountNames(params.index);
76
- send(adresses);
77
- } catch (e) {
78
- error(e);
79
- }
80
- },
81
- accounts: async ({send}) => {
82
- const accounts = await accountStore.get();
83
- send(accounts);
84
- },
85
- account: async (params, {send}) => {
86
- const account = await accountStore.get(params);
87
- send(account);
88
- },
89
- balance: async (params, {send, error}) => {
90
- console.log('balance');
91
- try {
92
- console.log(await api.getBalanceForAddress(params.address));
93
- const value = await api.getBalanceForAddress(params.address);
94
- send(value);
95
- } catch (e) {
96
- console.log(e);
97
- error(e);
98
- }
99
- },
100
- balanceAfter: async (params, {send, error}) => {
101
- try {
102
- const value = await api.getBalanceForAddressAfter(params.address, params.index);
103
- send(value);
104
- } catch (e) {
105
- error(e);
106
- }
107
- },
108
- mine: async (params, {send, error}) => {
109
- api.mine(params);
110
- send('ok');
111
- },
112
- lastBlock: async ({send, error}) => {
113
- try {
114
- const value = await api.lastBlock();
115
- send(value);
116
- } catch (e) {
117
- error(e);
118
- }
119
- },
120
- };
121
-
122
- const fullLog = text => {
123
- return console.log(`${new Date()}: ${text}`)
124
- };
125
-
126
- const originIsAllowed = (requestOrigin, origin) => {
127
- // put logic here to detect whether the specified origin is allowed.
128
- if (origin && requestOrigin !== origin) return false;
129
- return true;
130
- };
131
-
132
- /**
133
- * @module socketResponse
134
- *
135
- * @param {object} connection socket connection
136
- * @param {string} route the route to handle
137
- */
138
- var socketConnection = (request, protocol, origin) => {
139
- if (origin && !originIsAllowed(request.origin, origin)) {
140
- // Make sure we only accept requests from an allowed origin
141
- request.reject();
142
- fullLog(`Connection from origin ${request.origin} rejected.`);
143
- return;
144
- }
145
- // console.log(request);
146
- const connection = request.accept(protocol, request.origin);
147
- fullLog(`Connection accepted @${protocol}`);
148
- return connection;
149
- };
150
-
151
- /**
152
- * @module socketResponse
153
- *
154
- * @param {object} connection socket connection
155
- * @param {string} url the request url
156
- */
157
- var socketResponse = (connection, url, id, customEvent) => {
158
- const send = (data = 'ok', status = 200) => connection.send(
159
- JSON.stringify({url, status, value: data, id, customEvent})
160
- );
161
- const error = data => connection.send(JSON.stringify({url, value: data}));
162
- return {
163
- connection,
164
- send,
165
- error
166
- }
167
- };
168
-
169
- const socketRequestServer = (options, routes = {}) => {
170
- // if (!routes && !routes.port && options) routes = options;
171
- // else if (!options && !routes) return console.error('no routes defined');
172
-
173
- let {httpServer, httpsServer, port, protocol, credentials, origin, pubsub } = options;
174
- if (!pubsub) pubsub = new Pubsub({verbose: false});
175
- if (!port) port = 6000;
176
- const connections = [];
177
- let connection;
178
- const startTime = new Date().getTime();
179
- // default routes
180
- if (!routes.ping) routes.ping = (response) => response.send(new Date().getTime());
181
- if (!routes.uptime) routes.uptime = (response) => {
182
- const now = new Date().getTime();
183
- response.send(now - startTime);
184
- };
185
- if (!routes.pubsub) {
186
- routes.pubsub = (params, response) => {
187
- if (!response) {
188
- response = params;
189
- params = {};
190
- }
191
-
192
- if (!params.topic) params.topic = 'pubsub';
193
-
194
- const topic = params.topic;
195
- delete params.topic;
196
-
197
- if (params.subscribe) {
198
- pubsub.subscribe(topic, message => {
199
- response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
200
- });
201
- response.send('ok', 200);
202
- } else if (params.unsubscribe) {
203
- pubsub.unsubscribe(topic, message => {
204
- response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
205
- });
206
- for (const connection of connections) {
207
- if (connection !== response.connection) connection.send(JSON.stringify({url: topic, status: 200, value: params}));
208
- }
209
- response.send('ok', 200);
210
- }
211
- else if (params.value !== undefined)
212
- // should only be send raw to stars
213
- // for (const connection of connections) {
214
- // if (connection !== response.connection) connection.send(JSON.stringify({
215
- // url: topic, status: 200, value: params
216
- // }));
217
- // }
218
- pubsub.publish(topic, params.value);
219
- response.send('ok', 200);
220
- };
221
-
222
- }
223
- globalThis.peerMap = new Map();
224
- if (!routes.peernet) {
225
- routes.peernet = (params, response) => {
226
- if (params.join) {
227
- peerMap.set(params.peerId, params.address);
228
- response.send([...peerMap.values()]);
229
- for (const connection of connections) {
230
- if (connection !== response.connection)
231
- socketResponse(connection, 'peernet', 'peernet').send({discovered: params.address});
232
- }
233
- return
234
- }
235
- if (!params.join) {
236
- peerMap.delete(params.peerId);
237
- return response.send()
238
- }
239
- };
240
- }
241
- // if (!protocol) protocol = 'echo-protocol';
242
- if (!httpServer && !httpsServer) {
243
- const { createServer } = credentials ? require('https') : require('http');
244
- if (credentials) httpServer = createServer(credentials);
245
- else httpServer = createServer();
246
-
247
- httpServer.listen(port, () => {
248
- console.log(`listening on ${port}`);
249
- });
250
- }
251
-
252
- const socketServer = new server({
253
- httpServer,
254
- autoAcceptConnections: false
255
- });
256
-
257
- socketServer.on('request', request => {
258
-
259
- connection = socketConnection(request, protocol, origin);
260
- connections.push(connection);
261
-
262
- const routeHandler = message => {
263
- let data;
264
- if (message.type) {
265
- switch (message.type) {
266
- case 'binary':
267
- data = message.binaryData.toString();
268
- break;
269
- case 'utf8':
270
- data = message.utf8Data;
271
- break;
272
- }
273
- }
274
- const { route, params, url, id, customEvent } = JSON.parse(data);
275
- // ignore api when customEvent is defined
276
- if (customEvent) return;
277
- if (routes[url]) {
278
- if (!params) return routes[url](socketResponse(connection, url, id));
279
- return routes[url](params, socketResponse(connection, url, id));
280
- }
281
- else return socketResponse(connection, url, id).error(`nop handler found for '${message.url}'`);
282
- };
283
-
284
- connection.on('message', routeHandler);
285
- });
286
-
287
- return {
288
- close: () => socketServer.shutDown(),
289
- connections
290
- };
291
- };
292
-
293
- var http = (config = {}) => {
294
- if (typeof config !== 'object') config = {};
295
- if (!config.protocol) config.protocol = 'peernet-v0.1.0';
296
- if (!config.port) config.port = 2000;
297
- if (!config.host) config.host = '127.0.0.1';
298
-
299
- const app = new Koa();
300
-
301
- app.use(async (ctx) => {
302
- const url = ctx.url.split('/api/')[1];
303
- if (url === 'version') ctx.body = {client: '@peernet/api/http', version};
304
- });
305
-
306
- const httpServer = createServer(app.callback());
307
-
308
- config.httpServer = httpServer;
309
-
310
- httpServer.listen(config.port, () => {
311
- console.log(`listening on ${config.port}`);
312
- });
313
-
314
- return socketRequestServer(config, api$1)
315
- };
316
-
317
- export default http;