-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
73 lines (61 loc) · 2.58 KB
/
index.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
/**
* @original-authors:
* Marek Kotewicz <[email protected]>
* Marian Oancea <[email protected]>
* Fabian Vogelsteller <[email protected]>
* @date 2015
*/
// workaround to use httpprovider in different envs
const XHR2 = require('xhr2');
/**
* InvalidResponseError helper for invalid errors.
*/
function invalidResponseError(result, host) {
const message = !!result && !!result.error && !!result.error.message ? `[ethjs-provider-http] ${result.error.message}` : `[ethjs-provider-http] Invalid JSON RPC response from host provider ${host}: ${JSON.stringify(result, null, 2)}`;
return new Error(message);
}
/**
* HttpProvider should be used to send rpc calls over http
*/
function HttpProvider(host, timeout) {
if (!(this instanceof HttpProvider)) { throw new Error('[ethjs-provider-http] the HttpProvider instance requires the "new" flag in order to function normally (e.g. `const eth = new Eth(new HttpProvider());`).'); }
if (typeof host !== 'string') { throw new Error('[ethjs-provider-http] the HttpProvider instance requires that the host be specified (e.g. `new HttpProvider("http://localhost:8545")` or via service like infura `new HttpProvider("http://ropsten.infura.io")`)'); }
const self = this;
self.host = host;
self.timeout = timeout || 0;
}
/**
* Should be used to make async request
*
* @method sendAsync
* @param {Object} payload
* @param {Function} callback triggered on end with (err, result)
*/
HttpProvider.prototype.sendAsync = function (payload, callback) { // eslint-disable-line
const self = this;
var request = new XHR2(); // eslint-disable-line
request.timeout = self.timeout;
request.open('POST', self.host, true);
request.setRequestHeader('Content-Type', 'application/json');
request.onreadystatechange = () => {
if (request.readyState === 4 && request.timeout !== 1) {
var result = request.responseText; // eslint-disable-line
var error = null; // eslint-disable-line
try {
result = JSON.parse(result);
} catch (jsonError) {
error = invalidResponseError(request.responseText, self.host);
}
callback(error, result);
}
};
request.ontimeout = () => {
callback(`[ethjs-provider-http] CONNECTION TIMEOUT: http request timeout after ${self.timeout} ms. (i.e. your connect has timed out for whatever reason, check your provider).`, null);
};
try {
request.send(JSON.stringify(payload));
} catch (error) {
callback(`[ethjs-provider-http] CONNECTION ERROR: Couldn't connect to node '${self.host}': ${JSON.stringify(error, null, 2)}`, null);
}
};
module.exports = HttpProvider;