GithubHelp home page GithubHelp logo

jkyberneees / fast-proxy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fastify/fast-proxy

1.0 1.0 0.0 420 KB

Node.js framework agnostic library that enables you to forward an http request to another HTTP server. Supported protocols: HTTP, HTTPS, HTTP2

Home Page: https://www.npmjs.com/package/fast-proxy

License: MIT License

JavaScript 100.00%

fast-proxy's Introduction

fast-proxy

Node.js framework agnostic library that enables you to forward an http request to another HTTP server. Supported protocols: HTTP, HTTPS, HTTP2

This library forked from fastify-reply-from: https://github.com/fastify/fastify-reply-from

fast-proxy powers: https://www.npmjs.com/package/fast-gateway πŸš€

Install

npm i fast-proxy

Usage

The following examples describe how to use fast-proxy with restana:

Gateway:

const { proxy, close } = require('fast-proxy')({
  base: 'http://127.0.0.1:3000'
  // options
})
const gateway = require('restana')()

gateway.all('/service/*', function (req, res) {
  proxy(req, res, req.url, {})
})

gateway.start(8080)

Remote service:

const service = require('restana')()
service.get('/service/hi', (req, res) => res.send('Hello World!'))

service.start(3000)

Benchmarks

Benchmark scripts can be found in benchmark folder.

fast-proxy is fast with F

wrk -t8 -c50 -d20s http://127.0.0.1:8080/service/hi
  • fast-proxy-undici/0http: Requests/sec 19014.13 (HTTP pipelining = 10)
  • fast-proxy/0http: Requests/sec 17791.10
  • fast-proxy/restana: Requests/sec 17013.53
  • fast-proxy-undici/0http: Requests/sec 15320.76 (HTTP pipelining = 1)
  • fastify-reply-from: Requests/sec 12056.81
  • http-proxy: Requests/sec 408.97

API

Options

base

Set the base URL for all the forwarded requests. Will be required if http2 is set to true Note that path will be discarded.

http2

Set to true if target server is http2 enabled.

undici

Set to true to use undici instead of require('http'). Enabling this flag should guarantee 20-50% more throughput.

This flag could controls the settings of the undici client, like so:

...
  base: 'http://localhost:3001/',
  undici: {
    connections: 100,
    pipelining: 10
  }
...

cacheURLs

The number of parsed URLs that will be cached. Default: 100.

keepAliveMsecs

Defaults to 1 minute, passed down to [http.Agent][http-agent] and [https.Agent][https-agent] instances.

maxSockets

Defaults to 2048 sockets, passed down to [http.Agent][http-agent] and [https.Agent][https-agent] instances.

rejectUnauthorized

Defaults to true, passed down to [https.Agent][https-agent] instances. This needs to be set to falseΒ to reply from https servers with self-signed certificates.

Extended configurations

Other supported configurations in https://nodejs.org/api/http.html#http_new_agent_options can also be part of the opts object.

close

Optional "on close resource release" strategy. You can link this to your application shutdown hook as an example.

proxy(originReq, originRes, source, [opts])

Enables you to forward an http request to another HTTP server.

proxy(
  originReq,                          // http.IncomingMessage 
  originRes,                          // http.ServerResponse
  req.url,                            // String -> remote URL + path or path if base was set
  {}                                  // Options described below
)

opts

onResponse(req, res, stream)

Called when an http response is received from the source. The default behavior is pump(stream, res), which will be disabled if the option is specified.

rewriteHeaders(headers)

Called to rewrite the headers of the response, before them being copied over to the outer response. It must return the new headers object.

request

Extended options supported by http[s].request method (https://nodejs.org/api/http.html#http_http_request_options_callback) The following options are dynamically assigned: method, port, path, hostname, headers, agent.

http2 options are limited to timeout only, while undici supports none.

queryString

Replaces the original querystring of the request with what is specified. This will get passed to querystring.stringify.

Related topics

Contributions

Special thanks to fastify-reply-from developers for creating a production ready library that we could initially fork.

License

MIT

fast-proxy's People

Contributors

cemremengu avatar delvedor avatar eomm avatar greenkeeper[bot] avatar jkyberneees avatar mcollina avatar nileshmali avatar thejones avatar vincent178 avatar

Stargazers

 avatar

Watchers

 avatar

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.