@leofcoin/peernet 0.11.27 → 0.11.28
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 +2 -37
- package/dist/commonjs/peernet.js +3 -46
- package/dist/module/peernet.js +2 -37
- package/package.json +1 -1
- package/src/peernet.js +2 -1
- package/dist/commonjs/client-5633ba04.js +0 -321
- package/dist/commonjs/http-997ec597.js +0 -328
package/dist/browser/peernet.js
CHANGED
|
@@ -729,43 +729,6 @@ class Client {
|
|
|
729
729
|
|
|
730
730
|
}
|
|
731
731
|
|
|
732
|
-
class LeofcoinStorageClient {
|
|
733
|
-
constructor(name, root) {
|
|
734
|
-
this.name = name;
|
|
735
|
-
this.root = root;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
async get(key) {
|
|
739
|
-
try {
|
|
740
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
741
|
-
name: this.name,
|
|
742
|
-
root: this.root,
|
|
743
|
-
key,
|
|
744
|
-
});
|
|
745
|
-
return result
|
|
746
|
-
} catch (e) {
|
|
747
|
-
console.log(e);
|
|
748
|
-
return undefined
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
async put(key, value) {
|
|
753
|
-
try {
|
|
754
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
755
|
-
name: this.name,
|
|
756
|
-
root: this.root,
|
|
757
|
-
key,
|
|
758
|
-
value,
|
|
759
|
-
method: 'put',
|
|
760
|
-
});
|
|
761
|
-
return result
|
|
762
|
-
} catch (e) {
|
|
763
|
-
console.log(e);
|
|
764
|
-
return undefined
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
|
|
769
732
|
var proto$a = `
|
|
770
733
|
// PeernetMessage
|
|
771
734
|
message PeernetMessage {
|
|
@@ -1999,6 +1962,8 @@ class Peernet {
|
|
|
1999
1962
|
try {
|
|
2000
1963
|
const pub = await accountStore.get('public');
|
|
2001
1964
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1965
|
+
const accounts = await walletStore.get('accounts');
|
|
1966
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts));
|
|
2002
1967
|
} catch (e) {
|
|
2003
1968
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
2004
1969
|
const {identity, accounts, config} = await generateAccount(this.network);
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -727,43 +727,6 @@ class Client {
|
|
|
727
727
|
|
|
728
728
|
}
|
|
729
729
|
|
|
730
|
-
class LeofcoinStorageClient {
|
|
731
|
-
constructor(name, root) {
|
|
732
|
-
this.name = name;
|
|
733
|
-
this.root = root;
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
async get(key) {
|
|
737
|
-
try {
|
|
738
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
739
|
-
name: this.name,
|
|
740
|
-
root: this.root,
|
|
741
|
-
key,
|
|
742
|
-
});
|
|
743
|
-
return result
|
|
744
|
-
} catch (e) {
|
|
745
|
-
console.log(e);
|
|
746
|
-
return undefined
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
async put(key, value) {
|
|
751
|
-
try {
|
|
752
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
753
|
-
name: this.name,
|
|
754
|
-
root: this.root,
|
|
755
|
-
key,
|
|
756
|
-
value,
|
|
757
|
-
method: 'put',
|
|
758
|
-
});
|
|
759
|
-
return result
|
|
760
|
-
} catch (e) {
|
|
761
|
-
console.log(e);
|
|
762
|
-
return undefined
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
|
|
767
730
|
var proto$5 = `
|
|
768
731
|
// PeernetDataMessage
|
|
769
732
|
message PeernetDataMessage {
|
|
@@ -1882,15 +1845,7 @@ class Peernet {
|
|
|
1882
1845
|
const {daemon, environment} = await target();
|
|
1883
1846
|
this.hasDaemon = daemon;
|
|
1884
1847
|
|
|
1885
|
-
|
|
1886
|
-
const httpClient = await Promise.resolve().then(function () { return require('./client-5633ba04.js'); });
|
|
1887
|
-
globalThis.peernet.client = await httpClient.default({
|
|
1888
|
-
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1889
|
-
});
|
|
1890
|
-
} else {
|
|
1891
|
-
const http = await Promise.resolve().then(function () { return require('./http-997ec597.js'); });
|
|
1892
|
-
if (environment !== 'browser') http.default(options);
|
|
1893
|
-
}
|
|
1848
|
+
|
|
1894
1849
|
|
|
1895
1850
|
for (const store of this.defaultStores) {
|
|
1896
1851
|
await this.addStore(store, options.storePrefix, options.root);
|
|
@@ -1899,6 +1854,8 @@ class Peernet {
|
|
|
1899
1854
|
try {
|
|
1900
1855
|
const pub = await accountStore.get('public');
|
|
1901
1856
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1857
|
+
const accounts = await walletStore.get('accounts');
|
|
1858
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts));
|
|
1902
1859
|
} catch (e) {
|
|
1903
1860
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
1904
1861
|
const {identity, accounts, config} = await generateAccount(this.network);
|
package/dist/module/peernet.js
CHANGED
|
@@ -685,43 +685,6 @@ class Client {
|
|
|
685
685
|
|
|
686
686
|
}
|
|
687
687
|
|
|
688
|
-
class LeofcoinStorageClient {
|
|
689
|
-
constructor(name, root) {
|
|
690
|
-
this.name = name;
|
|
691
|
-
this.root = root;
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
async get(key) {
|
|
695
|
-
try {
|
|
696
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
697
|
-
name: this.name,
|
|
698
|
-
root: this.root,
|
|
699
|
-
key,
|
|
700
|
-
});
|
|
701
|
-
return result
|
|
702
|
-
} catch (e) {
|
|
703
|
-
console.log(e);
|
|
704
|
-
return undefined
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
async put(key, value) {
|
|
709
|
-
try {
|
|
710
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
711
|
-
name: this.name,
|
|
712
|
-
root: this.root,
|
|
713
|
-
key,
|
|
714
|
-
value,
|
|
715
|
-
method: 'put',
|
|
716
|
-
});
|
|
717
|
-
return result
|
|
718
|
-
} catch (e) {
|
|
719
|
-
console.log(e);
|
|
720
|
-
return undefined
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
|
|
725
688
|
var proto$a = `
|
|
726
689
|
// PeernetMessage
|
|
727
690
|
message PeernetMessage {
|
|
@@ -1955,6 +1918,8 @@ class Peernet {
|
|
|
1955
1918
|
try {
|
|
1956
1919
|
const pub = await accountStore.get('public');
|
|
1957
1920
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1921
|
+
const accounts = await walletStore.get('accounts');
|
|
1922
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts));
|
|
1958
1923
|
} catch (e) {
|
|
1959
1924
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
1960
1925
|
const {identity, accounts, config} = await generateAccount(this.network);
|
package/package.json
CHANGED
package/src/peernet.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug'
|
|
2
2
|
import Client from './../node_modules/@leofcoin/peernet-swarm/dist/es/client.js'
|
|
3
3
|
import LeofcoinStorage from '@leofcoin/storage'
|
|
4
|
-
import LeofcoinStorageClient from './http/client/storage.js'
|
|
5
4
|
import PeernetMessage from './messages/peernet-message.js'
|
|
6
5
|
import DHTMessage from './messages/dht.js'
|
|
7
6
|
import DHTMessageResponse from './messages/dht-response.js'
|
|
@@ -191,6 +190,8 @@ export default class Peernet {
|
|
|
191
190
|
try {
|
|
192
191
|
const pub = await accountStore.get('public')
|
|
193
192
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
|
|
193
|
+
const accounts = await walletStore.get('accounts')
|
|
194
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts))
|
|
194
195
|
} catch (e) {
|
|
195
196
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
196
197
|
const wallet = {}
|
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/* socket-request-client version 1.6.3 */
|
|
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.close();
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
return new Promise((resolve, reject) => {
|
|
195
|
-
const init = () => {
|
|
196
|
-
let ws;
|
|
197
|
-
if (typeof process === 'object' && !globalThis.WebSocket) {
|
|
198
|
-
ws = require('websocket').w3cwebsocket;
|
|
199
|
-
} else {
|
|
200
|
-
ws = WebSocket;
|
|
201
|
-
}
|
|
202
|
-
const client = new ws(url, protocols);
|
|
203
|
-
client.onmessage = onmessage;
|
|
204
|
-
client.onerror = onerror;
|
|
205
|
-
client.onopen = () => {
|
|
206
|
-
tries = 0;
|
|
207
|
-
resolve(clientConnection(client));
|
|
208
|
-
};
|
|
209
|
-
client.onclose = message => {
|
|
210
|
-
tries++;
|
|
211
|
-
if (!retry) return reject(options)
|
|
212
|
-
if (tries > 5) {
|
|
213
|
-
console.log(`${protocols} Client Closed`);
|
|
214
|
-
console.error(`could not connect to - ${url}/`);
|
|
215
|
-
return resolve(clientConnection(client))
|
|
216
|
-
}
|
|
217
|
-
if (message.code === 1006) {
|
|
218
|
-
console.log('Retrying in 10 seconds');
|
|
219
|
-
setTimeout(() => {
|
|
220
|
-
return init();
|
|
221
|
-
}, retry);
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
};
|
|
225
|
-
return init();
|
|
226
|
-
});
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
class HttpClientApi$1 {
|
|
230
|
-
constructor(config = {}) {
|
|
231
|
-
if (!config.apiPath) config.apiPath = 'api';
|
|
232
|
-
|
|
233
|
-
const address = `ws://${config.host}:${config.port}`;
|
|
234
|
-
|
|
235
|
-
this.apiUrl = (url) => `${address}/${url}`;
|
|
236
|
-
return (async () => {
|
|
237
|
-
this.client = await socketRequestClient(address, config.protocol, {pubsub: config.pubsub, retry: 3000});
|
|
238
|
-
return this
|
|
239
|
-
})()
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async get(url, obj) {
|
|
243
|
-
const headers = {};
|
|
244
|
-
let body = null;
|
|
245
|
-
let method = 'GET';
|
|
246
|
-
if (obj) {
|
|
247
|
-
method = 'POST';
|
|
248
|
-
headers['Content-Type'] = 'application/json';
|
|
249
|
-
body = JSON.stringify(obj);
|
|
250
|
-
}
|
|
251
|
-
let response = await this.client.request(url, {headers, body, method});
|
|
252
|
-
const type = response.headers.get('content-type').split(';')[0];
|
|
253
|
-
if (type==='application/json') response = await response.json();
|
|
254
|
-
return response
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async put(url, obj) {
|
|
258
|
-
const headers = {};
|
|
259
|
-
let body = {};
|
|
260
|
-
if (obj) {
|
|
261
|
-
headers['Content-Type'] = 'application/json';
|
|
262
|
-
body = JSON.stringify(obj);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body});
|
|
266
|
-
const type = response.headers.get('content-type').split(';')[0];
|
|
267
|
-
if (type==='application/json') response = await response.json();
|
|
268
|
-
return response
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
class HttpClientApi extends HttpClientApi$1 {
|
|
273
|
-
constructor(config = {}) {
|
|
274
|
-
config.apiPath = 'api';
|
|
275
|
-
return (async () => {
|
|
276
|
-
await super(config);
|
|
277
|
-
|
|
278
|
-
this.properties = {
|
|
279
|
-
wallet: 'get',
|
|
280
|
-
version: 'get',
|
|
281
|
-
addresses: 'get',
|
|
282
|
-
config: 'get',
|
|
283
|
-
account: 'get',
|
|
284
|
-
accounts: 'get',
|
|
285
|
-
transaction: 'any',
|
|
286
|
-
transactions: 'get',
|
|
287
|
-
block: 'get',
|
|
288
|
-
blocks: 'get',
|
|
289
|
-
};
|
|
290
|
-
this.keys = Object.keys(this.properties);
|
|
291
|
-
return this
|
|
292
|
-
})()
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
async request(url, data) {
|
|
296
|
-
return await this.client.request({url, params: data})
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async ready() {
|
|
300
|
-
return await this.request('ready')
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
async version() {
|
|
304
|
-
return await this.request('version')
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
async account(index) {
|
|
308
|
-
return await this.request('account', {index})
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
var client = (config = {}) => {
|
|
313
|
-
if (typeof config !== 'object') config = {};
|
|
314
|
-
if (!config.protocol) config.protocol = 'peernet-v0.1.0';
|
|
315
|
-
if (!config.port) config.port = 1000;
|
|
316
|
-
if (!config.host) config.host = '127.0.0.1';
|
|
317
|
-
|
|
318
|
-
return new HttpClientApi(config)
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
exports["default"] = client;
|
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var websocket = require('websocket');
|
|
4
|
-
var PubSub = require('@vandeurenglenn/little-pubsub');
|
|
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.11.25";
|
|
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
|
-
if (!globalThis.PubSub) globalThis.PubSub = PubSub__default["default"];
|
|
177
|
-
if (!globalThis.pubsub) globalThis.pubsub = new PubSub__default["default"]();
|
|
178
|
-
|
|
179
|
-
const socketRequestServer = (options, routes = {}) => {
|
|
180
|
-
// if (!routes && !routes.port && options) routes = options;
|
|
181
|
-
// else if (!options && !routes) return console.error('no routes defined');
|
|
182
|
-
|
|
183
|
-
let {httpServer, httpsServer, port, protocol, credentials, origin, pubsub } = options;
|
|
184
|
-
if (!pubsub) pubsub = new PubSub__default["default"]({verbose: false});
|
|
185
|
-
if (!port) port = 6000;
|
|
186
|
-
const connections = [];
|
|
187
|
-
let connection;
|
|
188
|
-
const startTime = new Date().getTime();
|
|
189
|
-
// default routes
|
|
190
|
-
if (!routes.ping) routes.ping = (response) => response.send(new Date().getTime());
|
|
191
|
-
if (!routes.uptime) routes.uptime = (response) => {
|
|
192
|
-
const now = new Date().getTime();
|
|
193
|
-
response.send(now - startTime);
|
|
194
|
-
};
|
|
195
|
-
if (!routes.pubsub) {
|
|
196
|
-
routes.pubsub = (params, response) => {
|
|
197
|
-
if (!response) {
|
|
198
|
-
response = params;
|
|
199
|
-
params = {};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (!params.topic) params.topic = 'pubsub';
|
|
203
|
-
|
|
204
|
-
const topic = params.topic;
|
|
205
|
-
delete params.topic;
|
|
206
|
-
|
|
207
|
-
if (params.subscribe) {
|
|
208
|
-
pubsub.subscribe(topic, message => {
|
|
209
|
-
response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
|
|
210
|
-
});
|
|
211
|
-
response.send('ok', 200);
|
|
212
|
-
} else if (params.unsubscribe) {
|
|
213
|
-
pubsub.unsubscribe(topic, message => {
|
|
214
|
-
response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
|
|
215
|
-
});
|
|
216
|
-
for (const connection of connections) {
|
|
217
|
-
if (connection !== response.connection) connection.send(JSON.stringify({url: topic, status: 200, value: params}));
|
|
218
|
-
}
|
|
219
|
-
response.send('ok', 200);
|
|
220
|
-
}
|
|
221
|
-
else if (params.value !== undefined)
|
|
222
|
-
// should only be send raw to stars
|
|
223
|
-
// for (const connection of connections) {
|
|
224
|
-
// if (connection !== response.connection) connection.send(JSON.stringify({
|
|
225
|
-
// url: topic, status: 200, value: params
|
|
226
|
-
// }));
|
|
227
|
-
// }
|
|
228
|
-
pubsub.publish(topic, params.value);
|
|
229
|
-
response.send('ok', 200);
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
globalThis.peerMap = new Map();
|
|
234
|
-
if (!routes.peernet) {
|
|
235
|
-
routes.peernet = (params, response) => {
|
|
236
|
-
if (params.join) {
|
|
237
|
-
peerMap.set(params.peerId, params.address);
|
|
238
|
-
response.send([...peerMap.values()]);
|
|
239
|
-
for (const connection of connections) {
|
|
240
|
-
if (connection !== response.connection)
|
|
241
|
-
socketResponse(connection, 'peernet', 'peernet').send({discovered: params.address});
|
|
242
|
-
}
|
|
243
|
-
return
|
|
244
|
-
}
|
|
245
|
-
if (!params.join) {
|
|
246
|
-
peerMap.delete(params.peerId);
|
|
247
|
-
return response.send()
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
// if (!protocol) protocol = 'echo-protocol';
|
|
252
|
-
if (!httpServer && !httpsServer) {
|
|
253
|
-
const { createServer } = credentials ? require('https') : require('http');
|
|
254
|
-
if (credentials) httpServer = createServer(credentials);
|
|
255
|
-
else httpServer = createServer();
|
|
256
|
-
|
|
257
|
-
httpServer.listen(port, () => {
|
|
258
|
-
console.log(`listening on ${port}`);
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const socketServer = new websocket.server({
|
|
263
|
-
httpServer,
|
|
264
|
-
autoAcceptConnections: false
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
socketServer.on('request', request => {
|
|
268
|
-
|
|
269
|
-
connection = socketConnection(request, protocol, origin);
|
|
270
|
-
connections.push(connection);
|
|
271
|
-
|
|
272
|
-
const routeHandler = message => {
|
|
273
|
-
let data;
|
|
274
|
-
if (message.type) {
|
|
275
|
-
switch (message.type) {
|
|
276
|
-
case 'binary':
|
|
277
|
-
data = message.binaryData.toString();
|
|
278
|
-
break;
|
|
279
|
-
case 'utf8':
|
|
280
|
-
data = message.utf8Data;
|
|
281
|
-
break;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
const { route, params, url, id, customEvent } = JSON.parse(data);
|
|
285
|
-
// ignore api when customEvent is defined
|
|
286
|
-
if (customEvent) return;
|
|
287
|
-
if (routes[url]) {
|
|
288
|
-
if (!params) return routes[url](socketResponse(connection, url, id), connections);
|
|
289
|
-
return routes[url](params, socketResponse(connection, url, id), connections);
|
|
290
|
-
}
|
|
291
|
-
else return socketResponse(connection, url, id).error(`no handler found for '${message.url}'`);
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
connection.on('message', routeHandler);
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
return {
|
|
298
|
-
close: () => socketServer.shutDown(),
|
|
299
|
-
connections
|
|
300
|
-
};
|
|
301
|
-
};
|
|
302
|
-
var server = socketRequestServer;
|
|
303
|
-
|
|
304
|
-
var http = (config = {}) => {
|
|
305
|
-
if (typeof config !== 'object') config = {};
|
|
306
|
-
if (!config.protocol) config.protocol = 'peernet-v0.1.0';
|
|
307
|
-
if (!config.port) config.port = 2000;
|
|
308
|
-
if (!config.host) config.host = '127.0.0.1';
|
|
309
|
-
|
|
310
|
-
const app = new Koa__default["default"]();
|
|
311
|
-
|
|
312
|
-
app.use(async (ctx) => {
|
|
313
|
-
const url = ctx.url.split('/api/')[1];
|
|
314
|
-
if (url === 'version') ctx.body = {client: '@peernet/api/http', version};
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
const httpServer = http$1.createServer(app.callback());
|
|
318
|
-
|
|
319
|
-
config.httpServer = httpServer;
|
|
320
|
-
|
|
321
|
-
httpServer.listen(config.port, () => {
|
|
322
|
-
console.log(`listening on ${config.port}`);
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
return server(config, api$1)
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
exports["default"] = http;
|