@leofcoin/peernet 0.11.26 → 0.11.27
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/package.json +1 -1
- package/rollup.config.js +1 -9
- package/src/http/api.js +0 -115
- package/src/http/client/api.js +0 -41
- package/src/http/client/client.js +0 -10
- package/src/http/client/http-client.js +0 -44
- package/src/http/client/storage.js +0 -36
- package/src/http/http.js +0 -28
package/package.json
CHANGED
package/rollup.config.js
CHANGED
|
@@ -19,15 +19,7 @@ export default [{
|
|
|
19
19
|
plugins: [
|
|
20
20
|
json(),
|
|
21
21
|
modify({
|
|
22
|
-
HTTP_IMPORT:
|
|
23
|
-
const httpClient = await import('./http/client/client.js')
|
|
24
|
-
globalThis.peernet.client = await httpClient.default({
|
|
25
|
-
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
26
|
-
})
|
|
27
|
-
} else {
|
|
28
|
-
const http = await import('./http/http.js')
|
|
29
|
-
if (environment !== 'browser') http.default(options)
|
|
30
|
-
}`,
|
|
22
|
+
HTTP_IMPORT: ``,
|
|
31
23
|
SUBTLE_IMPORT: `const { subtle } = require('crypto').webcrypto`
|
|
32
24
|
})
|
|
33
25
|
]
|
package/src/http/api.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import {version} from './../../package.json'
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
5
|
-
ready: ({send}) => {
|
|
6
|
-
if (globalThis.states.ready) send(true)
|
|
7
|
-
else pubsub.subscribe('ready', () => send(true))
|
|
8
|
-
},
|
|
9
|
-
storage: async (params, {send, error}) => {
|
|
10
|
-
console.log(params);
|
|
11
|
-
const {name, root, key, value, method} = params
|
|
12
|
-
try {
|
|
13
|
-
if (name && root) {
|
|
14
|
-
globalThis[name] = globalThis[name] || await new LeofcoinStorage(name, root)
|
|
15
|
-
} else {
|
|
16
|
-
return error('Expected name & root')
|
|
17
|
-
}
|
|
18
|
-
if (method === 'put') {
|
|
19
|
-
await globalThis[name].put(key, value)
|
|
20
|
-
return send('ok')
|
|
21
|
-
}
|
|
22
|
-
if (method === 'remove') {
|
|
23
|
-
await globalThis[name].remove(key, value)
|
|
24
|
-
return send('ok')
|
|
25
|
-
}
|
|
26
|
-
value = await globalThis[name].get(key)
|
|
27
|
-
return send(value)
|
|
28
|
-
} catch (e) {
|
|
29
|
-
return error(e)
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
getConfig: async (params, {send, error}) => {
|
|
33
|
-
try {
|
|
34
|
-
const config = await api.getConfig(params)
|
|
35
|
-
send(config)
|
|
36
|
-
} catch (e) {
|
|
37
|
-
error(e)
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
setMinerConfig: async (params, {send, error}) => {
|
|
41
|
-
try {
|
|
42
|
-
await api.setMinerConfig(params)
|
|
43
|
-
send('ok')
|
|
44
|
-
} catch (e) {
|
|
45
|
-
error(e)
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
getMinerConfig: async ({send, error}) => {
|
|
49
|
-
try {
|
|
50
|
-
const config = await api.getMinerConfig()
|
|
51
|
-
send(config)
|
|
52
|
-
} catch (e) {
|
|
53
|
-
error(e)
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
wallet: async ({send}) => {
|
|
57
|
-
const wallet = await walletStore.get()
|
|
58
|
-
send(wallet)
|
|
59
|
-
},
|
|
60
|
-
addresses: async ({send, error}) => {
|
|
61
|
-
try {
|
|
62
|
-
const adresses = await api.addresses()
|
|
63
|
-
send(adresses)
|
|
64
|
-
} catch (e) {
|
|
65
|
-
error(e)
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
accountNames: async (params, {send, error}) => {
|
|
69
|
-
try {
|
|
70
|
-
const adresses = await api.accountNames(params.index)
|
|
71
|
-
send(adresses)
|
|
72
|
-
} catch (e) {
|
|
73
|
-
error(e)
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
accounts: async ({send}) => {
|
|
77
|
-
const accounts = await accountStore.get()
|
|
78
|
-
send(accounts)
|
|
79
|
-
},
|
|
80
|
-
account: async (params, {send}) => {
|
|
81
|
-
const account = await accountStore.get(params)
|
|
82
|
-
send(account)
|
|
83
|
-
},
|
|
84
|
-
balance: async (params, {send, error}) => {
|
|
85
|
-
console.log('balance');
|
|
86
|
-
try {
|
|
87
|
-
console.log(await api.getBalanceForAddress(params.address));
|
|
88
|
-
const value = await api.getBalanceForAddress(params.address)
|
|
89
|
-
send(value)
|
|
90
|
-
} catch (e) {
|
|
91
|
-
console.log(e);
|
|
92
|
-
error(e)
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
balanceAfter: async (params, {send, error}) => {
|
|
96
|
-
try {
|
|
97
|
-
const value = await api.getBalanceForAddressAfter(params.address, params.index)
|
|
98
|
-
send(value)
|
|
99
|
-
} catch (e) {
|
|
100
|
-
error(e)
|
|
101
|
-
}
|
|
102
|
-
},
|
|
103
|
-
mine: async (params, {send, error}) => {
|
|
104
|
-
api.mine(params)
|
|
105
|
-
send('ok')
|
|
106
|
-
},
|
|
107
|
-
lastBlock: async ({send, error}) => {
|
|
108
|
-
try {
|
|
109
|
-
const value = await api.lastBlock()
|
|
110
|
-
send(value)
|
|
111
|
-
} catch (e) {
|
|
112
|
-
error(e)
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
}
|
package/src/http/client/api.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import HttpClientApi from './http-client.js'
|
|
2
|
-
|
|
3
|
-
export default class extends HttpClientApi {
|
|
4
|
-
constructor(config = {}) {
|
|
5
|
-
config.apiPath = 'api';
|
|
6
|
-
return (async () => {
|
|
7
|
-
await super(config)
|
|
8
|
-
|
|
9
|
-
this.properties = {
|
|
10
|
-
wallet: 'get',
|
|
11
|
-
version: 'get',
|
|
12
|
-
addresses: 'get',
|
|
13
|
-
config: 'get',
|
|
14
|
-
account: 'get',
|
|
15
|
-
accounts: 'get',
|
|
16
|
-
transaction: 'any',
|
|
17
|
-
transactions: 'get',
|
|
18
|
-
block: 'get',
|
|
19
|
-
blocks: 'get',
|
|
20
|
-
}
|
|
21
|
-
this.keys = Object.keys(this.properties)
|
|
22
|
-
return this
|
|
23
|
-
})()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async request(url, data) {
|
|
27
|
-
return await this.client.request({url, params: data})
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async ready() {
|
|
31
|
-
return await this.request('ready')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async version() {
|
|
35
|
-
return await this.request('version')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async account(index) {
|
|
39
|
-
return await this.request('account', {index})
|
|
40
|
-
}
|
|
41
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import HttpClientApi from './api.js'
|
|
2
|
-
|
|
3
|
-
export default (config = {}) => {
|
|
4
|
-
if (typeof config !== 'object') config = {}
|
|
5
|
-
if (!config.protocol) config.protocol = 'peernet-v0.1.0'
|
|
6
|
-
if (!config.port) config.port = 1000
|
|
7
|
-
if (!config.host) config.host = '127.0.0.1'
|
|
8
|
-
|
|
9
|
-
return new HttpClientApi(config)
|
|
10
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import Client from './../../../node_modules/socket-request-client/dist/es/index'
|
|
2
|
-
|
|
3
|
-
export default class HttpClientApi {
|
|
4
|
-
constructor(config = {}) {
|
|
5
|
-
if (!config.apiPath) config.apiPath = 'api'
|
|
6
|
-
|
|
7
|
-
const address = `ws://${config.host}:${config.port}`
|
|
8
|
-
|
|
9
|
-
this.apiUrl = (url) => `${address}/${url}`;
|
|
10
|
-
return (async () => {
|
|
11
|
-
this.client = await Client(address, config.protocol, {pubsub: config.pubsub, retry: 3000})
|
|
12
|
-
return this
|
|
13
|
-
})()
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async get(url, obj) {
|
|
17
|
-
const headers = {}
|
|
18
|
-
let body = null
|
|
19
|
-
let method = 'GET'
|
|
20
|
-
if (obj) {
|
|
21
|
-
method = 'POST'
|
|
22
|
-
headers['Content-Type'] = 'application/json'
|
|
23
|
-
body = JSON.stringify(obj)
|
|
24
|
-
}
|
|
25
|
-
let response = await this.client.request(url, {headers, body, method})
|
|
26
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
27
|
-
if (type==='application/json') response = await response.json()
|
|
28
|
-
return response
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async put(url, obj) {
|
|
32
|
-
const headers = {}
|
|
33
|
-
let body = {}
|
|
34
|
-
if (obj) {
|
|
35
|
-
headers['Content-Type'] = 'application/json'
|
|
36
|
-
body = JSON.stringify(obj)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body})
|
|
40
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
41
|
-
if (type==='application/json') response = await response.json()
|
|
42
|
-
return response
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export default class LeofcoinStorageClient {
|
|
2
|
-
constructor(name, root) {
|
|
3
|
-
this.name = name
|
|
4
|
-
this.root = root
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
async get(key) {
|
|
8
|
-
try {
|
|
9
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
10
|
-
name: this.name,
|
|
11
|
-
root: this.root,
|
|
12
|
-
key,
|
|
13
|
-
})
|
|
14
|
-
return result
|
|
15
|
-
} catch (e) {
|
|
16
|
-
console.log(e);
|
|
17
|
-
return undefined
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async put(key, value) {
|
|
22
|
-
try {
|
|
23
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
24
|
-
name: this.name,
|
|
25
|
-
root: this.root,
|
|
26
|
-
key,
|
|
27
|
-
value,
|
|
28
|
-
method: 'put',
|
|
29
|
-
})
|
|
30
|
-
return result
|
|
31
|
-
} catch (e) {
|
|
32
|
-
console.log(e);
|
|
33
|
-
return undefined
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
package/src/http/http.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import api from './api.js'
|
|
2
|
-
import server from './../../node_modules/socket-request-server/src/index'
|
|
3
|
-
import {createServer} from 'http'
|
|
4
|
-
import Koa from 'koa'
|
|
5
|
-
import {version} from './../../package.json'
|
|
6
|
-
export default (config = {}) => {
|
|
7
|
-
if (typeof config !== 'object') config = {}
|
|
8
|
-
if (!config.protocol) config.protocol = 'peernet-v0.1.0'
|
|
9
|
-
if (!config.port) config.port = 2000
|
|
10
|
-
if (!config.host) config.host = '127.0.0.1'
|
|
11
|
-
|
|
12
|
-
const app = new Koa()
|
|
13
|
-
|
|
14
|
-
app.use(async (ctx) => {
|
|
15
|
-
const url = ctx.url.split('/api/')[1]
|
|
16
|
-
if (url === 'version') ctx.body = {client: '@peernet/api/http', version}
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
const httpServer = createServer(app.callback())
|
|
20
|
-
|
|
21
|
-
config.httpServer = httpServer
|
|
22
|
-
|
|
23
|
-
httpServer.listen(config.port, () => {
|
|
24
|
-
console.log(`listening on ${config.port}`);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
return server(config, api)
|
|
28
|
-
}
|