Add detailed logging for ICE candidate storage and retrieval

- Log when ICE candidates are added with offerId, peerId, role, count, timestamp
- Log when ICE candidates are requested with peer type, targetRole, since
- Log D1 query parameters and result counts
- Use consistent timestamp for all candidates in a batch
- Log first candidate timestamp vs since parameter for debugging
This commit is contained in:
2025-11-14 21:01:15 +01:00
parent 2537b709fe
commit 30ea2bd28e
2 changed files with 16 additions and 2 deletions

View File

@@ -498,14 +498,17 @@ export function createApp(storage: Storage, config: Config) {
if (offer.peerId === peerId) { if (offer.peerId === peerId) {
// Offerer wants answerer's candidates // Offerer wants answerer's candidates
targetRole = 'answerer'; targetRole = 'answerer';
console.log(`[ICE GET] Offerer ${peerId} requesting answerer ICE candidates for offer ${offerId}, since=${since}, answererPeerId=${offer.answererPeerId}`);
} else if (offer.answererPeerId === peerId) { } else if (offer.answererPeerId === peerId) {
// Answerer wants offerer's candidates // Answerer wants offerer's candidates
targetRole = 'offerer'; targetRole = 'offerer';
console.log(`[ICE GET] Answerer ${peerId} requesting offerer ICE candidates for offer ${offerId}, since=${since}, offererPeerId=${offer.peerId}`);
} else { } else {
return c.json({ error: 'Not authorized to view ICE candidates for this offer' }, 403); return c.json({ error: 'Not authorized to view ICE candidates for this offer' }, 403);
} }
const candidates = await storage.getIceCandidates(offerId, targetRole, since); const candidates = await storage.getIceCandidates(offerId, targetRole, since);
console.log(`[ICE GET] Found ${candidates.length} candidates for offer ${offerId}, targetRole=${targetRole}, since=${since}`);
return c.json({ return c.json({
offerId, offerId,

View File

@@ -244,6 +244,9 @@ export class D1Storage implements Storage {
role: 'offerer' | 'answerer', role: 'offerer' | 'answerer',
candidates: RTCIceCandidateInit[] candidates: RTCIceCandidateInit[]
): Promise<number> { ): Promise<number> {
const now = Date.now();
console.log(`[D1] addIceCandidates: offerId=${offerId}, peerId=${peerId}, role=${role}, count=${candidates.length}, timestamp=${now}`);
// D1 doesn't have transactions, so insert one by one // D1 doesn't have transactions, so insert one by one
for (const cand of candidates) { for (const cand of candidates) {
await this.db.prepare(` await this.db.prepare(`
@@ -254,7 +257,7 @@ export class D1Storage implements Storage {
peerId, peerId,
role, role,
JSON.stringify(cand), // Store full object as JSON JSON.stringify(cand), // Store full object as JSON
Date.now() now
).run(); ).run();
} }
@@ -280,13 +283,15 @@ export class D1Storage implements Storage {
query += ' ORDER BY created_at ASC'; query += ' ORDER BY created_at ASC';
console.log(`[D1] getIceCandidates query: offerId=${offerId}, targetRole=${targetRole}, since=${since}`);
const result = await this.db.prepare(query).bind(...params).all(); const result = await this.db.prepare(query).bind(...params).all();
console.log(`[D1] getIceCandidates result: ${result.results?.length || 0} rows`);
if (!result.results) { if (!result.results) {
return []; return [];
} }
return result.results.map((row: any) => ({ const candidates = result.results.map((row: any) => ({
id: row.id, id: row.id,
offerId: row.offer_id, offerId: row.offer_id,
peerId: row.peer_id, peerId: row.peer_id,
@@ -294,6 +299,12 @@ export class D1Storage implements Storage {
candidate: JSON.parse(row.candidate), // Parse JSON back to object candidate: JSON.parse(row.candidate), // Parse JSON back to object
createdAt: row.created_at, createdAt: row.created_at,
})); }));
if (candidates.length > 0) {
console.log(`[D1] First candidate createdAt: ${candidates[0].createdAt}, since: ${since}`);
}
return candidates;
} }
async getTopics(limit: number, offset: number): Promise<{ async getTopics(limit: number, offset: number): Promise<{