Skip to content

Commit c4e3a96

Browse files
tyao1meta-codesync[bot]
authored andcommitted
Fix postMessage error
Summary: This diff is an attempt to fix Relay devtools postMessage error. When an event data contains non serializable data, when it will throw when it tries to postMessage. The diff fixes the error on Adsmanager by adding a `makeCloneable` function that recursively filter non-serializable data. Reviewed By: captbaritone Differential Revision: D88909583 fbshipit-source-id: 5236fcaf6bd749b976e86337d1339a697eb96b3b
1 parent 7cdde4e commit c4e3a96

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

shells/browser/chrome/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"manifest_version": 3,
33
"name": "Relay Developer Tools",
44
"description": "Adds Relay debugging tools to the Chrome Developer Tools.",
5-
"version": "0.9.18",
6-
"version_name": "0.9.18",
5+
"version": "0.9.19",
6+
"version_name": "0.9.19",
77
"minimum_chrome_version": "88",
88
"icons": {
99
"16": "icons/enabled16.png",

src/backend/EnvironmentWrapper.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,38 @@ function sanitizeEvent(event: Object): Object {
4747
const value = event[key];
4848
if (typeof value === 'function' || UNUSED_EXPENSIVE_FIELDS.includes(key)) {
4949
continue;
50-
} else if (value == null || typeof value !== 'object') {
51-
newEvent[key] = value;
52-
} else if (value instanceof Map) {
53-
newEvent[key] = Object.fromEntries((value: Map<mixed, mixed>));
54-
} else if (value instanceof Set) {
55-
newEvent[key] = Array.from(value);
56-
} else if (typeof value.toJSON == 'function') {
57-
// Convert RecordSource to Object, there are arbitary values for resolvers
58-
newEvent[key] = JSON.parse(JSON.stringify(value.toJSON()));
59-
} else if (
60-
(key === 'info' && event.name === 'network.info') ||
61-
event.name === 'network.start' ||
62-
key === 'cacheConfig'
63-
) {
64-
// Some network events contain arbitary data
65-
newEvent[key] = JSON.parse(JSON.stringify(value));
66-
} else {
67-
newEvent[key] = value;
6850
}
51+
newEvent[key] = makeCloneable(value);
6952
}
7053
return newEvent;
7154
}
7255

56+
function makeCloneable(obj: mixed): Object {
57+
if (obj === null || typeof obj !== 'object') return obj;
58+
59+
if (Array.isArray(obj)) {
60+
return obj.map(makeCloneable);
61+
}
62+
63+
if (typeof obj.toJSON == 'function') {
64+
// Convert RecordSource to Object, there are arbitary values for resolvers
65+
// $FlowFixMe[incompatible-use]
66+
return makeCloneable(obj.toJSON());
67+
}
68+
const result: Object = {};
69+
for (const [key, value] of Object.entries(obj)) {
70+
if (
71+
typeof value === 'function' ||
72+
typeof value === 'symbol' ||
73+
value instanceof Node
74+
) {
75+
continue;
76+
}
77+
result[key] = makeCloneable(value);
78+
}
79+
return result;
80+
}
81+
7382
export function attach(
7483
hook: DevToolsHook,
7584
rendererID: number,
@@ -80,7 +89,7 @@ export function attach(
8089
const store = environment.getStore();
8190

8291
const originalLog = environment.__log;
83-
environment.__log = event => {
92+
environment.__log = (event) => {
8493
originalLog(event);
8594
if (!SUPPORTED_EVENTS.has(event.name)) {
8695
return;
@@ -100,7 +109,7 @@ export function attach(
100109

101110
const storeOriginalLog = store.__log;
102111
// TODO(damassart): Make this cleaner
103-
store.__log = event => {
112+
store.__log = (event) => {
104113
if (storeOriginalLog !== null) {
105114
storeOriginalLog(event);
106115
}
@@ -133,7 +142,7 @@ export function attach(
133142
function flushInitialOperations() {
134143
// TODO(damassart): Make this a modular function
135144
if (pendingEventsQueue != null) {
136-
pendingEventsQueue.forEach(pendingEvent => {
145+
pendingEventsQueue.forEach((pendingEvent) => {
137146
hook.emit('environment.event', {
138147
id: rendererID,
139148
envName: environment.configName,

0 commit comments

Comments
 (0)