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

anchor-gateway

v0.3.5

Published

very simple microservice gateway

Downloads

31

Readme

anchor-gateway

api gateway for node with HTTPS, cluster and WebSocket support

Very small library that enables very easy handling of things like load balancing, proxying web sockets, dynamic routing and SSL encryption.

anchor-gateway includes everything you need for easy reverse routing of your applications. Great for handling microservice routing from different domains in one single host, handling SSL and web sockets.

Table of Contents

Installation

npm install anchor-gateway --save

Back to top

Core Concept

A new gateway is created by calling the AnchorGateway constructor and passing an options object as argument

const AnchorGateway = require('anchor-gateway');
let options = {
    ...
}

const gateway = new AnchorGateway(options);

Then it is easy to proxy requests by calling the init function with an array of objects that must have src and target properties.


gateway.init([
    {
        src: 'localhost',
        target: 'http://127.0.0.1:3333'
    },
        {
        src: 'localhost/api-2',
        target: 'http://127.0.0.1:4444'
    }
])

Back to top

Use Cases

Setup a basic HTTP gateway server


const AnchorGateway = require('anchor-gateway')

const gateway = new AnchorGateway({
    port: 8000,
    xfwd: false // OPTIONAL: Setup your gateway but disable the X-Forwarded-For header
})

gateway.init([
    {
        src: 'example.com',
        target: 'http://127.0.0.1:3333'
    },
    {
        src: 'example.com/static',
        target: 'http://174.11.45.7:5444'
    },
    {
        src: 'sub.example.com',
        target: 'http://174.11.45.7:4481'
    },
    {
        src: 'sub.example.com/second',
        target: 'http://174.11.45.7:4481/second'
    },
    // You can enable load balancing if you register the same source(src) with different targets
    {
        src: 'balance.tube',
        target: 'http://190.22.33.4:5556'
    },
        {
        src: 'balance.tube',
        target: 'http://190.23.31.4:3556'
    },
        {
        src: 'balance.tube',
        target: 'http://190.22.32.1:6356'
    },
  
])

//You can remove route objects from the array programmatically even at runtime by calling the remove function
//with the index of the route object you want to remove

gateway.remove(1) //will remove the object with index 1 ({src: 'example.com/static',target: 'http://174.11.45.7:5444'})

setTimeout(function() {
    gateway.remove(3)
}, 4000)

Using HTTPS


const AnchorGateway = require('anchor-gateway')
const fs = require('fs')

const gateway = new AnchorGateway({
    port: 8000,
    secure: false, //can be true depending on your need but the certs must be from authority
    ssl: {
        key: fs.readFileSync('path/to/key.pem', 'utf8'),
        cert: fs.readFileSync('path/to/cert.pem', 'utf8')
    }
})

gateway.init([
    {
        src: 'localhost',
        target: 'https://127.0.0.1:3455'
    },
        {
        src: 'localhost/api-2',
        target: 'https://127.0.0.1:7755'
    }
])

HTTP to HTTPS

You have to generate a PKCS12 client certificate

You can do it by running

/path/to $ openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile more.crt

const AnchorGateway = require('anchor-gateway')
const fs = require('fs')
 

const gateway = new AnchorGateway({
    port: 8000,
    pfx: fs.readFileSync('path/to/certificate.p12'),
    passphrase: 'password',
    secure: false, //can be true depending on your need but the certs must be from authority

})

gateway.init([
    {
        src: 'localhost',
        target: 'https://127.0.0.1:3455'
    },
        {
        src: 'localhost/api-2',
        target: 'http://127.0.0.1:7755'
    }
])

Proxying WebSockets


const AnchorGateway = require('anchor-gateway')


const gateway = new AnchorGateway({
    port: 3000,
    ws:true,
    secure: false, //can be true depending on your need but the certs must be from authority

})

gateway.init([
        {
            src: 'localhost/ws',
            target: 'ws://localhost:8080'
        }
])

And in socket.js

const WebSocket = require('ws')

const wss = new WebSocket.Server({ port: 8080 });
// wss.emit('upgrade')
wss.on('connection', function connection(ws) {
    ws.on('message', function incoming(message) {
        console.log('received: %s', message);
    });

    ws.send('something');
});
var client = new WebSocket('ws://localhost:3000/ws')
setInterval(function() {

    client.on('message', function(msg) {
        util.debug('Got message from client: ' + msg);
    });
}, 1000)

Adding Custom Resolvers

With custom resolvers you can:

  • Do path-based routing.
  • Do headers based routing.
  • Do wildcard domain routing.
var custom_resolver = function(host, url, req) {
   if(/^\/api-v2\//.test(url)){
      return 'http://127.0.0.1:3333';
   }
 };

const gateway = new AnchorGateway({
    port: 3000,
    resolvers: [
        custom_resolver,
        function(host, url, req) {

        }
    ]

})

// remove the resolver after 10 minutes,
setTimeout(function() {
  gateway.remove_resolver(custom_resolver);
}, 600000);

Docker Support

TODO

Options

AnchorGateway(options) supports the following options

  • host: valid hostname
  • port: port number to listen on
  • ssl: object to be passed to https.createServer()
  • ws: true/false, if you want to proxy websockets
  • xfwd: true/false, adds x-forward headers
  • secure: true/false, if you want to verify the SSL Certs
  • pfx: path to PKCS12 client certificate
  • passphrase: password for the PKCS12 client certificate
  • resolvers: array of functions aka custom resolvers
  • prependPath: true/false, Default: true - specify whether you want to prepend the target's path to the proxy path
  • changeOrigin: true/false, Default: false - changes the origin of the host header to the target URL

Back to top

Shutdown


gateway.close()

Test

$ npm run test

Logo

Logo created by Vanaya

Contributing and Issues

  • If you feel comfortable about fixing an issue, fork the repo
  • Commit to your local branch (which must be different from master)
  • Submit your Pull Request (be sure to include tests and update documentation)

License

The MIT License (MIT)

Copyright (c) 2018 - 2019 Constantine Gochev, & the Contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.