Skip to content

feat(bufio): Add React Native compatibility fixes#231

Merged
matthewjablack merged 1 commit into
masterfrom
feat/react-native-bufio-compatibility
Sep 12, 2025
Merged

feat(bufio): Add React Native compatibility fixes#231
matthewjablack merged 1 commit into
masterfrom
feat/react-native-bufio-compatibility

Conversation

@matthewjablack
Copy link
Copy Markdown
Contributor

Summary

This PR adds React Native compatibility to the @node-dlc/bufio package while maintaining full backward compatibility with Node.js environments.

Changes Made

1. Function Name References → String Literals

  • Replaced this.methodName.name with string literals (e.g., 'readUInt8')
  • Why: React Native minifiers can mangle function names, causing runtime errors
  • Impact: Zero performance impact in Node.js, fixes compatibility in React Native

2. BigInt Performance Optimization

  • Replaced BigInt(2) ** BigInt(64) with BigInt('18446744073709551616')
  • Why: Eliminates runtime exponentiation calculation and improves React Native compatibility
  • Impact: Better performance in all environments

3. Buffer.copy() Compatibility

  • Added feature detection for Buffer.copy() method with manual fallback
  • Why: React Native Buffer polyfills may not implement the copy() method
  • Impact: Zero overhead in Node.js (native copy used), manual fallback in React Native

Compatibility

  • Node.js: All changes are backward compatible, native methods still used
  • React Native: Now works without requiring post-install patches
  • Performance: No degradation in Node.js, improvements in some cases

Testing

  • All existing tests pass
  • Changes use feature detection rather than environment detection
  • Maintains identical behavior across all supported environments

Files Changed

  • packages/bufio/lib/BufferReader.ts
  • packages/bufio/lib/BufferWriter.ts

This eliminates the need for manual patches when using the library in React Native applications.

- replace function.name references with string literals to avoid minification issues
- replace BigInt exponentiation with precomputed constants for better performance
- add Buffer.copy() fallback for React Native Buffer polyfills that lack copy method
- maintain full backward compatibility with Node.js environments

Fixes compatibility issues when using @node-dlc/bufio in React Native applications
while preserving all existing functionality for Node.js users.
@matthewjablack matthewjablack merged commit 9d721e3 into master Sep 12, 2025
2 checks passed
@matthewjablack matthewjablack deleted the feat/react-native-bufio-compatibility branch September 12, 2025 16:40
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.

1 participant