( cpuCount: number, env: Environment = process.env, )
| 51 | } |
| 52 | |
| 53 | export const getXzCompressionOptions = ( |
| 54 | cpuCount: number, |
| 55 | env: Environment = process.env, |
| 56 | ): { preset: number; threads: number } => { |
| 57 | const parsedPreset = parseIntegerEnv('NOSTREAM_XZ_PRESET', env) |
| 58 | const preset = parsedPreset ?? DEFAULT_XZ_PRESET |
| 59 | |
| 60 | if (preset < MIN_XZ_PRESET || preset > MAX_XZ_PRESET) { |
| 61 | throw new Error( |
| 62 | `Invalid NOSTREAM_XZ_PRESET: ${preset}. Expected an integer between ${MIN_XZ_PRESET} and ${MAX_XZ_PRESET}.`, |
| 63 | ) |
| 64 | } |
| 65 | |
| 66 | const parsedThreadCap = parseIntegerEnv('NOSTREAM_XZ_THREADS', env) |
| 67 | if (parsedThreadCap !== undefined && parsedThreadCap <= 0) { |
| 68 | throw new Error('Invalid NOSTREAM_XZ_THREADS: expected a positive integer.') |
| 69 | } |
| 70 | |
| 71 | // Keep one core available by default to reduce contention with the running relay. |
| 72 | const availableThreads = Math.max(1, Math.max(1, Math.trunc(cpuCount)) - 1) |
| 73 | const maxThreads = parsedThreadCap ?? DEFAULT_MAX_XZ_THREADS |
| 74 | |
| 75 | return { |
| 76 | preset, |
| 77 | threads: Math.max(1, Math.min(availableThreads, maxThreads)), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | export const parseCompressionFormat = (input: string): CompressionFormat => { |
| 82 | switch (input.trim().toLowerCase()) { |
no test coverage detected