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

hyperprox

v2.3.1

Published

simple HTTP proxy based on hyperquest

Downloads

4,481

Readme

hyperprox

simple HTTP proxy based on hyperquest

install

$ npm install hyperprox

usage

Create a proxy by passing a function that will resolve what backend to use to the given request

var http = require("http")
var hyperprox = require('hyperprox')

var backends = hyperprox(function(req){
  // calculate the proxy destination
  var port = req.url=='/a' ? 8081 : 8082
  return 'http://127.0.0.1:' + port
})

// the front facing web server
var router = http.createServer(backends.handler())

backends.on('request', function(req, res){
	
})

backends.on('route', function(req, address){
	
})

var serverA = http.createServer(function(req, res){
  res.end('serverA')
})

var serverB = http.createServer(function(req, res){
  res.end('serverB')
})

router.listen(8080)
serverA.listen(8081)
serverB.listen(8082)

streams

We can generate a duplex stream for a request that will auto-route - this lets us filter the input and output:

var through = require('through2')
var backends = hyperprox(function(req){
  var port = req.url=='/a' ? 8081 : 8082
  return 'http://127.0.0.1:' + port
})

var router = http.createServer(function(req, res){
	var proxy = backends.duplex(req, res)

	// filter the request body
	var inputFilter = through(function(chunk, enc, next){
		this.push(chunk.toString() + 'a')
		next()
	})

	// filter the response body
	var outputFilter = through(function(chunk, enc, next){
		this.push(chunk.toUpperCase())
		next()
	})

	// REQUEST -> INPUT FILTER -> PROXY -> OUTPUT FILTER -> RESPONSE
	req.pipe(inputFilter).pipe(proxy).pipe(outputFilter).pipe(res)
})

async routing

Your routing function can be asynchronous - this means you can ask an external service for routing data:

If you define next in the parameters then it will be treated as an async router.

var proxy = hyperprox(function(req, next){

	loadRoute(req.url, function(err, address){
		next(err, address)
	})
	
})

api

hyperprox.proxy(req, res, address, [input, output])

A direct proxy function that will send req via address to res

Input and output are optional override streams to replace req and res

var backends = hyperprox(function(req, next){})

Create a new proxy by passing a function that will resolve the backend address and pass it to the 'next' function

backends.handler()

Return a function(req,res){} that will proxy requests using the routing function

backends.proxy(req, res, address, [input, output])

A direct proxy that will pipe req via address and to res

If input and output are provided - they will be used as the streams rather than req and res

backends.resolve(req, done)

The resolving function that goes via the user supplied function

backends.duplex(req, res)

Return a duplex stream going through the backend - you can write it to the original request / response how you want:

var duplex = backends.duplex(req, res)
req.pipe(duplex).pipe(res)

If there is an error with routing the response will be set to 500 and the backend skipped

events

backends.on('request', function(req, res){})

when a request arrives at the proxy

backends.on('route', function(req, address){})

Once a routing decision has been made

license

MIT