Add the debug packet recorder behind the developer-tools toggle.
This commit is contained in:
@@ -3,6 +3,87 @@ import { createClient, MatrixClient, IndexedDBStore, IndexedDBCryptoStore } from
|
||||
import { cryptoCallbacks } from './secretStorageKeys';
|
||||
import { clearNavToActivePathStore } from '../app/state/navToActivePath';
|
||||
import { Session, getSessionStoreName } from '../app/state/sessions';
|
||||
import { getSettings } from '../app/state/settings';
|
||||
import { recordPacket } from '../app/state/debugPackets';
|
||||
|
||||
const MAX_BODY = 8192;
|
||||
|
||||
const jsonField = (text: string | undefined, key: string) => {
|
||||
if (!text) return undefined;
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
return typeof parsed?.[key] === 'string' ? parsed[key] : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
// Bodies are only read from a clone, so the SDK still gets the response it asked for
|
||||
const readBody = async (res: Response) => {
|
||||
const type = res.headers.get('content-type') ?? '';
|
||||
if (!type.includes('json') && !type.includes('text')) return undefined;
|
||||
try {
|
||||
return (await res.clone().text()).slice(0, MAX_BODY);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const debugFetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
if (!getSettings().debugPackets) return globalThis.fetch(input, init);
|
||||
|
||||
// The SDK passes `URL | string`, and a URL instance has href, not url
|
||||
const url = input instanceof URL ? input : new URL(typeof input === 'string' ? input : input.url);
|
||||
const method = init?.method ?? (input instanceof Request ? input.method : 'GET');
|
||||
const req = typeof init?.body === 'string' ? init.body.slice(0, MAX_BODY) : undefined;
|
||||
const txnId = jsonField(req, 'transaction_id');
|
||||
const start = Date.now();
|
||||
|
||||
recordPacket({
|
||||
ts: start,
|
||||
phase: 'start',
|
||||
method,
|
||||
url: url.toString(),
|
||||
path: url.pathname,
|
||||
txnId,
|
||||
req,
|
||||
});
|
||||
|
||||
let res: Response;
|
||||
try {
|
||||
res = await globalThis.fetch(input, init);
|
||||
} catch (error) {
|
||||
recordPacket({
|
||||
ts: Date.now(),
|
||||
phase: 'end',
|
||||
method,
|
||||
url: url.toString(),
|
||||
path: url.pathname,
|
||||
ms: Date.now() - start,
|
||||
txnId,
|
||||
req,
|
||||
error: String(error),
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
||||
const body = await readBody(res);
|
||||
recordPacket({
|
||||
ts: Date.now(),
|
||||
phase: 'end',
|
||||
method,
|
||||
url: url.toString(),
|
||||
path: url.pathname,
|
||||
status: res.status,
|
||||
ms: Date.now() - start,
|
||||
txnId,
|
||||
eventId: jsonField(body, 'event_id'),
|
||||
req,
|
||||
res: body,
|
||||
});
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
// Track if we're currently handling a key conflict to prevent loops
|
||||
let handlingKeyConflict = false;
|
||||
@@ -136,7 +217,7 @@ const setupKeyConflictHandler = (mx: MatrixClient, session: Session): void => {
|
||||
|
||||
const errorHandler = (...args: any[]) => {
|
||||
if (checkKeyConflict(args)) {
|
||||
keyConflictCount++;
|
||||
keyConflictCount += 1;
|
||||
console.log(
|
||||
`[initMatrix] ⚠️ Detected key conflict (${keyConflictCount}/${KEY_CONFLICT_THRESHOLD})`,
|
||||
);
|
||||
@@ -181,7 +262,7 @@ const setupKeyConflictHandler = (mx: MatrixClient, session: Session): void => {
|
||||
console.error = errorHandler;
|
||||
console.warn = (...args: any[]) => {
|
||||
if (checkKeyConflict(args)) {
|
||||
keyConflictCount++;
|
||||
keyConflictCount += 1;
|
||||
}
|
||||
originalWarn.apply(console, args);
|
||||
};
|
||||
@@ -236,6 +317,7 @@ export const initClient = async (session: Session): Promise<MatrixClient> => {
|
||||
timelineSupport: true,
|
||||
cryptoCallbacks: cryptoCallbacks as any,
|
||||
verificationMethods: ['m.sas.v1'],
|
||||
fetchFn: debugFetch,
|
||||
});
|
||||
|
||||
await indexedDBStore.startup();
|
||||
@@ -399,11 +481,11 @@ export const logoutAllSessions = async () => {
|
||||
|
||||
// Clear all IndexedDB databases
|
||||
const dbs = await window.indexedDB.databases();
|
||||
for (const idbInfo of dbs) {
|
||||
dbs.forEach((idbInfo) => {
|
||||
if (idbInfo.name) {
|
||||
window.indexedDB.deleteDatabase(idbInfo.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user