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.

feat(uint8array): add getUtf8Length

Mary a562b635 8e3685ee

+84 -5
+5
.changeset/chubby-news-stick.md
··· 1 + --- 2 + '@atcute/uint8array': patch 3 + --- 4 + 5 + add getUtf8Length
+11
packages/misc/uint8array/lib/index.bun.ts
··· 3 3 import { Buffer as NodeBuffer } from 'node:buffer'; 4 4 import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto'; 5 5 6 + const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength; 7 + 6 8 const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare; 7 9 const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals; 8 10 const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice; ··· 142 144 if (result !== null) return result; 143 145 } 144 146 return _utf8Slice.call(from, offset, offset + length); 147 + }; 148 + 149 + /** 150 + * calculates the UTF-8 byte length of a string 151 + * @param str string to measure 152 + * @returns byte length when encoded as UTF-8 153 + */ 154 + export const getUtf8Length = (str: string): number => { 155 + return _byteLength(str, 'utf8'); 145 156 }; 146 157 147 158 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
+11
packages/misc/uint8array/lib/index.node.ts
··· 6 6 const _concat = /*#__PURE__*/ NodeBuffer.concat; 7 7 const _from = /*#__PURE__*/ NodeBuffer.from; 8 8 9 + const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength; 10 + 9 11 const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare; 10 12 const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals; 11 13 const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice; ··· 135 137 if (result !== null) return result; 136 138 } 137 139 return _utf8Slice.call(from, offset, offset + length); 140 + }; 141 + 142 + /** 143 + * calculates the UTF-8 byte length of a string 144 + * @param str string to measure 145 + * @returns byte length when encoded as UTF-8 146 + */ 147 + export const getUtf8Length = (str: string): number => { 148 + return _byteLength(str, 'utf8'); 138 149 }; 139 150 140 151 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
+48
packages/misc/uint8array/lib/index.ts
··· 226 226 }; 227 227 228 228 /** 229 + * calculates the UTF-8 byte length of a string 230 + * @param str string to measure 231 + * @returns byte length when encoded as UTF-8 232 + */ 233 + export const getUtf8Length = (str: string): number => { 234 + const len = str.length; 235 + 236 + let u16pos = 0; 237 + let u8pos = 0; 238 + 239 + // ASCII fast-path: batch process 4 chars at a time 240 + while (u16pos + 3 < len) { 241 + const a = str.charCodeAt(u16pos); 242 + const b = str.charCodeAt(u16pos + 1); 243 + const c = str.charCodeAt(u16pos + 2); 244 + const d = str.charCodeAt(u16pos + 3); 245 + 246 + if ((a | b | c | d) >= 0x80) { 247 + break; 248 + } 249 + 250 + u16pos += 4; 251 + u8pos += 4; 252 + } 253 + 254 + // handle remaining chars 255 + while (u16pos < len) { 256 + const code = str.charCodeAt(u16pos); 257 + 258 + if (code < 0x80) { 259 + u16pos += 1; 260 + u8pos += 1; 261 + } else if (code < 0x800) { 262 + u16pos += 1; 263 + u8pos += 2; 264 + } else if (code < 0xd800 || code > 0xdbff) { 265 + u16pos += 1; 266 + u8pos += 3; 267 + } else { 268 + u16pos += 2; 269 + u8pos += 4; 270 + } 271 + } 272 + 273 + return u8pos; 274 + }; 275 + 276 + /** 229 277 * get a SHA-256 digest of this buffer 230 278 */ 231 279 export const toSha256 = async (buffer: Uint8Array<ArrayBuffer>): Promise<Uint8Array<ArrayBuffer>> => {
+3
packages/misc/uint8array/package.json
··· 25 25 "scripts": { 26 26 "build": "tsc --project tsconfig.build.json", 27 27 "prepublish": "rm -rf dist; pnpm run build" 28 + }, 29 + "devDependencies": { 30 + "@types/bun": "^1.3.3" 28 31 } 29 32 }
+1 -1
packages/misc/uint8array/tsconfig.json
··· 1 1 { 2 2 "compilerOptions": { 3 - "types": [], 3 + "types": ["bun"], 4 4 "outDir": "dist/", 5 5 "esModuleInterop": true, 6 6 "skipLibCheck": true,
+5 -4
pnpm-lock.yaml
··· 624 624 specifier: ^4.0.14 625 625 version: 4.0.14(@types/node@24.10.1)(@vitest/browser-playwright@4.0.14)(jiti@2.6.1)(tsx@4.20.6)(yaml@2.8.0) 626 626 627 - packages/misc/uint8array: {} 627 + packages/misc/uint8array: 628 + devDependencies: 629 + '@types/bun': 630 + specifier: ^1.3.3 631 + version: 1.3.3 628 632 629 633 packages/misc/util-fetch: 630 634 dependencies: ··· 637 641 '@atcute/client': 638 642 specifier: workspace:^ 639 643 version: link:../../clients/client 640 - '@atcute/identity': 641 - specifier: workspace:^ 642 - version: link:../../identity/identity 643 644 '@atcute/identity-resolver': 644 645 specifier: workspace:^ 645 646 version: link:../../identity/identity-resolver