( header: string, fileSize: number )
| 311 | } |
| 312 | |
| 313 | const parseRange = ( |
| 314 | header: string, |
| 315 | fileSize: number |
| 316 | ): |
| 317 | | { readonly start: number; readonly end: number } |
| 318 | | "unsatisfiable" |
| 319 | | undefined => |
| 320 | { |
| 321 | const value = header.trim() |
| 322 | if (!value.toLowerCase().startsWith("bytes=")) { |
| 323 | return undefined |
| 324 | } |
| 325 | const rangeValue = value.slice(6).trim() |
| 326 | if (rangeValue.length === 0 || rangeValue.includes(",")) { |
| 327 | return undefined |
| 328 | } |
| 329 | const separatorIndex = rangeValue.indexOf("-") |
| 330 | if (separatorIndex === -1) { |
| 331 | return undefined |
| 332 | } |
| 333 | const startPart = rangeValue.slice(0, separatorIndex).trim() |
| 334 | const endPart = rangeValue.slice(separatorIndex + 1).trim() |
| 335 | if (startPart === "" && endPart === "") { |
| 336 | return undefined |
| 337 | } |
| 338 | if (startPart === "") { |
| 339 | const suffixLength = parseInteger(endPart) |
| 340 | if (suffixLength === undefined) { |
| 341 | return undefined |
| 342 | } |
| 343 | if (suffixLength === 0 || fileSize === 0) { |
| 344 | return "unsatisfiable" |
| 345 | } |
| 346 | return { |
| 347 | start: Math.max(fileSize - suffixLength, 0), |
| 348 | end: fileSize - 1 |
| 349 | } |
| 350 | } |
| 351 | const start = parseInteger(startPart) |
| 352 | if (start === undefined) { |
| 353 | return undefined |
| 354 | } |
| 355 | if (endPart === "") { |
| 356 | if (start >= fileSize) { |
| 357 | return "unsatisfiable" |
| 358 | } |
| 359 | return { |
| 360 | start, |
| 361 | end: fileSize - 1 |
| 362 | } |
| 363 | } |
| 364 | const end = parseInteger(endPart) |
| 365 | if (end === undefined) { |
| 366 | return undefined |
| 367 | } |
| 368 | if (start > end || start >= fileSize) { |
| 369 | return "unsatisfiable" |
| 370 | } |
no test coverage detected