npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

better-https-proxy-agent

v1.0.9

Published

An agent for HTTPS through an HTTP(S) proxy server using the CONNECT method

Downloads

5,680

Readme

better-https-proxy-agent

An agent for HTTPS through an HTTP(S) proxy server using the CONNECT method.

This is similar to https-proxy-agent but it leverages functionality available in the NodeJS http.Agent, https.Agent, http.request and/or https.request to provide:

  • connection pooling
  • timeout
  • TLS session resumption
  • authentication (basic or client certificate)
  • TLS options
  • potentially anything else the NodeJS modules support

All of the above apply both to connections to the proxy, as well as connections through the proxy.

It was partly inspired by this blog post and related gist.

Basic usage

npm install better-https-proxy-agent
const { Agent } = require('better-https-proxy-agent');
const fs = require('fs');
const https = require('https');

/*
 * Options suitable for `https.Agent`. These are applied to the HTTPS agent which
 * manages the proxied connections between the client and the final endpoint.
 */
const httpsAgentOptions = {
    keepAlive: true,
    timeout: 55000,
    maxSockets: 20,
    maxFreeSockets: 5,
    maxCachedSessions: 500
};

/*
 * Options suitable for `http(s).request`. These are used to make the request to
 * the proxy server.
 *
 * You should provide `host` here (unless you want the default `localhost`).
 *
 * `protocol` will default to `http:`. If you need to connect to the proxy over
 * HTTPS, set `protocol` to `https:`. `http.request` or `https.request` will be
 * used accordingly.
 *
 * You may include an `agent` here; if you do not, a default agent will be
 * constructed which will be reused for connections to the proxy. If you wish
 * to use the global agent, explicitly provide `http(s).globalAgent`.
 *
 * Using an agent for connection pooling will not work, because connections use
 * the HTTP CONNECT method; such connection cannot be reused, and furthermore
 * are removed from any agent as soon as a response is received. However, an
 * additional `maxSockets` option is provided directly in this options object
 * for the purpose of limiting the number of concurrent connections to a proxy.
 */
const proxyRequestOptions = {
    protocol: "https:", 
    host: "proxy.example.com",
    port: 3128,
    timeout: 123000,
    maxSockets: 100,
    cert: fs.readFileSync("proxy_auth_cert.pem"),
    key: fs.readFileSync("proxy_auth_key.pem"),
    passphrase: "secret"
};

const agent = new Agent(httpsAgentOptions, proxyRequestOptions);

https.request("https://api.example.com", {
    agent,
    cert: fs.readFileSync("api_auth_cert.pem"),
    key: fs.readFileSync("api_auth_key.pem"),
    passphrase: "secret"
});

Caveats

You can get yourself into a bit of trouble if you use maxSockets to limit the connections to the proxy, and also pool connections through the proxy. You can tie up all your proxy connections with connections through to a particular host, or few hosts (say api.example.com): the connections through the proxy will be pooled and remain open, holding the corresponding connections to the proxy open also. When you go to make a new connection to a different host (say to www.example.com) no connection through the proxy can be reused, and no new connection to the proxy can be opened either. This module isn't smart enough to close pooled connections through the proxy so that you can open a new connection to the proxy.

The timeout that is set on an HTTPS request that uses the proxy agent will be used to set the 'request timeout' (for requests through the proxy), including how long to wait for the proxy to connect to the target server, after the connection to the proxy has already been made. The timeout in the proxyRequestOptions (or agent provided in the proxyRequestOptions) controls how long to wait for a connection to be made to the proxy itself. Since it is a two-step process to connect to the proxy and then connect through the proxy, these two timeouts are cumulative, which may not be what the caller of https.request expects.

Furthermore, the timeout in the proxyRequestOptions (or agent provided in proxyRequestOptions) applies to inactivity on the proxy connection. This can occur during requests through the proxy, or between them if keepAlive is true in the httpsAgentOptions. More directly, the timeout for in-flight requests through the proxy is governed by any timeout option set on them, and the timeout for kept-alive connections is governed by timeout in the httpsAgentOptions. Consequently, timeout in proxyRequestOptions should be set higher than both of these other timeouts or it will gazump them, cutting them short.

Licence

MIT.