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