Skip to content

Commit

Permalink
improved handling of protocol-relative asset urls [#288]
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Apr 1, 2018
1 parent 261eeda commit ffcfb8a
Show file tree
Hide file tree
Showing 4 changed files with 667 additions and 245 deletions.
51 changes: 30 additions & 21 deletions lib/file-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const fs = require('fs-extra');
const _ = require('lodash');
const Bluebird = require('bluebird');
const request = require('request');
const got = require('got');
const debug = require('debug')('critical:file');
const mime = require('mime-types');
const slash = require('slash');
Expand Down Expand Up @@ -117,28 +117,37 @@ function temp(resp) {
/**
* Get external resource
* @param {string} uri
* @param {boolean} secure
* @returns {Promise}
*/
function requestAsync(uri) {
return new Bluebird((resolve, reject) => {
// Handle protocol-relative urls
uri = url.resolve('http://te.st', uri);
request({url: uri, strictSSL: false}, (err, resp) => {
if (err) {
return reject(err);
}
if (resp.statusCode === 403 || resp.statusCode === 404) {
console.log('Ignoring', uri, '(' + resp.statusCode + ')');
resp.body = '';
return resolve(resp);
}

if (resp.statusCode >= 200 && resp.statusCode < 300) {
resolve(resp);
}
function requestAsync(uri, secure = true) {
let check = Promise.resolve();

return reject(new Error('Wrong status code ' + resp.statusCode + ' for ' + uri));
if (/^\/\//.test(uri)) {
uri = url.resolve(`http${secure ? 's' : ''}://te.st`, uri);
check = got(uri, {method: 'HEAD'}).then(res => {
debug(`HEADERS for ${uri}: `, JSON.stringify(res.headers));
});
}

return check.then(() => got(uri).then(res => {
if (res.statusCode === 403 || res.statusCode === 404) {
console.log('Ignoring', uri, '(' + res.statusCode + ')');
res.body = '';
return res;
}

if (res.statusCode >= 200 && res.statusCode < 300) {
return res;
}

return Promise.reject(new Error('Wrong status code ' + res.statusCode + ' for ' + uri));
})).catch(err => {
if (secure) {
return requestAsync(uri, false);
}

return Promise.resolve(err);
});
}

Expand Down Expand Up @@ -174,13 +183,13 @@ function isVinyl(file) {
* @param {string} filePath
* @returns {Promise}
*/
function assertLocal(filePath) {
function assertLocal(filePath, secure) {
if (!isExternal(filePath)) {
return new Bluebird(resolve => {
resolve(filePath);
});
}
return requestAsync(filePath)
return requestAsync(filePath, secure)
.then(response => temp(response));
}

Expand Down
Loading

0 comments on commit ffcfb8a

Please sign in to comment.