Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: replace request by axios (#144) #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
54 changes: 23 additions & 31 deletions lib/millstone.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var env = process.env.NODE_ENV || 'development';
// Third party modules
var _ = require('underscore'),
srs = require('srs'),
request = require('request'),
axios = require('axios'),
zipfile = require('zipfile'),
Step = require('step'),
utils = require('./util.js');
Expand Down Expand Up @@ -137,25 +137,17 @@ function download(url, options, callback) {
return return_on_error(err);
} else {
if (env == 'development') console.error("[millstone] downloading: '" + url + "'");
var req;
try {
req = request({
url: url,
proxy: process.env.HTTP_PROXY
});
} catch (err) {
// catch Invalid URI error
return return_on_error(err);
}
req.on('error', function(err) {
return return_on_error(err);
});
req.pipe(fs.createWriteStream(dl)).on('error', function(err) {
// TODO
axios({
method: 'get',
url: url,
responseType: 'stream'
})
.then( (resp) => {
resp.data.pipe(fs.createWriteStream(dl))
.on('error', function(err) {
return return_on_error(err);
}).on('close', function() {
if (!req.response || (req.response && req.response.statusCode >= 400)) {
return return_on_error(new Error('server returned ' + req.response.statusCode));
} else {
}).on('close', function() {
pool.release(obj);
fs.rename(dl, options.filepath, function(err) {
if (err) {
Expand All @@ -169,9 +161,9 @@ function download(url, options, callback) {
// only use the `content-disposition` header to determine
// what kind of file we downloaded if it doesn't have an
// extension.
var req_meta = _(req.req.res.headers).clone();
if (req.req.res.request && req.req.res.request.path) {
req_meta.path = req.req.res.request.path;
var req_meta = _(resp.headers).clone();
if (resp.request && resp.request.path) {
req_meta.path = resp.request.path;
}
fs.writeFile(metapath(options.filepath), JSON.stringify(req_meta), 'utf-8', function(err) {
downloads[dkey].emit('done', err, options.filepath);
Expand All @@ -180,8 +172,10 @@ function download(url, options, callback) {
});
}
});
}
});
})
}).catch( (err) => {
return return_on_error(err);
})
}
});
}
Expand Down Expand Up @@ -614,17 +608,15 @@ function resolve(options, callback) {

// URL, download.
if (uri.protocol && (uri.protocol == 'http:' || uri.protocol == 'https:')) {
return request({
url: s,
proxy: process.env.HTTP_PROXY
}, function(err, response, data) {
if (err) return next(err);

axios.get(s)
.then( (response) => {
resolved.Stylesheet[index] = {
id: path.basename(uri.pathname),
data: data.toString()
data: response.data.toString()
};
localizeCartoURIs(resolved.Stylesheet[index],next);
}).catch( (err) => {
return next(err);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"underscore": "~1.6.0",
"step": "~0.0.5",
"generic-pool": "~2.4.1",
"request": "2.x",
"axios": "^0.20.0",
"srs": "1.x",
"zipfile": "~0.5.5",
"sqlite3": "4.1.0",
Expand Down