Skip to content

Commit

Permalink
[https-proxy-agent] Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 1, 2023
1 parent 3b258fd commit 941efab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
35 changes: 19 additions & 16 deletions packages/https-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;
type ConnectOptsMap = {
http: Omit<net.TcpNetConnectOpts, 'host' | 'port'>;
https: Omit<tls.ConnectionOptions, 'host' | 'port'>;
}
};

type ConnectOpts<T> = {
[P in keyof ConnectOptsMap]: Protocol<T> extends P
Expand All @@ -40,7 +40,7 @@ export type HttpsProxyAgentOptions<T> = ConnectOpts<T> & {
* the connection to the proxy server has been established.
*/
export class HttpsProxyAgent<Uri extends string> extends Agent {
static protocols = ["http", "https"] as const;
static protocols = ['http', 'https'] as const;

readonly proxy: URL;
proxyHeaders: OutgoingHttpHeaders;
Expand All @@ -52,12 +52,15 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {

constructor(proxy: Uri | URL, opts?: HttpsProxyAgentOptions<Uri>) {
super();
this.proxy = typeof proxy === "string" ? new URL(proxy) : proxy;
this.proxy = typeof proxy === 'string' ? new URL(proxy) : proxy;
this.proxyHeaders = opts?.headers ?? {};
debug("Creating new HttpsProxyAgent instance: %o", this.proxy.href);
debug('Creating new HttpsProxyAgent instance: %o', this.proxy.href);

// Trim off the brackets from IPv6 addresses
const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, '');
const host = (this.proxy.hostname || this.proxy.host).replace(
/^\[|\]$/g,
''
);
const port = this.proxy.port
? parseInt(this.proxy.port, 10)
: this.secureProxy
Expand Down Expand Up @@ -89,10 +92,10 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
// Create a socket connection to the proxy server.
let socket: net.Socket;
if (secureProxy) {
debug("Creating `tls.Socket`: %o", this.connectOpts);
debug('Creating `tls.Socket`: %o', this.connectOpts);
socket = tls.connect(this.connectOpts);
} else {
debug("Creating `net.Socket`: %o", this.connectOpts);
debug('Creating `net.Socket`: %o', this.connectOpts);
socket = net.connect(this.connectOpts);
}

Expand All @@ -105,9 +108,9 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
const auth = `${decodeURIComponent(
proxy.username
)}:${decodeURIComponent(proxy.password)}`;
headers["Proxy-Authorization"] = `Basic ${Buffer.from(
headers['Proxy-Authorization'] = `Basic ${Buffer.from(
auth
).toString("base64")}`;
).toString('base64')}`;
}

// The `Host` header should only include the port
Expand All @@ -118,7 +121,7 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
}
headers.Host = host;

headers.Connection = "close";
headers.Connection = 'close';
for (const name of Object.keys(headers)) {
payload += `${name}: ${headers[name]}\r\n`;
}
Expand All @@ -130,15 +133,15 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
const { statusCode, buffered } = await proxyResponsePromise;

if (statusCode === 200) {
req.once("socket", resume);
req.once('socket', resume);

if (opts.secureEndpoint) {
// The proxy is connecting to a TLS server, so upgrade
// this socket connection to a TLS connection.
debug("Upgrading socket connection to TLS");
debug('Upgrading socket connection to TLS');
const servername = opts.servername || opts.host;
const s = tls.connect({
...omit(opts, "host", "path", "port"),
...omit(opts, 'host', 'path', 'port'),
socket,
servername,
});
Expand Down Expand Up @@ -169,9 +172,9 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
fakeSocket.readable = true;

// Need to wait for the "socket" event to re-play the "data" events.
req.once("socket", (s: net.Socket) => {
debug("Replaying proxy buffer for failed request");
assert(s.listenerCount("data") > 0);
req.once('socket', (s: net.Socket) => {
debug('Replaying proxy buffer for failed request');
assert(s.listenerCount('data') > 0);

// Replay the "buffered" Buffer onto the fake `socket`, since at
// this point the HTTP module machinery has been hooked up for
Expand Down
36 changes: 14 additions & 22 deletions packages/https-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ describe('HttpsProxyAgent', () => {
beforeAll(async () => {
// setup target HTTP server
server = http.createServer();
serverUrl = await listen(server) as URL;
serverUrl = (await listen(server)) as URL;
});

beforeAll(async () => {
// setup HTTP proxy server
proxy = createProxy();
proxyUrl = await listen(proxy) as URL;
proxyUrl = (await listen(proxy)) as URL;
});

beforeAll(async () => {
// setup target HTTPS server
sslServer = https.createServer(sslOptions);
sslServerUrl = await listen(sslServer) as URL;
sslServerUrl = (await listen(sslServer)) as URL;
});

beforeAll(async () => {
// setup SSL HTTP proxy server
sslProxy = createProxy(https.createServer(sslOptions));
sslProxyUrl = await listen(sslProxy) as URL;
sslProxyUrl = (await listen(sslProxy)) as URL;
});

// shut down the test HTTP servers
Expand Down Expand Up @@ -79,15 +79,11 @@ describe('HttpsProxyAgent', () => {
});
describe('secureProxy', () => {
it('should be `false` when "http:" protocol is used', () => {
const agent = new HttpsProxyAgent(
proxyUrl
);
const agent = new HttpsProxyAgent(proxyUrl);
assert.equal(false, agent.secureProxy);
});
it('should be `true` when "https:" protocol is used', () => {
const agent = new HttpsProxyAgent(
sslProxyUrl
);
const agent = new HttpsProxyAgent(sslProxyUrl);
assert.equal(true, agent.secureProxy);
});
});
Expand Down Expand Up @@ -178,14 +174,11 @@ describe('HttpsProxyAgent', () => {
});

it('should allow custom proxy "headers"', async () => {
const agent = new HttpsProxyAgent(
serverUrl,
{
headers: {
Foo: 'bar',
},
}
);
const agent = new HttpsProxyAgent(serverUrl, {
headers: {
Foo: 'bar',
},
});

const connectPromise = once(server, 'connect');

Expand Down Expand Up @@ -252,10 +245,9 @@ describe('HttpsProxyAgent', () => {
res.end(JSON.stringify(req.headers));
});

const agent = new HttpsProxyAgent(
sslProxyUrl,
{ rejectUnauthorized: false }
);
const agent = new HttpsProxyAgent(sslProxyUrl, {
rejectUnauthorized: false,
});
agent.defaultPort = parseInt(sslServerUrl.port, 10);

const res = await req(sslServerUrl, {
Expand Down

1 comment on commit 941efab

@vercel
Copy link

@vercel vercel bot commented on 941efab May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

proxy-agents – ./

proxy-agents.vercel.app
proxy-agents-tootallnate.vercel.app
proxy-agents-git-main-tootallnate.vercel.app

Please sign in to comment.