Skip to content

Commit

Permalink
feat(netjet): create the middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
terinjokes committed Jan 29, 2016
1 parent 17ee4e3 commit dfbe66a
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
coverage/
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2016 CloudFlare, Inc.

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.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# netjet

netjet is a Node.js HTTP middleware to automatically insert [Preload][preload] link headers in HTML responses.
These Preload link headers allow for web browsers to initiate early resource fetching before being needed for execution.

## Example usage

```javascript
var express = require('express');
var netjet = require('netjet');
var root = '/path/to/static/folder';

express()
.use(netjet())
.use(express.static(root))
.listen(1337);
```

## License
[MIT](https://www.tldrlegal.com/l/mit), see `LICENSE.md`.

[preload]: https://www.w3.org/TR/preload/
79 changes: 79 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';
var posthtml = require('posthtml');
var posthtmlPreload = require('./posthtml-preload');

var unescape = require('lodash.unescape');
var defaults = require('lodash.defaults');
var hijackresponse = require('hijackresponse');
var bl = require('bl');

module.exports = function netjet(options) {
options = defaults(options, {
images: true,
scripts: true,
styles: true
});

return function (req, res, next) {
function appendHeader(field, value) {
var prev = res.getHeader(field);

if (prev) {
value = [].concat(prev, value);
}

res.setHeader(field, value);
}

function insertLinks(urls, asType) {
urls.forEach(function (url) {
appendHeader('Link', '<' + unescape(url) + '>; rel=preload; as=' + asType);
});
}

function processBody(body) {
var found = {};

posthtml().use(posthtmlPreload(options, found)).process(body);

if (options.images) {
insertLinks(found.images, 'image');
}

if (options.scripts) {
insertLinks(found.scripts, 'script');
}

if (options.styles) {
insertLinks(found.styles, 'style');
}
}

hijackresponse(res, function (err, res) {
/* istanbul ignore next */
// `err` from hijackresponse is currently hardcoded to "null"
if (err) {
res.unhijack();
return next(err);
}

// Only hijack HTML responses
if (!/^text\/html(?:;|\s|$)/.test(res.getHeader('Content-Type'))) {
return res.unhijack();
}

res.pipe(bl(function (err, data) {
if (err) {
res.unhijack();
return next(err);
}

processBody(data.toString());

res.end(data);
}));
});

next();
};
};
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "netjet",
"version": "1.0.0",
"description": "Express middleware to generate preload headers",
"main": "index.js",
"scripts": {
"test": "xo && istanbul cover -- _mocha --check-leaks --reporter spec"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cloudflare/vahevara.git"
},
"keywords": [
"express",
"middleware",
"link",
"preload"
],
"author": "Terin Stock <[email protected]> (https://terinstock.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/cloudflare/vahevara/issues"
},
"homepage": "https://github.com/cloudflare/vahevara#readme",
"files": [
"index.js",
"posthtml-preload.js"
],
"dependencies": {
"bl": "^1.0.1",
"hijackresponse": "^1.0.2",
"lodash.defaults": "^4.0.0",
"lodash.unescape": "^3.1.1",
"posthtml": "^0.8.1"
},
"devDependencies": {
"assume": "^1.3.1",
"detour": "^1.3.0",
"mocha": "^2.4.5",
"supertest": "^1.1.0",
"xo": "^0.12.1"
},
"xo": {
"space": true
}
}
46 changes: 46 additions & 0 deletions posthtml-preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
var _ = require('lodash');

module.exports = function (options, found) {
return function (tree) {
_.extend(found, {
images: [],
scripts: [],
styles: []
});

var matchers = [];

if (options.images) {
matchers.push({tag: 'img', attrs: {src: true}});
}

if (options.scripts) {
matchers.push({tag: 'script', attrs: {src: true}});
}

if (options.styles) {
matchers.push({tag: 'link', attrs: {rel: 'stylesheet'}});
}

if (matchers) {
tree.match(matchers, function (node) {
switch (node.tag) {
case 'img':
found.images.push(node.attrs.src);
break;
case 'script':
found.scripts.push(node.attrs.src);
break;
case 'link':
found.styles.push(node.attrs.href);
break;
// no default
}
return node;
});
}

return found;
};
};
Loading

0 comments on commit dfbe66a

Please sign in to comment.