also looking at this
Harden AnalyticsHQ release and dashboard quality
#52
34 files
+1236
-340
| @@ -0,0 +1,46 @@ | ||
| 1 | /** | |
| 2 | * Browser keepalive requests have a small total-body budget. Keeping event | |
| 3 | * properties below 48 KiB leaves room for the event envelope while allowing | |
| 4 | * long URLs and useful structured metadata. | |
| 5 | */ | |
| 6 | export const MAX_EVENT_PROPERTIES_BYTES = 48 * 1024 | |
| 7 | ||
| 8 | const URL_EVENTS = new Set(['Outbound Link', 'File Download']) | |
| 9 | ||
| 10 | function utf8Bytes(value: string): number { | |
| 11 | return new TextEncoder().encode(value).byteLength | |
| 12 | } | |
| 13 | ||
| 14 | /** | |
| 15 | * Serialize public event properties without allowing an unbounded database | |
| 16 | * value. Reserved URL events keep one canonical key so grouping is stable. | |
| 17 | * | |
| 18 | * Invalid or oversized properties are omitted, but the event itself can still | |
| 19 | * be recorded. The public collector must not become a 500 because optional | |
| 20 | * metadata was unusable. | |
| 21 | */ | |
| 22 | export function serializeEventProperties(event: unknown, properties: unknown): string | null { | |
| 23 | if (properties === null || properties === undefined) | |
| 24 | return null | |
| 25 | ||
| 26 | let normalized = properties | |
| 27 | if ( | |
| 28 | URL_EVENTS.has(String(event)) | |
| 29 | && typeof properties === 'object' | |
| 30 | && 'url' in properties | |
| 31 | && properties.url | |
| 32 | ) { | |
| 33 | normalized = { url: String(properties.url) } | |
| 34 | } | |
| 35 | ||
| 36 | try { | |
| 37 | const serialized = JSON.stringify(normalized) | |
| 38 | if (!serialized || utf8Bytes(serialized) > MAX_EVENT_PROPERTIES_BYTES) | |
| 39 | return null | |
| 40 | ||
| 41 | return serialized | |
| 42 | } | |
| 43 | catch { | |
| 44 | return null | |
| 45 | } | |
| 46 | } | |