|
| 1 | +// deuri doesn't support cjs output so I have to copy the code here |
| 2 | +// @see https://github.com/re-utils/deuri |
| 3 | + |
| 4 | +/* eslint-disable */ |
| 5 | +const hex: number[] = [] |
| 6 | +for (let i = 48; i < 58; i++) hex[i] = i - 48 |
| 7 | + |
| 8 | +// A - F (index 65 - 70) |
| 9 | +// a - f (index 97 - 102) |
| 10 | +for (let i = 0; i < 6; i++) |
| 11 | + // 10 to 15 |
| 12 | + hex[i + 65] = hex[i + 97] = i + 10 |
| 13 | + |
| 14 | +const calcHex = (a: number, b: number): number => { |
| 15 | + if (a in hex && b in hex) return (hex[a] << 4) | hex[b] |
| 16 | + |
| 17 | + return 255 |
| 18 | +} |
| 19 | + |
| 20 | +// Map bytes to character to a transition |
| 21 | +const type: number[] = [ |
| 22 | + ...new Array(128).fill(0), |
| 23 | + ...new Array(16).fill(1), |
| 24 | + ...new Array(16).fill(2), |
| 25 | + ...new Array(32).fill(3), |
| 26 | + |
| 27 | + 4, |
| 28 | + 4, |
| 29 | + 5, |
| 30 | + 5, |
| 31 | + 5, |
| 32 | + 5, |
| 33 | + 5, |
| 34 | + 5, |
| 35 | + 5, |
| 36 | + 5, |
| 37 | + 5, |
| 38 | + 5, |
| 39 | + 5, |
| 40 | + 5, |
| 41 | + 5, |
| 42 | + 5, |
| 43 | + 5, |
| 44 | + 5, |
| 45 | + 5, |
| 46 | + 5, |
| 47 | + 5, |
| 48 | + 5, |
| 49 | + 5, |
| 50 | + 5, |
| 51 | + 5, |
| 52 | + 5, |
| 53 | + 5, |
| 54 | + 5, |
| 55 | + 5, |
| 56 | + 5, |
| 57 | + 5, |
| 58 | + 5, |
| 59 | + 6, |
| 60 | + 7, |
| 61 | + 7, |
| 62 | + 7, |
| 63 | + 7, |
| 64 | + 7, |
| 65 | + 7, |
| 66 | + 7, |
| 67 | + 7, |
| 68 | + 7, |
| 69 | + 7, |
| 70 | + 7, |
| 71 | + 7, |
| 72 | + 8, |
| 73 | + 7, |
| 74 | + 7, |
| 75 | + 10, |
| 76 | + 9, |
| 77 | + 9, |
| 78 | + 9, |
| 79 | + 11, |
| 80 | + 4, |
| 81 | + 4, |
| 82 | + 4, |
| 83 | + 4, |
| 84 | + 4, |
| 85 | + 4, |
| 86 | + 4, |
| 87 | + 4, |
| 88 | + 4, |
| 89 | + 4, |
| 90 | + 4 |
| 91 | +] |
| 92 | + |
| 93 | +// Maps a state to a new state when adding a transition |
| 94 | +const next = [ |
| 95 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 24, 36, 48, 60, 72, 84, |
| 96 | + 96, 0, 12, 12, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, |
| 97 | + 0, 0, 24, 24, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 24, 0, 0, 0, 0, 0, 0, 0, 0, |
| 98 | + 0, 0, 48, 48, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 48, 0, 0, 0, 0, 0, 0, 0, |
| 99 | + 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 100 | +] |
| 101 | + |
| 102 | +// Maps the current transition to a mask that needs to apply to the byte |
| 103 | +const mask = type.map( |
| 104 | + (val) => |
| 105 | + [ |
| 106 | + 0x7f, 0x3f, 0x3f, 0x3f, 0x00, 0x1f, 0x0f, 0x0f, 0x0f, 0x07, 0x07, |
| 107 | + 0x07 |
| 108 | + ][val] |
| 109 | +) |
| 110 | + |
| 111 | +/** |
| 112 | + * Decode the full string |
| 113 | + */ |
| 114 | +export const decode = (url: string): string | null => { |
| 115 | + let percentPosition = url.indexOf('%') |
| 116 | + if (percentPosition === -1) return url |
| 117 | + |
| 118 | + // Ensure percentPosition always has 2 chars after |
| 119 | + let end = url.length - 3 |
| 120 | + if (percentPosition > end) return null |
| 121 | + |
| 122 | + let decoded = '', |
| 123 | + start = 0, |
| 124 | + codepoint = 0, |
| 125 | + startOfOctets = percentPosition, |
| 126 | + // UTF_ACCEPT |
| 127 | + state = 12, |
| 128 | + byte: number |
| 129 | + |
| 130 | + for (;;) { |
| 131 | + byte = calcHex( |
| 132 | + url.charCodeAt(percentPosition + 1), |
| 133 | + url.charCodeAt(percentPosition + 2) |
| 134 | + ) |
| 135 | + state = next[state + type[byte]] |
| 136 | + if (state === 0) return null |
| 137 | + if (state === 12) { |
| 138 | + decoded += url.substring(start, startOfOctets) |
| 139 | + |
| 140 | + // Calculate current codepoint |
| 141 | + codepoint = (codepoint << 6) | (byte & mask[byte]) |
| 142 | + |
| 143 | + if (codepoint > 0xffff) |
| 144 | + decoded += String.fromCharCode( |
| 145 | + 0xd7c0 + (codepoint >> 10), |
| 146 | + 0xdc00 + (codepoint & 0x3ff) |
| 147 | + ) |
| 148 | + else decoded += String.fromCharCode(codepoint) |
| 149 | + |
| 150 | + // Search next encoded component |
| 151 | + start = percentPosition + 3 |
| 152 | + |
| 153 | + percentPosition = url.indexOf('%', start) |
| 154 | + if (percentPosition === -1) return decoded + url.substring(start) |
| 155 | + |
| 156 | + // Ensure percentPosition always has 2 chars after |
| 157 | + if (percentPosition > end) return null |
| 158 | + |
| 159 | + startOfOctets = percentPosition |
| 160 | + codepoint = 0 |
| 161 | + } else { |
| 162 | + // Check next % |
| 163 | + percentPosition += 3 |
| 164 | + if (percentPosition > end || url.charCodeAt(percentPosition) !== 37) |
| 165 | + return null |
| 166 | + |
| 167 | + // Calculate current codepoint |
| 168 | + codepoint = (codepoint << 6) | (byte & mask[byte]) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * Encode URI components |
| 175 | + */ |
| 176 | +export const encode = (str: string): string | null => |
| 177 | + str.isWellFormed() ? encodeURIComponent(str) : null |
| 178 | + |
| 179 | +/** |
| 180 | + * Decode a substring of an input string |
| 181 | + */ |
| 182 | +export const decodeSegment = ( |
| 183 | + url: string, |
| 184 | + start: number, |
| 185 | + end: number |
| 186 | +): string | null => { |
| 187 | + let percentPosition = url.indexOf('%') |
| 188 | + if (percentPosition === -1) return url |
| 189 | + |
| 190 | + // Ensure percentPosition always has 2 chars after |
| 191 | + end -= 3 |
| 192 | + if (percentPosition > end) return null |
| 193 | + |
| 194 | + let decoded = '', |
| 195 | + codepoint = 0, |
| 196 | + startOfOctets = percentPosition, |
| 197 | + // UTF_ACCEPT |
| 198 | + state = 12, |
| 199 | + byte: number |
| 200 | + |
| 201 | + for (;;) { |
| 202 | + byte = calcHex( |
| 203 | + url.charCodeAt(percentPosition + 1), |
| 204 | + url.charCodeAt(percentPosition + 2) |
| 205 | + ) |
| 206 | + state = next[state + type[byte]] |
| 207 | + if (state === 0) return null |
| 208 | + if (state === 12) { |
| 209 | + decoded += url.substring(start, startOfOctets) |
| 210 | + |
| 211 | + // Calculate current codepoint |
| 212 | + codepoint = (codepoint << 6) | (byte & mask[byte]) |
| 213 | + |
| 214 | + if (codepoint > 0xffff) |
| 215 | + decoded += String.fromCharCode( |
| 216 | + 0xd7c0 + (codepoint >> 10), |
| 217 | + 0xdc00 + (codepoint & 0x3ff) |
| 218 | + ) |
| 219 | + else decoded += String.fromCharCode(codepoint) |
| 220 | + |
| 221 | + // Search next encoded component |
| 222 | + start = percentPosition + 3 |
| 223 | + |
| 224 | + percentPosition = url.indexOf('%', start) |
| 225 | + if (percentPosition === -1) return decoded + url.substring(start) |
| 226 | + |
| 227 | + // Ensure percentPosition always has 2 chars after |
| 228 | + if (percentPosition > end) return null |
| 229 | + |
| 230 | + startOfOctets = percentPosition |
| 231 | + codepoint = 0 |
| 232 | + } else { |
| 233 | + // Check next % |
| 234 | + percentPosition += 3 |
| 235 | + if (percentPosition > end || url.charCodeAt(percentPosition) !== 37) |
| 236 | + return null |
| 237 | + |
| 238 | + // Calculate current codepoint |
| 239 | + codepoint = (codepoint << 6) | (byte & mask[byte]) |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments