fix: Converting circular structure to JSON

This commit is contained in:
Draculabo committed 2026-01-15 22:58:05 +08:00
1 parent 5a004d9d16
commit b0ec96d4a6
5 files changed
+91 -12

No files matched your search

+20 -1
View File
@@ -20,9 +20,28 @@ import { ConfigManager } from './ipc/config/manager';
const packetLogPath = path.join(app.getPath('userData'), 'orpc_packets.log');
/**
* Safely stringify an object, handling circular references
*/
function safeStringifyPacket(obj: unknown): string {
const seen = new WeakSet();
return JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return '[Circular]';
}
seen.add(value);
}
return value;
});
}
function logPacket(data: any) {
try {
fs.appendFileSync(packetLogPath, `[${new Date().toISOString()}] ${JSON.stringify(data)}\n`);
fs.appendFileSync(
packetLogPath,
`[${new Date().toISOString()}] ${safeStringifyPacket(data)}\n`,
);
} catch (e) {
if (e instanceof Error) {
console.error(e.message);