Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Aug 17, 2016
0 parents commit b6128bc
Show file tree
Hide file tree
Showing 23 changed files with 2,148 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"directory": "test/vendor",
"color" : false
}
102 changes: 102 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"env": {
"browser": true,
"node": true,
"amd": true
},
"rules": {
//
// v1.0 rules
//
"camelcase": 2,
"comma-spacing": 2,
"consistent-return": 2,
"curly": [2, "all"],
"dot-notation": [2, { "allowKeywords": true }],
"eol-last": 2,
"eqeqeq": 2,
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"new-cap": 2,
"new-parens": 2,
"no-alert": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-catch-shadow": 2,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-parens": [2, "functions"],
"no-implied-eval": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-native-reassign": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-wrappers": 2,
"no-octal-escape": 2,
"no-process-exit": 2,
"no-proto": 2,
"no-return-assign": 2,
"no-script-url": 2,
"no-sequences": 2,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-trailing-spaces": 2,
"no-undef-init": 2,
"no-underscore-dangle": 2,
"no-unused-expressions": 2,
"no-use-before-define": 2,
"no-with": 2,
"quotes": [2, "double"],
"semi": 2,
"semi-spacing": [2, {"before": false, "after": true}],
"space-infix-ops": 2,
"keyword-spacing": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"strict": [2, "function"],
"yoda": [2, "never"],

//
// Enabled over default
//
"valid-jsdoc": [1, {
"requireReturn": false,
"prefer": {
"return": "returns"
}
}],
"guard-for-in": 2,
"no-div-regex": 2,
"no-eq-null": 2,
"no-floating-decimal": 2,
"no-self-compare": 2,
"no-void": 2,
"radix": 2,
"wrap-iife": 2,
"no-mixed-requires": 2,
"no-new-require": 2,
"no-path-concat": 2,
"brace-style": [2, "1tbs", { "allowSingleLine": false }],
"comma-style": [2, "last"],
"consistent-this": [2, "that"],
"no-inline-comments": 2,
"no-multiple-empty-lines": [1, {"max": 1}],
"no-nested-ternary": 2,
"space-before-function-paren": [2, "never"],
"space-before-blocks": 1,
"space-in-parens": 1,
"spaced-comment": 1,
"max-len": [1, 120, 4],
"no-loop-func": 2

//
// Disabled
//
}
}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/node_modules
.project
.settings/
npm-debug.log
/eslint.xml
/test/*.tap
/test/coverage
/test/vendor
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.bowerrc
.eslintrc
.gitmodules
.npmignore
.travis.yml
gulpfile.js
bower.json
dist
karma.config.js
test
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "6.0"
- "5.0"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
before_install: npm install -g gulp
script: gulp travis
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2016 Nic Jansma, http://nicj.net

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
170 changes: 170 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# cdn-detector.js

v0.1.0

[http://nicj.net](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:

* [WebpageTest](https://github.com/WPO-Foundation/webpagetest/blob/master/agent/wpthook/cdn.h)
* [cdnfinder.js](https://github.com/sajal/cdnfinder.js/blob/master/lib/guesscnamecdn.js)

## Download

Releases are available for download from [GitHub](https://github.com/nicjansma/cdn-detector.js).

### 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](https://github.com/nicjansma/cdn-detector.js/raw/master/dist/cdn-detector.js) - 13.8kb

__Production:__ [cdn-detector.min.js](https://github.com/nicjansma/cdn-detector.js/raw/master/dist/cdn-detector.min.js) - 7.0kb minified, 2.3kb gzipped

### NPM

cdn-detector.js is also available as the [npm cdn-detector module](https://npmjs.org/package/cdn-detector). You can install
using Node Package Manager (npm):

npm install cdn-detector

### Bower

cdn-detector.js is also available via [bower](http://bower.io/). You can install using:

bower install cdn-detector

## Usage

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

```html
<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:

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

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

```js
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:

```json
{
"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:

```json
{
"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.

```json
{
"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:

```json
{
"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.

## 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
28 changes: 28 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "cdn-detector",
"version": "0.1.0",
"homepage": "https://github.com/nicjansma/cdn-detector.js",
"authors": [
"Nic Jansma <nic@nicj.net>"
],
"description": "CDN detector",
"main": "src/cdn-detector.js",
"keywords": [
"cdn",
"content delivery network"
],
"license": "MIT",
"ignore": [
"**/.*",
"gulpfile.js",
"deps",
"dist",
"node_modules",
"bower_components",
"test"
],
"devDependencies": {
"mocha": "~1.21.5",
"chai": "~3.4.2"
}
}
Loading

0 comments on commit b6128bc

Please sign in to comment.