-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.js
140 lines (111 loc) · 4.46 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const http = require('http');
const https = require('https');
const request = require('superagent');
const _ = require('lodash');
const ServiceConfiguration = require('./ServiceConfiguration');
function isDoNotTrack(headers) {
return _.has(headers, 'DNT') ||
_.has(headers, 'dnt') ||
_.has(headers, 'do_not_track');
}
// superagent doesn't exposed the assembled GET request, so synthesize it
function synthesizeUrl(serviceConfig, req, res) {
const parameters = _.map(serviceConfig.getParameters(req, res), (value, key) => {
return `${key}=${value}`;
}).join('&');
if (parameters) {
return encodeURI(`${serviceConfig.getUrl(req)}?${parameters}`);
} else {
return serviceConfig.getUrl(req);
}
}
module.exports = function setup(serviceConfig) {
if (!(serviceConfig instanceof ServiceConfiguration)) {
throw Error('serviceConfig should be an instance of ServiceConfiguration');
}
const logger = require( 'pelias-logger' ).get( serviceConfig.getName() );
if (!serviceConfig.isEnabled()) {
logger.warn(`${serviceConfig.getName()} service disabled`);
return (req, res, callback) => {
// only req was passed in so treat res as callback
if (_.isUndefined(callback)) {
callback = res;
}
// respond with an error to any call
callback(`${serviceConfig.getName()} service disabled`);
};
}
logger.info(`using ${serviceConfig.getName()} service at ${serviceConfig.getBaseUrl()}`);
const connection_library = serviceConfig.getBaseUrl().startsWith('https') ? https : http;
// create one HTTP agent with keep alives enabled per service instance
const agent = new connection_library.Agent({
keepAlive: true
});
return (req, res, callback) => {
// only req was passed in so treat res as callback
if (_.isUndefined(callback)) {
callback = res;
res = undefined;
}
const headers = serviceConfig.getHeaders(req, res) || {};
// save off do_not_track value for later check
const do_not_track = isDoNotTrack(req.headers);
let url_for_logging;
if (do_not_track) {
headers.dnt = '1';
url_for_logging = serviceConfig.getBaseUrl();
} else {
url_for_logging = synthesizeUrl(serviceConfig, req, res);
}
logger.debug(`${serviceConfig.getName()}: ${url_for_logging}`);
const startTime = Date.now();
request
.get(serviceConfig.getUrl(req))
.set(headers)
.timeout(serviceConfig.getTimeout())
.retry(serviceConfig.getRetries())
.agent(agent)
.accept('json')
.query(serviceConfig.getParameters(req, res))
.on('error', (err) => {
if (err.status) {
// first handle case where a non-200 was returned
if (do_not_track) {
logger.error(`${url_for_logging} [do_not_track] returned status ${err.status}: ${err.response.text}`);
return callback(`${url_for_logging} [do_not_track] returned status ${err.status}: ${err.response.text}`);
} else {
logger.error(`${url_for_logging} returned status ${err.status}: ${err.response.text}`);
return callback(`${url_for_logging} returned status ${err.status}: ${err.response.text}`);
}
}
// handle case that something catastrophic happened while contacting the server
if (do_not_track) {
logger.error(`${url_for_logging} [do_not_track]: ${JSON.stringify(err)}`);
return callback(err);
} else {
logger.error(`${url_for_logging}: ${JSON.stringify(err)}`);
return callback(err);
}
})
.end((err, response) => {
// bail early if there's an error (shouldn't happen since it was already handled above)
if (err) {
return;
}
const metadata = {
response_time: Date.now() - startTime
};
// if json was returned then just return it
if (response.type === 'application/json') {
return callback(null, response.body, metadata);
}
if (do_not_track) {
logger.error(`${url_for_logging} [do_not_track] could not parse response: ${response.text}`);
return callback(`${url_for_logging} [do_not_track] could not parse response: ${response.text}`, null, metadata);
} else {
logger.error(`${url_for_logging} could not parse response: ${response.text}`);
return callback(`${url_for_logging} could not parse response: ${response.text}`, null, metadata);
}
});
};
};