Something went wrong. Try again.
STreaming ARchive format: simpler, stricter, smaller than CARs for a subset of use-cases.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176import { describe, expect, test } from 'vitest';
import { type Region, cborRegions, starRegions } from '../src/app/regions.ts';import { type Folding, type StructuredRow, rowIndexAt, structuredRows,} from '../src/app/structured.ts';import { analyzeDrisl } from '../src/drisl/index.ts';import { analyze } from '../src/star/index.ts';import { fromHex, toHex } from './helpers.ts';
const FOLDED: Folding = { unfolded: () => false, reveal: null };
/** Rows as text, two spaces per level: ` 666F6F # "foo"`. */const render = (bytes: Uint8Array, rows: StructuredRow[]): string => rows .map((row) => { const indent = ' '.repeat(row.depth); if (row.kind === 'more') { return `${indent}… +${row.span.end - row.span.start}`; } const hex = toHex(bytes.subarray(row.span.start, row.span.end)); const note = row.first && row.region?.note ? ` # ${row.region.note}` : ''; return `${indent}${hex}${note}`; }) .join('\n');
describe('structured rows', () => { test('nest under the length prefixes and heads that cover them', () => { // {"$type": "example"}, then foo -> bar, hello -> world const bytes = fromHex( '2A6C01000FA1652474797065676578616D706C650103666F6F03626172010568656C6C6F05776F726C6400', ); const { rows, maxDepth } = structuredRows( starRegions(analyze(bytes), bytes, true), bytes.length, FOLDED, ); expect(render(bytes, rows)).toBe( [ '2A6C0100 # magic', '0F # meta length prefix (15)', ' A1 # map(1)', ' 65 # text(5)', ' 2474797065 # "$type"', ' 67 # text(7)', ' 6578616D706C65 # "example"', '01 # data', '03 # key length prefix (3)', ' 666F6F # "foo"', '03 # value length prefix (3)', ' 626172 # "bar"', '01 # data', '05 # key length prefix (5)', ' 68656C6C6F # "hello"', '05 # value length prefix (5)', ' 776F726C64 # "world"', '00 # end', ].join('\n'), ); expect(maxDepth).toBe(3); });
test('a collapsed meta is one run under its length', () => { // {"$type": ""}, no pairs const bytes = fromHex('2A6C010008A16524747970656000'); const { rows } = structuredRows(starRegions(analyze(bytes), bytes), bytes.length, FOLDED); expect(render(bytes, rows)).toBe( [ '2A6C0100 # magic', '08 # meta length prefix (8)', ' A165247479706560 # meta', '00 # end', ].join('\n'), ); });
test('CBOR scalars read as values, and a CID as itself', () => { // {"a": 1, "b": true, "c": CID} // tag 42 around 37 bytes: 00, then a CIDv1 header (dag-cbor, sha-256) and digest const cid = ['D82A', '5825', '00', '01711220', 'AB'.repeat(32)].join(''); const bytes = fromHex(['A3', '6161', '01', '6162', 'F5', '6163', cid].join('')); const root = analyzeDrisl(bytes).root; expect(root).not.toBeNull(); const regions = root ? cborRegions(root, ['item']) : []; const lines = render(bytes, structuredRows(regions, bytes.length, FOLDED).rows).split('\n'); expect(lines.slice(0, 7)).toEqual([ 'A3 # map(3)', ' 61 # text(1)', ' 61 # "a"', ' 01 # 1', ' 61 # text(1)', ' 62 # "b"', ' F5 # true', ]); expect(lines.slice(9, 11)).toEqual([' D82A # tag(42, CID)', ' 5825 # bytes(37)']); expect(lines[11]).toMatch(/^ {6}0001711220[0-9A-F]{6} {2}# bafyrei[a-z2-7]+$/); });
test('bytes outside every region get rows of their own', () => { const region = (start: number, end: number): Region => ({ span: { start, end }, kind: 'data', label: '', note: `${start}-${end}`, }); const { rows } = structuredRows([region(2, 4)], 6, FOLDED); expect(rows.map((r) => [r.span, r.kind === 'bytes' ? r.region?.note : 'more'])).toEqual([ [{ start: 0, end: 2 }, undefined], [{ start: 2, end: 4 }, '2-4'], [{ start: 4, end: 6 }, undefined], ]); });
test('overlapping regions are a bug, not something to draw', () => { const region = (start: number, end: number): Region => ({ span: { start, end }, kind: 'data', label: '', note: '', }); expect(() => structuredRows([region(0, 4), region(2, 6)], 6, FOLDED)).toThrow(/overlaps/); });});
describe('folding long runs', () => { const run = (length: number): Region => ({ span: { start: 0, end: length }, kind: 'value', label: '', note: '', }); const shape = (rows: StructuredRow[]) => rows.map((r) => r.kind === 'more' ? `+${r.span.end - r.span.start}` : r.span.end - r.span.start, );
test('wraps at 8 bytes a line', () => { expect(shape(structuredRows([run(20)], 20, FOLDED).rows)).toEqual([8, 8, 4]); });
test('never folds away just one line', () => { expect(shape(structuredRows([run(32)], 32, FOLDED).rows)).toEqual([8, 8, 8, 8]); });
test('folds after 3 lines', () => { expect(shape(structuredRows([run(40)], 40, FOLDED).rows)).toEqual([8, 8, 8, '+16']); });
test('unfolds on request', () => { const folding = { unfolded: () => true, reveal: null }; expect(shape(structuredRows([run(40)], 40, folding).rows)).toEqual([8, 8, 8, 8, 8]); });
test('unfolds to reveal an offset it would hide', () => { expect(shape(structuredRows([run(40)], 40, { ...FOLDED, reveal: 30 }).rows)).toEqual([ 8, 8, 8, 8, 8, ]); expect(shape(structuredRows([run(40)], 40, { ...FOLDED, reveal: 0 }).rows)).toEqual([ 8, 8, 8, '+16', ]); });
test('finds the row for an offset, folded or not', () => { const { rows } = structuredRows([run(40)], 40, FOLDED); expect(rowIndexAt(rows, 9)).toBe(1); expect(rowIndexAt(rows, 35)).toBe(3); expect(rowIndexAt(rows, 99)).toBe(3); });});