(version: string | Environment['version'])
| 6 | import { logger } from '../logging'; |
| 7 | |
| 8 | export function getTelemetrySafeVersion(version: string | Environment['version']): string { |
| 9 | if (typeof version === 'string') { |
| 10 | try { |
| 11 | // Split by `.` & take only the first 3 numbers. |
| 12 | // Suffix with '.', so we know we'll always have 3 items in the array. |
| 13 | const [major, minor, patch] = `${version.trim()}...`.split('.').map((item) => parseInt(item, 10)); |
| 14 | if (isNaN(major)) { |
| 15 | return ''; |
| 16 | } else if (isNaN(minor)) { |
| 17 | return major.toString(); |
| 18 | } else if (isNaN(patch)) { |
| 19 | return `${major}.${minor}`; |
| 20 | } |
| 21 | return `${major}.${minor}.${patch}`; |
| 22 | } catch (ex) { |
| 23 | logger.error(`Failed to parse version ${version}`, ex); |
| 24 | return ''; |
| 25 | } |
| 26 | } |
| 27 | if (!version) { |
| 28 | return ''; |
| 29 | } |
| 30 | |
| 31 | return `${version.major}.${version.minor}.${version.micro}`; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Safe way to send data in telemetry (obfuscate PII). |
no test coverage detected