Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
9 changes: 9 additions & 0 deletions packages/web3-core-helpers/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/

import * as net from 'net';
import * as http from 'http';
import * as https from 'https';

export class formatters {
static outputBigNumberFormatter(number: number): number;
Expand Down Expand Up @@ -158,6 +160,13 @@ export interface HttpProviderOptions {
timeout?: number;
headers?: HttpHeader[];
withCredentials?: boolean;
agent?: HttpAgent
}

export interface HttpAgent {
http?: http.Agent;
https?: https.Agent;
baseUrl?: string;
}

export interface HttpHeader {
Expand Down
26 changes: 16 additions & 10 deletions packages/web3-providers-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ var https = require('https');
var HttpProvider = function HttpProvider(host, options) {
options = options || {};

this.withCredentials = options.withCredentials || false;
this.timeout = options.timeout || 0;
this.headers = options.headers;
this.agent = options.agent;
this.connected = false;

var keepAlive = (options.keepAlive === true || options.keepAlive !== false) ? true : false;
this.host = host || 'http://localhost:8545';
if (this.host.substring(0,5) === "https") {
if (!this.agent && this.host.substring(0,5) === "https") {
this.httpsAgent = new https.Agent({ keepAlive: keepAlive });
} else {
this.httpAgent = new http.Agent({ keepAlive: keepAlive });
}

this.withCredentials = options.withCredentials || false;
this.timeout = options.timeout || 0;
this.headers = options.headers;
this.connected = false;
};

HttpProvider.prototype._prepareRequest = function(){
Expand All @@ -56,10 +57,15 @@ HttpProvider.prototype._prepareRequest = function(){
request = new XMLHttpRequest();
} else {
request = new XHR2();
request.nodejsSet({
httpsAgent:this.httpsAgent,
httpAgent:this.httpAgent
});
var agents = {httpsAgent: this.httpsAgent, httpAgent: this.httpAgent, baseUrl: this.baseUrl};

if (this.agent) {
agents.httpsAgent = this.agent.https;
agents.httpAgent = this.agent.http;
agents.baseUrl = this.agent.baseUrl;
}

request.nodejsSet(agents);
}

request.open('POST', this.host, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @date 2018
*/

import * as http from 'http';
import * as https from 'https';
import { HttpProvider } from 'web3-providers';
import { JsonRpcResponse } from 'web3-core-helpers';

Expand All @@ -31,7 +33,12 @@ const httpProvider = new HttpProvider('http://localhost:8545', {
value: '*'
}
],
withCredentials: false
withCredentials: false,
agent: {
baseUrl: 'base',
http: new http.Agent({}),
https: new https.Agent({})
}
});

// $ExpectType void
Expand Down