Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
81 lines (74 loc) · 1.59 KB
/
Copy pathutils.js
File metadata and controls
81 lines (74 loc) · 1.59 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
/**
* Add Benchmark HTTP response headers.
*
* Add HTTP response headers `Server` and `Date` which is required by the test suite.
*
* https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview
*
* @param {import('hono').Context} c
*/
export function addBenchmarkHeaders(c) {
c.header("Server", "Hono");
}
/**
* Handle error for response
*
* @param {Error} err
* @param {import('hono').Context} c
*/
export function handleError(err, c) {
console.error(err);
addBenchmarkHeaders(c);
c.text("Internal Server Error");
}
/**
* Get queries count
*
* @param {import('hono').Context} c
*/
export function getQueriesCount(c) {
return Math.min(parseInt(c.req.query("queries")) || 1, 500);
}
/**
* Generate random number
*
*/
export function generateRandomNumber() {
return Math.ceil(Math.random() * 10000);
}
/**
* Escape unsafe HTML Code
*
*/
const escapeHTMLRules = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
};
const unsafeHTMLMatcher = /[&<>"'\/]/g;
export function escape(text) {
if (unsafeHTMLMatcher.test(text) === false) return text;
return text.replace(unsafeHTMLMatcher, function (m) {
return escapeHTMLRules[m] || m;
});
}
/**
* Using Sort method which is performant for the test scenario
* @returns
*/
export function sortByMessage(arr) {
const n = arr.length;
for (let i = 1; i < n; i++) {
const c = arr[i];
let j = i - 1;
while (j > -1 && c.message < arr[j].message) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = c;
}
return arr;
}