Socket
Book a DemoInstallSign in
Socket

proxychooser

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

proxychooser

A simple proxy chooser to choose proxies from a list, which supports http/https proxies.

npmnpm
Version
1.0.2
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

ProxyChooser

A simple proxy chooser to choose proxies from a list, which supports http/https proxies.

Installation

Install the proxy-chooser package using npm:

npm install proxy-chooser

Usage

Import the ProxyChooser class from the proxy-chooser package:

const ProxyChooser = require("proxy-chooser");

Create an instance of the ProxyChooser class with a list of proxies and options:

const proxyList = ["proxy1:port1", "proxy2:port2", ...];
// All Options are not required.
const options = {
  verbose: true, // default: false
  maxTimeout: 2000, // default: 1000
  forceRetry: false // default: false
  verboseIdentifier: "[proxyChooser]" // default: "[proxyChooser]"
};

const proxyChooser = new ProxyChooser(proxyList, options);

Choosing a Proxy

To choose a proxy from the list, use the getProxy() method. It returns a promise that resolves to a randomly selected working proxy from the list:

proxyChooser.getProxy()
  .then(proxy => {
    console.log(proxy);
  })
  .catch(error => {
    console.error(error);
  });

Examples

Here's an example of how you can use the ProxyChooser class:

const ProxyChooser = require("proxy-chooser");

const proxyList = ["proxy1:port1", "proxy2:port2", ...];
const options = {
  verbose: true,
  maxTimeout: 1000
};

const proxyChooser = new ProxyChooser(proxyList, options);

proxyChooser.getProxy()
  .then(proxy => {
    console.log(proxy);
  })
  .catch(error => {
    console.error(error);
  });

API

ProxyChooser

The ProxyChooser class provides methods to choose a proxy from a list.

Constructor

Creates an instance of the ProxyChooser class.

new ProxyChooser(proxyList, options);
  • proxyList (Array): List of proxies to use.
  • options (Object): Options to configure the proxy chooser.
    • verbose (boolean?): If true, enables logging to the console. Default: false.
    • verboseIdentifier (string?): Identifier for verbose logging. Default: "[proxyChooser]".
    • maxTimeout (number?): Maximum timeout for a request in milliseconds. Default: 1000.
    • pingUrl (string?): URL to use for checking the proxy. Default: "http://myexternalip.com/raw".
    • forceRetry (boolean?): Whether the getProxy() function should continue searching even on error. Default: false.

validateOptions()

Validates the provided options and ensures they have the correct format. This method is called internally and doesn't need to be called directly.

proxyChooser.validateOptions();

proxyType(proxy)

Determines the type of the proxy based on its format.

const proxyType = proxyChooser.proxyType(proxy);
  • proxy (string): Proxy to check.
  • Returns: Proxy type as a string. Possible values: "direct", "auth", "invalid".

convertProxy(proxy)

Converts the proxy to a specific format.

const convertedProxy = await proxyChooser.convertProxy(proxy);
  • proxy (string): Proxy to convert.
  • Returns: Promise that resolves to the converted proxy as a string.

getPing()

Gets the ping between the client and the pingUrl specified in the options.

const ping = await proxyChooser.getPing();
  • Returns: Promise that resolves to the ping in milliseconds.

getProxy()

Chooses a working proxy from the list based on ping and availability.

proxyChooser.getProxy()
  .then(proxy => {
    console.log(proxy);
  })
  .catch(error => {
    console.error(error);
  });
  • Returns: Promise that resolves to the selected working proxy as a string.

License

This project is licensed under the MIT License.

FAQs

Package last updated on 10 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts