You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
binary.encode()throwing due to an unboundTextEncoder.encodemethodUint8Array_return type