import type { PFrameInternal } from "@milaboratories/pl-model-middle-layer"; import type { IncomingMessage } from "node:http"; export function parseRange(request: IncomingMessage): PFrameInternal.HttpRange | null | undefined { const range = request.headers["range"]; if (range === undefined) return undefined; const match = range.match(/^bytes=(\d*)-(\d*)$/); if (!match) return null; const [, startStr, endStr] = match; const start = startStr ? parseInt(startStr, 10) : null; const end = endStr ? parseInt(endStr, 10) : null; if ( (start !== null && (isNaN(start) || start < 0)) || (end !== null && (isNaN(end) || end < 0)) || (start !== null && end !== null && start > end) ) return null; // Both start and end are specified - bounded range if (start !== null && end !== null) { return { type: "bounded", start, end }; } // Only start is specified - offset range (e.g., bytes=500-) if (start !== null && end === null) { return { type: "offset", offset: start }; } // Only end is specified - suffix range (e.g., bytes=-500) if (start === null && end !== null) { return { type: "suffix", suffix: end }; } // Neither start nor end specified (bytes=-) - invalid return null; }