Skip to content

Lazy-load fast-png to fix crash in React Native / Hermes#1166

Draft
Copilot wants to merge 3 commits intodevelopfrom
copilot/fix-latin1-textdecoder-crash
Draft

Lazy-load fast-png to fix crash in React Native / Hermes#1166
Copilot wants to merge 3 commits intodevelopfrom
copilot/fix-latin1-textdecoder-crash

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

  • Understand the issue: fast-png creates a TextDecoder('latin1') at import-time, crashing Hermes (React Native)
  • Change decompressPng.ts to use dynamic import("fast-png") instead of static import, making function async
  • Update handleRosbridgePngMessage in Transport.ts to handle the async decompressPng promise
  • Add inline comment explaining why the lazy import is necessary, with a link to the upstream code
  • Run tests to verify changes
  • Code review and security scan
Original prompt

This section details on the original issue you should resolve

<issue_title>fast-png dependency creates a latin1 TextDecoder at import-time, crashing runtimes that don't contain latin1 support</issue_title>
<issue_description>## Environment

  • roslib version: 2.x (latest)
  • Platform: React Native (Expo Bare)
  • JS engine: Hermes
  • OS: Android (also reproducible on iOS with Hermes)
  • Transport: rosbridge WebSocket

Description

After upgrading from roslib 1.4.1 to 2.x, the app crashes at startup in a React Native environment using Hermes. The error appears to originate from dependencies (likely BSON / Buffer usage) that rely on encodings unsupported by Hermes.

Error Logs

WARN  BSON: For React Native please polyfill crypto.getRandomValues
ERROR RangeError: Unknown encoding: latin1 (normalized: latin1), js engine: hermes

Steps to Reproduce

  1. Create a React Native project (Expo Bare or CLI).
  2. Enable Hermes (default in recent RN versions).
  3. Install roslib 2.x:
npm install roslib
  1. Import and create a Ros instance:
import { Ros } from 'roslib'

const ros = new Ros({
  url: 'ws://localhost:9090'
})
  1. Run on Android or iOS.

Expected Behavior

roslib should initialize normally in React Native, or documentation should clarify that RN is not supported.

Actual Behavior

Application crashes with:

RangeError: Unknown encoding: latin1

Questions

  1. Is React Native (Hermes) officially supported in roslib v2?
  2. Are there recommended polyfills or configuration steps to make v2 work in RN?
  3. If RN is not supported, would it be possible to document this limitation?
  4. Is there any plan to provide a RN-compatible build (without Node-specific encoding dependencies)?

Thank you!
</issue_description>

<agent_instructions>change handleRosbridgePngMessage to lazily import fast-png to avoid it being imported when users do not require PNG support</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@MatthijsBurgh I don't think we support any frameworks like react. They work or not. We are open to fixes that extend compatibility.

@EzraBrooks do you have any tips to get it working?</comment_new>
<comment_new>@EzraBrooks
I think we certainly should support React, etc. since it's how most people write JS apps.

This seems like a bug internal to bson, unfortunately. Are you actually using BSON on your rosbridge, or is roslibjs erroneously attempting to do BSON things where it's not needed?</comment_new>
<comment_new>@EzraBrooks
Actually, this line in fast-png appears to be the culprit:

https://github.com/image-js/fast-png/blob/77a4479d68d84246793f58f7bbf2a2ea3a80c0f5/src/helpers/text.ts#L11

A globally-scoped TextDecoder that is constructed at import time.

I think perhaps roslibjs can lazy-load PNG support.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ative/Hermes

Co-authored-by: EzraBrooks <1959658+EzraBrooks@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix fast-png dependency causing crash in React Native with Hermes Lazy-load fast-png to fix crash in React Native / Hermes Mar 2, 2026
@EzraBrooks
Copy link
Contributor

@copilot please document in the comments why this change was made so someone doesn't attempt to optimize it out later.

Co-authored-by: EzraBrooks <1959658+EzraBrooks@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot please document in the comments why this change was made so someone doesn't attempt to optimize it out later.

Done in 8f0d0ab. Added a comment above the await import("fast-png") line explaining that the lazy import is intentional to avoid crashing Hermes (React Native), which doesn't support the 'latin1' encoding that fast-png uses at module load time, with a link to the upstream code.

@EzraBrooks
Copy link
Contributor

@cn1001wang could you test if this fixes your issue?

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.

fast-png dependency creates a latin1 TextDecoder at import-time, crashing runtimes that don't contain latin1 support

2 participants