Skip to content

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

Open

Description

@cn1001wang

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!

Activity

  1. MatthijsBurgh commented on Feb 25, 2026

    @MatthijsBurgh
    Contributor

    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?

  2. EzraBrooks commented on Mar 2, 2026

    @EzraBrooks
    Contributor

    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?

  3. EzraBrooks commented on Mar 2, 2026

    @EzraBrooks
    Contributor

    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.

  4. changed the title [-]React Native (Expo / Hermes) crashes with RangeError: Unknown encoding: latin1 when using roslib v2+[/-] [+]`fast-png` dependency creates a `latin1` TextDecoder at import-time, crashing runtimes that don't contain `latin1` support[/+] on Mar 2, 2026
  5. cn1001wang commented on Mar 11, 2026

    @cn1001wang
    Author

    Hi @EzraBrooks,

    I just tested this PR in a React Native environment, and I’m happy to report that the TextDecoder('latin1') error (triggered by the fast-png dependency) is no longer occurring. Everything is working smoothly now!

    For other developers using this in React Native, just a small tip: since roslib depends on uuid, you need to import react-native-get-random-values at the very top of your entry file to provide the necessary polyfill.

  6. ahmedbilal205 commented on Oct 26, 2026

    @ahmedbilal205

    I got roslib@2.1.0 working on a physical Android tablet using Expo SDK 57, React Native 0.86, and Hermes with two polyfills:

    npm install react-native-get-random-values text-encoding

    The important detail was initialization order: initialize Expo, install the polyfills, then load Expo Router.

    // index.js — set package.json "main" to "index.js"
    import 'expo';
    import 'react-native-get-random-values';
    import './utils/textDecoderPolyfill';
    import 'expo-router/entry';
    // utils/textDecoderPolyfill.js
    try {
      new globalThis.TextDecoder('latin1').decode();
    } catch {
      // Under Metro, text-encoding can preserve and re-export Expo's
      // existing UTF-8-only decoder. Clear it before loading the polyfill.
      globalThis.TextDecoder = undefined;
      globalThis.TextDecoder = require('text-encoding').TextDecoder;
    }

    Simply assigning require('text-encoding').TextDecoder without first clearing the existing decoder still produced the same latin1 crash on Android, even though it passed our initial Node test.

    react-native-get-random-values supplies the crypto API needed by UUID/BSON. Rebuild the Android app after installing it because it includes a native module.

    We also needed a separate Metro resolver adjustment to select ws’s browser entry, since Metro otherwise tried to bundle the unused Node transport.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions