encode.ts
This file
Path
src/utils/encode.ts
34 lines · 1.1 KiB · TypeScript
Last changed by
chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
History of this fileTake it
The walk stopped after 200 commits: lines older than 4a1dd5f point at it.
Revert "fix(etag): fallback if `res.clone()` is not supported (#4198)" (#4200)Yusuke Wadabefore adoption4a1dd5f
lines 1–5why, one step back1/**2 * @module3 * Encode utility.4 */5chore: update `typescript` to `5.9.2` (#4362)Yusuke Wadabefore adoption3e5a6e8
line 6why, one step back6export const decodeBase64Url = (str: string): Uint8Array<ArrayBuffer> => {chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
line 7why, one step back7 return decodeBase64(str.replace(/_|-/g, (m) => ({ _: '/', '-': '+' })[m] ?? m))Revert "fix(etag): fallback if `res.clone()` is not supported (#4198)" (#4200)Yusuke Wadabefore adoption4a1dd5f
lines 8–10why, one step back8}910export const encodeBase64Url = (buf: ArrayBufferLike): string =>chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
line 11why, one step back11 encodeBase64(buf).replace(/\/|\+/g, (m) => ({ '/': '_', '+': '-' })[m] ?? m)Revert "fix(etag): fallback if `res.clone()` is not supported (#4198)" (#4200)Yusuke Wadabefore adoption4a1dd5f
lines 12–24why, one step back1213// This approach is written in MDN.14// btoa does not support utf-8 characters. So we need a little bit hack.15export const encodeBase64 = (buf: ArrayBufferLike): string => {16 let binary = ''17 const bytes = new Uint8Array(buf)18 for (let i = 0, len = bytes.length; i < len; i++) {19 binary += String.fromCharCode(bytes[i])20 }21 return btoa(binary)22}2324// atob does not support utf-8 characters. So we need a little bit hack.chore: update `typescript` to `5.9.2` (#4362)Yusuke Wadabefore adoption3e5a6e8
line 25why, one step back25export const decodeBase64 = (str: string): Uint8Array<ArrayBuffer> => {Revert "fix(etag): fallback if `res.clone()` is not supported (#4198)" (#4200)Yusuke Wadabefore adoption4a1dd5f
line 26why, one step back26 const binary = atob(str)chore: update `typescript` to `5.9.2` (#4362)Yusuke Wadabefore adoption3e5a6e8
line 27why, one step back27 const bytes = new Uint8Array<ArrayBuffer>(new ArrayBuffer(binary.length))Revert "fix(etag): fallback if `res.clone()` is not supported (#4198)" (#4200)Yusuke Wadabefore adoption4a1dd5f
lines 28–34why, one step back28 const half = binary.length / 229 for (let i = 0, j = binary.length - 1; i <= half; i++, j--) {30 bytes[i] = binary.charCodeAt(i)31 bytes[j] = binary.charCodeAt(j)32 }33 return bytes34}