Skip to content

Commit

Permalink
Ensure early writes are queued (microsoft/vscode#185098)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Jul 7, 2023
1 parent 2292b05 commit 3821172
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ function patchNetConnect(params: ProxyAgentParams, original: typeof net.connect)
}
params.log(LogLevel.Trace, 'ProxyResolver#net.connect', ...args);
const socket = new net.Socket();
(socket as any).connecting = true;
getCaCertificates(params)
.then(() => {
socket.connect.apply(socket, arguments as any);
Expand Down Expand Up @@ -433,6 +434,7 @@ function patchTlsConnect(params: ProxyAgentParams, original: typeof tls.connect)
}
params.log(LogLevel.Trace, 'ProxyResolver#connect creating unconnected socket');
const socket = options.socket = new net.Socket();
(socket as any).connecting = true;
getCaCertificates(params)
.then(caCertificates => {
params.log(LogLevel.Trace, 'ProxyResolver#connect adding certs before connecting socket');
Expand Down
2 changes: 1 addition & 1 deletion tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
- test-servers
working_dir: /repo/tests/test-client
environment:
- MOCHA_TESTS=src/direct.test.ts src/tls.test.ts
- MOCHA_TESTS=src/direct.test.ts src/tls.test.ts src/socket.test.ts
command: /bin/sh -c 'rm -rf /root/.npm && npm run test:watch'
test-proxy-client:
image: node:16
Expand Down
55 changes: 55 additions & 0 deletions tests/test-client/src/socket.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as net from 'net';
import * as tls from 'tls';
import { createNetPatch, createTlsPatch, resetCaches, SecureContextOptionsPatch } from '../../../src/index';
import { testRequest, ca, directProxyAgentParams } from './utils';
import * as assert from 'assert';

describe('Socket client', function () {
it('net.connect() should work without delay', async () => {
const netPatched = {
...net,
...createNetPatch(directProxyAgentParams, net),
};
const socket = netPatched.connect(808, 'test-https-server');
const p = new Promise<string>((resolve, reject) => {
socket.on('error', reject);
const chunks: Buffer[] = [];
socket.on('data', chunk => chunks.push(chunk));
socket.on('end', () => {
resolve(Buffer.concat(chunks).toString());
});
});
socket.write(`GET /test-path-unencrypted HTTP/1.1
Host: test-http-server
`);
const response = await p;
assert.ok(response.startsWith('HTTP/1.1 200 OK'), `Unexpected response: ${response}`);
});

it('tls.connect() should work without delay', async () => {
const tlsPatched = {
...tls,
...createTlsPatch(directProxyAgentParams, tls),
};
const socket = tlsPatched.connect({
host: 'test-https-server',
port: 443,
servername: 'test-https-server', // for SNI
});
const p = new Promise<string>((resolve, reject) => {
socket.on('error', reject);
const chunks: Buffer[] = [];
socket.on('data', chunk => chunks.push(chunk));
socket.on('end', () => {
resolve(Buffer.concat(chunks).toString());
});
});
socket.write(`GET /test-path HTTP/1.1
Host: test-https-server
`);
const response = await p;
assert.ok(response.startsWith('HTTP/1.1 200 OK'), `Unexpected response: ${response}`);
});
});
19 changes: 19 additions & 0 deletions tests/test-https-server/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,23 @@ http {
add_header Content-Type application/json;
}
}

server {
listen 808;
server_name test-http-server;

location / {
return 404;
}

location = /test-path-unencrypted {
return 200 '{
"status": "OK!",
"headers": {
"host": "$http_host"
}
}';
add_header Content-Type application/json;
}
}
}

0 comments on commit 3821172

Please sign in to comment.