|
| 1 | +/* eslint-env jest, node */ |
| 2 | +import { |
| 3 | + decode, |
| 4 | + encode |
| 5 | + // @ts-ignore ambiguous import |
| 6 | +} from "./base64url.ts"; |
| 7 | + |
| 8 | +const unicodeText = "Zombies everywhere 🧟"; |
| 9 | + |
| 10 | +describe("base64url", () => { |
| 11 | + it("works in browsers using provided atob/btoa", () => { |
| 12 | + const atob = (encodedString: string) => { |
| 13 | + let data = encodedString.replace(/[ \t\n\f\r]/g, ""); |
| 14 | + |
| 15 | + if (data.length % 4 === 0) { |
| 16 | + data = data.replace(/==?$/, ""); |
| 17 | + } |
| 18 | + |
| 19 | + if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) { |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + let output = ""; |
| 24 | + |
| 25 | + let buffer = 0; |
| 26 | + let accumulatedBits = 0; |
| 27 | + |
| 28 | + for (let i = 0; i < data.length; i++) { |
| 29 | + buffer <<= 6; |
| 30 | + buffer |= atobLookup(data[i]); |
| 31 | + accumulatedBits += 6; |
| 32 | + |
| 33 | + if (accumulatedBits === 24) { |
| 34 | + output += String.fromCharCode((buffer & 0xff0000) >> 16); |
| 35 | + output += String.fromCharCode((buffer & 0xff00) >> 8); |
| 36 | + output += String.fromCharCode(buffer & 0xff); |
| 37 | + buffer = accumulatedBits = 0; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (accumulatedBits === 12) { |
| 42 | + buffer >>= 4; |
| 43 | + output += String.fromCharCode(buffer); |
| 44 | + } else if (accumulatedBits === 18) { |
| 45 | + buffer >>= 2; |
| 46 | + output += String.fromCharCode((buffer & 0xff00) >> 8); |
| 47 | + output += String.fromCharCode(buffer & 0xff); |
| 48 | + } |
| 49 | + |
| 50 | + return output; |
| 51 | + }; |
| 52 | + |
| 53 | + const atobLookup = (character: string) => { |
| 54 | + if (/[A-Z]/.test(character)) { |
| 55 | + return character.charCodeAt(0) - "A".charCodeAt(0); |
| 56 | + } else if (/[a-z]/.test(character)) { |
| 57 | + return character.charCodeAt(0) - "a".charCodeAt(0) + 26; |
| 58 | + } else if (/[0-9]/.test(character)) { |
| 59 | + return character.charCodeAt(0) - "0".charCodeAt(0) + 52; |
| 60 | + } else if (character === "+") { |
| 61 | + return 62; |
| 62 | + } else if (character === "/") { |
| 63 | + return 63; |
| 64 | + } |
| 65 | + |
| 66 | + throw new RangeError("Character out of range."); |
| 67 | + }; |
| 68 | + |
| 69 | + const btoa = (rawString: string) => { |
| 70 | + const s = rawString; |
| 71 | + |
| 72 | + for (let i = 0; i < s.length; i++) { |
| 73 | + if (s.charCodeAt(i) > 255) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + let out = ""; |
| 79 | + |
| 80 | + for (let i = 0; i < s.length; i += 3) { |
| 81 | + const groupsOfSix = [undefined, undefined, undefined, undefined]; |
| 82 | + groupsOfSix[0] = s.charCodeAt(i) >> 2; |
| 83 | + groupsOfSix[1] = (s.charCodeAt(i) & 0x03) << 4; |
| 84 | + |
| 85 | + if (s.length > i + 1) { |
| 86 | + groupsOfSix[1] |= s.charCodeAt(i + 1) >> 4; |
| 87 | + groupsOfSix[2] = (s.charCodeAt(i + 1) & 0x0f) << 2; |
| 88 | + } |
| 89 | + |
| 90 | + if (s.length > i + 2) { |
| 91 | + groupsOfSix[2] |= s.charCodeAt(i + 2) >> 6; |
| 92 | + groupsOfSix[3] = s.charCodeAt(i + 2) & 0x3f; |
| 93 | + } |
| 94 | + |
| 95 | + for (let j = 0; j < groupsOfSix.length; j++) { |
| 96 | + if (typeof groupsOfSix[j] === "undefined") { |
| 97 | + out += "="; |
| 98 | + } else { |
| 99 | + out += btoaLookup(groupsOfSix[j]); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return out; |
| 105 | + }; |
| 106 | + |
| 107 | + const btoaLookup = (index: number) => { |
| 108 | + if (index >= 0 && index < 26) { |
| 109 | + return String.fromCharCode(index + "A".charCodeAt(0)); |
| 110 | + } else if (index < 52) { |
| 111 | + return String.fromCharCode(index - 26 + "a".charCodeAt(0)); |
| 112 | + } else if (index < 62) { |
| 113 | + return String.fromCharCode(index - 52 + "0".charCodeAt(0)); |
| 114 | + } else if (index === 62) { |
| 115 | + return "+"; |
| 116 | + } else if (index === 63) { |
| 117 | + return "/"; |
| 118 | + } |
| 119 | + |
| 120 | + throw new RangeError("Index out of range."); |
| 121 | + }; |
| 122 | + |
| 123 | + const context = { |
| 124 | + atob, |
| 125 | + btoa, |
| 126 | + TextEncoder: global["TextEncoder"], |
| 127 | + TextDecoder: global["TextDecoder"] |
| 128 | + }; |
| 129 | + |
| 130 | + expect(decode(encode(unicodeText, context), context)).toEqual(unicodeText); |
| 131 | + |
| 132 | + global["window"] = context; |
| 133 | + |
| 134 | + expect(decode(encode(unicodeText))).toEqual(unicodeText); |
| 135 | + |
| 136 | + delete global["window"]; |
| 137 | + }); |
| 138 | +}); |
0 commit comments