pcapFileWith<THeader, TRecord>(): Coder<PcapFile<THeader, TRecord>>
Creates a coder for a complete pcap capture file using the supplied header and record coders.
Records are decoded greedily until fewer than 16 bytes (the record-header size) remain in the buffer; this matches how typical pcap readers consume a file to its end without needing an explicit count.
This is the builder tier of the package's coder API: it deliberately keeps both coders required, because there is no single obvious pair to default to once the caller has opted into custom header or record handling. Callers who just want to read or write an ordinary capture use pcapFile, the zero-argument sibling that supplies the standard pair and picks the byte order for you.
Compose a custom file coder
Compose a custom file coder
import { assertEquals } from "@std/assert"; import { pcapFileWith, pcapGlobalHeader, pcapRecord, PCAP_MAGIC_MICROS, LINKTYPE, } from "@binstruct/pcap"; const file = pcapFileWith(pcapGlobalHeader("le"), pcapRecord("le")); const value = { header: { magic: PCAP_MAGIC_MICROS, versionMajor: 2, versionMinor: 4, thisZone: 0, sigFigs: 0, snapLen: 65535, network: LINKTYPE.ETHERNET, }, records: [ { tsSec: 100, tsUsec: 0, inclLen: 1, origLen: 1, data: new Uint8Array([0xff]), }, ], }; const buffer = new Uint8Array(64); const written = file.encode(value, buffer); const [decoded] = file.decode(buffer.subarray(0, written)); assertEquals(decoded.records.length, 1); assertEquals(decoded.records[0].data, new Uint8Array([0xff]));