fix: local content should use fs for inlining

The issue here was that although the "simple" detection was in place if the URL looked like HTML (i.e. has an angle bracket in it), but it didn't ever set the `isFile` flag.

This fix corrects this, but also swallows ENOENT errors, to mirror >=400 response codes being swallowed (i.e. return an empty body).

Fixes #89
This commit is contained in:
Remy Sharp 2016-04-20 21:42:48 +01:00
parent 0840c84140
commit 0ddff8886f
4 changed files with 26 additions and 2 deletions

View File

@ -35,6 +35,17 @@ module.exports = function get(url, options) {
'content-type': mime.lookup(url),
},
};
}).catch(function (error) {
if (error.code === 'ENOENT') {
inliner.emit('warning', 'no such file: ' + url);
}
return {
body: '',
headers: {
'content-type': mime.lookup(url),
},
};
});
return cache[url];

View File

@ -113,7 +113,7 @@ function Inliner(source, options, callback) {
this.jobs.done[key] = this.completeJob.bind(this, key);
}.bind(this));
this.isFile = false;
this.isFile = options.useStdin || false;
this.on('error', function localErrorHandler(event) {
inliner.callback(event);
@ -192,6 +192,7 @@ function main() {
// otherwise we're dealing with an inline string
debug('inlining by string: ', inliner.source);
inliner.isFile = true;
inliner.url = '.';
var res = {
body: new Buffer(inliner.source),

View File

@ -6,7 +6,8 @@
"scripts": {
"cover": "tap test/*.test.js --cov --coverage-report=lcov",
"lint": "jscs -v cli/*.js lib/*.js",
"test": "npm run lint && tap test/*.test.js --cov -R spec",
"tap": "tap test/*.test.js --cov -R spec",
"test": "npm run lint && npm run tap",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"keywords": [

View File

@ -36,6 +36,17 @@ test('inliner core functions', function coreTests(t) {
});
});
test('inliner handles given source as local', function sourcedTests(t) {
var Inliner = require('../');
t.plan(1);
var content = fs.readFileSync(__dirname + '/fixtures/css-ext-import.src.html', 'utf8');
new Inliner(content, function (error, content) {
t.equal(error, null, 'treats local content as file');
});
});
test('failures', function failureTests(t) {
var Inliner = require('../');
var throwBack = function (e) { return e; };