(chunk: ProfileChunk)
| 257 | * - Required metadata fields |
| 258 | */ |
| 259 | export function validateProfileChunk(chunk: ProfileChunk): { valid: true } | { reason: string } { |
| 260 | try { |
| 261 | // Required metadata |
| 262 | if (!chunk || typeof chunk !== 'object') { |
| 263 | return { reason: 'chunk is not an object' }; |
| 264 | } |
| 265 | |
| 266 | // profiler_id and chunk_id must be 32 lowercase hex chars |
| 267 | const isHex32 = (val: unknown): boolean => typeof val === 'string' && /^[a-f0-9]{32}$/.test(val); |
| 268 | if (!isHex32(chunk.profiler_id)) { |
| 269 | return { reason: 'missing or invalid profiler_id' }; |
| 270 | } |
| 271 | if (!isHex32(chunk.chunk_id)) { |
| 272 | return { reason: 'missing or invalid chunk_id' }; |
| 273 | } |
| 274 | |
| 275 | if (!chunk.client_sdk) { |
| 276 | return { reason: 'missing client_sdk metadata' }; |
| 277 | } |
| 278 | |
| 279 | // Profile data must have frames, stacks, samples |
| 280 | const profile = chunk.profile as { frames?: unknown[]; stacks?: unknown[]; samples?: unknown[] } | undefined; |
| 281 | if (!profile) { |
| 282 | return { reason: 'missing profile data' }; |
| 283 | } |
| 284 | |
| 285 | if (!Array.isArray(profile.frames) || !profile.frames.length) { |
| 286 | return { reason: 'profile has no frames' }; |
| 287 | } |
| 288 | if (!Array.isArray(profile.stacks) || !profile.stacks.length) { |
| 289 | return { reason: 'profile has no stacks' }; |
| 290 | } |
| 291 | if (!Array.isArray(profile.samples) || !profile.samples.length) { |
| 292 | return { reason: 'profile has no samples' }; |
| 293 | } |
| 294 | |
| 295 | return { valid: true }; |
| 296 | } catch (e) { |
| 297 | return { reason: `unknown validation error: ${e}` }; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Convert from JSSelfProfile format to ContinuousThreadCpuProfile format. |
no test coverage detected