HTTP Range Header Parser (lib/httpRange.ts)

kimi-code · packages/kap-server/src/lib/httpRange.ts · typescript · MIT
infra, integration, realtime, dev-tools, fw:node, tag:range-header

Parses and validates HTTP Range headers (e.g. 'bytes=0-499') into start/end/length values for partial content responses, and extracts header values from possibly array-typed header objects.

// Source: https://github.com/MoonshotAI/kimi-code | License: MIT | Copyright 2026 Moonshot AI
// Provided via justclaw.it.com; keep this notice with the code.
export function pickHeader(
  headers: Record<string, unknown>,
  name: string,
): string | undefined {
  const v = headers[name];
  if (v === undefined) return undefined;
  return Array.isArray(v) ? (v[0] as string | undefined) : (v as string);
}

export function parseRangeHeader(
  raw: string | undefined,
  size: number,
): { start: number; end: number; length: number } | null {
  if (raw === undefined) return null;
  if (!raw.startsWith('bytes=')) return null;
  const spec = raw.slice('bytes='.length);
  if (spec.includes(',')) return null;
  const dash = spec.indexOf('-');
  if (dash < 0) return null;
  const leftRaw = spec.sli

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key