Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 8f027e5 point at it.

fix(ssg): add `atom+xml` and `rss+xml` to `defaultExtensionMap` (#4899)Yuinbefore adoption8f027e5

lines 1–111why, 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}2829const constantTimeEqualString = (a: string, b: string): boolean => {30 const aLen = a.length31 const bLen = b.length32 const maxLen = Math.max(aLen, bLen)33 let out = aLen ^ bLen34 for (let i = 0; i < maxLen; i++) {35 const aChar = i < aLen ? a.charCodeAt(i) : 036 const bChar = i < bLen ? b.charCodeAt(i) : 037 out |= aChar ^ bChar38 }39 return out === 040}4142type StringHashFunction = (input: string) => string | null | Promise<string | null>4344const timingSafeEqualString = async (45 a: string,46 b: string,47 hashFunction?: StringHashFunction48): Promise<boolean> => {49 if (!hashFunction) {50 hashFunction = sha25651 }5253 const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])5455 if (sa == null || sb == null || typeof sa !== 'string' || typeof sb !== 'string') {56 return false57 }5859 const hashEqual = constantTimeEqualString(sa, sb)60 const originalEqual = constantTimeEqualString(a, b)6162 return hashEqual && originalEqual63}6465type TimingSafeEqual = {66 (a: string, b: string, hashFunction?: StringHashFunction): Promise<boolean>67 /**68 * @deprecated object and boolean signatures that take boolean as first and second arguments, and functions with signatures that take non-string arguments have been deprecated69 */70 (71 a: string | object | boolean,72 b: string | object | boolean,73 hashFunction?: Function74 ): Promise<boolean>75}76export const timingSafeEqual: TimingSafeEqual = async (77 a,78 b,79 hashFunction?: Function80): Promise<boolean> => {81 if (typeof a === 'string' && typeof b === 'string') {82 return timingSafeEqualString(a, b, hashFunction as StringHashFunction)83 }8485 if (!hashFunction) {86 hashFunction = sha25687 }8889 const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])9091 if (!sa || !sb || typeof sa !== 'string' || typeof sb !== 'string') {92 return false93 }9495 return timingSafeEqualString(sa, sb)96}9798export const bufferToString = (buffer: ArrayBuffer): string => {99 if (buffer instanceof ArrayBuffer) {100 const enc = new TextDecoder('utf-8')101 return enc.decode(buffer)102 }103 return buffer104}105106export const bufferToFormData = (107 arrayBuffer: ArrayBuffer,108 contentType: string109): Promise<FormData> => {110 const response = new Response(arrayBuffer, {111 headers: {

fix(utils/body,validator): normalize Content-Type media type for case-insensitive matching (#5067)Yusuke Wadabefore adoptionb20d422

lines 112–113why, one step back
112 // Normalize the media type (case-insensitive) while keeping parameters like the boundary
113 'Content-Type': contentType.replace(/^[^;]+/, (mediaType) => mediaType.toLowerCase()),

fix(ssg): add `atom+xml` and `rss+xml` to `defaultExtensionMap` (#4899)Yuinbefore adoption8f027e5

lines 114–117why, one step back
114 },
115 })116 return response.formData()117}