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

@change-org/longlinks

v0.1.6

Published

A serverless URL shortener that leverages Amazon Lambda for short link creation, and S3 for link storage and redirection.

Downloads

7

Readme

CircleCI

longlinks

A serverless URL shortener that leverages AWS Lambda for short link creation, and S3 for link storage and redirection.

The serverless scaffolding for this was largely lifted from @danielireson's excellent tutorial.

longlinks uses a consistent hashing algorithm to convert long URLs into short hashes, encoded with a base-48 alphanumeric character set. It is optimized for ease of operation, cost effectiveness at scale, and for generating URLs suitable for social media sharing. A list of allowed domains (for input URLs) must be specified as part of your configuration.

Note: The consistent hashing algorithm allows for fast and simple link creation, but also means that there's a very small but non-zero chance of "collisions", where two different URLs shorten to the same value. If your links will stay relevant in perpetuity, it's probably not the approach you want.

Installing

Either checkout this repo, or else just install via npm:

  npm install @change-org/longlinks
  cd node_modules/@change-org/longlinks

The longlinks package is actually a deployable serverless app. Deploying it will automatically provision the necessary AWS services (Lambda, API Gateway, S3 bucket, using a CloudFormation stack) according to your configuration file.

To deploy it, you'll just need a couple of things:

AWS Credentials & Serverless

The serverless quick start guide explains how to set up your AWS provider credentials.

Also install the serverless CLI tool via npm:

  npm install -g serverless

config.json

Create a file called config.json in the longlinks directory. You can start by copying config.sample.json, and filling in a few details, especially:

| Field | Description | | :----------------- | :-------------------------------------------------------- | | bucket | The name of the S3 bucket you want to create / deploy to | | domain_safe_list | List of input domains that your shortener will accept | | short_domain | If you have a short domain, you'll also need to configure it as an alias for your S3 bucket's static website hostname, later. |

If you are going to use a short domain, you probably want to manage the DNS records for it using Amazon Route 53, in which case Amazon requires that your bucket name matches the short domain. So, for example use "bucket": "xyz.io" and "short_domain": "http://xyz.io".

Deploying

Now you can deploy:

  serverless deploy

This will output the URL of the API Gateway endpoint for your Lambda function. You can also log in to the AWS console, go to CloudFormation, and see all the details about the stack that was created.

Creating a short link

Try creating a new "short" link by using curl to call the endpoint:

  curl -X POST -H "Content-Type: application/json" -d '{"url":"http://example.com/"}' <endpoint_url>

The response will show you the short hash value for that URL, as well as the final short URL. A file with that name will have been created in your S3 bucket, and accessing that file via the static website hostname (ie., the one with the hostname including <bucket-name>.s3-website-) for the bucket will result in a 301 redirect to your destination URL!

Attempts to shorten invalid URLs, or anything not matching the domain_safe_list will result in a 400 response.

Specifying a custom hash length

By default, longlinks generates 10-character hashes. But you can specify any length in the range [7, 12] characters by including a hashLength parameter in the JSON body. Shorter hashes will have a greater chance of collisions, but that might be acceptable for certain uses.

  -d '{"url":"http://example.com","hashLength":7}'