Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit b89a4da

Browse files
authored
Merge pull request #2980 from AsyncLegs/custom-http-agent-support
Added support for custom http agent
2 parents 7918661 + 35a5e4e commit b89a4da

File tree

7 files changed

+99
-19
lines changed

7 files changed

+99
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Released with 1.0.0-beta.37 code base.
110110
- Revert instruction handling added which can get activated with the ``handleRevert`` module property (#3248)
111111
- The ``receipt`` does now exist as property on the error object for transaction related errors (#3259)
112112
- ``internalType`` added to ``AbiInput`` TS interface in ``web3-utils`` (#3279)
113+
- Agent option added to the ``HttpProvider`` options (#2980)
113114

114115
### Changed
115116

packages/web3-core-helpers/types/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919

2020
import * as net from 'net';
21+
import * as http from 'http';
22+
import * as https from 'https';
2123

2224
export class formatters {
2325
static outputBigNumberFormatter(number: number): number;
@@ -158,6 +160,13 @@ export interface HttpProviderOptions {
158160
timeout?: number;
159161
headers?: HttpHeader[];
160162
withCredentials?: boolean;
163+
agent?: HttpAgent
164+
}
165+
166+
export interface HttpAgent {
167+
http?: http.Agent;
168+
https?: https.Agent;
169+
baseUrl?: string;
161170
}
162171

163172
export interface HttpHeader {

packages/web3-providers-http/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ This will expose the `Web3HttpProvider` object on the window object.
2828

2929
```js
3030
// in node.js
31+
var http = require('http');
3132
var Web3HttpProvider = require('web3-providers-http');
3233

3334
var options = {
35+
keepAlive: true,
3436
timeout: 20000, // milliseconds,
35-
headers: [{name: 'Access-Control-Allow-Origin', value: '*'},{...}]
37+
headers: [{name: 'Access-Control-Allow-Origin', value: '*'},{...}],
38+
withCredentials: false,
39+
agent: {http: http.Agent(...), baseUrl: ''}
3640
};
37-
var http = new Web3HttpProvider('http://localhost:8545', options);
41+
42+
var provider = new Web3HttpProvider('http://localhost:8545', options);
3843
```
3944

4045
## Types

packages/web3-providers-http/src/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ var https = require('https');
3434
var HttpProvider = function HttpProvider(host, options) {
3535
options = options || {};
3636

37-
var keepAlive = (options.keepAlive === true || options.keepAlive !== false) ? true : false;
38-
this.host = host || 'http://localhost:8545';
39-
if (this.host.substring(0,5) === "https") {
40-
this.httpsAgent = new https.Agent({ keepAlive: keepAlive });
41-
} else {
42-
this.httpAgent = new http.Agent({ keepAlive: keepAlive });
43-
}
44-
4537
this.withCredentials = options.withCredentials || false;
4638
this.timeout = options.timeout || 0;
4739
this.headers = options.headers;
40+
this.agent = options.agent;
4841
this.connected = false;
42+
43+
var keepAlive = (options.keepAlive === true || options.keepAlive !== false) ? true : false;
44+
this.host = host || 'http://localhost:8545';
45+
if (!this.agent) {
46+
if (this.host.substring(0,5) === "https") {
47+
this.httpsAgent = new https.Agent({ keepAlive: keepAlive });
48+
} else {
49+
this.httpAgent = new http.Agent({ keepAlive: keepAlive });
50+
}
51+
}
4952
};
5053

5154
HttpProvider.prototype._prepareRequest = function(){
@@ -56,10 +59,15 @@ HttpProvider.prototype._prepareRequest = function(){
5659
request = new XMLHttpRequest();
5760
} else {
5861
request = new XHR2();
59-
request.nodejsSet({
60-
httpsAgent:this.httpsAgent,
61-
httpAgent:this.httpAgent
62-
});
62+
var agents = {httpsAgent: this.httpsAgent, httpAgent: this.httpAgent, baseUrl: this.baseUrl};
63+
64+
if (this.agent) {
65+
agents.httpsAgent = this.agent.https;
66+
agents.httpAgent = this.agent.http;
67+
agents.baseUrl = this.agent.baseUrl;
68+
}
69+
70+
request.nodejsSet(agents);
6371
}
6472

6573
request.open('POST', this.host, true);

packages/web3-providers-http/types/tests/web3-provider-http-tests.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* @date 2018
2121
*/
2222

23+
import * as http from 'http';
24+
import * as https from 'https';
2325
import { HttpProvider } from 'web3-providers';
2426
import { JsonRpcResponse } from 'web3-core-helpers';
2527

@@ -31,7 +33,12 @@ const httpProvider = new HttpProvider('http://localhost:8545', {
3133
value: '*'
3234
}
3335
],
34-
withCredentials: false
36+
withCredentials: false,
37+
agent: {
38+
baseUrl: 'base',
39+
http: new http.Agent({}),
40+
https: new https.Agent({})
41+
}
3542
});
3643

3744
// $ExpectType void

test/helpers/FakeXHR2.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ var FakeXHR2 = function () {
77
this.readyState = 4;
88
this.onreadystatechange = null;
99
this.async = true;
10+
this.agents = {};
1011
this.headers = {
1112
'Content-Type': 'text/plain'
1213
};
1314
};
1415

16+
FakeXHR2.prototype.nodejsSet = function (agents) {
17+
this.agents = agents;
18+
};
19+
1520
FakeXHR2.prototype.open = function (method, host, async) {
1621
assert.equal(method, 'POST');
1722
assert.notEqual(host, null);
@@ -33,6 +38,4 @@ FakeXHR2.prototype.send = function (payload) {
3338
}
3439
};
3540

36-
FakeXHR2.prototype.nodejsSet = Function.prototype;
37-
3841
module.exports = {XMLHttpRequest: FakeXHR2};

test/httpprovider.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var chai = require('chai');
22
var assert = chai.assert;
3+
var http = require('http');
4+
var https = require('https');
35
var SandboxedModule = require('sandboxed-module');
46

57
SandboxedModule.registerBuiltInSourceTransformer('istanbul');
@@ -13,13 +15,58 @@ var HttpProvider = SandboxedModule.require('../packages/web3-providers-http', {
1315

1416
describe('web3-providers-http', function () {
1517
describe('prepareRequest', function () {
18+
let provider;
19+
let result;
20+
let options;
21+
let agent;
22+
1623
it('should set request header', function () {
17-
var provider = new HttpProvider('http://localhost:8545', {headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]});
18-
var result = provider._prepareRequest();
24+
provider = new HttpProvider('http://localhost:8545', {headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]});
25+
result = provider._prepareRequest();
1926

2027
assert.equal(typeof result, 'object');
2128
assert.equal(result.headers['Access-Control-Allow-Origin'], '*');
2229
});
30+
31+
it('should use the passed custom http agent', function () {
32+
agent = new http.Agent();
33+
options = {agent: {http: agent}};
34+
provider = new HttpProvider('http://localhost:8545', options);
35+
result = provider._prepareRequest();
36+
37+
assert.equal(typeof result, 'object');
38+
assert.equal(result.agents.httpAgent, agent);
39+
assert.equal(provider.httpAgent, undefined);
40+
assert.equal(provider.httpsAgent, undefined);
41+
assert.equal(provider.agent, options.agent);
42+
});
43+
44+
it('should use the passed custom https agent', function () {
45+
agent = new https.Agent();
46+
options = {agent: {https: agent}};
47+
provider = new HttpProvider('http://localhost:8545', options);
48+
result = provider._prepareRequest();
49+
50+
assert.equal(typeof result, 'object');
51+
assert.equal(result.agents.httpsAgent, agent);
52+
assert.equal(provider.httpAgent, undefined);
53+
assert.equal(provider.httpsAgent, undefined);
54+
assert.equal(provider.agent, options.agent);
55+
});
56+
57+
it('should use the passed baseUrl', function () {
58+
agent = new https.Agent();
59+
options = {agent: {https: agent, baseUrl: 'base'}};
60+
provider = new HttpProvider('http://localhost:8545', options);
61+
result = provider._prepareRequest();
62+
63+
assert.equal(typeof result, 'object');
64+
assert.equal(result.agents.httpsAgent, agent);
65+
assert.equal(result.agents.baseUrl, 'base');
66+
assert.equal(provider.httpAgent, undefined);
67+
assert.equal(provider.httpsAgent, undefined);
68+
assert.equal(provider.agent, options.agent);
69+
});
2370
});
2471

2572
describe('send', function () {

0 commit comments

Comments
 (0)