buffer.ts
This file
Path
src/utils/buffer.ts
74 lines · 1.7 KiB · TypeScript
Last changed by
chore: set up prettier (#106)Yusuke Wadabefore adoption9c397fb
History of this fileTake it
refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636
lines 1–21why, one step back1export const equal = (a: ArrayBuffer, b: ArrayBuffer) => {2 if (a === b) {3 return true4 }5 if (a.byteLength !== b.byteLength) {6 return false7 }89 const va = new DataView(a)10 const vb = new DataView(b)1112 let i = va.byteLength13 while (i--) {14 if (va.getUint8(i) !== vb.getUint8(i)) {15 return false16 }17 }1819 return true20}21feat: Basic-auth with polyfill (#86)Yusuke Wadabefore adoptiond5280a1
lines 22–32why, one step back22export const decodeBase64 = (str: string) => {23 try {24 const text = atob(str)25 const length = text.length26 const bytes = new Uint8Array(length)27 for (let i = 0; i < length; i++) {28 bytes[i] = text.charCodeAt(i)29 }30 const decoder = new TextDecoder()31 return decoder.decode(bytes)32 } catch {}refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636
line 33why, one step back33feat: Basic-auth with polyfill (#86)Yusuke Wadabefore adoptiond5280a1
lines 34–41why, one step back34 try {35 const { Buffer } = require('buffer')36 return Buffer.from(str, 'base64').toString()37 } catch (e) {38 console.error('If you want to do "decodeBase64", polyfill "buffer" module.')39 throw e40 }41}refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636
line 42why, one step back42feat: check response type (#94)Yusuke Wadabefore adoptiond77d890
line 43why, one step back43export const sha256 = async (a: string | object | boolean): Promise<string> => {feat: Basic-auth with polyfill (#86)Yusuke Wadabefore adoptiond5280a1
lines 44–50why, one step back44 if (crypto && crypto.subtle) {45 const buffer = await crypto.subtle.digest(46 {47 name: 'SHA-256',48 },49 new TextEncoder().encode(String(a))50 )chore: set up prettier (#106)Yusuke Wadabefore adoption9c397fb
lines 51–53why, one step back51 const hash = Array.prototype.map52 .call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2))53 .join('')feat: Basic-auth with polyfill (#86)Yusuke Wadabefore adoptiond5280a1
lines 54–57why, one step back54 return hash55 }5657 try {fix: use require (#87)Yusuke Wadabefore adoption30b0c44
line 58why, one step back58 const crypto = require('crypto')feat: Basic-auth with polyfill (#86)Yusuke Wadabefore adoptiond5280a1
lines 59–66why, one step back59 const hash = crypto.createHash('sha256').update(a).digest('hex')60 return hash61 } catch (e) {62 console.error('If you want to do "sha256", polyfill "crypto" module.')63 throw e64 }65}66chore: set up prettier (#106)Yusuke Wadabefore adoption9c397fb
lines 67–70why, one step back67export const timingSafeEqual = async (68 a: string | object | boolean,69 b: string | object | boolean70) => {feat: Basic-auth with polyfill (#86)Yusuke Wadabefore adoptiond5280a1
lines 71–73why, one step back71 const sa = await sha256(a)72 const sb = await sha256(b)73 return sa === sb && a === brefactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636
line 74why, one step back74}