Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,31 +443,47 @@ module.exports = function(/*options, callback*/) {
function (callback) {
let repository = options.repositories[repositoryIndex];
let url = repository.url + urlPath;
let req_options = { url: url };
let req_options = {
url: url,
responseType: 'stream', // Use stream for better memory handling
timeout: 30000
};

if (repository.hasOwnProperty('credentials')) {
let username = repository.credentials.username;
let password = repository.credentials.password;
req_options = {
url: url,
auth: {
username,
password
}
req_options.auth = {
username: repository.credentials.username,
password: repository.credentials.password
};
}

debug('downloading ' + url);
axios.get(url, req_options).catch(err => {
error = err
return callback();
}).then(res => {
fs.promises.writeFile(destinationFile, res.data).then(() => {
foundUrl = url;
return callback()
}).catch(() => {
axios.get(url, req_options)
.then(response => {
if (response.status !== 200) {
error = new Error('download failed for ' + url + (reason ? ' (' + reason + ')' : '') + ' [status: ' + response.status + ']');
repositoryIndex++;
return callback();
}

const writer = fs.createWriteStream(destinationFile);
response.data.pipe(writer);

writer.on('finish', () => {
foundUrl = url;
return callback();
});

writer.on('error', (writeErr) => {
error = writeErr;
repositoryIndex++;
return callback();
});
})
})
repositoryIndex++;
.catch(err => {
error = new Error('download failed for ' + url + (reason ? ' (' + reason + ')' : '') + ' [status: ' + (err.response?.status || 'network error') + ']');
repositoryIndex++;
return callback();
});
},
function () {
if (foundUrl) {
Expand All @@ -476,7 +492,6 @@ module.exports = function(/*options, callback*/) {
return callback(error);
}
);

})
.catch((err) => callback(err));
}
Expand Down