|
| 1 | +// This file is taken from Node.js project. |
| 2 | +// Full implementation can be found from https://github.com/nodejs/node/blob/main/lib/internal/querystring.js |
| 3 | + |
| 4 | +const hexTable = Array.from( |
| 5 | + { length: 256 }, |
| 6 | + (_, i) => "%" + ((i < 16 ? "0" : "") + i.toString(16)).toUpperCase(), |
| 7 | +); |
| 8 | + |
| 9 | +// These characters do not need escaping when generating query strings: |
| 10 | +// ! - . _ ~ |
| 11 | +// ' ( ) * |
| 12 | +// digits |
| 13 | +// alpha (uppercase) |
| 14 | +// alpha (lowercase) |
| 15 | +// rome-ignore format: the array should not be formatted |
| 16 | +const noEscape = new Int8Array([ |
| 17 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15 |
| 18 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31 |
| 19 | + 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 32 - 47 |
| 20 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63 |
| 21 | + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79 |
| 22 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 80 - 95 |
| 23 | + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111 |
| 24 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 112 - 127 |
| 25 | +]); |
| 26 | + |
| 27 | +/** |
| 28 | + * @param {string} str |
| 29 | + * @returns {string} |
| 30 | + */ |
| 31 | +function encodeString(str) { |
| 32 | + const len = str.length; |
| 33 | + if (len === 0) return ""; |
| 34 | + |
| 35 | + let out = ""; |
| 36 | + let lastPos = 0; |
| 37 | + let i = 0; |
| 38 | + |
| 39 | + outer: for (; i < len; i++) { |
| 40 | + let c = str.charCodeAt(i); |
| 41 | + |
| 42 | + // ASCII |
| 43 | + while (c < 0x80) { |
| 44 | + if (noEscape[c] !== 1) { |
| 45 | + if (lastPos < i) out += str.slice(lastPos, i); |
| 46 | + lastPos = i + 1; |
| 47 | + out += hexTable[c]; |
| 48 | + } |
| 49 | + |
| 50 | + if (++i === len) break outer; |
| 51 | + |
| 52 | + c = str.charCodeAt(i); |
| 53 | + } |
| 54 | + |
| 55 | + if (lastPos < i) out += str.slice(lastPos, i); |
| 56 | + |
| 57 | + // Multi-byte characters ... |
| 58 | + if (c < 0x800) { |
| 59 | + lastPos = i + 1; |
| 60 | + out += hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]; |
| 61 | + continue; |
| 62 | + } |
| 63 | + if (c < 0xD800 || c >= 0xE000) { |
| 64 | + lastPos = i + 1; |
| 65 | + out += |
| 66 | + hexTable[0xE0 | (c >> 12)] + |
| 67 | + hexTable[0x80 | ((c >> 6) & 0x3F)] + |
| 68 | + hexTable[0x80 | (c & 0x3F)]; |
| 69 | + continue; |
| 70 | + } |
| 71 | + // Surrogate pair |
| 72 | + ++i; |
| 73 | + |
| 74 | + // This branch should never happen because all URLSearchParams entries |
| 75 | + // should already be converted to USVString. But, included for |
| 76 | + // completion's sake anyway. |
| 77 | + if (i >= len) throw new Error("Invalid URI"); |
| 78 | + |
| 79 | + const c2 = str.charCodeAt(i) & 0x3FF; |
| 80 | + |
| 81 | + lastPos = i + 1; |
| 82 | + c = 0x10000 + (((c & 0x3FF) << 10) | c2); |
| 83 | + out += |
| 84 | + hexTable[0xF0 | (c >> 18)] + |
| 85 | + hexTable[0x80 | ((c >> 12) & 0x3F)] + |
| 86 | + hexTable[0x80 | ((c >> 6) & 0x3F)] + |
| 87 | + hexTable[0x80 | (c & 0x3F)]; |
| 88 | + } |
| 89 | + if (lastPos === 0) return str; |
| 90 | + if (lastPos < len) return out + str.slice(lastPos); |
| 91 | + return out; |
| 92 | +} |
| 93 | + |
| 94 | +module.exports = { encodeString }; |
0 commit comments