a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(uint8array): missing fast path for isUtf8LengthInRange in Node.js and Bun

Mary 441b28ac 61ed153b

+33 -4
+5
.changeset/large-nails-marry.md
··· 1 + --- 2 + '@atcute/uint8array': patch 3 + --- 4 + 5 + missing fast path for isUtf8LengthInRange in Node.js and Bun
+14 -2
packages/misc/uint8array/lib/index.bun.ts
··· 162 162 * @returns true if byte length is within [min, max] 163 163 */ 164 164 export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => { 165 - const len = _byteLength(str, 'utf8'); 166 - return len >= min && len <= max; 165 + const len = str.length; 166 + 167 + // fast path: if max possible UTF-8 length is below min, fail 168 + if (len * 3 < min) { 169 + return false; 170 + } 171 + 172 + // fast path: if UTF-16 length satisfies min and max possible satisfies max 173 + if (len >= min && len * 3 <= max) { 174 + return true; 175 + } 176 + 177 + const utf8len = _byteLength(str, 'utf8'); 178 + return utf8len >= min && utf8len <= max; 167 179 }; 168 180 169 181 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
+14 -2
packages/misc/uint8array/lib/index.node.ts
··· 160 160 * @returns true if byte length is within [min, max] 161 161 */ 162 162 export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => { 163 - const len = _byteLength(str, 'utf8'); 164 - return len >= min && len <= max; 163 + const len = str.length; 164 + 165 + // fast path: if max possible UTF-8 length is below min, fail 166 + if (len * 3 < min) { 167 + return false; 168 + } 169 + 170 + // fast path: if UTF-16 length satisfies min and max possible satisfies max 171 + if (len >= min && len * 3 <= max) { 172 + return true; 173 + } 174 + 175 + const utf8len = _byteLength(str, 'utf8'); 176 + return utf8len >= min && utf8len <= max; 165 177 }; 166 178 167 179 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {