• Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Short links expander for node.js

url-unshort

CI NPM version

This library expands urls provided by url shortening services (see full list).

Why should I use it?

It has been argued that โ€œshorteners are bad for the ecosystem as a wholeโ€. In particular, if you're running a forum or a blog, such services might cause trouble for your users:

  • such links load slower than usual (shortening services require an extra DNS and HTTP request)
  • it adds another point of failure (should this service go down, the links will die; 301works tries to solve this, but it's better to avoid the issue in the first place)
  • users don't see where the link points to (tinyurl previews don't really solve this)
  • it can be used for user activity tracking
  • certain shortening services are displaying ads before redirect
  • shortening services can be malicious or be hacked so they could redirect to a completely different place next month

Also, short links are used to bypass the spam filters. So if you're implementing a domain black list for your blog comments, you might want to check where all those short links actually point to.

Installation

$ npm install url-unshort

Basic usage

const uu = require('url-unshort')()

try {
  const url = await uu.expand('http://goo.gl/HwUfwd')

  if (url) console.log('Original url is: ${url}')
  else console.log('This url can\'t be expanded')

} catch (err) {
  console.log(err);
}

Retrying errors

Temporary network errors are retried automatically once (options.request.retry=1 by default).

You may choose to retry some errors after an extended period of time using code like this:

const uu = require('url-unshort')()
const { isErrorFatal } = require('url-unshort')
let tries = 0

while (true) {
  try {
    tries++
    const url = await uu.expand('http://goo.gl/HwUfwd')

    // If url is expanded, it returns string (expanded url);
    // "undefined" is returned if service is unknown
    if (url) console.log(`Original url is: ${url}`)
    else console.log("This url can't be expanded")
    break

  } catch (err) {
    // use isErrorFatal function to check if url can be retried or not
    if (isErrorFatal(err)) {
      // this url can't be expanded (e.g. 404 error)
      console.log(`Unshort error (fatal): ${err}`)
      break
    }

    // Temporary error, trying again in 10 minutes
    // (5xx errors, ECONNRESET, etc.)
    console.log(`Unshort error (retrying): ${err}`)
    if (tries >= 3) {
      console.log(`Too many errors, aborting`)
      break
    }
    await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000))
  }
}

API

Creating an instance

When you create an instance, you can pass an options object to fine-tune unshortener behavior.

const uu = require('url-unshort')({
  nesting: 3,
  cache: {
    get: async key => {},
    set: async (key, value) => {}
  }
});

Available options are:

  • nesting (Number, default: 3) - stop resolving urls when nesting amount of redirects is reached.

    It happens if one shortening service refers to a link belonging to another shortening service which in turn points to yet another one and so on.

    If this limit is reached, expand() will return an error.

  • cache (Object) - set a custom cache implementation (e.g. if you wish to store urls in Redis).

    You need to specify 2 promise-based functions, set(key, value) & get(key).

  • request (Object) - default options for got in .request() method. Can be used to set custom User-Agent and other headers.

uu.expand(url) -> Promise

Expand an URL supplied. If we don't know how to expand it, returns null.

const uu = require('url-unshort')();

try {
  const url = await uu.expand('http://goo.gl/HwUfwd')

  if (url) console.log('Original url is: ${url}')
  // no shortening service or an unknown one is used
  else console.log('This url can\'t be expanded')

} catch (err) {
  console.log(err)
}

uu.add(domain [, options])

Add a new url shortening service (domain name or an array of them) to the white list of domains we know how to expand.

uu.add([ 'tinyurl.com', 'bit.ly' ])

The default behavior will be to follow the URL with a HEAD request and check the status code. If it's 3xx, return the Location header. You can override this behavior by supplying your own function in options.

Options:

  • aliases (Array) - Optional. List of alternate domaine names, if exist.
  • match (String|RegExp) - Optional. Custom regexp to use for URL match. For example, if you need to match wildcard prefixes or country-specific suffixes. If used with validate, then regexp may be not precise, only to filter out noise. If match not passed, then exact value auto-generated from domain & aliases.
  • validate (Function) - Optional. Does exact URL check, when complex logic required and regexp is not enouth (when match is only preliminary). See ./lib/providers/* for example.
  • fetch (Function) - Optional. Specifies custom function to retrieve expanded url, see ./lib/providers/* for examples. If not set - default method used (it checks 30X redirect codes & <meta http-equiv="refresh" content='...'> in HTML).
  • link_selector (String) - Optional. Some sites may return HTML pages instead of 302 redirects. This option allows use jquery-like selector to extract <a href="..."> value.

Example:

const uu = require('url-unshort')()

uu.add('notlong.com', {
  match: '^(https?:)//[a-zA-Z0-9_-]+[.]notlong[.]com/'
})

uu.add('tw.gs', {
  link_selector: '#lurllink > a'
})

uu.remove(domain)

(String|Array|Undefined). Opposite to .add(). Remove selected domains from instance config. If no params passed - remove everything.

Security considerations

Only http and https protocols are allowed in the output. Browsers technically support redirects to other protocols (like ftp or magnet), but most url shortening services limit redirects to http and https anyway. In case service redirects to an unknown protocol, expand() will return an error.

expand() function returns url from the url shortening as is without any escaping or even ensuring that the url is valid. If you want to guarantee a valid url as an output, you're encouraged to re-encode it like this:

var URL = require('url');

url = await uu.expand('http://goo.gl/HwUfwd')

if (url) url = URL.format(URL.parse(url, null, true))

console.log(url));

License

MIT

More Repositories

1

js-yaml

JavaScript YAML parser and dumper. Very fast.
JavaScript
6,289
star
2

pako

high speed zlib port to javascript, works in browser & node.js
JavaScript
5,550
star
3

pica

Resize image in browser with high quality and high speed
JavaScript
3,759
star
4

probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
JavaScript
984
star
5

argparse

CLI arguments parser for node.js. JS port of python's argparse module.
JavaScript
489
star
6

image-blob-reduce

Resize image blobs with high quality. Pica's wrapper to work with file inputs.
JavaScript
272
star
7

babelfish

human friendly i18n for javascript (node.js + browser)
JavaScript
255
star
8

tabex

Cross-tab message bus for browsers.
JavaScript
219
star
9

ndoc

js port of pdoc, with extentions
JavaScript
101
star
10

charlatan

Fake identities generator for node.js (names, addresses, phones, IPs and others). Supports multiple languages.
JavaScript
96
star
11

nodeca

Forums / Blogs / Groups / Classfields / ... platform. Fork this to make your own config.
JavaScript
96
star
12

bag.js

JS / CSS / files loader + key/value storage
JavaScript
90
star
13

idoit

Redis-backed task queue engine with advanced task control and eventual consistency
JavaScript
74
star
14

glur

Fast gaussian blur in pure JavaScript via IIR filer
JavaScript
72
star
15

multimath

WebAssembly wrapper to simplify fast math coding
JavaScript
70
star
16

embedza

Create HTML snippets/embeds from URLs using info from oEmbed, Open Graph, meta tags.
JavaScript
64
star
17

promise-memoize

Memoize promise-returning functions. Includes cache expire and prefetch.
JavaScript
59
star
18

navit

Simple client testing from your scripts
JavaScript
48
star
19

unhomoglyph

Replace all homoglyphs with base characters. Useful to detect similar strings.
JavaScript
39
star
20

nntp-server

NNTP server for readers
JavaScript
27
star
21

puncher

Library to set timestamps in your application requests & genegate profiling tree.
JavaScript
14
star
22

eslint-plugin-nodeca

Indentation check rule for ESLint
JavaScript
13
star
23

nodeca.core

Nodeca core app (admin panel, loader, bundler)
JavaScript
12
star
24

types

Collection of extra types (structures, classes) for JavaScript.
JavaScript
12
star
25

nodeca.users

Nodeca user app (login / register / albums / profiles)
JavaScript
11
star
26

event-wire

Mediator with dynamic responsibility chains
JavaScript
10
star
27

hike-js

Javascript port of Hike (Ruby) - a library for finding files in a set of paths
JavaScript
10
star
28

plurals-cldr

Plurals suport for JS, autogenerated from CLDR.
JavaScript
9
star
29

relimit

Rate limiter with tuneable scheduler and distributed run support
JavaScript
9
star
30

js-yaml-js-types

Extra js types for js-yaml
JavaScript
8
star
31

mimoza

Simple mime-type tools library
JavaScript
8
star
32

pointer

router for node.js / client
JavaScript
7
star
33

nodeca.forum

Nodeca forum app
JavaScript
4
star
34

redis-if

JavaScript
4
star
35

nodeca-design

Nodeca interface mockups
HTML
4
star
36

fontomas

3
star
37

nodeca.blogs

Blogs component for Nodeca
JavaScript
3
star
38

nodeca.market

Market component for Nodeca
JavaScript
2
star
39

nodeca.clubs

Clubs component for Nodeca
JavaScript
2
star
40

charcount

Count visual length of javascript string
JavaScript
2
star
41

bkv

JavaScript
1
star
42

nodeca.nntp

JavaScript
1
star
43

nodeca.vbconvert

vBulletin -> Nodeca convertor
JavaScript
1
star
44

nodeca-doc

Nodeca internals documentation
CSS
1
star
45

nodeca.search

JavaScript
1
star
46

.github

1
star
47

nodeca.site

Custom configuration for nodeca
JavaScript
1
star