function ethernet2Frame
ethernet2Frame(): Coder<Ethernet2Frame>

Creates a coder for Ethernet II frames.

Layout: 6-byte destination MAC, 6-byte source MAC, 2-byte EtherType (big-endian), then variable-length payload (default: "rest of buffer").

Pass parts to override individual sub-coders — for example, hand in your own bytes(N) to fix the payload size, or your own u16be() for etherType so a composer can ref() it for protocol dispatch.

Examples

Basic encoding and decoding

import { assertEquals } from "@std/assert";
import { ethernet2Frame } from "@binstruct/ethernet";

const frameCoder = ethernet2Frame();
const testFrame = {
  dstMac: new Uint8Array([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]),
  srcMac: new Uint8Array([0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB]),
  etherType: 0x0800,
  payload: new Uint8Array([0x45, 0x00, 0x00, 0x14]),
};

const buffer = new Uint8Array(1500);
const bytesWritten = frameCoder.encode(testFrame, buffer);
const [decodedFrame, bytesRead] = frameCoder.decode(buffer);

assertEquals(bytesRead, buffer.length);
assertEquals(bytesWritten, 18);
assertEquals(decodedFrame.dstMac, testFrame.dstMac);
assertEquals(decodedFrame.srcMac, testFrame.srcMac);
assertEquals(decodedFrame.etherType, testFrame.etherType);

Return Type

A Coder<Ethernet2Frame>.

Usage

import { ethernet2Frame } from ".";