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

url-shorten

v0.0.1

Published

A url shortener that works with S3, Redis or MongoDB

Downloads

7

Readme

url-shorten

A url-shortener for S3, mongodb and redis.

The MongoDB and REDIS versions are mostly there for the sake of completeness (there are other libraries that do that) but the interesting part is the S3 version. This uses the capability of S3 to do redirection so no active service needs to be on the path for the redirection to work. In particular, this works with cloudfront too which makes the redirection quite fast.

NPM info

Travis build status

Install

npm install url-shorten

API

The default implementation requires a mongodb instance to support an auto-increment counter. The generated short urls are basically base-36 encoded versions of this counter and so will only use lower case characters and numbers -- no other special characters at all.

Basic Usage

   var config = {mongoUrl: 'mongodb://user:pass@host:port/database'};
   config.s3key = '<your S3 key>';
   config.s3secret = '<your S3 secret>';
   config.s3bucket = '<your S3 bucket>';
   config.shortUrlPrefix = 'http://mysite.com.s3-website-us-east-1.amazonaws.com/';
   config.uniqueIdPrefix = 'shortUrls-'; // all short urls will have this suffixed to shortUrlPrefix

   var shortener = require('url-shorten')(config);

   shortener.shorten('http://www.example.com/some/longurl?ok', function (err, shortUrl) {
      // shortUrl will be something like
      // http://mysite.com.s3-website-us-east-1.amazonaws.com/shortUrls-a3
   });

   // you can find out the long URL for a short url via unshorten
   shortener.unshorten('http://mysite.com.s3-website-us-east-1.amazonaws.com/shortUrls-a3', function (err, url) {
     // you can expect url to be the long url (such as http://www.example.com/some/longurl?ok)
   }

   // by default shortener.shorten will always generate a *new* URL each time
   // you can force it to try to do unique URLs via shortenUnique. 
   // Note that S3 is only *mostly* unique (there are race conditions).
   // But MongoDB and Redis guarantee unique values.
   shortener.shortenUnique( same parameters as shorten );

Changing counter implementation

This uses mongodb-counter as the default implementation for the unique id generator. You can pass your own unique ID generator as follows:


   config.uniqueIdGenerator = function (done) { ... done(null, newUniqueId); ... }
   var shortener = require('url-shorten')(config);

In particular, this does not use the fast unique id generation that is provided by default in the mongodb-counter package but it is trivial to override and pass that instead.

Also, if you would rather not use mongodb but use redis instead for the counter, you can use the redis-counter package:


   var redisCounter = require('redis-counter');
   config.counters = redisCounter.createCounters({redisUrl: ....});
   var shortener = require('url-shorten')(config);

Using MongoDB or REDIS to store the mappings

If you do not want to use S3, you can store the shortUrl to longUrl mapping in mongodb or redis.


   config.store = require('url-shorten').mongodb({mongoUrl: ....});
   var shortener = require('url-shorten')(config);

   // or for redis
   config.store = require('url-shorten').redis({redisUrl: 'redis://user:pass@host:port'})