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

sveisvei-fast-proxy

v2.1.0

Published

Forward your HTTP request to another server.

Downloads

4

Readme

fast-proxy

NPM version

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

This library was initially 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)

Using imports:

import fastProxy from 'fast-proxy'

const { proxy, close } = fastProxy({
  base: 'http://127.0.0.1:3000'
})

Benchmarks

Please see: https://github.com/jkyberneees/nodejs-proxy-benchmarks

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
  }
...

See undici demo at: demos/gateway-undici.js

cacheURLs

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

Use value = 0 to disable the caching mechanism

requests.http and requests.https

Allows to optionally overwrite the internal http and https client agents implementation. Defaults: http and https.

For example, this could be used to add support for following redirects, like so:

...
  requests: {
    http: require('follow-redirects/http'),
    https: require('follow-redirects/https')
  }
...

If using undici or http2 this settings are ignored!

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

base

Optionally indicates the base URL for the current request proxy. When used, the global base config is overwriten.

This configuration value is ignored when using HTTP2.

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.

rewriteRequestHeaders(req, headers)

Called to rewrite the headers of the request, before them being sent to the downstream server. It must return the new headers object.

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

  • http-agent: https://nodejs.org/api/http.html#http_new_agent_options
  • https-agent: https://nodejs.org/api/https.html#https_class_https_agent

Contributions

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

License

MIT