function pcapFileWith
pcapFileWith<THeader, TRecord>(
headerCoder: Coder<THeader>,
recordCoder: Coder<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.

Examples

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]));

Type Parameters

THeader

Decoded shape produced by the header coder.

TRecord

Decoded shape produced by the record coder.

Parameters

headerCoder: Coder<THeader>

Coder for the 24-byte global header.

recordCoder: Coder<TRecord>

Coder for each per-packet record.

Return Type

A coder for a PcapFile carrying the supplied types.

Throws

Propagates whatever the supplied coders throw on malformed input. In particular, a buffer that ends mid-record (16 or more trailing bytes that don't form a complete record) will fail in the record coder, not silently truncate.