chore: merge branch 'feature/headers'

This commit is contained in:
Remy Sharp 2016-01-18 22:15:34 +00:00
commit 7c33ee7d23
9 changed files with 59 additions and 15 deletions

View File

@ -30,6 +30,7 @@ function options(args) {
s: 'nosvg',
o: 'videos',
m: 'inlinemin',
H: 'header',
},
});

View File

@ -7,24 +7,26 @@
Flags:
-n, --nocompress don't compress CSS or HTML - useful for debugging
-i, --noimages don't encode images - keeps files size small, but more requests
-o, --videos encode videos (and their poster image) - disabled by default
-s, --nosvg don't compress SVG (through SVGO)
-v, --verbose echo on STDERR the progress of inlining
-V, --version output the version number
-h, --help output usage information
-m, --inlinemin inline minified files
-n, --nocompress don't compress CSS or HTML - useful for debugging
-i, --noimages don't encode images - keeps files size small, but more requests
-o, --videos encode videos (and their poster image) - disabled by default
-s, --nosvg don't compress SVG (through SVGO)
-v, --verbose echo on STDERR the progress of inlining
-V, --version output the version number
-h, --help output usage information
-m, --inlinemin inline minified files
Options:
-e, --encoding override encoding detection
-H, --header LINE custom header pass to the server
-e, --encoding ENC override encoding detection
Examples:
$ inliner -v https://twitter.com > twitter.html
$ inliner -ni local-file.html > local-file.min.html
$ inliner -e windows-1253 http://foofootos.gr > foofootos-utf8.html
$ inliner -H User-Agent="Inliner Custom" https://httpbin.org/headers
$ cat local-file.html | inliner
For more details see http://github.com/remy/inliner/

View File

@ -88,4 +88,4 @@ function compress(css) {
// .replace(/\{ /g, '{')
.replace(/; /g, ';')
.replace(/\n+/g, '');
}
}

View File

@ -44,9 +44,10 @@ module.exports = function get(url, options) {
var settings = assign({}, options, {
encoding: null,
followRedirect: true,
headers: inliner.headers,
});
debug('request %s', url);
debug('request %s', url, settings);
cache[url] = new Promise(function promise(resolve, reject) {
request(url, settings, function response(error, res, body) {

View File

@ -12,6 +12,7 @@ var findAssets = require('./find-assets');
var iconv = require('iconv-lite');
var charset = require('charset');
var jschardet = require('jschardet');
var querystring = require('querystring');
// note: these tasks (excluding HTML), match up to files in lib/tasks/*.js
var tasks = {
@ -65,6 +66,29 @@ function Inliner(source, options, callback) {
this.source = source;
}
// this is an intentioal change. `.headers` is compatible with the request
// module, but -H and --header is compatible (i.e. the same as) cURL
if (options.header) {
options.headers = options.header;
delete options.header;
}
if (options.headers && !Array.isArray(options.headers)) {
options.headers = [options.headers];
}
if (options.headers && typeof options.headers[0] === 'string') {
// convert to an object of key/value pairs
options.headers = options.headers.reduce(function (acc, curr) {
var pair = querystring.parse(curr);
var key = Object.keys(pair).shift();
acc[key] = pair[key];
return acc;
}, {});
}
this.headers = options.headers || {};
this.callback = function wrapper(error, res) {
// noop the callback once it's fired
inliner.callback = function noop() {

View File

@ -6,13 +6,24 @@ var UglifyJS = require('uglify-js');
function uglify(source) {
this.emit('progress', 'compressing javascript');
if (source.trim() === '') {
source = source.trim();
if (source === '') {
return '';
}
debug('uglifying %sbytes', source.length);
return UglifyJS.minify(source, {
fromString: true,
}).code;
var result = '';
try {
result = UglifyJS.minify(source, {
fromString: true,
}).code;
} catch (e) {
// failed to uglify, just return it plain
result = source;
}
return result;
}

3
test/fixtures/headers.opts.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"headers": "User-Agent=inliner"
}

1
test/fixtures/headers.result.html vendored Normal file
View File

@ -0,0 +1 @@
<script>{ "user-agent": "inliner" }</script>

1
test/fixtures/headers.src.html vendored Normal file
View File

@ -0,0 +1 @@
<script src="https://httpbin.org/user-agent"></script>