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

cdn-detector

v0.1.6

Published

CDN detection

Downloads

19

Readme

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