-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhandler.js
More file actions
145 lines (122 loc) · 3.72 KB
/
handler.js
File metadata and controls
145 lines (122 loc) · 3.72 KB
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
141
142
143
144
145
import pMap from 'p-map';
const { parse } = require('url');
const { json } = require('micro');
const go = require('./go');
const java = require('./java');
const nuget = require('./nuget');
const ping = require('./ping');
const registries = require('./registries');
const log = require('./utils/log');
const cache = require('./utils/cache');
const tracking = require('./utils/tracking');
const preparePayload = require('./utils/payload');
const logPrefix = log.prefix;
const mapper = async (item) => {
let result;
if (registries.supported.includes(item.type)) {
result = await registries.resolve(item.type, item.target);
} else if (item.type === 'go') {
result = await go(item.target);
} else if (item.type === 'java') {
result = await java(item.target);
} else if (item.type === 'ping') {
result = await ping(item.target);
} else if (item.type === 'nuget') {
result = await nuget(item.target);
} else {
return;
}
return {
...item,
result,
};
};
async function requestHandler(payload) {
return pMap(payload, mapper, { concurrency: 5 });
}
function errorHandler(error, res) {
log(error);
res.statusCode = 500;
res.end('Internal server error');
}
tracking.init();
module.exports = async (req, res) => {
if (['POST', 'GET'].includes(req.method)) {
const timingTotalStart = Date.now();
let body;
if (req.method === 'POST') {
body = await json(req);
} else {
const { query } = parse(req.url, true);
body = [].concat(
...Object.entries(query).map(([type, values]) => values.split(',').map((target) => ({
type,
target,
}))),
);
}
const timingCacheAuthStart = Date.now();
await cache.auth();
const timingCacheAuth = Date.now() - timingCacheAuthStart;
let result = [];
let timingTotalEnd;
let completed = false;
const payload = preparePayload(body);
try {
result = await requestHandler(payload);
completed = true;
} catch (error) {
return errorHandler(error, res);
} finally {
timingTotalEnd = Date.now();
log('Request completed', completed);
log('Instance Name', logPrefix);
log('Redis Status', cache.getRedisStatus());
log('Redis Auth Duration', timingCacheAuth);
log('Simple Cache Size', cache.simpleCacheSize());
log('Result Length', (result && result.length) || 0);
log('Duration', timingTotalEnd - timingTotalStart);
const meta = {
event: 'meta',
properties: {
Region: process.env.NOW_REGION || 'unknown',
'Request completed': completed,
'Instance Name': logPrefix,
'Redis Status': cache.getRedisStatus(),
'Redis Auth Duration': timingCacheAuth,
'Simple Cache Size': cache.simpleCacheSize(),
'Result Length': (result && result.length) || 0,
Duration: timingTotalEnd - timingTotalStart,
},
};
const bulkData = [
meta,
...result.map((item) => ({
event: 'packages',
properties: {
Type: item.type,
Target: item.target,
Result: item.result,
},
})),
];
if (process.env.NODE_ENV !== 'test') {
console.time('Mixpanel');
await tracking.track(bulkData);
console.timeEnd('Mixpanel');
}
}
res.setHeader('Access-Control-Allow-Origin', '*');
if (req.method === 'GET') {
res.setHeader('Cache-Control', 'public, max-age=300, s-maxage=300, stale-while-revalidate');
}
return res.end(
JSON.stringify({
result,
}),
);
}
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'content-type');
res.end();
};