encode.ts
This file
Path
src/utils/encode.ts
29 lines · 1.0 KiB · TypeScript
Last changed by
fix: avoid range-error (#1168)IGUCHI Kanahirobefore adoptiona6b0833
History of this fileTake it
The walk stopped after 200 commits: lines older than d273210 point at it.
refactor: Refactoring base64 logic for JWT (#999)Kei Kamikawabefore adoptionbbdc64b
lines 1–2why, one step back1export const decodeBase64Url = (str: string): Uint8Array => {2 return decodeBase64(str.replace(/_|-/g, (m) => ({ _: '/', '-': '+' }[m] ?? m)))v3.0.4Yusuke Wadabefore adoptiond273210
lines 3–4why, one step back3}4refactor: Refactoring base64 logic for JWT (#999)Kei Kamikawabefore adoptionbbdc64b
lines 5–6why, one step back5export const encodeBase64Url = (buf: ArrayBufferLike): string =>6 encodeBase64(buf).replace(/\/|\+/g, (m) => ({ '/': '_', '+': '-' }[m] ?? m))v3.0.4Yusuke Wadabefore adoptiond273210
line 7why, one step back7refactor: Refactoring base64 logic for JWT (#999)Kei Kamikawabefore adoptionbbdc64b
lines 8–10why, one step back8// This approach is written in MDN.9// btoa does not support utf-8 characters. So we need a little bit hack.10export const encodeBase64 = (buf: ArrayBufferLike): string => {fix: avoid range-error (#1168)IGUCHI Kanahirobefore adoptiona6b0833
lines 11–15why, one step back11 let binary = ''12 const bytes = new Uint8Array(buf)13 for (let i = 0; i < bytes.length; i++) {14 binary += String.fromCharCode(bytes[i])15 }refactor: Refactoring base64 logic for JWT (#999)Kei Kamikawabefore adoptionbbdc64b
line 16why, one step back16 return btoa(binary)v3.0.4Yusuke Wadabefore adoptiond273210
lines 17–18why, one step back17}18refactor: Refactoring base64 logic for JWT (#999)Kei Kamikawabefore adoptionbbdc64b
lines 19–26why, one step back19// atob does not support utf-8 characters. So we need a little bit hack.20export const decodeBase64 = (str: string): Uint8Array => {21 const binary = atob(str)22 const bytes = new Uint8Array(new ArrayBuffer(binary.length))23 const half = binary.length / 224 for (let i = 0, j = binary.length - 1; i <= half; i++, j--) {25 bytes[i] = binary.charCodeAt(i)26 bytes[j] = binary.charCodeAt(j)v3.0.4Yusuke Wadabefore adoptiond273210
line 27why, one step back27 }refactor: Refactoring base64 logic for JWT (#999)Kei Kamikawabefore adoptionbbdc64b
line 28why, one step back28 return bytesv3.0.4Yusuke Wadabefore adoptiond273210
line 29why, one step back29}