a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky

refactor(bluesky-richtext-builder): faster UTF-8 encoding

mary.my.id 61ed153b 33f811ad

verified
+15 -5
+5
.changeset/stale-years-press.md
··· 1 + --- 2 + '@atcute/bluesky-richtext-builder': patch 3 + --- 4 + 5 + faster UTF-8 encoding
+5 -4
packages/bluesky/richtext-builder/lib/index.ts
··· 1 1 import type { AppBskyRichtextFacet } from '@atcute/bluesky'; 2 2 import type { Did, GenericUri } from '@atcute/lexicons'; 3 + import { getUtf8Length } from '@atcute/uint8array'; 3 4 4 5 type UnwrapArray<T> = T extends (infer V)[] ? V : never; 5 6 ··· 7 8 export type Facet = AppBskyRichtextFacet.Main; 8 9 /** Feature union type from Facet['features'] */ 9 10 export type FacetFeature = UnwrapArray<Facet['features']>; 10 - 11 - const encoder = new TextEncoder(); 12 11 13 12 /** Resulting rich text */ 14 13 export interface BakedRichtext { ··· 88 87 // Calculate the starting index 89 88 let start = 0; 90 89 91 - start += encoder.encode(segments[last] as string).byteLength; 90 + start += getUtf8Length(segments[last] as string); 92 91 if (last !== 0) { 93 92 start += (segments[last - 1] as Facet).index.byteEnd; 94 93 } 95 94 95 + const byteLength = getUtf8Length(substr); 96 + 96 97 const facet: Facet = { 97 98 index: { 98 99 byteStart: start, 99 - byteEnd: start + encoder.encode(substr).byteLength, 100 + byteEnd: start + byteLength, 100 101 }, 101 102 features: [feature], 102 103 };
+2 -1
packages/bluesky/richtext-builder/package.json
··· 30 30 }, 31 31 "dependencies": { 32 32 "@atcute/bluesky": "workspace:^", 33 - "@atcute/lexicons": "workspace:^" 33 + "@atcute/lexicons": "workspace:^", 34 + "@atcute/uint8array": "workspace:^" 34 35 } 35 36 }
+3
pnpm-lock.yaml
··· 66 66 '@atcute/lexicons': 67 67 specifier: workspace:^ 68 68 version: link:../../lexicons/lexicons 69 + '@atcute/uint8array': 70 + specifier: workspace:^ 71 + version: link:../../misc/uint8array 69 72 70 73 packages/bluesky/richtext-parser: 71 74 devDependencies: