-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathencoding.js
129 lines (118 loc) · 3.48 KB
/
encoding.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
123
124
125
126
127
128
129
/* jshint esversion: 8 */
/* jshint node: true */
"use strict";
/**
* @function is_c0_control_percent_encoded
* @description
* The C0 control percent-encode set are the C0 controls and all code points greater than U+007E (~).
* A C0 control is a code point in the range U+0000 NULL to U+001F INFORMATION SEPARATOR ONE, inclusive.
*
* [Specification]{@link https://url.spec.whatwg.org/#c0-control-percent-encode-set}
*
* @param {number} code_point
* @returns {boolean}
*/
function is_c0_control_percent_encoded(code_point) {
return code_point <= 0x1f || code_point > 0x7e;
}
const query_percent_encodes = {
0x20: true,
0x22: true,
0x23: true,
0x3C: true,
0x3E: true,
};
/**
* @function is_query_percent_encoded
* @description The query percent-encode set is the C0 control percent-encode set and U+0020 SPACE, U+0022 ("),
* U+0023 (#), U+003C (<), and U+003E (>).
* [Specification]{@link https://url.spec.whatwg.org/#query-percent-encode-set}
* @param {number} code
* @returns {boolean}
*/
function is_query_percent_encoded(code) {
return (
typeof query_percent_encodes[code] !== "undefined" ||
is_c0_control_percent_encoded(code)
);
}
const path_percent_encodes = { 0x3F: true, 0x60: true, 0x7B: true, 0x7D: true };
/**
* @function is_path_percent_encoded
* @description The path percent-encode set is the query percent-encode set and U+003F (?), U+0060 (`), U+007B ({), and U+007D (}).
* [Specification]{@link https://url.spec.whatwg.org/#path-percent-encode-set}
* @param {number} code
* @returns {boolean}
*/
function is_path_percent_encoded(code) {
return (
typeof path_percent_encodes[code] !== "undefined" ||
is_query_percent_encoded(code)
);
}
const user_info_percent_encodes = {
0x2F: true,
0x3A: true,
0x3B: true,
0x3D: true,
0x40: true,
0x5B: true,
0x5E: true,
0x5C: true,
0x5D: true,
0x7C: true,
};
/**
* @function is_userinfo_percent_encoded
* @description The userinfo percent-encode set is the path percent-encode set and U+002F (/), U+003A (:), U+003B (;),
* U+003D (=), U+0040 (@), U+005B ([) to U+005E (^), inclusive, and U+007C (|).
* [Specification]{@link https://url.spec.whatwg.org/#userinfo-percent-encode-set}
*
* @param {number} code
* @returns {boolean}
*/
function is_userinfo_percent_encoded(code) {
return (
typeof user_info_percent_encodes[code] !== "undefined" ||
is_path_percent_encoded(code)
);
}
const extra_fragment_percent_encodes = {
0x20: true,
0x22: true,
0x3C: true,
0x3E: true,
0x60: true,
};
/**
* @function is_fragment_percent_encoded
* @description The fragment percent-encode set is the C0 control percent-encode set and U+0020 SPACE,
* U+0022 ("), U+003C (<), U+003E (>), and U+0060 (`).
* [Specification]{@link https://url.spec.whatwg.org/#fragment-percent-encode-set}
*
* @param {number} code
* @returns {boolean}
*/
function is_fragment_percent_encoded(code) {
return (
typeof extra_fragment_percent_encodes[code] !== "undefined" ||
is_c0_control_percent_encoded(code)
);
}
/**
* @function is_special_query_percent_encoded
* [Specification]{@link https://url.spec.whatwg.org/#special-query-percent-encode-set}
* @param {number} code
* @returns {boolean}
*/
function is_special_query_percent_encoded(code) {
return code === 39 || is_query_percent_encoded(code);
}
module.exports = {
is_c0_control_percent_encoded,
is_userinfo_percent_encoded,
is_fragment_percent_encoded,
is_special_query_percent_encoded,
is_query_percent_encoded,
is_path_percent_encoded,
};