|
| 1 | +import axios, { AxiosRequestConfig } from "axios"; |
| 2 | +import type { JsonRpcResponse } from "web3-core-helpers"; |
| 3 | +import { errors } from "web3-core-helpers"; |
| 4 | +import _Web3HttpProvider, { HttpProvider } from "web3-providers-http"; |
| 5 | +const JSONStream = require("JSONStream"); |
| 6 | + |
| 7 | +// they export types, but in the wrong place |
| 8 | +const Web3HttpProvider = (_Web3HttpProvider as any) as typeof HttpProvider; |
| 9 | + |
| 10 | +export class StreamingWeb3HttpProvider extends Web3HttpProvider { |
| 11 | + /** |
| 12 | + * Should be used to make async request |
| 13 | + * |
| 14 | + * @method send |
| 15 | + * @param {Object} payload |
| 16 | + * @param {Function} callback triggered on end with (err, result) |
| 17 | + */ |
| 18 | + send( |
| 19 | + payload: any, |
| 20 | + callback: (error: Error | null, result: JsonRpcResponse | undefined) => void |
| 21 | + ) { |
| 22 | + if ( |
| 23 | + typeof payload === "object" && |
| 24 | + payload.method === "debug_traceTransaction" |
| 25 | + ) { |
| 26 | + const requestOptions: AxiosRequestConfig = { |
| 27 | + method: "post", |
| 28 | + url: this.host, |
| 29 | + responseType: "stream", |
| 30 | + data: payload, |
| 31 | + timeout: this.timeout, |
| 32 | + withCredentials: this.withCredentials, |
| 33 | + headers: this.headers |
| 34 | + ? this.headers.reduce((acc, header) => { |
| 35 | + acc[header.name] = header.value; |
| 36 | + return acc; |
| 37 | + }, {} as Record<string, string>) |
| 38 | + : undefined |
| 39 | + }; |
| 40 | + // transitional.clarifyTimeoutError is required so we can detect and emit |
| 41 | + // a timeout error the way web3 already does |
| 42 | + (requestOptions as any).transitional = { |
| 43 | + clarifyTimeoutError: true |
| 44 | + }; |
| 45 | + const agents = { |
| 46 | + httpsAgent: (this as any).httpsAgent, |
| 47 | + httpAgent: (this as any).httpAgent, |
| 48 | + baseUrl: (this as any).baseUrl |
| 49 | + }; |
| 50 | + if (this.agent) { |
| 51 | + agents.httpsAgent = this.agent.https; |
| 52 | + agents.httpAgent = this.agent.http; |
| 53 | + agents.baseUrl = this.agent.baseUrl; |
| 54 | + } |
| 55 | + requestOptions.httpAgent = agents.httpAgent; |
| 56 | + requestOptions.httpsAgent = agents.httpsAgent; |
| 57 | + requestOptions.baseURL = agents.baseUrl; |
| 58 | + |
| 59 | + axios(requestOptions) |
| 60 | + .then(async response => { |
| 61 | + let error = null; |
| 62 | + let result: any = {}; |
| 63 | + try { |
| 64 | + for await (const { key, value } of response.data.pipe( |
| 65 | + JSONStream.parse([true, { emitKey: true }]) |
| 66 | + )) { |
| 67 | + result[key] = value; |
| 68 | + } |
| 69 | + } catch (e) { |
| 70 | + error = errors.InvalidResponse(e); |
| 71 | + } |
| 72 | + this.connected = true; |
| 73 | + // process.nextTick so an exception thrown in the callback doesn't |
| 74 | + // bubble back up to here |
| 75 | + process.nextTick(callback, error, result); |
| 76 | + }) |
| 77 | + .catch(error => { |
| 78 | + this.connected = false; |
| 79 | + if (error.code === "ETIMEDOUT") { |
| 80 | + // web3 passes timeout as a number to ConnectionTimeout, despite the |
| 81 | + // type requiring a string |
| 82 | + callback(errors.ConnectionTimeout(this.timeout as any), undefined); |
| 83 | + } else { |
| 84 | + callback(errors.InvalidConnection(this.host), undefined); |
| 85 | + } |
| 86 | + }); |
| 87 | + } else { |
| 88 | + return super.send(payload, callback); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments