ti2 1.0.50 → 1.0.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache.js +3 -2
- package/index.js +65 -8
- package/package.json +1 -1
package/cache.js
CHANGED
|
@@ -3,7 +3,6 @@ const hash = require('object-hash');
|
|
|
3
3
|
|
|
4
4
|
const REDIS_URL = process.env.REDIS_URL || 'redis://redis:6379';
|
|
5
5
|
const cache = new Redis(`${REDIS_URL}/2`);
|
|
6
|
-
|
|
7
6
|
const defaultTTL = 60 * 60; // 1 hour
|
|
8
7
|
|
|
9
8
|
const save = async ({
|
|
@@ -57,10 +56,11 @@ const getOrExec = async ({
|
|
|
57
56
|
|
|
58
57
|
const drop = async ({
|
|
59
58
|
pluginName,
|
|
59
|
+
key: keyParam,
|
|
60
60
|
fn,
|
|
61
61
|
fnParams,
|
|
62
62
|
}) => {
|
|
63
|
-
let key = hash({ fn, fnParams });
|
|
63
|
+
let key = keyParam || hash({ fn, fnParams });
|
|
64
64
|
key = `${pluginName}:${key}`;
|
|
65
65
|
await cache.del(key);
|
|
66
66
|
};
|
|
@@ -70,4 +70,5 @@ module.exports = {
|
|
|
70
70
|
save,
|
|
71
71
|
getOrExec,
|
|
72
72
|
drop,
|
|
73
|
+
get,
|
|
73
74
|
};
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const createMiddleware = require('swagger-express-middleware');
|
|
|
3
3
|
const { connector } = require('swagger-routes-express');
|
|
4
4
|
const swaggerUi = require('swagger-ui-express');
|
|
5
5
|
const yaml = require('js-yaml');
|
|
6
|
+
const hash = require('object-hash');
|
|
6
7
|
const fs = require('fs');
|
|
7
8
|
const { pickBy } = require('ramda');
|
|
8
9
|
const bb = require('bluebird');
|
|
@@ -11,8 +12,20 @@ const R = require('ramda');
|
|
|
11
12
|
const { v4: uuidv4 } = require('uuid');
|
|
12
13
|
const EventEmitter = require('eventemitter2');
|
|
13
14
|
|
|
15
|
+
const cacheSettings = {
|
|
16
|
+
'*': [
|
|
17
|
+
'tokenTemplate',
|
|
18
|
+
'getAffiliateAgents',
|
|
19
|
+
'getAffiliateDesks',
|
|
20
|
+
'getPickupPoints',
|
|
21
|
+
'bookingsProductSearch',
|
|
22
|
+
],
|
|
23
|
+
ventrata: [],
|
|
24
|
+
fareharbor: [],
|
|
25
|
+
};
|
|
14
26
|
const ti2Events = new EventEmitter({ captureRejections: true, wildcard: true });
|
|
15
27
|
ti2Events.on('event error', console.error);
|
|
28
|
+
ti2Events.on('error', console.error);
|
|
16
29
|
|
|
17
30
|
const schema = yaml.load(fs.readFileSync(`${__dirname}/api.yml`));
|
|
18
31
|
|
|
@@ -136,11 +149,9 @@ module.exports = async ({
|
|
|
136
149
|
eventHandlerPlugins.forEach(plugin => {
|
|
137
150
|
plugin.eventHandler(ti2Events);
|
|
138
151
|
});
|
|
139
|
-
|
|
140
|
-
app.use((req, res, next) => {
|
|
141
|
-
const startHrTime = process.hrtime();
|
|
152
|
+
const composeBodyFromReq = req => {
|
|
142
153
|
const requestId = uuidv4();
|
|
143
|
-
|
|
154
|
+
return {
|
|
144
155
|
requestId,
|
|
145
156
|
date: Math.floor(Date.now() / 1e3),
|
|
146
157
|
url: req.url,
|
|
@@ -152,16 +163,21 @@ module.exports = async ({
|
|
|
152
163
|
operationId: R.path(['openapi', 'operation', 'operationId'], req),
|
|
153
164
|
client: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
|
|
154
165
|
};
|
|
166
|
+
};
|
|
167
|
+
app.use(async (req, res, next) => {
|
|
168
|
+
const startHrTime = process.hrtime();
|
|
169
|
+
const body = composeBodyFromReq(req);
|
|
170
|
+
req.customBody = body;
|
|
155
171
|
ti2Events.emit('request.start', body);
|
|
156
|
-
req.requestId = requestId;
|
|
157
|
-
res.on('finish', () => {
|
|
172
|
+
req.requestId = body.requestId;
|
|
173
|
+
res.on('finish', async () => {
|
|
158
174
|
const elapsedHrTime = process.hrtime(startHrTime);
|
|
159
175
|
const responseTimeInMs = parseInt(
|
|
160
176
|
elapsedHrTime[0] * 1e3 + elapsedHrTime[1] / 1e6,
|
|
161
177
|
10,
|
|
162
178
|
);
|
|
163
179
|
ti2Events.emit('request.end', {
|
|
164
|
-
...
|
|
180
|
+
...req.customBody,
|
|
165
181
|
responseTimeInMs,
|
|
166
182
|
responseStatusCode: res.statusCode,
|
|
167
183
|
});
|
|
@@ -169,6 +185,46 @@ module.exports = async ({
|
|
|
169
185
|
next();
|
|
170
186
|
});
|
|
171
187
|
|
|
188
|
+
app.use(async (req, res, next) => {
|
|
189
|
+
try {
|
|
190
|
+
const currentPlugin = plugins.find(p => p.name === req.pathParams.appKey);
|
|
191
|
+
const cachingOperations = [
|
|
192
|
+
...cacheSettings['*'],
|
|
193
|
+
...(currentPlugin ? R.pathOr([], [currentPlugin.name], cacheSettings) : []),
|
|
194
|
+
];
|
|
195
|
+
const body = req.customBody;
|
|
196
|
+
if (cachingOperations.indexOf(body.operationId) > -1) {
|
|
197
|
+
const cacheKey = hash(R.omit(['requestId', 'date'], body));
|
|
198
|
+
|
|
199
|
+
const foundCache = await cache.get({
|
|
200
|
+
pluginName: body.params.appKey,
|
|
201
|
+
key: cacheKey,
|
|
202
|
+
});
|
|
203
|
+
if (foundCache) {
|
|
204
|
+
// console.log('foundCache', cacheKey);
|
|
205
|
+
req.customBody.usedCache = cacheKey;
|
|
206
|
+
res.json(foundCache);
|
|
207
|
+
}
|
|
208
|
+
const realSend = res.json;
|
|
209
|
+
res.json = newData => { // new res.json
|
|
210
|
+
cache.save({
|
|
211
|
+
pluginName: req.pathParams.appKey,
|
|
212
|
+
key: cacheKey,
|
|
213
|
+
value: newData,
|
|
214
|
+
ttl: 60 * 60 * 24, // one day
|
|
215
|
+
});
|
|
216
|
+
// console.log('newData', cacheKey);
|
|
217
|
+
if (!res.headersSent) {
|
|
218
|
+
// console.log('send new data', cacheKey);
|
|
219
|
+
return realSend.apply(res, [newData]);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return next();
|
|
224
|
+
} catch (err) {
|
|
225
|
+
return next(err);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
172
228
|
connect(app);
|
|
173
229
|
app.use(middleware.mock());
|
|
174
230
|
// global error Handling
|
|
@@ -179,10 +235,11 @@ module.exports = async ({
|
|
|
179
235
|
if (process.env.CONSOLE_ERRORS || process.env.JEST_WORKER_ID) {
|
|
180
236
|
console.error(R.path(['response', 'data'], err), err);
|
|
181
237
|
}
|
|
238
|
+
console.log('hit hre');
|
|
182
239
|
return res.status((() => {
|
|
183
240
|
if (!isNumber(err.status)) return 500;
|
|
184
241
|
return Number(err.status);
|
|
185
|
-
})()).
|
|
242
|
+
})()).send({
|
|
186
243
|
message: R.path(['response', 'data', 'errorMessage'], err) || err.message || 'Internal Error',
|
|
187
244
|
});
|
|
188
245
|
});
|