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.

fix(lexicon-doc): faulty UTF-16 fast-path constraint validation

Mary d17735ec dee1e70f

+14 -7
+5
.changeset/yellow-cycles-warn.md
··· 1 + --- 2 + '@atcute/lexicon-doc': patch 3 + --- 4 + 5 + fix faulty UTF-16 fast-path constraint validation
+9 -7
packages/lexicons/lexicon-doc/lib/utils.ts
··· 1 1 const segmenter = new Intl.Segmenter(); 2 2 3 3 export const isWithinUtf8Bounds = (input: string, min = 0, max = Infinity): 'max' | 'min' | undefined => { 4 - const maybeUtf8Len = input.length * 3; 4 + const utf16Len = input.length; 5 + const maybeUtf8Len = utf16Len * 3; 5 6 6 - // fail early if we're still less than minimum length 7 + // fail early if estimated upper bound is too small 7 8 if (maybeUtf8Len < min) { 8 9 return 'min'; 9 10 } 10 11 11 - // skip if we're still within maximum length 12 - if (maybeUtf8Len <= max) { 12 + // skip if UTF-16 length already satisfies both constraints 13 + if (utf16Len >= min && maybeUtf8Len <= max) { 13 14 return undefined; 14 15 } 15 16 ··· 32 33 33 34 const utf16Len = input.length; 34 35 35 - // fail early if UTF-16 length is less than grapheme length 36 + // fail early if UTF-16 length is too small 36 37 if (utf16Len < min) { 37 38 return 'min'; 38 39 } 39 40 40 - // skip if we're still within maximum constraint 41 - if (utf16Len <= max) { 41 + // if there is no minimum bounds, we can safely skip when UTF-16 is 42 + // within the maximum bounds. 43 + if (min === 0 && utf16Len <= max) { 42 44 return undefined; 43 45 } 44 46