IF Commons GitHub

logical/xdr

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0006-anyfs-serve-nfs FR-001

XDR (RFC 4506) as far as ONC RPC and NFS version 3 need it: a writer that appends 32 and 64-bit unsigned integers, booleans, fixed and variable opaque data and strings, each padded to four bytes, and a reader that takes them back and refuses everything the standard does not allow (a read past the end, a length that does not fit, padding that is not zero, a boolean that is not 0 or 1, a string that is not UTF-8). The reader throws one error type inside and decode turns it into a Result, so that the code that decodes a call reads straight and no failure escapes.

import { decode, XdrWriter } from "./xdr.ts";
const w = new XdrWriter();
w.u32(7).string("a.txt");
decode(w.bytes(), (r) => [r.u32(), r.string()]); // ok([7, "a.txt"])

Source: commons/anyfs-serve/src/logical/xdr.ts

3 of 4 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

XdrErrorclassno test imports it
class XdrError extends Error

The one error of reading: what was wrong with the bytes.

MemberTypeWhat it is
cause?unknown
namestring
messagestring
stack?string
XdrWriterclass
class XdrWriter

Writes XDR.

MemberTypeWhat it is
u32(n: number) => XdrWriter
u64(n: number | bigint) => XdrWriterAn unsigned 64-bit integer from a whole number (or a bigint).
bool(b: boolean) => XdrWriter
fixed(bytes: Uint8Array<ArrayBufferLike>) => XdrWriterBytes as they are, padded: the length is not written.
opaque(bytes: Uint8Array<ArrayBufferLike>) => XdrWriterBytes with their length.
string(text: string) => XdrWriter
bytes() => Uint8Array<ArrayBufferLike>What was written so far.
lengthnumber

Imported by 21 tests of the run (21 passed, 0 failed): the library's results

XdrReaderclass
class XdrReader

Reads XDR. Every read throws XdrError where the bytes are not what was asked.

MemberTypeWhat it is
leftnumber
u32() => number
u64big() => bigintAn unsigned 64-bit integer as a bigint.
u64() => numberAn unsigned 64-bit integer as a number: refused where it is not a safe integer.
bool() => boolean
fixed(length: number) => Uint8Array<ArrayBufferLike>length bytes and their padding, which must be zero.
opaque(max?: number) => Uint8Array<ArrayBufferLike>Bytes with their length, at most max.
string(max?: number) => string
rest() => Uint8Array<ArrayBufferLike>The rest of the bytes, unread.

Imported by 6 tests of the run (6 passed, 0 failed): the library's results

decodefunction
function decode<T>(bytes: Uint8Array<ArrayBufferLike>, read: (r: XdrReader) => T): Result<T, string>

Reads a value with a function over a reader: the value, or why the bytes are not what it reads.

Imported by 6 tests of the run (6 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.