|
| 1 | +# pcf — Partitioned Container Format (TypeScript implementation) |
| 2 | + |
| 3 | +A TypeScript reader/writer for **PCF v1.0**, a language-agnostic binary |
| 4 | +container that stores multiple independent byte regions ("partitions") in one |
| 5 | +file. |
| 6 | + |
| 7 | +This package is a faithful port of the Rust reference implementation |
| 8 | +(`reference/PCF-v1.0/`) and mirrors the written specification |
| 9 | +(`specs/PCF-spec-v1.0.txt`) field-for-field. It favours auditability over |
| 10 | +performance and produces the **byte-exact** canonical test vector from spec |
| 11 | +section 15. |
| 12 | + |
| 13 | +## Layout |
| 14 | + |
| 15 | +``` |
| 16 | +[ 20-byte header ] [ table block(s) ] [ partition data regions ] |
| 17 | +``` |
| 18 | + |
| 19 | +- **Header** (20 B): magic `0x89 K P R T 0x0D 0x0A 0x1A`, major/minor version, |
| 20 | + absolute offset of the first table block. |
| 21 | +- **Table block**: 74-byte header (`partitionCount`, `nextTableOffset`, |
| 22 | + hash algo + 64-byte block hash) followed by `partitionCount` entries. Blocks |
| 23 | + form a singly linked chain to hold more than 255 partitions. |
| 24 | +- **Entry** (141 B): `type`, 16-byte UID, 32-byte ASCII label, `startOffset`, |
| 25 | + `maxLength`, `usedBytes`, 1-byte data-hash algorithm, 64-byte data hash. |
| 26 | + |
| 27 | +All integers are little-endian. `u64` fields are modelled as `bigint` to |
| 28 | +preserve full 64-bit fidelity; free space is `maxLength - usedBytes`. |
| 29 | + |
| 30 | +## Hash registry |
| 31 | + |
| 32 | +| id | algorithm | id | algorithm | |
| 33 | +|----|------------------|----|-----------| |
| 34 | +| 0 | none | 5 | SHA-1 | |
| 35 | +| 1 | CRC-32/ISO-HDLC | 16 | SHA-256 (default) | |
| 36 | +| 2 | CRC-32C | 17 | SHA-512 | |
| 37 | +| 3 | CRC-64/XZ | 18 | BLAKE3 | |
| 38 | +| 4 | MD5 | | | |
| 39 | + |
| 40 | +Digests are provided by the audited [`@noble/hashes`](https://github.com/paulmillr/noble-hashes) |
| 41 | +package; the three CRC variants are implemented in pure TypeScript |
| 42 | +(`src/crc.ts`). |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +```ts |
| 47 | +import { Container, HashAlgo } from "pcf"; |
| 48 | + |
| 49 | +const c = Container.create(); |
| 50 | +const uid = new Uint8Array(16).fill(1); |
| 51 | +c.addPartition( |
| 52 | + 0x10, |
| 53 | + uid, |
| 54 | + "notes", |
| 55 | + new TextEncoder().encode("hello world"), |
| 56 | + 64, |
| 57 | + HashAlgo.Sha256, |
| 58 | +); |
| 59 | + |
| 60 | +c.verify(); |
| 61 | +const entries = c.entries(); |
| 62 | +const data = c.readPartitionData(entries[0]); |
| 63 | +console.log(new TextDecoder().decode(data)); // "hello world" |
| 64 | +``` |
| 65 | + |
| 66 | +A `Container` is backed by any `Storage`. Two implementations ship with the |
| 67 | +package: |
| 68 | + |
| 69 | +- `MemoryStorage` — an in-memory growable buffer (the default for |
| 70 | + `Container.create()`). |
| 71 | +- `NodeFileStorage` — backed by a Node file descriptor: |
| 72 | + |
| 73 | +```ts |
| 74 | +import { Container, NodeFileStorage, HashAlgo } from "pcf"; |
| 75 | + |
| 76 | +const store = NodeFileStorage.open("container.pcf", /* truncate */ true); |
| 77 | +const c = Container.create(store); |
| 78 | +// … add partitions … |
| 79 | +store.close(); |
| 80 | +``` |
| 81 | + |
| 82 | +## Project layout |
| 83 | + |
| 84 | +``` |
| 85 | +implementations/ts/ |
| 86 | +├── package.json |
| 87 | +├── tsconfig.json |
| 88 | +├── vitest.config.ts |
| 89 | +├── src/ # library sources |
| 90 | +│ ├── consts.ts errors.ts crc.ts hash.ts |
| 91 | +│ ├── header.ts entry.ts table.ts |
| 92 | +│ ├── storage.ts node-storage.ts container.ts |
| 93 | +│ └── index.ts # public re-exports |
| 94 | +├── test/ |
| 95 | +│ ├── roundtrip.test.ts # end-to-end black-box tests |
| 96 | +│ ├── coverage.test.ts # targeted error-path / edge-case tests |
| 97 | +│ └── spec-compliance.test.ts # one test per normative MUST/SHALL |
| 98 | +└── examples/ |
| 99 | + └── gen-testvector.ts # produces the canonical 395-byte spec vector |
| 100 | +``` |
| 101 | + |
| 102 | +## Scripts |
| 103 | + |
| 104 | +Run from this directory: |
| 105 | + |
| 106 | +``` |
| 107 | +npm install # install dependencies |
| 108 | +npm run build # type-check and emit dist/ (tsc, strict) |
| 109 | +npm test # run the full vitest suite |
| 110 | +npm run coverage # vitest + v8 coverage (95% line / 100% function floor) |
| 111 | +npm run gen-testvector # writes pcf_testvector.bin (the 395-byte spec vector) |
| 112 | +``` |
| 113 | + |
| 114 | +CI (`.github/workflows/ts-ci.yml`) runs the type-check/build, the test suite on |
| 115 | +Linux/macOS/Windows, regenerates and size-checks the spec test vector, and |
| 116 | +enforces the coverage floor. |
0 commit comments