buffer.ts
This file
Path
src/utils/buffer.ts
117 lines 路 2.9 KiB 路 TypeScript
Last changed by
fix(utils/body,validator): normalize Content-Type media type for case-insensitive matching (#5067)Yusuke Wadabefore adoptionb20d422
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118/** * @module * Buffer utility. */
import { sha256 } from './crypto'
export const equal = (a: ArrayBuffer, b: ArrayBuffer): boolean => { if (a === b) { return true } if (a.byteLength !== b.byteLength) { return false }
const va = new DataView(a) const vb = new DataView(b)
let i = va.byteLength while (i--) { if (va.getUint8(i) !== vb.getUint8(i)) { return false } }
return true}
const constantTimeEqualString = (a: string, b: string): boolean => { const aLen = a.length const bLen = b.length const maxLen = Math.max(aLen, bLen) let out = aLen ^ bLen for (let i = 0; i < maxLen; i++) { const aChar = i < aLen ? a.charCodeAt(i) : 0 const bChar = i < bLen ? b.charCodeAt(i) : 0 out |= aChar ^ bChar } return out === 0}
type StringHashFunction = (input: string) => string | null | Promise<string | null>
const timingSafeEqualString = async ( a: string, b: string, hashFunction?: StringHashFunction): Promise<boolean> => { if (!hashFunction) { hashFunction = sha256 }
const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])
if (sa == null || sb == null || typeof sa !== 'string' || typeof sb !== 'string') { return false }
const hashEqual = constantTimeEqualString(sa, sb) const originalEqual = constantTimeEqualString(a, b)
return hashEqual && originalEqual}
type TimingSafeEqual = { (a: string, b: string, hashFunction?: StringHashFunction): Promise<boolean> /** * @deprecated object and boolean signatures that take boolean as first and second arguments, and functions with signatures that take non-string arguments have been deprecated */ ( a: string | object | boolean, b: string | object | boolean, hashFunction?: Function ): Promise<boolean>}export const timingSafeEqual: TimingSafeEqual = async ( a, b, hashFunction?: Function): Promise<boolean> => { if (typeof a === 'string' && typeof b === 'string') { return timingSafeEqualString(a, b, hashFunction as StringHashFunction) }
if (!hashFunction) { hashFunction = sha256 }
const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])
if (!sa || !sb || typeof sa !== 'string' || typeof sb !== 'string') { return false }
return timingSafeEqualString(sa, sb)}
export const bufferToString = (buffer: ArrayBuffer): string => { if (buffer instanceof ArrayBuffer) { const enc = new TextDecoder('utf-8') return enc.decode(buffer) } return buffer}
export const bufferToFormData = ( arrayBuffer: ArrayBuffer, contentType: string): Promise<FormData> => { const response = new Response(arrayBuffer, { headers: { // Normalize the media type (case-insensitive) while keeping parameters like the boundary 'Content-Type': contentType.replace(/^[^;]+/, (mediaType) => mediaType.toLowerCase()), }, }) return response.formData()}