-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.js
149 lines (123 loc) · 3.77 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const https = require('https')
const { spawn } = require('child_process')
const { LRUMap } = require('lru_map')
const blockchainApi = process.env.BLOCKSCRAPEAPI
const blockchainApiToken = process.env.BLOCKSCRAPEAPITOKEN
const blockchainCli = process.env.BLOCKSCRAPECLI || 'litecoin-cli'
const cacheSize = process.env.BLOCKSCRAPECACHESIZE || 100000
let txVoutCache = new LRUMap(cacheSize)
// args should be an array structured like [operation, key, value]
const cache = (args) => {
return new Promise ( (resolve, reject) => {
let result = undefined
switch (args[0]) {
case 'get':
result = txVoutCache.get(args[1])
resolve(result)
break
case 'set':
result = txVoutCache.set(args[1], args[2])
resolve(result)
break
case 'exists':
result = txVoutCache.has(args[1])
resolve(result)
break
default:
reject(`Error! Operation ${args[0]} not recognized!`)
}
})
}
const local = (command) => {
return new Promise( (resolve, reject) => {
let result = ''
let resultError = ''
let child = spawn(command, { shell: '/bin/bash' })
child.stderr.setEncoding('utf8')
child.stdout.setEncoding('utf8')
child.stdout.on('data', (chunk) => {
result += chunk
})
child.stderr.on('data', (chunk) => {
resultError += chunk
})
child.on('error', (err) => {
console.error(`Errored with: ${err}!`)
})
child.on('exit', (code, signal) => {
if (code !== 0 || signal !== null) {
console.warn(`Exited with code ${code} and signal: ${signal}`)
}
})
child.on('close', (reason) => {
if (reason !== 0) {
console.warn(`Closed for reason: ${reason}`)
reject(resultError)
} else {
resolve(result)
}
})
child.on('message', (message) => {
console.log(`Message recieved: ${message}`)
})
})
}
const remote = (request, args) => {
const requestHostName = blockchainApi.match(/[^/]*/)[0]
const requestPath = `${blockchainApi.match(/\/(.*)/)[0]}${request}`
const options = {
hostname: requestHostName,
path: requestPath,
port: args.port,
method: args.method,
strictSSL: args.strictSSL
}
return new Promise( (resolve, reject) => {
https.request(options, (response) => {
let data = ''
response.setEncoding('utf8')
response.on('data', (chunk) => {
data += chunk
})
response.on('end', () => {
resolve(data)
})
}).on('error', (err) => {
reject(err)
})
})
}
/**
* This is the central component for spawning https requests or child processes which in turn query data from the user
* defined blockchain (i.e. litecoin-cli or api.blockcypher.com).
*
* [Spawns a promisified data stream using spawn() or https.request() based on "command" with optional "params"]
* @param {[array]} command :: the command/endpoint/url with required argument(s) -- required
* @param {[array]} params :: optional parameters for the given command -- optional
* @return {[promise]} :: returns a promise; resolved when data stream ends, rejected on error
**/
const client = (command, params, args) => {
if (typeof(params) !== 'undefined' && Array.isArray(params) === false) {
console.error(`Optional params must be an array! Instead got ${typeof(params)}`)
process.exit(1)
}
if (blockchainApi) {
let queryString = ''
if (Array.isArray(params)) {
queryString = `?${params.join('&')}`
}
return remote(`${command.join('/')}${queryString}`, args)
} else {
if (Array.isArray(params)) {
command.push(...params)
}
return local(`${blockchainCli} ${command.join(' ')}`)
}
}
module.exports = {
cache,
client,
blockchainApi,
blockchainApiToken,
blockchainCli
}