diff --git a/packages/web3-providers/package-lock.json b/packages/web3-providers/package-lock.json index 7900465cc22..ba790901802 100644 --- a/packages/web3-providers/package-lock.json +++ b/packages/web3-providers/package-lock.json @@ -1,6 +1,6 @@ { "name": "web3-providers", - "version": "1.0.0-beta.49", + "version": "1.0.0-beta.50", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/packages/web3-providers/src/factories/ProvidersModuleFactory.js b/packages/web3-providers/src/factories/ProvidersModuleFactory.js index 31e148dc08a..d669a970804 100644 --- a/packages/web3-providers/src/factories/ProvidersModuleFactory.js +++ b/packages/web3-providers/src/factories/ProvidersModuleFactory.js @@ -81,17 +81,25 @@ export default class ProvidersModuleFactory { * @param {Number} timeout * @param {Array} headers * @param {Object} agent + * @param {Boolean} withCredentials * * @returns {XMLHttpRequest} */ - createXMLHttpRequest(host, timeout = 0, headers, agent) { - const request = new XHR(); - request.nodejsSet(agent); + createXMLHttpRequest(host, timeout = 0, headers, agent, withCredentials) { + let request; + + // runtime is of type node + if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) { + request = new XHR(); + request.nodejsSet(agent); + } else { + request = new XMLHttpRequest(); + } request.open('POST', host, true); request.setRequestHeader('Content-Type', 'application/json'); request.timeout = timeout; - request.withCredentials = true; + request.withCredentials = withCredentials; if (headers) { headers.forEach((header) => { diff --git a/packages/web3-providers/src/providers/HttpProvider.js b/packages/web3-providers/src/providers/HttpProvider.js index 14235c1f72e..f513c3c2f4c 100644 --- a/packages/web3-providers/src/providers/HttpProvider.js +++ b/packages/web3-providers/src/providers/HttpProvider.js @@ -37,6 +37,7 @@ export default class HttpProvider { this.host = host; this.timeout = options.timeout || 0; this.headers = options.headers; + this.withCredentials = options.withCredentials || false; this.connected = true; this.providersModuleFactory = providersModuleFactory; this.agent = {}; @@ -135,7 +136,8 @@ export default class HttpProvider { this.host, this.timeout, this.headers, - this.agent + this.agent, + this.withCredentials ); request.onreadystatechange = () => { diff --git a/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js b/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js index 26117f5a664..ac10ba8225b 100644 --- a/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js +++ b/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js @@ -43,7 +43,7 @@ describe('ProvidersModuleFactoryTest', () => { }); it('createXMLHttpRequest returns instance of XMLHttpRequest', () => { - expect(providersModuleFactory.createXMLHttpRequest('', 0, [{name: 'name', value: 'value'}], {})).toBeInstanceOf( + expect(providersModuleFactory.createXMLHttpRequest('', 0, [{name: 'name', value: 'value'}], {}, true)).toBeInstanceOf( XHR ); diff --git a/packages/web3-providers/tests/src/providers/HttpProviderTest.js b/packages/web3-providers/tests/src/providers/HttpProviderTest.js index 6ee355fb13b..afad977c05d 100644 --- a/packages/web3-providers/tests/src/providers/HttpProviderTest.js +++ b/packages/web3-providers/tests/src/providers/HttpProviderTest.js @@ -27,7 +27,7 @@ describe('HttpProviderTest', () => { httpProvider = new HttpProvider( 'https', - {headers: [], timeout: 1, keepAlive: true}, + {headers: [], timeout: 1, keepAlive: true, withCredentials: true}, providersModuleFactoryMock ); }); @@ -47,7 +47,33 @@ describe('HttpProviderTest', () => { }); it('constructor check with http', () => { - httpProvider = new HttpProvider('http', {headers: [], timeout: 1}, providersModuleFactoryMock); + httpProvider = new HttpProvider( + 'http', + {headers: [], timeout: 1, keepAlive: true, withCredentials: true}, + providersModuleFactoryMock + ); + + expect(httpProvider.host).toEqual('http'); + + expect(httpProvider.headers).toEqual([]); + + expect(httpProvider.timeout).toEqual(1); + + expect(httpProvider.connected).toEqual(true); + + expect(httpProvider.withCredentials).toEqual(true); + + expect(httpProvider.providersModuleFactory).toEqual(providersModuleFactoryMock); + + expect(httpProvider.agent.httpAgent).toBeInstanceOf(http.Agent); + }); + + it('constructor check without the property withCredentials in the options', () => { + httpProvider = new HttpProvider( + 'http', + {headers: [], timeout: 1}, + providersModuleFactoryMock + ); expect(httpProvider.host).toEqual('http'); @@ -57,6 +83,8 @@ describe('HttpProviderTest', () => { expect(httpProvider.connected).toEqual(true); + expect(httpProvider.withCredentials).toEqual(false); + expect(httpProvider.providersModuleFactory).toEqual(providersModuleFactoryMock); expect(httpProvider.agent.httpAgent).toBeInstanceOf(http.Agent); @@ -110,7 +138,8 @@ describe('HttpProviderTest', () => { httpProvider.host, httpProvider.timeout, httpProvider.headers, - httpProvider.agent + httpProvider.agent, + httpProvider.withCredentials, ); expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}'); @@ -148,7 +177,8 @@ describe('HttpProviderTest', () => { httpProvider.host, httpProvider.timeout, httpProvider.headers, - httpProvider.agent + httpProvider.agent, + httpProvider.withCredentials ); expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}'); @@ -189,7 +219,8 @@ describe('HttpProviderTest', () => { httpProvider.host, httpProvider.timeout, httpProvider.headers, - httpProvider.agent + httpProvider.agent, + httpProvider.withCredentials ); expect(xhrMock.send).toHaveBeenCalledWith('[{"id":"0x0"}]'); @@ -219,7 +250,8 @@ describe('HttpProviderTest', () => { httpProvider.host, httpProvider.timeout, httpProvider.headers, - httpProvider.agent + httpProvider.agent, + httpProvider.withCredentials ); expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}'); @@ -247,7 +279,8 @@ describe('HttpProviderTest', () => { httpProvider.host, httpProvider.timeout, httpProvider.headers, - httpProvider.agent + httpProvider.agent, + httpProvider.withCredentials ); expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}'); @@ -273,7 +306,8 @@ describe('HttpProviderTest', () => { httpProvider.host, httpProvider.timeout, httpProvider.headers, - httpProvider.agent + httpProvider.agent, + httpProvider.withCredentials ); expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}'); @@ -297,7 +331,8 @@ describe('HttpProviderTest', () => { httpProvider.host, httpProvider.timeout, httpProvider.headers, - httpProvider.agent + httpProvider.agent, + httpProvider.withCredentials ); expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}'); diff --git a/packages/web3-providers/types/index.d.ts b/packages/web3-providers/types/index.d.ts index a90545bcb53..3d7a53b66da 100644 --- a/packages/web3-providers/types/index.d.ts +++ b/packages/web3-providers/types/index.d.ts @@ -156,6 +156,7 @@ export interface HttpProviderOptions { host?: string; timeout?: number; headers?: {}; + withCredentials?: boolean; } export interface WebsocketProviderOptions { diff --git a/packages/web3-providers/types/tests/http-provider-test.ts b/packages/web3-providers/types/tests/http-provider-test.ts index 9163f77d53c..a5ce2f53d9b 100644 --- a/packages/web3-providers/types/tests/http-provider-test.ts +++ b/packages/web3-providers/types/tests/http-provider-test.ts @@ -30,7 +30,8 @@ const httpProvider = new HttpProvider('http://localhost:8545', { name: 'Access-Control-Allow-Origin', value: '*' } - ] + ], + withCredentials: false }); // $ExpectType Promise