Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
47 lines (42 loc) · 1.55 KB
/
Copy pathutils.js
File metadata and controls
47 lines (42 loc) · 1.55 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
module.exports = {
writeResponse: function (res, data) {
data = data.toString();
res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': data.length });
res.write(data);
res.end();
},
writeError: function (res, data) {
data = data.toString();
res.writeHead(500, { 'Content-Type': 'text/html', 'Content-Length': data.length });
res.write(data);
res.end();
},
writeStatus: function (res, statusCode, data) {
data = data.toString();
res.writeHead(statusCode, { 'Content-Type': 'text/html', 'Content-Length': Buffer.byteLength(data) });
res.write(data);
res.end();
},
// Reads a positive integer setting, falling back to the default when the
// configured value is missing or not a usable positive integer.
positiveIntSetting: function (value, defaultValue) {
var parsed = Number(value);
if (Number.isSafeInteger(parsed) && parsed > 0) {
return parsed;
}
return defaultValue;
},
// Returns the requested record count, or null when the body is not a
// non-negative integer within the allowed range.
parseEntryCount: function (body, maxEntries) {
var trimmed = String(body === undefined || body === null ? "" : body).trim();
if (!/^\d+$/.test(trimmed)) {
return null;
}
var entries = Number(trimmed);
if (!Number.isSafeInteger(entries) || entries > maxEntries) {
return null;
}
return entries;
}
}