Skip to content

Commit

Permalink
fix(replay): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Feb 4, 2025
1 parent ea9f724 commit 39cab70
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions packages/app/src/app/api/monitoring/envelope/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,47 @@ export async function POST(request: NextRequest) {
console.log("Envelope body length:", body.length);
console.log("Envelope newlines count:", (body.match(/\n/g) || []).length);

// Split envelope into parts using double newlines
const parts = body.split("\n\n");
if (parts.length < 2) {
console.error("Invalid envelope format: missing parts");
// Split envelope into header and items while preserving exact format
const [headerRaw, ...itemParts] = body.split(/\n\n/);
if (!headerRaw || itemParts.length === 0) {
console.error("Invalid envelope format: missing header or items");
return new Response("Invalid envelope format", { status: 400 });
}

// First part is the header
const headerRaw = parts[0];
let projectId: string | undefined;
let publicKey: string | undefined;

// Process remaining parts as items
const items = parts.slice(1).map(part => {
// Process items while preserving exact format including newlines
const items: string[] = [];
for (const part of itemParts) {
const lines = part.split("\n");
const itemHeaderLine = lines[0];
const itemPayload = lines.slice(1).join("\n");

try {
// Try to parse item header
// Validate item header
const itemHeader = JSON.parse(itemHeaderLine);
console.log("Processing item:", {
type: itemHeader.type,
hasPayload: lines.length > 1,
lines: lines.length,
length: lines.length,
});

// For replay events, ensure we preserve exact format
if (itemHeader.type === "replay_event" || itemHeader.type === "replay_recording") {
items.push(part); // Keep original format intact
} else {
// For other items, normalize format
const itemPayload = lines.slice(1).join("\n");
items.push(itemPayload ? `${itemHeaderLine}\n${itemPayload}` : itemHeaderLine);
}
} catch (e) {
// Skip invalid items
console.warn("Invalid item header:", itemHeaderLine);
items.push(part); // Keep original format if parsing fails
}
}

// Return the item with proper newlines:
// header + \n + payload (if any)
return itemPayload ? `${itemHeaderLine}\n${itemPayload}` : itemHeaderLine;
});

// Log parsed items with detailed structure
items.forEach((item, index) => {
console.log(`Item ${index} structure:`, {
content: item,
length: item.length,
newlines: (item.match(/\n/g) || []).length,
endsWithNewline: item.endsWith("\n"),
});
});

// Reconstruct envelope with proper format:
// header + \n\n + item1 + \n\n + item2 + \n\n ... + \n
const envelope = headerRaw + "\n\n" + items.join("\n\n") + "\n";
// Reconstruct envelope preserving exact format:
// header + \n\n + items joined with \n\n + trailing \n
const envelope = `${headerRaw}\n\n${items.join("\n\n")}\n`;

// Log detailed envelope structure
console.log("Envelope structure:", {
Expand Down

0 comments on commit 39cab70

Please sign in to comment.