Skip to content

fix(font): avoid serializing full p5.Font data in previewEntry and IDE hook #3461

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

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions client/modules/IDE/hooks/useHandleMessageEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export default function useHandleMessageEvent() {
) => {
if (typeof obj !== 'object' || obj === null) return obj;

if (obj && obj.constructor && obj.constructor.name === 'p5.Font') {
return {
_type: 'p5.Font',
name: obj.name || 'Font'
};
}

if (depth >= maxDepth) {
if (seen.has(obj)) return '[Circular Reference]';
}
Expand Down
29 changes: 28 additions & 1 deletion client/utils/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,35 @@ function notifyListener(message) {
if (listener) listener(message);
}

// Helper function to handle p5.Font objects
const handleP5Font = (obj) => {
if (obj && obj.constructor && obj.constructor.name === 'p5.Font') {
return {
_type: 'p5.Font',
name: obj.name || 'Font'
};
}
return obj;
};

// Message processing logic to handle nested objects and arrays
const processMessage = (message) => {
if (typeof message === 'object' && message !== null) {
if (Array.isArray(message)) {
return message.map(processMessage);
}
return Object.fromEntries(
Object.entries(message).map(([key, value]) => [
key,
processMessage(handleP5Font(value))
])
);
}
return message;
};

function notifyFrames(message) {
const rawMessage = JSON.parse(JSON.stringify(message));
const rawMessage = processMessage(message);
Object.keys(frames).forEach((frameId) => {
const { frame, origin } = frames[frameId];
if (frame && frame.postMessage) {
Expand Down
18 changes: 17 additions & 1 deletion client/utils/previewEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,27 @@ window.objectPaths[blobPath] = 'index.html';

window.loopProtect = loopProtect;

// Helper function to handle p5.Font objects
const handleP5Font = (obj) => {
if (obj && obj.constructor && obj.constructor.name === 'p5.Font') {
return {
_type: 'p5.Font',
name: obj.name || 'Font'
};
}
return obj;
};

const consoleBuffer = [];
const LOGWAIT = 500;
Hook(window.console, (log) => {
// Process p5.Font objects in logs
const processedLog = {
...log,
data: log.data ? log.data.map(handleP5Font) : log.data
};
consoleBuffer.push({
log
log: processedLog
});
});
setInterval(() => {
Expand Down