Skip to content

fix(binary): bind TextEncoder when encoding - #28

Merged
bytaesu merged 1 commit into
mainfrom
2026-07-25/fix/binary-encoder-binding
Jul 24, 2026
Merged

fix(binary): bind TextEncoder when encoding#28
bytaesu merged 1 commit into
mainfrom
2026-07-25/fix/binary-encoder-binding

Conversation

@bytaesu

@bytaesu bytaesu commented Jul 24, 2026

Copy link
Copy Markdown
Member
  • Fix binary.encode() throwing due to an unbound TextEncoder.encode method
  • Preserve the portable Uint8Array_ return type

Copilot AI review requested due to automatic review settings July 24, 2026 22:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@greptile-apps

greptile-apps Bot commented Jul 24, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes binary.encode throwing TypeError: Illegal invocation in environments where the method was called without its original this context, by wrapping encoder.encode in an arrow function that captures the TextEncoder instance via closure. It also adds the portable Uint8Array_ return type for TypeScript 5.7 compatibility, and ships two tests covering basic encoding and a full round-trip.

  • src/binary.ts: encode is now (input?: string): Uint8Array_ => encoder.encode(input) — the arrow function correctly closes over the module-level encoder constant, restoring the required this binding.
  • src/binary.test.ts: new test file verifies the fix with an encode assertion ("hi"[104, 105]) and a decode(encode(input)) round-trip.

Confidence Score: 5/5

Safe to merge — the change is minimal, targeted, and verified by new tests.

The arrow-function wrapper is the canonical fix for an unbound method; the closure over the module-level encoder constant is safe and does not introduce any shared-state concerns. The Uint8Array_ return type correctly reflects what TextEncoder.encode returns across TypeScript versions. Tests cover both the previously-broken encode path and a round-trip, giving good confidence the fix is complete.

Files Needing Attention: No files require special attention.

Reviews (1): Last reviewed commit: "fix(binary): bind TextEncoder when encod..." | Re-trigger Greptile

@bytaesu

bytaesu commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

The largest consumer of @better-auth/utils (better-auth), only uses binary.decode, so this likely had no practical impact. However, since binary.encode is broken across all runtimes, I'll fix it anyway.

@bytaesu
bytaesu merged commit f67b8ce into main Jul 24, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants