Skip to content
Myr

buffer.ts

This file

Path

src/utils/buffer.ts

65 lines · 1.2 KiB · TypeScript

Last changed by

perf(utils/buffer): use promise all for better performance (#3031)yasuaki640before adoptiona9e4176

History of this file

Take it

The walk stopped after 200 commits: lines older than 7ee829b point at it.

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 1–37why, one step back
1/**
2 * @module3 * Buffer utility.4 */56import { sha256 } from './crypto'78export const equal = (a: ArrayBuffer, b: ArrayBuffer): boolean => {9 if (a === b) {10 return true11 }12 if (a.byteLength !== b.byteLength) {13 return false14 }1516 const va = new DataView(a)17 const vb = new DataView(b)1819 let i = va.byteLength20 while (i--) {21 if (va.getUint8(i) !== vb.getUint8(i)) {22 return false23 }24 }2526 return true27}2829export const timingSafeEqual = async (30 a: string | object | boolean,31 b: string | object | boolean,32 hashFunction?: Function33): Promise<boolean> => {34 if (!hashFunction) {35 hashFunction = sha25636 }37

perf(utils/buffer): use promise all for better performance (#3031)yasuaki640before adoptiona9e4176

line 38why, one step back
38 const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 39–65why, one step back
39
40 if (!sa || !sb) {41 return false42 }4344 return sa === sb && a === b45}4647export const bufferToString = (buffer: ArrayBuffer): string => {48 if (buffer instanceof ArrayBuffer) {49 const enc = new TextDecoder('utf-8')50 return enc.decode(buffer)51 }52 return buffer53}5455export const bufferToFormData = (56 arrayBuffer: ArrayBuffer,57 contentType: string58): Promise<FormData> => {59 const response = new Response(arrayBuffer, {60 headers: {61 'Content-Type': contentType,62 },63 })64 return response.formData()65}