ui-react: bytes-to-base64 and back are hand-written in five places

Open Beginner friendly
#4,726 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
80/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
bun, typescript
Domain
frontend, testing

Research direction

Add the shared helpers in packages/ui-react/src/lib/base64.ts and update the five listed encode and decode sites plus drafts.ts and image-attachments.ts. Start by reading those implementations and the corresponding tests; add packages/ui-react/tests/base64.test.ts for the stated round trips and decoded length checks. Done means the acceptance criteria pass, existing tests remain unchanged and pass, and api-report/ui-react.txt is unchanged.

Written by the indexing model from the issue text.

Description

agent-ready area: ui-react priority: p3 type: refactor

Context

packages/ui-react converts between bytes and base64 by hand at each place that needs it.

Encode, by appending one character per byte to a string and calling btoa:

  • packages/ui-react/src/voice/asr-gemini.ts:241-243, once per captured PCM frame;
  • packages/ui-react/src/voice/conversation-client.ts:589-591, once per captured PCM frame;
  • packages/ui-react/src/lib/push-registration.ts:164-166, followed by the base64url substitutions.

Decode:

  • (decodeBase64, packages/ui-react/src/lib/draft-api.ts:37-42), a charCodeAt loop into a preallocated array;
  • packages/ui-react/src/voice/conversation-client.ts:1041, Uint8Array.from(atob(pcm), c => c.charCodeAt(0)), once per conversation_output frame on the playback path.

Decoded length, from the base64 text's length and padding, is a third pair of identical copies: (decodedBytes, packages/ui-react/src/lib/drafts.ts:19-23) and (decodedBytesFromBase64, packages/ui-react/src/lib/image-attachments.ts:30-34).

The impact is small: five copies of a loop that is easy to get subtly wrong, with two different decode strategies. The package has no shared binary helper; packages/ui-react/src/lib/ is where its other pure helpers live, so the surviving implementation is a new lib/base64.ts.

Rule that wins: encode with the per-byte loop the three sites share; decode with the preallocated charCodeAt loop from draft-api.ts. Both produce the same bytes as today.

Scope

  • Add bytesToBase64(bytes: Uint8Array): string and base64ToBytes(base64: string): Uint8Array to packages/ui-react/src/lib/base64.ts.
  • Add base64DecodedLength(base64: string): number beside them; drafts.ts and image-attachments.ts call it and delete their copies.
  • The three encode sites and the two decode sites call them. push-registration.ts keeps its base64url substitutions after the call.

Out of scope

  • Replacing the loops with Uint8Array.prototype.toBase64 / Uint8Array.fromBase64. Whether the platform methods may be used depends on the browser support policy under #2587 and has to be checked against current support data first.
  • String.fromCharCode(...payload) in packages/ui-react/src/voice/encoded-duration.ts:79, which compares a codec tag and is not base64.
  • Base64 handling outside packages/ui-react.
  • Exporting the helper from the package entry.

Acceptance criteria

  • git grep -n "String.fromCharCode\|atob(\|btoa(" -- packages/ui-react/src returns only lib/base64.ts and voice/encoded-duration.ts:79.
  • git grep -n 'b64.length \* 3' -- packages/ui-react/src returns one line, in lib/base64.ts, and the unit test asserts base64DecodedLength(bytesToBase64(b)) === b.length for every length in the round-trip set.
  • A unit test round-trips byte arrays of length 0, 1, 2, 3, 4 and 4,097 containing every byte value, and pins bytesToBase64(new Uint8Array([0, 255, 16])) to "AP8Q".
  • The existing voice, push-registration and draft tests pass unchanged (packages/ui-react/tests/asr-gemini.test.ts and the tests that cover the other four files).
  • api-report/ui-react.txt is unchanged.

Verification

bun run test packages/ui-react/tests/base64.test.ts packages/ui-react/tests/asr-gemini.test.ts
bun run test packages/ui-react/tests
NODE_OPTIONS=--max-old-space-size=6144 bun run typecheck
bun run lint
bun run check:pr --base origin/main

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

The bytes and strings produced are the same. The changeset gate still asks for a changeset on any edit under a published package's src; add a patch entry that says the change is internal.

Found by a repository audit on 2026-11-26 (main 87b96b10c); independently reviewed before filing.

Dominant language
TypeScript
Stars
5
Forks
0
Avg merge
8h 3m
Merged PRs (30d)
606

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from schlessera/brain-kit

All issues in schlessera/brain-kit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.