Skip to content

Commit 9672262

Browse files
committed
fix(serialization): handle large objects and p5.Font serialization
1 parent 17c045e commit 9672262

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

client/modules/IDE/hooks/useHandleMessageEvent.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,49 @@ export default function useHandleMessageEvent() {
1010
obj,
1111
depth = 0,
1212
maxDepth = 10,
13-
seen = new WeakMap()
13+
seen = new WeakMap(),
14+
maxSize = 1000000 // Add maximum serialization size limit
1415
) => {
1516
if (typeof obj !== 'object' || obj === null) return obj;
1617

18+
// Check if object is p5.Font
19+
if (obj.constructor && obj.constructor.name === 'p5.Font') {
20+
return {
21+
type: 'p5.Font',
22+
name: obj.name || 'Unknown Font',
23+
size: obj.size || 'Unknown Size'
24+
};
25+
}
26+
27+
// Check object size
28+
try {
29+
const size = JSON.stringify(obj).length;
30+
if (size > maxSize) {
31+
return {
32+
type: 'Large Object',
33+
size: size,
34+
message: 'Object too large to serialize'
35+
};
36+
}
37+
} catch (e) {
38+
return {
39+
type: 'Unserializable Object',
40+
message: 'Object cannot be serialized'
41+
};
42+
}
43+
1744
if (depth >= maxDepth) {
1845
if (seen.has(obj)) return '[Circular Reference]';
1946
}
2047

2148
seen.set(obj, true);
2249

2350
return Array.isArray(obj)
24-
? obj.map((item) => safeStringify(item, depth + 1, maxDepth, seen))
51+
? obj.map((item) => safeStringify(item, depth + 1, maxDepth, seen, maxSize))
2552
: Object.fromEntries(
2653
Object.entries(obj).map(([key, value]) => [
2754
key,
28-
safeStringify(value, depth + 1, maxDepth, seen)
55+
safeStringify(value, depth + 1, maxDepth, seen, maxSize)
2956
])
3057
);
3158
};

client/utils/previewEntry.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,46 @@ window.objectPaths[blobPath] = 'index.html';
1717

1818
window.loopProtect = loopProtect;
1919

20+
// Add safe stringify function to handle large objects
21+
const safeStringify = (obj, maxSize = 1000000) => {
22+
// Handle p5.Font objects
23+
if (obj && obj.constructor && obj.constructor.name === 'p5.Font') {
24+
return {
25+
type: 'p5.Font',
26+
name: obj.name || 'Unknown Font',
27+
size: obj.size || 'Unknown Size'
28+
};
29+
}
30+
31+
// Check object size
32+
try {
33+
const size = JSON.stringify(obj).length;
34+
if (size > maxSize) {
35+
return {
36+
type: 'Large Object',
37+
size: size,
38+
message: 'Object too large to serialize'
39+
};
40+
}
41+
return obj;
42+
} catch (e) {
43+
return {
44+
type: 'Unserializable Object',
45+
message: 'Object cannot be serialized'
46+
};
47+
}
48+
};
49+
2050
const consoleBuffer = [];
2151
const LOGWAIT = 500;
2252
Hook(window.console, (log) => {
53+
// Apply safe stringify to log data
54+
const safeLog = {
55+
...log,
56+
data: log.data ? log.data.map(item => safeStringify(item)) : []
57+
};
2358
consoleBuffer.push({
24-
log
59+
log: safeLog
2560
});
2661
});
2762
setInterval(() => {
@@ -36,7 +71,6 @@ setInterval(() => {
3671
}, LOGWAIT);
3772

3873
function handleMessageEvent(e) {
39-
// maybe don't need this?? idk!
4074
if (window.origin !== e.origin) return;
4175
const { data } = e;
4276
const { source, messages } = data;
@@ -45,8 +79,10 @@ function handleMessageEvent(e) {
4579
decodedMessages.forEach((message) => {
4680
const { data: args } = message;
4781
const { result, error } = evaluateExpression(args);
82+
// Apply safe stringify to result
83+
const safeResult = safeStringify(result);
4884
const resultMessages = [
49-
{ log: Encode({ method: error ? 'error' : 'result', data: [result] }) }
85+
{ log: Encode({ method: error ? 'error' : 'result', data: [safeResult] }) }
5086
];
5187
editor.postMessage(
5288
{

0 commit comments

Comments
 (0)