-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.js
122 lines (110 loc) · 3.22 KB
/
util.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
'use strict';
const https = require('https');
const querystring = require('querystring');
const nsort = exports.nsort = function nsort(array) {
array.sort((a, b) => a - b);
};
const percentile = exports.percentile = function percentile(array, p) {
return array[Math.max(0, Math.floor(p * array.length / 100.0) - 1)];
};
const agent = new https.Agent({ maxSockets: 4096, keepAlive: true });
function makeURL(url, params) {
if (url.substr(0, 8) !== 'https://') {
throw new Error(`Invalid https url: ${url}`);
}
url = `${url.substr(8)}?${querystring.stringify(params)}`;
const index = url.indexOf('/');
return { host: url.substr(0, index), path: url.substr(index) };
}
const httpsGet = exports.httpsGet = function httpsGet(url, params, callback) {
const u = makeURL(url, params);
const req = https.request({
hostname: u.host,
port: 443,
path: u.path,
method: 'GET',
agent: agent,
}, res => {
if (res.statusCode !== 200) {
callback(res.statusCode);
return;
}
const chunks = [];
res.on('data', data => {
chunks.push(data);
});
res.on('end', () => {
const body = chunks.join('');
const payload = body ? JSON.parse(body) : {};
callback(null, payload);
});
});
req.on('error', e => {
callback(999);
});
req.end();
};
const httpsPost = exports.httpsPost = function httpsPost(url, params, callback) {
const u = makeURL(url, params);
const body = JSON.stringify(params);
const req = https.request({
hostname: u.host,
port: 443,
path: u.path,
method: 'POST',
agent: agent,
headers : {
'Content-Type': 'application/json',
'Content-Length': body.length,
},
}, res => {
if (res.statusCode !== 200) {
callback(res.statusCode);
return;
}
const chunks = [];
res.on('data', data => {
chunks.push(data);
});
res.on('end', () => {
const body = chunks.join('');
const payload = body ? JSON.parse(body) : {};
callback(null, payload);
});
});
req.on('error', e => {
callback(999);
});
req.write(body);
req.end();
};
const extractQueryStringParameter = exports.extractQueryStringParameter =
function extractQueryStringParameter(request, requestQueryFieldName, respondWithError, parameterName, ...filters) {
function reportError(code, message) {
if (respondWithError) {
console.error(`Error ${code}: ${message}`);
respondWithError(code, message);
}
}
let value = request[requestQueryFieldName] && request[requestQueryFieldName][parameterName];
if (value === undefined) {
return reportError(400, `Missing query string parameter: ${parameterName}`);
}
for (const filter of filters) {
if (filter instanceof RegExp) {
if (!filter.test(value)) {
return reportError(400, `Invalid value for query string parameter ${parameterName}: ${value}`);
}
}
else if (typeof(filter) === 'function') {
value = filter(value);
if (value === undefined) {
return reportError(400, `Invalid value for query string parameter ${parameterName}: ${value}`);
}
}
else {
return reportError(500, `Invalid filter for query string parameter: ${parameterName}`);
}
}
return value;
};