Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions src/libs/Pusher/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import type PusherModule from './types';

let shouldForceOffline = false;
Onyx.connect({

Check warning on line 16 in src/libs/Pusher/index.native.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NETWORK,
callback: (network) => {
if (!network) {
Expand Down Expand Up @@ -88,6 +88,39 @@
return socket.getChannel(channelName);
}

/**
* Parses JSON data that may be single or double-encoded
* This handles cases where the backend sometimes sends double-encoded JSON
* Reference issue: https://github.com/Expensify/App/issues/60332
*/
function parseEventData<EventName extends PusherEventName>(eventData: EventData<EventName>): EventData<EventName> | null {
if (isObject(eventData)) {
return eventData;
}

if (typeof eventData !== 'string') {
Log.alert('[Pusher] Event data is neither object nor string', {eventData});
return null;
}

try {
const firstParse = JSON.parse(eventData) as EventData<EventName> | string;

// If result is still a string, it was double-encoded - parse again
if (typeof firstParse === 'string') {
return JSON.parse(firstParse) as EventData<EventName>;
}

return firstParse;
} catch (error) {
Log.alert('[Pusher] Failed to parse event data', {
error: error instanceof Error ? error.message : 'Unknown error',
eventData,
});
return null;
}
}

/**
* Binds an event callback to a channel + eventName
*/
Expand All @@ -103,11 +136,9 @@
return;
}

let data: EventData<EventName>;
try {
data = isObject(eventData) ? eventData : (JSON.parse(eventData) as EventData<EventName>);
} catch (err) {
Log.alert('[Pusher] Unable to parse single JSON event data from Pusher', {error: err, eventData});
const data = parseEventData(eventData);
if (!data) {
// Error already logged in parseEventData
return;
}
if (data.id === undefined || data.chunk === undefined || data.final === undefined) {
Expand Down
Loading