Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified apps/api/src/storage/thread-message-parts.test.ts
Binary file not shown.
15 changes: 14 additions & 1 deletion apps/api/src/storage/thread-message-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ function sanitizeForPg(value: string): string {
return value.replace(NUL, "").replace(LONE_SURROGATE, "\uFFFD");
}

// The replacer below only reaches values, never object keys.
function sanitizeKeysForPg(value: unknown): unknown {
if (Array.isArray(value)) return value.map(sanitizeKeysForPg);
if (value !== null && typeof value === "object") {
const out: Record<string, unknown> = {};
for (const [key, v] of Object.entries(value)) {
out[sanitizeForPg(key)] = sanitizeKeysForPg(v);
}
return out;
}
return value;
}

export function serializePayload(payload: unknown): string {
if (
estimatePayloadBytes(payload, MAX_PART_PAYLOAD_BYTES) >
Expand All @@ -79,7 +92,7 @@ export function serializePayload(payload: unknown): string {
// A JSON replacer visits every string in the payload tree; clean strings pass
// through untouched (byte-identical output, so ids derived from the payload
// stay stable).
return JSON.stringify(payload, (_key, value) =>
return JSON.stringify(sanitizeKeysForPg(payload), (_key, value) =>
typeof value === "string" ? sanitizeForPg(value) : value,
);
}
Expand Down
Loading