12 lines
722 B
TypeScript
12 lines
722 B
TypeScript
/** A single immutable complete ZIP can be resumed using a normal GET request. */
|
|
export function parseDownloadByteRange(value: string | null, bytes: number): { start: number; end: number } | null {
|
|
if (!value) return null;
|
|
const match = /^bytes=(\d*)-(\d*)$/.exec(value);
|
|
if (!match || (!match[1] && !match[2]) || bytes < 1) throw new Error("Invalid range");
|
|
const start = match[1] ? Number(match[1]) : Math.max(0, bytes - Number(match[2]));
|
|
const end = match[1] && match[2] ? Math.min(bytes - 1, Number(match[2])) : bytes - 1;
|
|
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 0 || start > end || start >= bytes)
|
|
throw new Error("Unsatisfiable range");
|
|
return { start, end };
|
|
}
|