Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nodejs warning (close #2051) #2109

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/processing/encoding/charset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum CharsetPriority {
META = 1,
DEFAULT = 0
}
/*eslint-disable no-unused-vars*/
/*eslint-enable no-unused-vars*/

export default class Charset {
charset: string = DEFAULT_CHARSET;
Expand Down
30 changes: 18 additions & 12 deletions src/request-pipeline/destination-request/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import RequestOptions from '../request-options';
import http from 'http';
import https from 'https';
import LRUCache from 'lru-cache';
// @ts-ignore
import tunnel from 'tunnel-agent';

const SSL3_HOST_CACHE_SIZE: number = 1000;

const TYPE = {
SSL3: 'SSL3',
TLS: 'TLS',
HTTP: 'HTTP'
};
/* eslint-disable no-unused-vars */
enum TYPE {
SSL3 = 'SSL3',
TLS = 'TLS',
HTTP = 'HTTP'
}
/* eslint-enable no-unused-vars */

const ssl3HostCache = new LRUCache({ max: SSL3_HOST_CACHE_SIZE });

Expand All @@ -36,7 +39,7 @@ const agents = {


// Utils
function getAgent (type) {
function getAgent (type: string) {
const agent = agents[type];

if (!agent.instance) {
Expand All @@ -50,8 +53,8 @@ function getAgent (type) {
return agent.instance;
}

function isSSLProtocolErr (err): boolean {
return err.message && err.message.includes('SSL routines');
function isSSLProtocolErr (err: Error): boolean {
return !!err.message && err.message.includes('SSL routines');
}


Expand All @@ -60,12 +63,15 @@ export function assign (reqOpts: RequestOptions): void {
const proxy = reqOpts.proxy;

if (proxy && reqOpts.protocol === 'https:') {
reqOpts.agent = tunnel.httpsOverHttp({ proxy });
reqOpts.agent = tunnel.httpsOverHttp({
proxy,
rejectUnauthorized: false
});

return;
}

let type = void 0;
let type: string = '';

if (reqOpts.protocol === 'http:')
type = TYPE.HTTP;
Expand All @@ -79,11 +85,11 @@ export function assign (reqOpts: RequestOptions): void {
reqOpts.agent = getAgent(type);
}

export function shouldRegressHttps (reqErr, reqOpts): boolean {
export function shouldRegressHttps (reqErr: Error, reqOpts: RequestOptions): boolean {
return reqOpts.agent === agents[TYPE.TLS] && isSSLProtocolErr(reqErr);
}

export function regressHttps (reqOpts): void {
export function regressHttps (reqOpts: RequestOptions): void {
ssl3HostCache.set(reqOpts.host, true);
reqOpts.agent = getAgent(TYPE.SSL3);
}
Expand Down
22 changes: 9 additions & 13 deletions src/request-pipeline/destination-request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import connectionResetGuard from '../connection-reset-guard';
import { MESSAGE, getText } from '../../messages';
import { transformHeadersCaseToRaw } from '../header-transforms';

// HACK: Ignore SSL auth. The rejectUnauthorized option in the https.request method
// doesn't work (see: https://github.com/mikeal/request/issues/418).
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

const TUNNELING_SOCKET_ERR_RE: RegExp = /tunneling socket could not be established/i;
const TUNNELING_AUTHORIZE_ERR_RE: RegExp = /statusCode=407/i;
const SOCKET_HANG_UP_ERR_RE: RegExp = /socket hang up/i;
Expand All @@ -41,9 +37,9 @@ export default class DestinationRequest extends EventEmitter implements Destinat
private hasResponse: boolean = false;
private credentialsSent: boolean = false;
private aborted: boolean = false;
private opts: RequestOptions;
private isHttps: boolean;
private protocolInterface: any;
private readonly opts: RequestOptions;
private readonly isHttps: boolean;
private readonly protocolInterface: any;

static TIMEOUT: number = 25 * 1000;
static XHR_TIMEOUT: number = 2 * 60 * 1000;
Expand All @@ -67,7 +63,7 @@ export default class DestinationRequest extends EventEmitter implements Destinat
this._send();
}

_send (waitForData?: boolean) {
_send (waitForData?: boolean): void {
connectionResetGuard(() => {
const timeout = this.opts.isXhr ? DestinationRequest.XHR_TIMEOUT : DestinationRequest.TIMEOUT;
const storedHeaders = this.opts.headers;
Expand Down Expand Up @@ -95,7 +91,7 @@ export default class DestinationRequest extends EventEmitter implements Destinat
});
}

_shouldResendWithCredentials (res) {
_shouldResendWithCredentials (res): boolean {
if (res.statusCode === 401 && this.opts.credentials) {
const authInfo = getAuthInfo(res);

Expand All @@ -109,7 +105,7 @@ export default class DestinationRequest extends EventEmitter implements Destinat
return false;
}

_onResponse (res: http.IncomingMessage) {
_onResponse (res: http.IncomingMessage): void {
if (this._shouldResendWithCredentials(res))
this._resendWithCredentials(res);
else if (!this.isHttps && this.opts.proxy && res.statusCode === 407)
Expand All @@ -120,14 +116,14 @@ export default class DestinationRequest extends EventEmitter implements Destinat
}
}

_onUpgrade (res: http.IncomingMessage, socket: net.Socket, head: Buffer) {
_onUpgrade (res: http.IncomingMessage, socket: net.Socket, head: Buffer): void {
if (head && head.length)
socket.unshift(head);

this._onResponse(res);
}

async _resendWithCredentials (res) {
async _resendWithCredentials (res): Promise<void> {
addCredentials(this.opts.credentials, this.opts, res, this.protocolInterface);
this.credentialsSent = true;

Expand All @@ -137,7 +133,7 @@ export default class DestinationRequest extends EventEmitter implements Destinat
this._send(requiresResBody(res));
}

_fatalError (msg: string, url?: string) {
_fatalError (msg: string, url?: string): void {
if (!this.aborted) {
this.aborted = true;
this.req.abort();
Expand Down
8 changes: 5 additions & 3 deletions src/session/events/names.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable no-unused-vars */
enum RequestEventNames {
onRequest = 'onRequest', // eslint-disable-line no-unused-vars
onConfigureResponse = 'onConfigureResponse', // eslint-disable-line no-unused-vars
onResponse = 'onResponse' // eslint-disable-line no-unused-vars
onRequest = 'onRequest',
onConfigureResponse = 'onConfigureResponse',
onResponse = 'onResponse'
}
/* eslint-enable no-unused-vars */

export default RequestEventNames;
3 changes: 2 additions & 1 deletion test/server/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2775,7 +2775,8 @@ describe('Proxy', () => {
url: proxy.openSession('http://127.0.0.1:2000/page', session),
headers: {
accept: PAGE_ACCEPT_HEADER
}
},
rejectUnauthorized: false
};

expect(options.url).eql('https://127.0.0.1:1836/sessionId/http://127.0.0.1:2000/page');
Expand Down