Add the debug packet window to the Electron main process.

This commit is contained in:
2026-11-01 09:01:45 +11:00
parent 54c4cf4386
commit 121244e97b
3 changed files with 179 additions and 0 deletions
+122
View File
@@ -0,0 +1,122 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Debug Packets</title>
<style>
:root { color-scheme: dark; }
body { margin: 0; background: #14161a; color: #d9dce1; font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; }
main { display: flex; flex-direction: column; height: 100vh; }
header { display: flex; gap: 8px; align-items: center; padding: 8px; border-bottom: 1px solid #262a31; flex-wrap: wrap; }
input[type="search"] { flex: 1 1 160px; min-width: 120px; background: #1b1f25; color: inherit; border: 1px solid #2b3038; border-radius: 4px; padding: 6px 8px; font: inherit; }
button { background: #1b1f25; color: inherit; border: 1px solid #2b3038; border-radius: 4px; padding: 6px 10px; font: inherit; cursor: pointer; }
button:hover { border-color: #3b424d; }
label { display: flex; gap: 6px; align-items: center; }
#count { color: #8b93a1; }
#list { flex: 1; overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 4px 8px; border-bottom: 1px solid #21252b; white-space: nowrap; }
th { position: sticky; top: 0; background: #171a1f; color: #8b93a1; }
tbody tr { cursor: pointer; }
tbody tr:hover, tbody tr.sel { background: #1b1f25; }
#detail { max-height: 40vh; overflow: auto; margin: 0; padding: 10px; border-top: 1px solid #262a31; white-space: pre-wrap; word-break: break-word; }
</style>
</head>
<body>
<main>
<header>
<input id="filter" type="search" placeholder="Filter (method, path, status, txnId, eventId, body)" aria-label="Filter packets">
<label><input id="autoscroll" type="checkbox" checked> Autoscroll</label>
<button id="clear">Clear</button>
<button id="copy">Copy all</button>
<button id="download">Download JSON lines</button>
<span id="count">0</span>
</header>
<div id="list">
<table>
<thead>
<tr><th>Time</th><th>Phase</th><th>Method</th><th>Path</th><th>Status</th><th>ms</th><th>txnId / eventId</th></tr>
</thead>
<tbody id="rows"></tbody>
</table>
</div>
<pre id="detail"></pre>
</main>
<script>
const MAX = 2000;
const packets = [];
const rows = document.getElementById('rows');
const list = document.getElementById('list');
const detail = document.getElementById('detail');
const filter = document.getElementById('filter');
const autoscroll = document.getElementById('autoscroll');
const count = document.getElementById('count');
const text = (v) => (v === undefined || v === null ? '' : String(v));
function matches(p) {
const q = filter.value.trim().toLowerCase();
if (!q) return true;
return [p.method, p.path, p.status, p.txnId, p.eventId, p.error, p.req, p.res]
.some((f) => text(f).toLowerCase().includes(q));
}
function addRow(p) {
const tr = document.createElement('tr');
[new Date(p.ts).toLocaleTimeString(), p.phase, p.method, p.path, p.status, p.ms, p.txnId || p.eventId]
.forEach((v) => {
const td = document.createElement('td');
td.textContent = text(v);
tr.appendChild(td);
});
tr.addEventListener('click', () => {
document.querySelectorAll('tr.sel').forEach((r) => r.classList.remove('sel'));
tr.classList.add('sel');
detail.textContent = JSON.stringify(p, null, 2);
});
rows.appendChild(tr);
}
function render() {
rows.textContent = '';
packets.filter(matches).forEach(addRow);
count.textContent = packets.length;
if (autoscroll.checked) list.scrollTop = list.scrollHeight;
}
function add(p) {
packets.push(p);
if (packets.length > MAX) packets.shift();
count.textContent = packets.length;
if (!matches(p)) return;
addRow(p);
if (autoscroll.checked) list.scrollTop = list.scrollHeight;
}
filter.addEventListener('input', render);
document.getElementById('clear').addEventListener('click', () => {
packets.length = 0;
detail.textContent = '';
render();
});
const lines = () => packets.map((p) => JSON.stringify(p)).join('\n');
document.getElementById('copy').addEventListener('click', () => {
window.electron?.clipboard?.writeText(lines());
});
document.getElementById('download').addEventListener('click', () => {
window.electron?.media?.saveFile({
filename: 'packets.jsonl',
mimeType: 'application/x-jsonlines',
data: lines(),
});
});
// Main sends the backlog as one batch (array), later packets one at a time
const unsubscribe = window.electron?.debug?.onPacket((data) => {
(Array.isArray(data) ? data : [data]).forEach(add);
});
window.addEventListener('beforeunload', () => unsubscribe?.());
</script>
</body>
</html>
+46
View File
@@ -113,6 +113,9 @@ let tray = null;
let isQuitting = false; let isQuitting = false;
let apiServer = null; let apiServer = null;
let pendingDeepLink = null; let pendingDeepLink = null;
let debugWindow = null;
const debugPacketBacklog = [];
const DEBUG_PACKET_LIMIT = 2000;
let protocolRegistrationState = { let protocolRegistrationState = {
before: false, before: false,
registerCall: false, registerCall: false,
@@ -1960,6 +1963,49 @@ ipcMain.handle('discord-collectibles:download-assets', async (event, { assets })
} }
}); });
// Debug packet window
ipcMain.handle('debug:open-window', () => {
if (debugWindow && !debugWindow.isDestroyed()) {
debugWindow.focus();
return { success: true };
}
debugWindow = new BrowserWindow({
width: 900,
height: 600,
title: 'Debug Packets',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
webSecurity: true
}
});
debugWindow.on('closed', () => {
debugWindow = null;
});
// Send the backlog as one batch once the page has registered its listener
debugWindow.webContents.on('did-finish-load', () => {
debugWindow.webContents.send('debug:packet', debugPacketBacklog);
});
debugWindow.loadFile(path.join(__dirname, 'debug.html'));
return { success: true };
});
ipcMain.on('debug:packet', (event, packet) => {
if (!packet || typeof packet !== 'object') return;
debugPacketBacklog.push(packet);
if (debugPacketBacklog.length > DEBUG_PACKET_LIMIT) debugPacketBacklog.shift(); // Keep last 2000
if (debugWindow && !debugWindow.isDestroyed()) {
debugWindow.webContents.send('debug:packet', packet);
}
});
console.log('Paarrot Electron main process started'); console.log('Paarrot Electron main process started');
console.log('Development mode:', isDev); console.log('Development mode:', isDev);
console.log('Platform:', process.platform); console.log('Platform:', process.platform);
+11
View File
@@ -185,6 +185,17 @@ contextBridge.exposeInMainWorld('electron', {
} }
}, },
// Debug packet recorder
debug: {
openWindow: () => ipcRenderer.invoke('debug:open-window'),
send: (packet) => ipcRenderer.send('debug:packet', packet),
onPacket: (callback) => {
const handler = (_, data) => callback(data);
ipcRenderer.on('debug:packet', handler);
return () => ipcRenderer.removeListener('debug:packet', handler);
}
},
// Plugin management // Plugin management
plugins: { plugins: {
getPath: () => ipcRenderer.invoke('plugin:get-path'), getPath: () => ipcRenderer.invoke('plugin:get-path'),