import {u8Encode, u8Decode} from "./text.ts";
const MINIPACK_NAME = 1;const MINIPACK_BODY = 4;
export interface DataMap{ name: string; body: Uint8Array;}
export function minipackEncode(files:DataMap[]):Uint8Array{ const archive = new Uint8Array(files.reduce((size, {name, body}) => size + MINIPACK_NAME + MINIPACK_BODY + u8Encode(name).byteLength + body.byteLength, 0));
let i = 0; for(const {name, body} of files){ const u8name = u8Encode(name);
new DataView(archive.buffer, i).setUint8(0, u8name.byteLength); i += MINIPACK_NAME;
new DataView(archive.buffer, i).setUint32(0, body.byteLength); i += MINIPACK_BODY;
archive.set(u8name, i); i += u8name.byteLength;
archive.set(body, i); i += body.byteLength; }
return archive;}
export function minipackDecode(archive:Uint8Array):DataMap[]{ const files:DataMap[] = [];
for(let i = 0; i < archive.byteLength;){ const ns = new DataView(archive.buffer, i).getUint8(0); i += MINIPACK_NAME;
const bs = new DataView(archive.buffer, i).getUint32(0); i += MINIPACK_BODY;
files.push({ name: u8Decode(archive.subarray(i, i += ns)), body: archive.slice(i, i += bs) }); }
return files;}