Drop CBOR for simple custom manifest
This commit is contained in:
@@ -18,12 +18,12 @@
|
||||
* Offset 8B u64 BE
|
||||
* Length 8B u64 BE
|
||||
* SHA256Digest 32B raw
|
||||
* Manifest: canonical CBOR-encoded map (cborg output from Haskell)
|
||||
* Manifest: fixed-order core + TLV tail (ARBMNFST magic)
|
||||
* Nodes: binary section
|
||||
*/
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
import { decodeCbor } from "./cbor.js";
|
||||
import { decodeManifest } from "./manifest.js";
|
||||
|
||||
// ── Constants ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -173,37 +173,12 @@ export function parseBundle(buffer) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Post-process a CBOR-decoded manifest to normalize hash fields
|
||||
* from raw bytes to hex strings (matching the old JSON wire format).
|
||||
*/
|
||||
function normalizeManifest(raw) {
|
||||
const tree = raw.tree;
|
||||
if (tree && tree.nodeHash && tree.nodeHash.domain) {
|
||||
tree.nodeHash.domain = tree.nodeHash.domain;
|
||||
}
|
||||
|
||||
// Convert root hashes from raw bytes to hex
|
||||
const roots = (raw.roots || []).map((r) => ({
|
||||
...r,
|
||||
hash: r.hash instanceof Uint8Array ? Buffer.from(r.hash).toString("hex") : r.hash,
|
||||
}));
|
||||
|
||||
// Convert export root hashes from raw bytes to hex
|
||||
const exports = (raw.exports || []).map((e) => ({
|
||||
...e,
|
||||
root: e.root instanceof Uint8Array ? Buffer.from(e.root).toString("hex") : e.root,
|
||||
}));
|
||||
|
||||
return { ...raw, roots, exports };
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience: parse and return the manifest from CBOR.
|
||||
* Convenience: parse and return the manifest from the fixed-order binary format.
|
||||
*/
|
||||
export function parseManifest(buffer) {
|
||||
const bundle = parseBundle(buffer);
|
||||
const manifestEntry = bundle.sections.get(SECTION_MANIFEST);
|
||||
return normalizeManifest(decodeCbor(manifestEntry.data));
|
||||
return decodeManifest(manifestEntry.data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
/**
|
||||
* cbor.js — Minimal CBOR decoder for the Arboricx manifest format.
|
||||
*
|
||||
* Decodes the canonical CBOR produced by the Haskell cborg library:
|
||||
* - Maps: major type 5 (0xa0 + length)
|
||||
* - Arrays: major type 4 (0x80 + length)
|
||||
* - Text strings: major type 3, UTF-8 encoded
|
||||
* - Byte strings: major type 2
|
||||
* - Unsigned ints: major type 0
|
||||
* - Simple values: 0xc2 = false, 0xc3 = true
|
||||
*
|
||||
* Only covers the subset needed for the manifest.
|
||||
*/
|
||||
|
||||
// ── Decoding state ──────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* @param {Buffer} data
|
||||
* @returns {number} remaining buffer
|
||||
*/
|
||||
function makeDecoder(data) {
|
||||
let offset = 0;
|
||||
|
||||
return {
|
||||
/** @returns {number} current offset */
|
||||
getPos() { return offset; },
|
||||
|
||||
/** @returns {number} remaining bytes */
|
||||
remaining() { return data.length - offset; },
|
||||
|
||||
/** @returns {number} total length */
|
||||
length() { return data.length; },
|
||||
|
||||
/** Read N bytes and advance */
|
||||
read(n) {
|
||||
if (offset + n > data.length) {
|
||||
throw new Error(`CBOR read: expected ${n} bytes, ${data.length - offset} remaining at offset ${offset}`);
|
||||
}
|
||||
const slice = data.slice(offset, offset + n);
|
||||
offset += n;
|
||||
return slice;
|
||||
},
|
||||
|
||||
/** Read a single byte */
|
||||
readByte() {
|
||||
if (offset >= data.length) {
|
||||
throw new Error(`CBOR readByte: no bytes remaining at offset ${offset}`);
|
||||
}
|
||||
return data[offset++];
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ── CBOR helpers ────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Read a CBOR length (major type initial byte encodes length for values < 24).
|
||||
* For 24+, reads additional bytes per spec.
|
||||
* @returns {number}
|
||||
*/
|
||||
function cborReadLength(dec, startByte) {
|
||||
const additional = startByte & 0x1f;
|
||||
if (additional < 24) return additional;
|
||||
if (additional === 24) return dec.read(1)[0];
|
||||
if (additional === 25) return dec.read(2).readUint16BE(0);
|
||||
if (additional === 26) return dec.read(4).readUint32BE(0);
|
||||
throw new Error(`CBOR: unsupported additional info ${additional}`);
|
||||
}
|
||||
|
||||
// ── Top-level decode ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Decode a single CBOR value from buffer bytes.
|
||||
* @param {Buffer} buf
|
||||
* @returns {*}
|
||||
*/
|
||||
export function decodeCbor(buf) {
|
||||
const dec = makeDecoder(buf);
|
||||
const result = cborDecode(dec);
|
||||
return result;
|
||||
}
|
||||
|
||||
function cborDecode(dec) {
|
||||
const first = dec.readByte();
|
||||
const major = (first >> 5) & 0x07;
|
||||
const info = first & 0x1f;
|
||||
|
||||
switch (major) {
|
||||
case 0: // unsigned int
|
||||
case 1: // negative int
|
||||
return cborReadLength(dec, first);
|
||||
|
||||
case 2: // byte string
|
||||
return dec.read(cborReadLength(dec, first));
|
||||
|
||||
case 3: // text string (UTF-8)
|
||||
const len = cborReadLength(dec, first);
|
||||
return dec.read(len).toString("utf-8");
|
||||
|
||||
case 4: // array
|
||||
const arrLen = cborReadLength(dec, first);
|
||||
const arr = [];
|
||||
for (let i = 0; i < arrLen; i++) {
|
||||
arr.push(cborDecode(dec));
|
||||
}
|
||||
return arr;
|
||||
|
||||
case 5: // map
|
||||
const mapLen = cborReadLength(dec, first);
|
||||
const map = {};
|
||||
for (let i = 0; i < mapLen; i++) {
|
||||
const key = cborDecode(dec);
|
||||
const val = cborDecode(dec);
|
||||
map[key] = val;
|
||||
}
|
||||
return map;
|
||||
|
||||
case 7: // simple values / floats
|
||||
if (info === 20) return false;
|
||||
if (info === 21) return true;
|
||||
if (info === 22) return null; // undefined
|
||||
if (info === 23) return null; // break (shouldn't appear in definite-length)
|
||||
// 0xf9-fb are half/float/double floats — not used by our writer
|
||||
throw new Error(`CBOR: unsupported simple value ${info}`);
|
||||
|
||||
default:
|
||||
// Tags (major 6) and break (0xff) — not used in our manifest
|
||||
throw new Error(`CBOR: unsupported major type ${major}, info ${info}`);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,220 @@
|
||||
/**
|
||||
* manifest.js — Minimal manifest parsing and export lookup.
|
||||
* manifest.js — Fixed-order manifest parsing and export lookup.
|
||||
*
|
||||
* The manifest is a JSON object with fields:
|
||||
* schema, bundleType, tree, runtime, closure, roots, exports,
|
||||
* imports, sections, metadata
|
||||
* The manifest binary format (ManifestV1):
|
||||
* magic(8) + major(u16) + minor(u16)
|
||||
* + schema(string) + bundleType(string)
|
||||
* + treeCalculus(string) + treeHashAlgorithm(string) + treeHashDomain(string) + treeNodePayload(string)
|
||||
* + runtimeSemantics(string) + runtimeEvaluation(string) + runtimeAbi(string)
|
||||
* + capabilityCount(u32) + capabilities(string[])
|
||||
* + closure(u8)
|
||||
* + rootCount(u32) + roots[]
|
||||
* + exportCount(u32) + exports[]
|
||||
* + metadataFieldCount(u32) + metadataTLVs[]
|
||||
* + extensionFieldCount(u32) + extensionTLVs[]
|
||||
*
|
||||
* We parse only what we need for runtime entrypoint selection.
|
||||
* String format: u32 BE length + UTF-8 bytes.
|
||||
* Root: 32 bytes raw hash + role(string).
|
||||
* Export: name(string) + 32 bytes raw root hash + kind(string) + abi(string).
|
||||
* TLV: u16 tag + u32 length + value bytes.
|
||||
*/
|
||||
|
||||
// ── Constants ───────────────────────────────────────────────────────────────
|
||||
|
||||
const MANIFEST_MAGIC = "ARBMNFST";
|
||||
const MANIFEST_MAJOR = 1;
|
||||
const MANIFEST_MINOR = 0;
|
||||
|
||||
// Metadata TLV tags
|
||||
const TAG_PACKAGE = 1;
|
||||
const TAG_VERSION = 2;
|
||||
const TAG_DESCRIPTION = 3;
|
||||
const TAG_LICENSE = 4;
|
||||
const TAG_CREATED_BY = 5;
|
||||
|
||||
// Closure bytes
|
||||
const CLOSURE_COMPLETE = 0;
|
||||
const CLOSURE_PARTIAL = 1;
|
||||
|
||||
// ── Binary helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
function u16(buf, off) {
|
||||
if (off + 2 > buf.length) throw new Error("manifest: not enough bytes for u16");
|
||||
return { value: buf.readUint16BE(off), next: off + 2 };
|
||||
}
|
||||
|
||||
function u32(buf, off) {
|
||||
if (off + 4 > buf.length) throw new Error("manifest: not enough bytes for u32");
|
||||
return { value: buf.readUint32BE(off), next: off + 4 };
|
||||
}
|
||||
|
||||
function u8(buf, off) {
|
||||
if (off >= buf.length) throw new Error("manifest: not enough bytes for u8");
|
||||
return { value: buf.readUint8(off), next: off + 1 };
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a length-prefixed UTF-8 string: u32 BE length + UTF-8 bytes.
|
||||
* Returns { text, next }.
|
||||
*/
|
||||
function readStr(buf, off) {
|
||||
const { value: len, next: afterLen } = u32(buf, off);
|
||||
if (afterLen + len > buf.length) throw new Error("manifest: string extends beyond input");
|
||||
return { text: buf.toString("utf-8", afterLen, afterLen + len), next: afterLen + len };
|
||||
}
|
||||
|
||||
/**
|
||||
* Read raw bytes of given length.
|
||||
* Returns { bytes, next }.
|
||||
*/
|
||||
function readRaw(buf, off, n) {
|
||||
if (off + n > buf.length) throw new Error(`manifest: not enough bytes for ${n}-byte read`);
|
||||
return { value: buf.slice(off, off + n), next: off + n };
|
||||
}
|
||||
|
||||
// ── Manifest decoder ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Decode the manifest binary from a Buffer.
|
||||
*
|
||||
* Returns a normalized manifest object matching the shape expected
|
||||
* by validateManifest / selectExport.
|
||||
*/
|
||||
export function decodeManifest(buf) {
|
||||
let off = 0;
|
||||
|
||||
// Magic (8 bytes)
|
||||
const magic = buf.toString("utf-8", 0, 8);
|
||||
if (magic !== MANIFEST_MAGIC) {
|
||||
throw new Error(`invalid manifest magic: expected ${MANIFEST_MAGIC}, got "${magic}"`);
|
||||
}
|
||||
off = 8;
|
||||
|
||||
// Version
|
||||
const { value: major } = u16(buf, off);
|
||||
if (major !== MANIFEST_MAJOR) throw new Error(`unsupported manifest major version: ${major}`);
|
||||
off += 4; // u16 major + u16 minor
|
||||
|
||||
// Helper: read length-prefixed text
|
||||
const readText = () => {
|
||||
const { text, next } = readStr(buf, off);
|
||||
off = next;
|
||||
return text;
|
||||
};
|
||||
|
||||
// Core strings
|
||||
const schema = readText();
|
||||
const bundleType = readText();
|
||||
const treeCalculus = readText();
|
||||
const treeHashAlgorithm = readText();
|
||||
const treeHashDomain = readText();
|
||||
const treeNodePayload = readText();
|
||||
const runtimeSemantics = readText();
|
||||
const runtimeEvaluation = readText();
|
||||
const runtimeAbi = readText();
|
||||
|
||||
// Capabilities (u32 count + string[])
|
||||
const { value: capCount } = u32(buf, off);
|
||||
off += 4;
|
||||
const capabilities = [];
|
||||
for (let i = 0; i < capCount; i++) {
|
||||
capabilities.push(readText());
|
||||
}
|
||||
|
||||
// Closure (u8)
|
||||
const { value: closureByte } = u8(buf, off);
|
||||
off += 1;
|
||||
const closure = closureByte === CLOSURE_COMPLETE ? "complete" : "partial";
|
||||
|
||||
// Roots (u32 count + Root[])
|
||||
// Root: 32 bytes raw hash + role(string)
|
||||
const { value: rootCount } = u32(buf, off);
|
||||
off += 4;
|
||||
const roots = [];
|
||||
for (let i = 0; i < rootCount; i++) {
|
||||
const { value: hashRaw } = readRaw(buf, off, 32);
|
||||
off += 32;
|
||||
const { text: role, next: rOff } = readStr(buf, off);
|
||||
off = rOff;
|
||||
roots.push({ hash: hashRaw.toString("hex"), role });
|
||||
}
|
||||
|
||||
// Exports (u32 count + Export[])
|
||||
// Export: name(string) + 32 bytes raw root hash + kind(string) + abi(string)
|
||||
const { value: exportCount } = u32(buf, off);
|
||||
off += 4;
|
||||
const exports = [];
|
||||
for (let i = 0; i < exportCount; i++) {
|
||||
const { text: name, next: nOff } = readStr(buf, off);
|
||||
off = nOff;
|
||||
const { value: expHashRaw } = readRaw(buf, off, 32);
|
||||
off += 32;
|
||||
const { text: kind, next: kOff } = readStr(buf, off);
|
||||
off = kOff;
|
||||
const { text: abi, next: aOff } = readStr(buf, off);
|
||||
off = aOff;
|
||||
exports.push({ name, root: expHashRaw.toString("hex"), kind, abi });
|
||||
}
|
||||
|
||||
// Metadata (u32 count + TLV[])
|
||||
// TLV: u16 tag + u32 length + value bytes
|
||||
const { value: metaCount } = u32(buf, off);
|
||||
off += 4;
|
||||
const metadata = {};
|
||||
for (let i = 0; i < metaCount; i++) {
|
||||
const { value: tag } = u16(buf, off);
|
||||
off += 2;
|
||||
const { value: tlvLen } = u32(buf, off);
|
||||
off += 4;
|
||||
const { value: tlvRaw } = readRaw(buf, off, tlvLen);
|
||||
off += tlvLen;
|
||||
const val = tlvRaw.toString("utf-8");
|
||||
switch (tag) {
|
||||
case TAG_PACKAGE: metadata.package = val; break;
|
||||
case TAG_VERSION: metadata.version = val; break;
|
||||
case TAG_DESCRIPTION: metadata.description = val; break;
|
||||
case TAG_LICENSE: metadata.license = val; break;
|
||||
case TAG_CREATED_BY: metadata.createdBy = val; break;
|
||||
}
|
||||
}
|
||||
|
||||
// Extensions (u32 count + TLV[] — skip all)
|
||||
const { value: extCount } = u32(buf, off);
|
||||
off += 4;
|
||||
for (let i = 0; i < extCount; i++) {
|
||||
const { value: _tag } = u16(buf, off);
|
||||
off += 2;
|
||||
const { value: tlvLen } = u32(buf, off);
|
||||
off += 4;
|
||||
off += tlvLen; // skip value
|
||||
}
|
||||
|
||||
return {
|
||||
schema,
|
||||
bundleType,
|
||||
tree: {
|
||||
calculus: treeCalculus,
|
||||
nodeHash: {
|
||||
algorithm: treeHashAlgorithm,
|
||||
domain: treeHashDomain,
|
||||
},
|
||||
nodePayload: treeNodePayload,
|
||||
},
|
||||
runtime: {
|
||||
semantics: runtimeSemantics,
|
||||
evaluation: runtimeEvaluation,
|
||||
abi: runtimeAbi,
|
||||
capabilities,
|
||||
},
|
||||
closure,
|
||||
roots,
|
||||
exports,
|
||||
metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Validation ──────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Validate the manifest against the runtime profile requirements.
|
||||
* Throws on violation.
|
||||
|
||||
Reference in New Issue
Block a user