Skip to content

Commit 7899fe7

Browse files
committed
Some code cleanup.
1 parent c3d00c6 commit 7899fe7

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

src/HttpsProxySocket.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,18 @@ export class HttpsProxySocket {
111111
cb(err, null);
112112
}
113113

114+
const END_OF_HEADERS = '\r\n\r\n';
115+
114116
function ondata(b: Buffer) {
115117
buffers.push(b);
116118
buffersLength += b.length;
117-
var buffered = Buffer.concat(buffers, buffersLength);
118-
var str = buffered.toString('ascii');
119119

120-
if (!~str.indexOf('\r\n\r\n')) {
120+
// Headers (including URLs) are generally ISO-8859-1 or ASCII.
121+
// The subset used by an HTTPS proxy should always be safe as ASCII.
122+
const buffered = Buffer.concat(buffers, buffersLength);
123+
const str = buffered.toString('ascii');
124+
125+
if (str.indexOf(END_OF_HEADERS) < 0) {
121126
// keep buffering
122127
debug('have not received end of HTTP headers yet...');
123128
if (socket.read) {
@@ -128,16 +133,16 @@ export class HttpsProxySocket {
128133
return;
129134
}
130135

131-
var firstLine = str.substring(0, str.indexOf('\r\n'));
132-
var statusCode = +firstLine.split(' ')[1];
136+
const firstLine = str.substring(0, str.indexOf('\r\n'));
137+
const statusCode = parseInt(firstLine.split(' ')[1], 10);
133138
debug('got proxy server response: %o', firstLine);
134139

135140
if (200 == statusCode) {
136141
// 200 Connected status code!
137-
var sock = socket;
142+
const sock = socket;
138143

139144
// nullify the buffered data since we won't be needing it
140-
buffers = buffered = null;
145+
buffers = null;
141146

142147
cleanup();
143148
cb(null, sock);
@@ -148,7 +153,7 @@ export class HttpsProxySocket {
148153
cleanup();
149154

150155
// nullify the buffered data since we won't be needing it
151-
buffers = buffered = null;
156+
buffers = null;
152157

153158
cleanup();
154159
socket.end();

src/TediousPatch.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ export function useProxy(proxy: HttpsProxySocket) {
1212
const { Connector } = require('tedious/lib/connector');
1313
Connector.prototype.execute = async function(cb: any) {
1414
debug(`opening sql connection to ${this.options.host}:${this.options.port}`);
15-
proxy.connect(this.options).then(
16-
socket => {
17-
cb(null, socket);
18-
},
19-
error => {
20-
cb(error);
21-
}
22-
);
15+
try {
16+
const socket = await proxy.connect(this.options);
17+
cb(null, socket);
18+
} catch (error) {
19+
cb(error);
20+
}
2321
};
2422
}

0 commit comments

Comments
 (0)