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

duplicator

v0.1.1

Published

TCP proxy that also duplicates traffic to a secondary host

Downloads

17

Readme

duplicator build status

TCP proxy that also duplicates traffic to a secondary host

I built this because I needed a way to "tap" production traffic and shoot it at a new system to see how it handles load.

usage (cli)

npm install -g duplicator
duplicator -f localhost:80 -d localhost:3000 -p 8080 [-i 127.0.0.1]
  • forward all traffic to localhost:80
  • duplicate all traffic to localhost:3000, ignoring responses
  • listen on port 8080
  • if -i is specified only listen to incoming traffic for ip address 127.0.0.1

Note: the cli automatically uses the cluster API to run several workers to handle connections, and restart workers if they die.

usage (code)

The equivalent to the call above, in node.js:

var duplicator = require('duplicator')

var server = duplicator(function(connection, forward, duplicate) {
  forward('localhost:80')
  duplicate('localhost:3000')
}).listen(8080, ["127.0.0.1"])

reference

duplicator(cb)

Creates a new net.Server. When cb is called on a successful connection, it will receive the connection to the client and two special callback functions, forward and duplicate.

duplicator(function(connection, forward, duplicate) {
  forward('origin:80')
  duplicate(Math.random() < 0.5 ? 'host1:80' : 'host2:80')
})

In this example, we always use origin:80 as the primary server to forward requests to, but we send half the duplicated rquests to host1, and the other half to host2.

forward(host, [stream])

Forward stream to host. stream defaults to the current connection unless otherwise specified.

duplicator(function(connection, forward, duplicate) {
  // forward every connection to localhost:80
  forward('localhost:80')
})

Note that it is not safe to forward the same connection multiple times, as the responses from the different servers may interfere with one another. Therefore if you do call forward multiple times, only the first call will succeed, and subsequent calls will merely duplicate the connection.

duplicate(host, [stream], [rate])

Duplicate on average rate copies of stream to host. stream defaults to the current connection unless otherwise specified, rate defaults to 1.

Rate is the expected number of copies sent per connection, and is interpreted as follows:

  • 0.1 -> send once with 10% probability
  • 1 -> send exactly one copy
  • 5 -> send out 5 copies
  • 2.5 -> send out 2 copies, with a 50% chance of a third

Note that both stream and rate are optional, but have sensible defaults. If only two parameters are specified, the second will be interpreted as rate if it's a number, stream otherwise.

duplicator(function(connection, forward, duplicate) {
  // duplicate 50% of requests to localhost:3000
  duplicate('localhost:3000', 0.5)
  // duplicate all connections to stdout
  duplicate('localhost:3000', process.stdout)
})

specifying hosts

A host can be any one of:

  • an object as net.connect would expect it ({ host: 'google.com', port: 80 })
  • a number representing a port (80, 3000), host is implied to be localhost
  • a string representing a host (localhost:3000, google.com:80)
  • a function that behaves like net.connect (function(cb) { net.connect({ port:80 }, cb) })

contributing and attribution

Thanks to @netroy for contributing the cluster support!

If you'd like to contribute, please open a pull request or file an issue.

mit license