From 9cc87ffb978e63725bd8cf480629aad15b31aa89 Mon Sep 17 00:00:00 2001 From: Sebastian Pape <0xneo11@gmail.com> Date: Tue, 10 Sep 2024 13:26:05 +0200 Subject: [PATCH] build --- dist/esm/index.evm.js | 76 +++++++++++++++++++++++++++++-------------- dist/esm/index.js | 76 +++++++++++++++++++++++++++++-------------- dist/umd/index.evm.js | 76 +++++++++++++++++++++++++++++-------------- dist/umd/index.js | 76 +++++++++++++++++++++++++++++-------------- 4 files changed, 208 insertions(+), 96 deletions(-) diff --git a/dist/esm/index.evm.js b/dist/esm/index.evm.js index d3ff9c7..8caa916 100644 --- a/dist/esm/index.evm.js +++ b/dist/esm/index.evm.js @@ -99,51 +99,79 @@ class StaticJsonRpcBatchProvider extends ethers.providers.JsonRpcProvider { this._pendingBatch = []; } + handleError(error, attempt, chunk) { + if(attempt < MAX_RETRY && error) { + const index = this._endpoints.indexOf(this._endpoint)+1; + this._failover(); + this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; + this.requestChunk(chunk, this._endpoint, attempt+1); + } else { + chunk.forEach((inflightRequest) => { + inflightRequest.reject(error); + }); + } + } + detectNetwork() { return Promise.resolve(Blockchains.findByName(this._network).id) } + batchRequest(batch, attempt) { + return new Promise((resolve, reject) => { + + if (batch.length === 0) resolve([]); // Do nothing if requests is empty + + fetch( + this._endpoint, + { + method: 'POST', + body: JSON.stringify(batch), + headers: { 'Content-Type': 'application/json' }, + } + ).then((response)=>{ + if(response.ok) { + response.json().then((parsedJson)=>{ + if(parsedJson.find((entry)=>_optionalChain$2([entry, 'optionalAccess', _ => _.error]))) { + if(attempt < MAX_RETRY) { + reject('Error in batch found!'); + } else { + resolve(parsedJson); + } + } else { + resolve(parsedJson); + } + }).catch(reject); + } else { + reject(`${response.status} ${response.text}`); + } + }).catch(reject); + }) + } + requestChunk(chunk, endpoint, attempt) { - try { + const batch = chunk.map((inflight) => inflight.request); - const request = chunk.map((inflight) => inflight.request); - return ethers.utils.fetchJson(endpoint, JSON.stringify(request)) + try { + return this.batchRequest(batch, attempt) .then((result) => { // For each result, feed it to the correct Promise, depending // on whether it was a success or error chunk.forEach((inflightRequest, index) => { const payload = result[index]; - if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) { + if (_optionalChain$2([payload, 'optionalAccess', _2 => _2.error])) { const error = new Error(payload.error.message); error.code = payload.error.code; error.data = payload.error.data; inflightRequest.reject(error); - } else if(_optionalChain$2([payload, 'optionalAccess', _2 => _2.result])) { + } else if(_optionalChain$2([payload, 'optionalAccess', _3 => _3.result])) { inflightRequest.resolve(payload.result); } else { inflightRequest.reject(); } }); - }).catch((error) => { - if(attempt < MAX_RETRY && error && error.code == 'SERVER_ERROR') { - const index = this._endpoints.indexOf(this._endpoint)+1; - this._failover(); - this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; - this.requestChunk(chunk, this._endpoint, attempt+1); - } else { - chunk.forEach((inflightRequest) => { - inflightRequest.reject(error); - }); - } - }) - - } catch (e) { - - chunk.forEach((inflightRequest) => { - inflightRequest.reject(); - }); - } + }).catch((error) => this.handleError(error, attempt, chunk)) + } catch (error){ this.handleError(error, attempt, chunk); } } send(method, params) { diff --git a/dist/esm/index.js b/dist/esm/index.js index dc57256..44be8f9 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -42,51 +42,79 @@ class StaticJsonRpcBatchProvider extends ethers.providers.JsonRpcProvider { this._pendingBatch = []; } + handleError(error, attempt, chunk) { + if(attempt < MAX_RETRY$1 && error) { + const index = this._endpoints.indexOf(this._endpoint)+1; + this._failover(); + this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; + this.requestChunk(chunk, this._endpoint, attempt+1); + } else { + chunk.forEach((inflightRequest) => { + inflightRequest.reject(error); + }); + } + } + detectNetwork() { return Promise.resolve(Blockchains.findByName(this._network).id) } + batchRequest(batch, attempt) { + return new Promise((resolve, reject) => { + + if (batch.length === 0) resolve([]); // Do nothing if requests is empty + + fetch( + this._endpoint, + { + method: 'POST', + body: JSON.stringify(batch), + headers: { 'Content-Type': 'application/json' }, + } + ).then((response)=>{ + if(response.ok) { + response.json().then((parsedJson)=>{ + if(parsedJson.find((entry)=>_optionalChain$5([entry, 'optionalAccess', _ => _.error]))) { + if(attempt < MAX_RETRY$1) { + reject('Error in batch found!'); + } else { + resolve(parsedJson); + } + } else { + resolve(parsedJson); + } + }).catch(reject); + } else { + reject(`${response.status} ${response.text}`); + } + }).catch(reject); + }) + } + requestChunk(chunk, endpoint, attempt) { - try { + const batch = chunk.map((inflight) => inflight.request); - const request = chunk.map((inflight) => inflight.request); - return ethers.utils.fetchJson(endpoint, JSON.stringify(request)) + try { + return this.batchRequest(batch, attempt) .then((result) => { // For each result, feed it to the correct Promise, depending // on whether it was a success or error chunk.forEach((inflightRequest, index) => { const payload = result[index]; - if (_optionalChain$5([payload, 'optionalAccess', _ => _.error])) { + if (_optionalChain$5([payload, 'optionalAccess', _2 => _2.error])) { const error = new Error(payload.error.message); error.code = payload.error.code; error.data = payload.error.data; inflightRequest.reject(error); - } else if(_optionalChain$5([payload, 'optionalAccess', _2 => _2.result])) { + } else if(_optionalChain$5([payload, 'optionalAccess', _3 => _3.result])) { inflightRequest.resolve(payload.result); } else { inflightRequest.reject(); } }); - }).catch((error) => { - if(attempt < MAX_RETRY$1 && error && error.code == 'SERVER_ERROR') { - const index = this._endpoints.indexOf(this._endpoint)+1; - this._failover(); - this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; - this.requestChunk(chunk, this._endpoint, attempt+1); - } else { - chunk.forEach((inflightRequest) => { - inflightRequest.reject(error); - }); - } - }) - - } catch (e) { - - chunk.forEach((inflightRequest) => { - inflightRequest.reject(); - }); - } + }).catch((error) => this.handleError(error, attempt, chunk)) + } catch (error){ this.handleError(error, attempt, chunk); } } send(method, params) { diff --git a/dist/umd/index.evm.js b/dist/umd/index.evm.js index 1493a5d..4dc1120 100644 --- a/dist/umd/index.evm.js +++ b/dist/umd/index.evm.js @@ -106,51 +106,79 @@ this._pendingBatch = []; } + handleError(error, attempt, chunk) { + if(attempt < MAX_RETRY && error) { + const index = this._endpoints.indexOf(this._endpoint)+1; + this._failover(); + this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; + this.requestChunk(chunk, this._endpoint, attempt+1); + } else { + chunk.forEach((inflightRequest) => { + inflightRequest.reject(error); + }); + } + } + detectNetwork() { return Promise.resolve(Blockchains__default["default"].findByName(this._network).id) } + batchRequest(batch, attempt) { + return new Promise((resolve, reject) => { + + if (batch.length === 0) resolve([]); // Do nothing if requests is empty + + fetch( + this._endpoint, + { + method: 'POST', + body: JSON.stringify(batch), + headers: { 'Content-Type': 'application/json' }, + } + ).then((response)=>{ + if(response.ok) { + response.json().then((parsedJson)=>{ + if(parsedJson.find((entry)=>_optionalChain$2([entry, 'optionalAccess', _ => _.error]))) { + if(attempt < MAX_RETRY) { + reject('Error in batch found!'); + } else { + resolve(parsedJson); + } + } else { + resolve(parsedJson); + } + }).catch(reject); + } else { + reject(`${response.status} ${response.text}`); + } + }).catch(reject); + }) + } + requestChunk(chunk, endpoint, attempt) { - try { + const batch = chunk.map((inflight) => inflight.request); - const request = chunk.map((inflight) => inflight.request); - return ethers.ethers.utils.fetchJson(endpoint, JSON.stringify(request)) + try { + return this.batchRequest(batch, attempt) .then((result) => { // For each result, feed it to the correct Promise, depending // on whether it was a success or error chunk.forEach((inflightRequest, index) => { const payload = result[index]; - if (_optionalChain$2([payload, 'optionalAccess', _ => _.error])) { + if (_optionalChain$2([payload, 'optionalAccess', _2 => _2.error])) { const error = new Error(payload.error.message); error.code = payload.error.code; error.data = payload.error.data; inflightRequest.reject(error); - } else if(_optionalChain$2([payload, 'optionalAccess', _2 => _2.result])) { + } else if(_optionalChain$2([payload, 'optionalAccess', _3 => _3.result])) { inflightRequest.resolve(payload.result); } else { inflightRequest.reject(); } }); - }).catch((error) => { - if(attempt < MAX_RETRY && error && error.code == 'SERVER_ERROR') { - const index = this._endpoints.indexOf(this._endpoint)+1; - this._failover(); - this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; - this.requestChunk(chunk, this._endpoint, attempt+1); - } else { - chunk.forEach((inflightRequest) => { - inflightRequest.reject(error); - }); - } - }) - - } catch (e) { - - chunk.forEach((inflightRequest) => { - inflightRequest.reject(); - }); - } + }).catch((error) => this.handleError(error, attempt, chunk)) + } catch (error){ this.handleError(error, attempt, chunk); } } send(method, params) { diff --git a/dist/umd/index.js b/dist/umd/index.js index 400d0d8..6a54990 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -48,51 +48,79 @@ this._pendingBatch = []; } + handleError(error, attempt, chunk) { + if(attempt < MAX_RETRY$1 && error) { + const index = this._endpoints.indexOf(this._endpoint)+1; + this._failover(); + this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; + this.requestChunk(chunk, this._endpoint, attempt+1); + } else { + chunk.forEach((inflightRequest) => { + inflightRequest.reject(error); + }); + } + } + detectNetwork() { return Promise.resolve(Blockchains__default["default"].findByName(this._network).id) } + batchRequest(batch, attempt) { + return new Promise((resolve, reject) => { + + if (batch.length === 0) resolve([]); // Do nothing if requests is empty + + fetch( + this._endpoint, + { + method: 'POST', + body: JSON.stringify(batch), + headers: { 'Content-Type': 'application/json' }, + } + ).then((response)=>{ + if(response.ok) { + response.json().then((parsedJson)=>{ + if(parsedJson.find((entry)=>_optionalChain$5([entry, 'optionalAccess', _ => _.error]))) { + if(attempt < MAX_RETRY$1) { + reject('Error in batch found!'); + } else { + resolve(parsedJson); + } + } else { + resolve(parsedJson); + } + }).catch(reject); + } else { + reject(`${response.status} ${response.text}`); + } + }).catch(reject); + }) + } + requestChunk(chunk, endpoint, attempt) { - try { + const batch = chunk.map((inflight) => inflight.request); - const request = chunk.map((inflight) => inflight.request); - return ethers.ethers.utils.fetchJson(endpoint, JSON.stringify(request)) + try { + return this.batchRequest(batch, attempt) .then((result) => { // For each result, feed it to the correct Promise, depending // on whether it was a success or error chunk.forEach((inflightRequest, index) => { const payload = result[index]; - if (_optionalChain$5([payload, 'optionalAccess', _ => _.error])) { + if (_optionalChain$5([payload, 'optionalAccess', _2 => _2.error])) { const error = new Error(payload.error.message); error.code = payload.error.code; error.data = payload.error.data; inflightRequest.reject(error); - } else if(_optionalChain$5([payload, 'optionalAccess', _2 => _2.result])) { + } else if(_optionalChain$5([payload, 'optionalAccess', _3 => _3.result])) { inflightRequest.resolve(payload.result); } else { inflightRequest.reject(); } }); - }).catch((error) => { - if(attempt < MAX_RETRY$1 && error && error.code == 'SERVER_ERROR') { - const index = this._endpoints.indexOf(this._endpoint)+1; - this._failover(); - this._endpoint = index >= this._endpoints.length ? this._endpoints[0] : this._endpoints[index]; - this.requestChunk(chunk, this._endpoint, attempt+1); - } else { - chunk.forEach((inflightRequest) => { - inflightRequest.reject(error); - }); - } - }) - - } catch (e) { - - chunk.forEach((inflightRequest) => { - inflightRequest.reject(); - }); - } + }).catch((error) => this.handleError(error, attempt, chunk)) + } catch (error){ this.handleError(error, attempt, chunk); } } send(method, params) {