Something went wrong. Try again.
STreaming ARchive format: simpler, stricter, smaller than CARs for a subset of use-cases.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113import type { Span } from '../drisl/index.ts';import type { Region } from './regions.ts';
/** Bytes on each line of the structured view. */export const BYTES_PER_LINE = 8;/** Lines of a long run shown before the rest is folded away. */export const FOLDED_LINES = 3;
export type StructuredRow = /** One line of a run of bytes. The first line of each run carries its note. */ | { kind: 'bytes'; span: Span; depth: number; region: Region | null; first: boolean } /** The folded rest of a long run: `span` is the hidden part of `run`. */ | { kind: 'more'; span: Span; depth: number; run: Span };
export interface Folding { /** Whether the run at `span` has been unfolded. */ unfolded: (span: Span) => boolean; /** An offset to keep in view, unfolding whichever run would hide it. */ reveal: number | null;}
export interface Structured { rows: StructuredRow[]; maxDepth: number;}
/** * One run of lines per region, in byte order, each indented under the length * prefixes and CBOR heads that cover it. Bytes outside every region get runs * of their own. `regions` must be sorted and non-overlapping. */export const structuredRows = ( regions: Region[], byteLength: number, folding: Folding,): Structured => { const rows: StructuredRow[] = []; let maxDepth = 0; // the spans that framing seen so far covers, innermost last const open: Span[] = [];
const run = (span: Span, region: Region | null) => { for (let top = open.at(-1); top && top.end <= span.start; top = open.at(-1)) { open.pop(); } const depth = open.length; maxDepth = Math.max(maxDepth, depth); const lines = Math.ceil((span.end - span.start) / BYTES_PER_LINE); const foldAt = span.start + FOLDED_LINES * BYTES_PER_LINE; const revealed = folding.reveal !== null && folding.reveal >= foldAt && folding.reveal < span.end; // folding away a single line would only swap it for the line that unfolds it const folded = lines > FOLDED_LINES + 1 && !revealed && !folding.unfolded(span); const shownEnd = folded ? foldAt : span.end; for (let start = span.start; start < shownEnd; start += BYTES_PER_LINE) { const end = Math.min(start + BYTES_PER_LINE, shownEnd); rows.push({ kind: 'bytes', span: { start, end }, depth, region, first: start === span.start, }); } if (folded) { rows.push({ kind: 'more', span: { start: foldAt, end: span.end }, depth, run: span }); } const covers = region?.covers; if (covers && covers.end > covers.start) { open.push(covers); } };
let at = 0; for (const region of regions) { if (region.span.start < at) { throw new Error( `unreachable: region at ${region.span.start} overlaps the one before, to ${at}`, ); } if (region.span.start > at) { run({ start: at, end: region.span.start }, null); } run(region.span, region); at = region.span.end; } if (at < byteLength) { run({ start: at, end: byteLength }, null); } return { rows, maxDepth };};
/** The index of the row holding `offset`, or the nearest row when none does. */export const rowIndexAt = (rows: StructuredRow[], offset: number): number => { let lo = 0; let hi = rows.length - 1; while (lo <= hi) { const mid = (lo + hi) >> 1; const row = rows[mid]; if (row === undefined) { throw new Error(`unreachable: row index ${mid} of ${rows.length}`); } if (offset < row.span.start) { hi = mid - 1; } else if (offset >= row.span.end) { lo = mid + 1; } else { return mid; } } return Math.max(0, Math.min(lo, rows.length - 1));};