-
Notifications
You must be signed in to change notification settings - Fork 417
Lazy-load fast-png to fix crash in React Native / Hermes
#1166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ | |
| */ | ||
|
|
||
| import type { DecodedPng } from "fast-png"; | ||
| import { decode } from "fast-png"; | ||
|
|
||
| const textDecoder = new TextDecoder(); | ||
|
|
||
|
|
@@ -15,22 +14,27 @@ const textDecoder = new TextDecoder(); | |
| * | ||
| * @param data - An object containing the PNG data. | ||
| */ | ||
| export default function decompressPng(data: string): unknown { | ||
| export default async function decompressPng(data: string): Promise<unknown> { | ||
| // fast-png is imported dynamically (lazily) rather than statically to avoid | ||
| // a crash in environments such as React Native / Hermes. fast-png constructs | ||
| // a `new TextDecoder('latin1')` at module load time, and Hermes does not | ||
| // support the 'latin1' encoding, causing an immediate RangeError on import. | ||
| // By deferring the import until a PNG message is actually received, users | ||
| // who do not use PNG-compressed rosbridge messages are unaffected. | ||
| // See: https://github.com/image-js/fast-png/blob/77a4479d68d84246793f58f7bbf2a2ea3a80c0f5/src/helpers/text.ts#L11 | ||
| const { decode } = await import("fast-png"); | ||
| const buffer = Uint8Array.from(atob(data), (char) => char.charCodeAt(0)); | ||
|
Comment on lines
+18
to
26
|
||
|
|
||
| const decoded = tryDecodeBuffer(buffer); | ||
|
|
||
| let decoded: DecodedPng; | ||
| try { | ||
| return JSON.parse(textDecoder.decode(decoded.data)); | ||
| decoded = decode(buffer); | ||
| } catch (error) { | ||
| throw new Error("Error parsing PNG JSON contents", { cause: error }); | ||
| throw new Error("Error decoding PNG buffer", { cause: error }); | ||
| } | ||
| } | ||
|
|
||
| function tryDecodeBuffer(buffer: Uint8Array): DecodedPng { | ||
| try { | ||
| return decode(buffer); | ||
| return JSON.parse(textDecoder.decode(decoded.data)); | ||
| } catch (error) { | ||
| throw new Error("Error decoding PNG buffer", { cause: error }); | ||
| throw new Error("Error parsing PNG JSON contents", { cause: error }); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSDoc for
datasays "An object containing the PNG data", butdatais a base64-encoded PNG string. Updating the parameter description would avoid confusion for callers and keep the docs aligned with the actual API.