Neverending Package Mountain
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

cdn-detector

0.1.6 • Public • Published

cdn-detector.js

v0.1.6

http://nicj.net

Licensed under the MIT license

Introduction

cdn-detector.js detects whether or not a HTTP response is being served from a Content Delivery Network (CDN), by inspecting the request's hostname and HTTP response headers.

This project is based on data from:

Download

Releases are available for download from GitHub.

Web

The main source file is src/cdn-detector.js. There are additional JSON data files in data/*.json. These files automatically get converted to .js files and are merged into the single-file distribution dist/cdn-detector.[min].js via gulp. If you want to use this project in a browser, use one of the dist/*.js files:

Development: cdn-detector.js - 13.8kb

Production: cdn-detector.min.js - 7.0kb minified, 2.3kb gzipped

NPM

cdn-detector.js is also available as the npm cdn-detector module. You can install using Node Package Manager (npm):

npm install cdn-detector

Bower

cdn-detector.js is also available via bower. You can install using:

bower install cdn-detector

Usage

To include cdn-detector.js in the browser, include it via a script tag:

<script type="text/javascript" src="cdn-detector.min.js"></script>

Once included in the page, a top-level CdnDetector object is available on window. If AMD or CommonJS environments are detected, it will expose itself via those methods.

From the NPM module:

var cdnDetector = require("cdn-detector");

Once loaded, cdn-detector can be used by calling CdnDetector.detect(hostname, headers):

var cdnDetector = require("cdn-detector");
 
var result = cdnDetector.detect(
    "foo.googleusercontent.com", {
        "gws": "1",
    });

If result is non-null, the response was served from a CDN:

{
    "cdn": "Google",
    "evidence": {
        "hostname": "\\.googleusercontent\\.com$",
        "headers": [
            "gws: *"
        ]
    }
}

API

CdnDetector.detect(hostname, headers)

Determines whether the hostname and HTTP response headers indicate that the resource was served via a known CDN.

Arguments:

  • hostname: The requested resource's hostname, such as foo.googleusercontent.com.
  • headers: The HTTP response headers in map form, such as { "Server": "Foo", "Via": "Bar" }

Returns: If no CDN was matched, null. If a CDN was matched, an object with the CDN name and evidence why:

{
    "cdn": "Google",
    "evidence": {
        "hostname": "\\.googleusercontent\\.com$",
        "headers": [
            "gws: *"
        ]
    }
}

CdnDetector.detectFromHostname(hostname)

Determines whether the hostname indicates that the resource was served via a known CDN.

Arguments:

  • hostname: The requested resource's hostname, such as foo.googleusercontent.com.

Returns: If no CDN was matched, null. If a CDN was matched, an object with the CDN name and evidence why. The evidence is the regex string that matched the hostname.

{
    "cdn": "Google",
    "evidence": "\\.googleusercontent\\.com$"
}

CdnDetector.detectFromHeaders(hostname, headers)

Determines whether the HTTP response headers indicate that the resource was served via a known CDN.

Arguments:

  • headers: The HTTP response headers in map form, such as { "Server": "Foo", "Via": "Bar" }

Returns: If no CDN was matched, null. If a CDN was matched, an object with the CDN name and evidence why. The evidence is an array of HTTP response headers that matched:

{
    "cdn": "Google",
    "evidence": [
        "server: foo"
    ]
}

Data

The data files for hostnames and headers are in the following files:

  • data/headers.json
  • data/hostnames.json
  • data/multi-headers.json

Via gulp, these are built as data/*.js and are included in the dist/*.js files for use on the web.

In NodeJS, these files are used as-is.

Forcing Headers

Some CDNs may require a HTTP Request Header to be present before they expose a HTTP Response Header that can be used for fingerprinting.

Known examples:

  • Akamai: Pragma: akamai-x-get-request-id will return a X-Akamai-Request-ID

If you're using request to fetch the headers, you would add the HTTP Request Header to the request() options:

request({
    url: url,
    gzip: true,
    time: true,
    headers: {
        'pragma': 'akamai-x-get-request-id'
    }
}, function(err, response, body) {
    ...
});

Tests

Tests are provided in the test/ directory, and can be run via mocha:

mocha test/*

Or via gulp:

gulp test

Version History

  • v0.1.0 - 2016-08-17: Initial version
  • v0.1.1 - 2016-08-17: Changed what index.js points to
  • v0.1.2 - 2016-08-17: Fixed module.exports
  • v0.1.3 - 2016-10-31: jQuery CDN added
  • v0.1.4 - 2017-07-02: CDN list updated
  • v0.1.5 - 2018-05-12: Added a rule for Akamai and a note about forcing headers
  • v0.1.6 - 2018-05-12: Moved one dependency to dev

install

npm i cdn-detector

Downloadsweekly downloads

7

version

0.1.6

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability