Skip to content

Commit 107773d

Browse files
committed
Use cookies, correct behavior on redirect
1 parent b322797 commit 107773d

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

lib/load.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,26 @@ var Loader = function (data) {
7373
return processingFunction;
7474
}
7575

76-
function loadFromUrl(url, desiredFilename) {
77-
var localFilename = getLoadedFilename(url);
76+
function loadFromUrl(options) {
77+
var localFilename = getLoadedFilename(options.url);
7878
var fileProcessingFunction;
7979

8080
if (!localFilename) {
8181
// Get filename
82-
localFilename = getFilename(desiredFilename || path.basename(url));
83-
// Get processing function for file
84-
fileProcessingFunction = getFileProcessingFunction(url, localFilename);
82+
localFilename = getFilename(options.filename || path.basename(options.url));
8583
// Set file as loaded
86-
setLoadedFilename(url, localFilename);
84+
setLoadedFilename(options.url, localFilename);
85+
8786
// Request -> processing -> save to fs
88-
return utils.makeRequest(url).then(fileProcessingFunction).then(function saveFileToFS(text) {
87+
return utils.makeRequest(options.url).then(function requestCompleted(data) {
88+
options.url = data.url; // Url may be changed in redirects
89+
fileProcessingFunction = getFileProcessingFunction(options.url, localFilename);
90+
91+
return fileProcessingFunction(data.body);
92+
}).then(function saveFileToFS(text) {
8993
return fs.outputFileAsync(localFilename, text, {encoding: 'binary'});
9094
}).then(function fileSavedToFS() {
91-
logger.log(url + ' -> ' + localFilename);
95+
logger.log(options.url + ' -> ' + localFilename);
9296
return localFilename;
9397
});
9498
}
@@ -148,8 +152,10 @@ var Loader = function (data) {
148152
var attr = self.attr(attribute);
149153

150154
if (attr) {
151-
var htmlSourceUrl = utils.getUrl(htmlUrl, attr);
152-
return loadFromUrl(htmlSourceUrl).then(function loadedHtmlSourceFileFromUrl(htmlSourceLocalPath) {
155+
var loadOptions = {
156+
url: utils.getUrl(htmlUrl, attr)
157+
};
158+
return loadFromUrl(loadOptions).then(function loadedHtmlSourceFileFromUrl(htmlSourceLocalPath) {
153159
var srcRelativePath = utils.getUnixPath(path.relative(htmlDirectoryPath, htmlSourceLocalPath));
154160
self.attr(attribute, srcRelativePath);
155161
return Promise.resolve();
@@ -167,9 +173,11 @@ var Loader = function (data) {
167173
function loadCssSources(cssUrl, cssLocalPath, text) {
168174
var cssDirectoryPath = path.dirname(cssLocalPath);
169175
var cssSourcesPromises = _.map(css.getSourcesPaths(text), function loadCssSourceFile(cssSourcePath) {
170-
var cssSourceUrl = utils.getUrl(cssUrl, cssSourcePath);
176+
var loadOptions = {
177+
url: utils.getUrl(cssUrl, cssSourcePath)
178+
};
171179

172-
return loadFromUrl(cssSourceUrl).then(function loadedCssSourceFileFromUrl(cssSourceLocalPath) {
180+
return loadFromUrl(loadOptions).then(function loadedCssSourceFileFromUrl(cssSourceLocalPath) {
173181
var srcRelativePath = utils.getUnixPath(path.relative(cssDirectoryPath, cssSourceLocalPath));
174182
text = text.replace(new RegExp(cssSourcePath, 'g'), srcRelativePath);
175183
return Promise.resolve();
@@ -222,7 +230,7 @@ var Loader = function (data) {
222230

223231
function load(loadOptions) {
224232
return Promise.reduce(loadOptions, function (result, currentOptions) {
225-
return loadFromUrl(currentOptions.url, currentOptions.filename).then(function htmlLoaded(filename) {
233+
return loadFromUrl(currentOptions).then(function htmlLoaded(filename) {
226234
result.push({
227235
url: currentOptions.url,
228236
filename: filename

lib/utils/utils.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ function makeRequest(url) {
3030
url: url,
3131
method: 'GET',
3232
encoding: 'binary',
33-
strictSSL: false
34-
}).then(function (response) {
35-
return response[1];
33+
strictSSL: false,
34+
jar: true
35+
}).then(function (data) {
36+
return {
37+
url: data[0].request.href,
38+
body: data[0].body
39+
}
3640
});
3741
}
3842

3943
module.exports.isUrl = isUrl;
4044
module.exports.getUrl = getUrl;
4145
module.exports.getUnixPath = getUnixPath;
4246
module.exports.trimFilename = trimFilename;
43-
module.exports.makeRequest = makeRequest;
47+
module.exports.makeRequest = makeRequest;

0 commit comments

Comments
 (0)