LinkService

LinkService

The LinkService class manages Resolver instances, stores configuration, and performs the main look-up look for links including SafeBrowsing hits if configured.

Constructor

new LinkService(optsopt)

Source:
Parameters:
Name Type Attributes Description
opts Object <optional>

Options for initializing the serice.

Properties
Name Type Attributes Default Description
fetch function <optional>

A fetch method to use for performing requests. This should implement the standard fetch API.

AbortController Class <optional>

A class to use as an AbortController with fetch.

cache CacheInterface <optional>

A cache interface for use in caching intermediate and final responses.

max_redirects Number <optional>
20

The maximum number of redirects to follow.

domain_cache_size Number <optional>
300

The maximum number of domains to cache in the LRU cache.

safe_browsing_server String <optional>

The URL of a Google SafeBrowsing API server to use for looking up SafeBrowsing data.

use_cloudflare_dns Boolean <optional>

When true, domains will be checked for malware/safety against Cloudflare DNS.

use_dnszero Boolean <optional>

When true, domains will be checked for malware/safety against DNS0 Zero.

user_agent String <optional>

The User-Agent to send with all HTTP requests.

default_referrer String | URL <optional>

The default Referer to use when making HTTP requests.

resolver_timeout Number <optional>
4000

The number of miliseconds to wait for a remote server before timing out.

image_proxy Object <optional>

Options for an imageproxy server.

Properties
Name Type Attributes Description
host String <optional>

The URL of an imageproxy server for use when proxying images.

key String <optional>

The base-64 signing key to use for authenticating proxied image URLs.

Methods

getExamples() → {Array.<ExampleURL>}

Source:

Gather an array of example URLs from all the registered resolvers, for use in populating a selection field in a testing client.

Returns:

List of URLs.

Type
Array.<ExampleURL>

normalizeURL(url, base) → {URL}

Source:

Normalize a URL. This method is used by the LinkService to normalize all URLs that it encounters. Normalization helps ensure better cache hit rates and lets the service work with a degree of garbage input.

This method can be overwritten for custom behavior, and by default it just calls the normalizeURL method from utilities.

Parameters:
Name Type Description
url String | URL

The URL to normalize

base URL

A base URL to use to build an absolute URL, if the input URL is relative.

Returns:

A normalized URL

Type
URL

pickResolver(url) → {Resolver}

Source:

Pick the best resolver to handle a given URL from the list of known Resolver instances. This also caches the decision in a LRU cache to speed up subsequent URLs from the same domain.

Parameters:
Name Type Description
url String | URL

The URL to pick a resolver for. If this is a String, it will be run through LinkService#normalizeURL first.

Returns:

The resolver instance to use for processing.

Type
Resolver

proxyImage(url, sizeopt) → {String}

Source:

Create a URL for passing an image through a proxy, used to avoid leaking end-user IP addresses and to perform sanity checks on the contents of the image.

Currently, this method is written to generate URLs for the https://github.com/willnorris/imageproxy project, as that's what FrankerFaceZ is using.

This returns null if no image_proxy_host is set in options as end-user security should be the default. If you really, really want to pass URLs through unmodified this must be set to LinkService.ALLOW_UNSAFE_IMAGES.

Parameters:
Name Type Attributes Default Description
url String | URL

The URL to proxy.

size Number <optional>
324

The size parameter to pass to the proxy server.

Returns:

The proxied image URL, or null if no proxy server is configured.

Type
String

registerDefaultResolvers()

Source:

Register all of the default resolvers that come packaged with the LinkService. A list of those resolvers can be found at https://github.com/FrankerFaceZ/link-service/tree/master/lib/resolvers

registerResolver(resolver) → {Resolver}

Source:

Register a new Resolver with the LinkService. If a class is passed, an instance will be created automatically.

Note: Registering a resolver has the side effect of clearing the domain cache.

Parameters:
Name Type Description
resolver Resolver

The resolver to register.

Returns:

The registered resolver.

Type
Resolver

registerSafetyCheck(checker) → {SafetyCheck}

Source:

Register a new SafetyCheck with the LinkService. If a class is passed, an instance will be created automatically.

Parameters:
Name Type Description
checker SafetyCheck

The safety check to register.

Returns:

The registered safety check.

Type
SafetyCheck

registerShortenerCheck(checker) → {ShortenerCheck}

Source:

Register a new ShortenerCheck with the LinkService. If a class is passed, an instance will be created automatically.

Parameters:
Name Type Description
checker ShortenerCheck

The shortener check to register.

Returns:

The registered shortener check.

Type
ShortenerCheck

(async) resolve(url) → {Object}

Source:

Normalize a URL and use Resolver instances to retrieve metadata for the URL, keeping track of redirects and looking up SafeBrowsing records on all URLs. Essentially: the heart of the service.

Returns a response, as in: responses

Parameters:
Name Type Description
url String | URL

The URL to resolve.

Returns:

The metadata to be sent to clients.

Type
Object