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

Replace request with got #169

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
51 changes: 25 additions & 26 deletions lib/controllers/esri-token-auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* jshint node: true, esnext: true */
"use strict";
let got;
(async () => {
got = await import("got");
})();
var router = require('express').Router();
var request = require('request');
var bodyParser = require('body-parser');
var url = require('url');

Expand All @@ -17,7 +20,7 @@ module.exports = function(options) {
tokenServers = validateServerConfig(tokenServers);

router.use(bodyParser.json({limit:postSizeLimit, type:'application/json'}));
router.post('/', function(req, res, next) {
router.post('/', async function(req, res, next) {
let parameters = req.body;

if (!parameters.url) {
Expand All @@ -34,32 +37,28 @@ module.exports = function(options) {
return res.status(400).send('Unsupported URL specified.');
}

request({
url: tokenServer.tokenUrl,
method: 'POST',
headers: {
'User-Agent': 'TerriaJSESRITokenAuth',
},
form:{
username: tokenServer.username,
password: tokenServer.password,
f: 'JSON'
}
}, function(error, response, body) {
try {
res.set('Content-Type', 'application/json');

if (response.statusCode !== 200) {
return res.status(502).send('Token server failed.');
} else {
let value = JSON.parse(response.body);
return res.status(200).send(JSON.stringify(value));
try {
const response = await got({
url: tokenServer.tokenUrl,
method: 'POST',
headers: {
'User-Agent': 'TerriaJSESRITokenAuth',
},
form: {
username: tokenServer.username,
password: tokenServer.password,
f: 'JSON'
}
});
res.set('Content-Type', 'application/json');

if (response.statusCode !== 200) {
return res.status(502).send('Token server failed.');
}
catch (error) {
return res.status(500).send('Error processing server response.');
}
});
return res.status(200).send(response.json());
} catch (error) {
return res.status(500).send('Error processing server response.');
}
});

return router;
Expand Down
25 changes: 13 additions & 12 deletions lib/controllers/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@

var bodyParser = require('body-parser');
var router = require('express').Router();
var request = require('request');
let got;
(async () => {
got = await import("got");
})();

module.exports = function(options) {
if (!options || !options.issuesUrl || !options.accessToken) {
return;
}

router.use(bodyParser.json());
router.post('/', function(req, res, next) {
router.post('/', async function (req, res, next) {
var parameters = req.body;

request({
const response = await got({
url: options.issuesUrl,
method: 'POST',
headers: {
'User-Agent': options.userAgent || 'TerriaBot (TerriaJS Feedback)',
'Accept': 'application/vnd.github.v3+json',
'Authorization': `token ${options.accessToken}`
},
body: JSON.stringify({
json: {
title: parameters.title ? parameters.title : 'User Feedback',
body: formatBody(req, parameters, options.additionalParameters)
})
}, function(error, response, body) {
res.set('Content-Type', 'application/json');
if (response.statusCode < 200 || response.statusCode >= 300) {
res.status(response.statusCode).send(JSON.stringify({result: 'FAILED'}));
} else {
res.status(200).send(JSON.stringify({result: 'SUCCESS'}));
}
});

res.set('Content-Type', 'application/json');
if (response.statusCode < 200 || response.statusCode >= 300) {
res.status(response.statusCode).send(JSON.stringify({result: 'FAILED'}));
} else {
res.status(200).send(JSON.stringify({result: 'SUCCESS'}));
}
});

return router;
Expand Down
33 changes: 24 additions & 9 deletions lib/controllers/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"use strict";

var express = require('express');
var defaultRequest = require('request');
let defaultRequest;
(async () => {
defaultRequest = await import("got");
})();
const { HttpProxyAgent } = require('hpagent')
var url = require('url');
var bodyParser = require('body-parser');
var rangeCheck = require('range_check');
Expand Down Expand Up @@ -198,9 +202,10 @@ module.exports = function(options) {
// encoding : null means "body" passed to the callback will be raw bytes

var proxiedRequest;
req.on('close', function() {
req.on('close', async function () {
if (proxiedRequest) {
proxiedRequest.abort();
const r = await proxiedRequest;
r.cancel();
}
});

Expand Down Expand Up @@ -239,14 +244,23 @@ module.exports = function(options) {

function buildReqHandler(httpVerb) {
function handler(req, res, next) {
return doProxy(req, res, next, req.retryWithoutAuth, function(remoteUrl, filteredRequestHeaders, proxy, maxAgeSeconds) {
return doProxy(req, res, next, req.retryWithoutAuth, async function(remoteUrl, filteredRequestHeaders, proxy, maxAgeSeconds) {
try {
var proxiedRequest = request({
var proxiedRequest = await request({
method: httpVerb,
url: url.format(remoteUrl),
headers: filteredRequestHeaders,
encoding: null,
proxy: proxy,
responseType: "buffer",
agent: {
http: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: proxy
})
},
body: req.body,
followRedirect: function(response) {
var location = response.headers.location;
Expand All @@ -263,11 +277,12 @@ module.exports = function(options) {
return false;
}
}).on('socket', function(socket) {
socket.once('lookup', function(err, address, family, host) {
socket.once('lookup', async function(err, address, family, host) {
if (rangeCheck.inRange(address, blacklistedAddresses)) {
res.status(403).send('IP address is not allowed: ' + address);
res.end();
proxiedRequest.abort();
const r = await proxiedRequest;
r.cancel();
}
});
}).on('error', function(err) {
Expand Down
79 changes: 53 additions & 26 deletions lib/controllers/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
'use strict';

var bodyParser = require('body-parser');
var requestp = require('request-promise');
var rperrors = require('request-promise/errors');
let got;
(async () => {
got = await import("got");
})();

var gistAPI = 'https://api.github.com/gists';

Expand All @@ -23,22 +25,37 @@ function makeGist(serviceOptions, body) {
if (serviceOptions.accessToken !== undefined) {
headers['Authorization'] = 'token ' + serviceOptions.accessToken;
}
return requestp({
return got({
url: gistAPI,
method: 'POST',
headers: headers,
json: true,
body: {
json: {
files: gistFile,
description: (serviceOptions.gistDescription || 'User-created catalog'),
public: false
}, transform: function(body, response) {
if (response.statusCode === 201) {
console.log('Created ID ' + response.body.id + ' using Gist service');
return response.body.id;
} else {
return response;
}
},
hooks: {
afterResponse: [
(response, _retryWithMergedOptions) => {
if (response.statusCode === 201) {
console.log('Created ID ' + response.body.id + ' using Gist service');
return response.body.id;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doubt this is tested by the test suite too, and I have no idea if body has an id attribute with got.

} else {
return response;
}
}
],
beforeError: [
error => {
const {response} = error;
if (response && response.body) {
console.error(JSON.stringify(response.body.message, null, 2));
error.message = response.json()
error.response.statusCode = 500;
}
return error;
}
]
}
});
}
Expand All @@ -52,16 +69,32 @@ function resolveGist(serviceOptions, id) {
if (serviceOptions.accessToken !== undefined) {
headers['Authorization'] = 'token ' + serviceOptions.accessToken;
}
return requestp({
return got({
url: gistAPI + '/' + id,
headers: headers,
json: true,
transform: function(body, response) {
if (response.statusCode >= 300) {
return response;
} else {
return body.files[Object.keys(body.files)[0]].content; // find the contents of the first file in the gist
}
responseType: 'json',
hooks: {
afterResponse: [
(response, _retryWithMergedOptions) => {
if (response.statusCode >= 300) {
return response;
} else {
return response.body.files[Object.keys(response.body.files)[0]].content; // find the contents of the first file in the gist
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got seems pretty compatible with request, many options have the same names.

However, I don't think this is tested by the test suite, and I have no idea if it is correct with got.

}
}
],
beforeError: [
error => {
const {response} = error;
if (response && response.body) {
console.error(JSON.stringify(response.body.message, null, 2));
error.message = response.body.message
error.response.statusCode = 500;
}
return error;
}
]

}
});
}
Expand Down Expand Up @@ -171,9 +204,6 @@ module.exports = function(hostName, port, options) {
res .location(resUrl)
.status(201)
.json({ id: id, path: resPath, url: resUrl });
}).catch(rperrors.TransformError, function (reason) {
console.error(JSON.stringify(reason, null, 2));
res.status(500).json({ message: reason.cause.message });
}).catch(function(reason) {
console.warn(JSON.stringify(reason, null, 2));
res.status(500) // probably safest if we always return a consistent error code
Expand All @@ -199,9 +229,6 @@ module.exports = function(hostName, port, options) {
}
resolver(serviceOptions, id).then(function(content) {
res.send(content);
}).catch(rperrors.TransformError, function (reason) {
console.error(JSON.stringify(reason, null, 2));
res.status(500).send(reason.cause.message);
}).catch(function(reason) {
console.warn(JSON.stringify(reason.response, null, 2));
res.status(404) // probably safest if we always return 404 rather than whatever the upstream provider sets.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "NodeJS server for TerriaJS, consisting of a CORS proxy, proj4 CRS lookup service, and express static server.",
"engineStrict": true,
"engines": {
"node": ">=12.0.0"
"node": ">=20.0.0"
},
"resolutions": {
"underscore": "^1.12.1"
Expand Down Expand Up @@ -40,13 +40,13 @@
"cors": "^2.7.1",
"express": "^4.8.0",
"express-brute": "^1.0.1",
"got": "14.3.0",
"hpagent": "^1.2.0",
"json5": "^2.2.3",
"morgan": "^1.7.0",
"proj4": "^2.3.12",
"proj4js-defs": "0.0.1",
"range_check": "^1.4.0",
"request": "^2.88.2",
"request-promise": "^4.0.1",
"yargs": "^13.2.4"
},
"devDependencies": {
Expand Down
Loading