New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

balancing-url

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

balancing-url

A URL class to randomise URLs

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

#Balancing URL

A simple class to randomise the generation of a URL based upon a matching route. The tests in test/ provide a range of implementation details.

When instantiating the class, you may pass either a single string with a domain (eg. http://example.com) or an array of domains. This will create a wildcard match; when generating a URL, it will wildcard match and always return a URL. If you, instead, instantiate the class and configure an array of objects of the format { match: '/path', urls: [ 'http://example.com', 'http://example.org' ] }, then it will check that the request URI passed to the generateUrl function matches a path and return a URL with one of the randomised URLs.

You may specify the randomisation process for the URLs by defining which route type that the class should use like so:

var balancingUrl = require('balancing-url');
var routes = [
  'http://example.com',
  'http://example.org'
];
var generateUrl = balancingUrl(routes, balancingUrl.routeTypes.RANDOM);

console.log(generateUrl('/here'));
// http://example.com/here

Available route types include: BalancingUrl.routeTypes.RANDOM and BalancingUrl.routeTypes.ROUND_ROBIN. Alternatively, you may create your own route type interface. Simply ensure that the public interface of the object matches the interface of the route type objects included in this module. Use the route type tests for further assistance.

##Routes

You can specify routes in several formats:

Single String

If you're crazy, you can pass a single string when instantiating your generator:

var generator = balancingUrl('http://example.com');
var url = generator('/crazy');
// http://example.com/crazy

When you pass your path in, it will simply build a full URL using http://example.com as the base.

Array of strings

This is the simplest method of providing a set of base URLs to balance with:

var urls = [
  'http://example.com',
  'http://example.org'
];
var generator = balancingUrl(urls);
var url = generator('/test.jpg');
// http://example.com/test.jpg

When you path your path in, it will use the route type (defaulting to balancingUrl.routeType.RANDOM) to build the full URL.

Path matching object

Use this method when you wish to match paths to particular sets of base URLs.

var routes = [
  {
     match: '/example_path',
     urls: [
       'http://example.com',
       'http://example.org'
     ]
  },
  {
     match: '/example_path_2',
     urls: [
       'http://example2.com',
       'http://example2.org'
     ]
  }
];
var generator = balancingUrl(urls);
var url = generator('/example_path_2/test');
// http://example2.org/test

In the above example, the generator first matches the passed path and uses the matching set of URLs. Note that the result will not contain the matching part of the path. This is by design so that you can create pseudo paths for matching only.

FAQs

Package last updated on 13 Apr 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc