rocketh 0.4.40 → 0.5.1

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/providerengine.js DELETED
@@ -1,128 +0,0 @@
1
- const ethers = require('ethers');
2
-
3
- const ProviderEngine = function(fallbackURL, providers) {
4
- this.lastId = 0;
5
- if(fallbackURL) {
6
- this.fallbackProvider = new ethers.providers.JsonRpcProvider(fallbackURL);
7
- }
8
- this.providers = providers || [];
9
- for(let i = 0; i < providers.length; i++) {
10
- providers[i].setEngine(this);
11
- }
12
- }
13
-
14
- ProviderEngine.prototype.send = function(payload, callback) {
15
- this.sendPayload(payload, callback);
16
- }
17
-
18
- ProviderEngine.prototype.sendAsync = function(payload, callback) {
19
- this.sendPayload(payload, callback);
20
- }
21
-
22
- ProviderEngine.prototype.sendPayload = async function(payload, callback) {
23
- self = this;
24
- let currentProvider = -1;
25
- let result = null;
26
- let error = null;
27
- let stack = []
28
-
29
- next();
30
-
31
- function next(after) {
32
- currentProvider++;
33
- stack.unshift(after);
34
- if (currentProvider >= self.providers.length) {
35
- if(self.fallbackProvider) {
36
- // console.log('calling ', payload.method, payload.params);
37
- self.fallbackProvider.send(payload.method, payload.params)
38
- .then((result) => {
39
- callback(null, {id: payload.id, result: result, jsonrpc: payload.jsonrpc});
40
- })
41
- .catch((err) => {
42
- let actualError = err;
43
- if(err.responseText) {
44
- try {
45
- actualError = JSON.parse(err.responseText);
46
- // if(actualError.error) {
47
- // console.log('error ', (typeof actualError.error), actualError.error);
48
- // // actualError = { data : actualError.error }; // wrap in data for web3.js error handling
49
- // actualError = actualError.error;
50
- // }
51
- } catch(e) {
52
- actualError = err;
53
- }
54
- }
55
- callback(actualError);
56
- });
57
- } else {
58
- callback(new Error('Request for method "' + payload.method + '" not handled by any subprovider. Please check your subprovider configuration to ensure this method is handled.'))
59
- }
60
- } else {
61
- try {
62
- let provider = self.providers[currentProvider]
63
- provider.handleRequest(payload, next, end);
64
- } catch (e) {
65
- end(e);
66
- }
67
- }
68
- }
69
-
70
- function end(_error, _result) {
71
- result = _result;
72
- error = _error;
73
-
74
- let current = -1;
75
-
76
- nextAfter();
77
-
78
- function nextAfter() {
79
- current++;
80
- if(current >= stack.length) {
81
- done();
82
- } else {
83
- if (stack[current]) {
84
- stack[current](error, result, nextAfter)
85
- } else {
86
- nextAfter();
87
- }
88
- }
89
- }
90
- }
91
-
92
- function done() {
93
- var resultObj = {
94
- id: payload.id,
95
- jsonrpc: payload.jsonrpc,
96
- result: result
97
- }
98
-
99
- if (error != null) {
100
- resultObj.error = {
101
- message: error.stack || error.message || error,
102
- code: error.code || -32000 // TODO different code
103
- }
104
- // console.log('error', error, resultObj, payload);
105
- // respond with both error formats
106
- callback(error, resultObj)
107
- } else {
108
- // console.log('result', resultObj, payload);
109
- callback(null, resultObj)
110
- }
111
- }
112
- }
113
-
114
- ProviderEngine.prototype.subscribe = function() {
115
- console.error('Subscriptions are not supported yet');
116
- throw new Error('Subscriptions are not supported yet');
117
- }
118
-
119
- ProviderEngine.prototype.unsubscribe = function() {
120
- console.error('Subscriptions are not supported yet');
121
- throw new Error('Subscriptions are not supported yet');
122
- }
123
-
124
- ProviderEngine.prototype.disconnect = function() {
125
- return true;
126
- }
127
-
128
- module.exports = ProviderEngine;