Skip to content

Commit 6c9481a

Browse files
test
1 parent 98ca849 commit 6c9481a

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/HttpsProxySocket.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { createProxyAgent } from './createProxyAgent';
33
import { setServername } from './utils/setServername';
44
import { parseOptions } from './utils/parseOptions';
55

6-
// import { debug as nodeDebug } from 'util';
7-
//
8-
// const debug = nodeDebug('https-proxy');
6+
import { debug as nodeDebug } from 'util';
7+
8+
const debug = nodeDebug('https-proxy');
99

1010
export interface HttpsProxyConfig extends tls.ConnectionOptions {
1111
headers?: { [key: string]: string };
@@ -38,7 +38,7 @@ export class HttpsProxySocket {
3838
if (!options) {
3939
throw new Error('an HTTP(S) proxy server `host` and `port` must be specified!');
4040
}
41-
console.log('creating new HttpsProxyAgent instance: %o', sanitizedOptions);
41+
debug('creating new HttpsProxyAgent instance: %o', sanitizedOptions);
4242

4343
this.proxyConfig = proxyConfig || {};
4444
this.proxy = sanitizedOptions as tls.ConnectionOptions;
@@ -104,11 +104,11 @@ export class HttpsProxySocket {
104104
}
105105

106106
function onclose(err: any) {
107-
console.log('onclose had error %o', err);
107+
debug('onclose had error %o', err);
108108
}
109109

110110
function onend() {
111-
console.log('onend');
111+
debug('onend');
112112
}
113113

114114
function onerror(err: any) {
@@ -129,7 +129,7 @@ export class HttpsProxySocket {
129129

130130
if (str.indexOf(END_OF_HEADERS) < 0) {
131131
// keep buffering
132-
console.log('have not received end of HTTP headers yet...');
132+
debug('have not received end of HTTP headers yet...');
133133
if (socket.readable) {
134134
read();
135135
} else {
@@ -140,7 +140,7 @@ export class HttpsProxySocket {
140140

141141
const firstLine = str.substring(0, str.indexOf('\r\n'));
142142
const statusCode = parseInt(firstLine.split(' ')[1], 10);
143-
console.log('got proxy server response: %o', firstLine);
143+
debug('got proxy server response: %o', firstLine);
144144

145145
if (200 == statusCode) {
146146
// 200 Connected status code!

src/mongoPatch.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ interface Config {
1515
*/
1616
export function useProxyForMongo(config: Config) {
1717
const sockets: tls.TLSSocket[] = [];
18-
let proxy: HttpsProxySocket | undefined = new HttpsProxySocket(`https://${config.proxy}`, { auth: config.auth });
18+
const proxy: HttpsProxySocket = new HttpsProxySocket(`https://${config.proxy}`, { auth: config.auth });
1919
socks.SocksClient.createConnection = async (options, callback) => {
20-
const socket = await proxy!.connect({ host: options.destination.host, port: options.destination.port });
20+
const socket = await proxy.connect({ host: options.destination.host, port: options.destination.port });
21+
22+
socket.on('error', (err) => {
23+
if (err) {
24+
console.error('MongoDB connection socket error:', err);
25+
}
26+
});
2127
sockets.push(socket);
2228
return {
2329
socket,
@@ -32,13 +38,14 @@ export function useProxyForMongo(config: Config) {
3238
new Promise<void>((resolve) => {
3339
socket.once('close', () => {
3440
count++;
41+
socket.removeAllListeners();
42+
socket.destroySoon();
3543
resolve();
3644
});
3745
socket.end();
3846
}),
3947
),
4048
);
41-
proxy = undefined;
4249
if (count === sockets.length) {
4350
console.log(`Closed ${sockets.length} MongoDB connection sockets`);
4451
}

0 commit comments

Comments
 (0)