ntpPacket(): Coder<NtpPacket>
Creates a coder for a fixed 48-byte NTPv4 packet header.
Covers the header only — extension fields and the optional MAC that can
trail an authenticated packet are not part of this coder; use bytes()
with a caller-supplied length to capture them separately.
Decode a known server response
Decode a known server response
import { assertEquals } from "@std/assert"; import { ntpPacket, NTP_MODE, NTP_PACKET_SIZE } from "@binstruct/ntp"; // deno-fmt-ignore const wire = new Uint8Array([ 0x24, // LI=0 VN=4 Mode=4 (server) 0x01, // stratum = 1 (primary reference) 0x04, // poll = 4 0xec, // precision = -20 0x00, 0x00, 0x00, 0x00, // rootDelay = 0 0x00, 0x00, 0x00, 0x0a, // rootDispersion = 10 0x47, 0x50, 0x53, 0x00, // referenceId = "GPS\0" 0xe4, 0xc5, 0xc4, 0x60, 0x00, 0x00, 0x00, 0x00, // referenceTimestamp 0xe4, 0xc5, 0xc4, 0x67, 0x00, 0x00, 0x00, 0x00, // originTimestamp 0xe4, 0xc5, 0xc4, 0x67, 0x00, 0x00, 0x00, 0x00, // receiveTimestamp 0xe4, 0xc5, 0xc4, 0x67, 0x80, 0x00, 0x00, 0x00, // transmitTimestamp ]); const [decoded, read] = ntpPacket().decode(wire); assertEquals(read, NTP_PACKET_SIZE); assertEquals(decoded.leapVersionMode.version, 4); assertEquals(decoded.leapVersionMode.mode, NTP_MODE.SERVER); assertEquals(decoded.stratum, 1); assertEquals(decoded.precision, -20); assertEquals(decoded.rootDispersion, 10); assertEquals(decoded.referenceId, 0x47505300); assertEquals(decoded.transmitTimestamp, 0xe4c5c46780000000n);