Coder Social home page Coder Social logo

trasherdk / hpagent Goto Github PK

View Code? Open in Web Editor NEW

This project forked from delvedor/hpagent

0.0 3.0 0.0 45 KB

A ready to use http and https agent for working with proxies that keeps connections alive!

License: MIT License

JavaScript 98.61% TypeScript 1.24% Shell 0.15%

hpagent's Introduction

hpagent

js-standard-style build. npm

A ready to use http and https agent for working with proxies that keeps connections alive!

Install

npm install hpagent

Usage

Based on your infrastructure, you should use the http agent or the https agent. The following table will help you picking the right one.

Type Proxy Server
HttpProxyAgent HTTP HTTP
HttpProxyAgent HTTPS HTTP
HttpsProxyAgent HTTP HTTPS
HttpsProxyAgent HTTPS HTTPS
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent')

Once you have understood the right agent for your use case, you can instance it. It takes the same parameter of the Node.js core's http(s) agent and an additional proxy option, which is the url of your proxy.

const http = require('http')
const { HttpProxyAgent } = require('hpagent')

const agent = new HttpProxyAgent({
  keepAlive: true,
  keepAliveMsecs: 1000,
  maxSockets: 256,
  maxFreeSockets: 256,
  proxy: 'http://localhost:8080'
})

http.get('http://localhost:9200', { agent })
    .on('response', console.log)
    .end()

If your proxy requires basic authentication, you can configure it in the proxy url:

const http = require('http')
const { HttpProxyAgent } = require('hpagent')

const agent = new HttpProxyAgent({
  keepAlive: true,
  keepAliveMsecs: 1000,
  maxSockets: 256,
  maxFreeSockets: 256,
  proxy: 'http://user:pwd@localhost:8080'
})

http.get('http://localhost:9200', { agent })
    .on('response', console.log)
    .end()

You can also pass custom options intended only for the proxy CONNECT request with the proxyConnectOptions option, such as headers or tls.connect() options:

const fs = require('fs')
const http = require('http')
const { HttpProxyAgent } = require('hpagent')

const agent = new HttpProxyAgent({
  keepAlive: true,
  keepAliveMsecs: 1000,
  maxSockets: 256,
  maxFreeSockets: 256,
  proxy: 'https://localhost:8080',
  proxyConnectOptions: {
    headers: {
      'Proxy-Authorization': 'Basic YWxhZGRpbjpvcGVuc2VzYW1l',
    },
    ca: [ fs.readFileSync('custom-proxy-cert.pem') ]
  }
})

http.get('http://localhost:9200', { agent })
    .on('response', console.log)
    .end()

Integrations

Following you can find the list of userland http libraries that are tested with this agent.

got('http://localhost:9200', {
  agent: {
    http: new HttpProxyAgent({
      keepAlive: true,
      keepAliveMsecs: 1000,
      maxSockets: 256,
      maxFreeSockets: 256,
      scheduling: 'lifo',
      proxy: 'http://localhost:8080'
    })
  }
})
needle('get', 'http://localhost:9200', {
  agent: new HttpProxyAgent({
    keepAlive: true,
    keepAliveMsecs: 1000,
    maxSockets: 256,
    maxFreeSockets: 256,
    scheduling: 'lifo',
    proxy: 'http://localhost:8080'
  })
})
fetch('http://localhost:9200', {
  agent: new HttpProxyAgent({
    keepAlive: true,
    keepAliveMsecs: 1000,
    maxSockets: 256,
    maxFreeSockets: 256,
    scheduling: 'lifo',
    proxy: 'http://localhost:8080'
  })
})
sget.concat({
  url: `http://${server.address().address}:${server.address().port}`,
  agent: new HttpProxyAgent({
    keepAlive: true,
    keepAliveMsecs: 1000,
    maxSockets: 256,
    maxFreeSockets: 256,
    scheduling: 'lifo',
    proxy: `https://${proxy.address().address}:${proxy.address().port}`
  })
}, function (err, response, data) {
  // handle the response
})

License

This software is licensed under the MIT.

hpagent's People

Contributors

ckcr4lyf avatar davidebianchi avatar delvedor avatar dependabot-preview[bot] avatar dependabot[bot] avatar dilame avatar hardillb avatar mbargiel avatar mojavelinux avatar

Watchers

 avatar  avatar  avatar

hpagent's Issues

Ping

Create IMAP folder.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.