Skip to main content

Schema Definition

Binary data encoding/decoding in Byteform is done using schemas. A schema is a set of rules that define how data is encoded and decoded. Schemas are defined using a simple, human-readable syntax:

import { Struct, List, Text, f32, u8 } from '@evenstar/byteform';

const schema = new Struct({
name: new Text(32), // Max 32 bytes
age: u8, // Unsigned 8-bit integer, e.g. number between 0 and 255
scores: new List(f32), // List of 32-bit floating point numbers
});

In the example above, we define a schema with three fields: name, age, and scores.

info

As you might have noticed, some types are defined using a constructor function, while others are defined using a shorthand. This is because some field types have additional configuration options that can be passed to the constructor.

However, looking ahead, constructable types start with the Uppercase letter, while shorthand types start with the lowercase letter.

Byteform provides a set of built-in types that can be used to define schemas. We will cover the different types and their configuration options in the following sections.


Struct

Struct is a collection of fields. It's a synonym for a JavaScript object.

It is used to group multiple fields together. A struct can contain any number of fields of any type.

The constructor takes an object as an argument, where each key-value pair represents a field in the struct. There are a few examples of how to define a struct:

import { Struct, Text, u8 } from '@evenstar/byteform';

/**
* Define a schema with two fields: name and age.
* Similar to JavaScript object: { name: 'Alice', age: 25 }
*/
const schema = new Struct({
name: new Text(32), // Max 32 bytes string
age: u8, // Unsigned 8-bit integer, e.g. number between 0 and 255
});

List

List is a collection of elements of the same type. It's a synonym for a JavaScript array.

Like a struct, list is used to group multiple elements together. A list can contain any number of elements.

The constructor takes a single argument, which is the type of elements in the list. There are a few examples of how to define a list:

import { List, u8 } from '@evenstar/byteform';

/**
* Define a schema with a list of unsigned 8-bit integers.
* Similar to JavaScript array: [0, 1, 2, 3, 4]
*/
const schema = new List(u8); // Unsigned 8-bit integer, e.g. number between 0 and 255

Text

Text is a string of characters. It's a synonym for a JavaScript string.

Text is used to represent strings of characters. Text has a maximum length.

The constructor takes a single argument, which is the maximum byte length of the text.

import { Text } from '@evenstar/byteform';

const schema = new Text(32);

Max Byte Length

For performance reasons, Text type was designed to have a maximum byte length. If you try to encode a string that exceeds the maximum byte length, an error will be thrown.

You are allowed to encode a string that is shorter than the maximum byte length. In this case, only written bytes will be used.

You can also set the maximum byte length manually by providing it as a first argument to the constructor.

import { Text } from '@evenstar/byteform';

const schema = new Text(4098); // Max 4KB

You can read here about how to determine the maximum byte length for a given string.

text

Byteform also provides a shorthand for new Text(256).

import { text } from '@evenstar/byteform';

const schema = text;

Number Types

Byteform provides an extensive set of number types.

Shortcuts

TypeDescriptionSize in bytesValue Range
u8Unsigned 8-bit integer10 to 255
u16Unsigned 16-bit big-endian integer20 to 65535
u32Unsigned 32-bit big-endian integer40 to 4294967295
u64Unsigned 64-bit big-endian integer80 to 2^264 - 1
i8Signed 8-bit integer1-128 to 127
i16Signed 16-bit big-endian integer2-32768 to 32767
i32Signed 32-bit big-endian integer4-2147483648 to 2147483647
i64Signed 64-bit big-endian integer8-2^63 to 2^63 - 1
f3232-bit floating point big-endian number4-3.4e38 to 3.4e38
f6464-bit floating point big-endian number8-1.8e308 to 1.8e308

Endianness

There are also endian-specific versions of the number types:

TypeDescriptionEndianness
u16leUnsigned 16-bit integerlittle-endian
u16beUnsigned 16-bit integerbig-endian
u32leUnsigned 32-bit integerlittle-endian
u32beUnsigned 32-bit integerbig-endian
u64leUnsigned 64-bit integerlittle-endian
u64beUnsigned 64-bit integerbig-endian
i16leSigned 16-bit integerlittle-endian
i16beSigned 16-bit integerbig-endian
i32leSigned 32-bit integerlittle-endian
i32beSigned 32-bit integerbig-endian
i64leSigned 64-bit integerlittle-endian
i64beSigned 64-bit integerbig-endian
f32le32-bit floating pointlittle-endian
f32be32-bit floating pointbig-endian
f64le64-bit floating pointlittle-endian
f64be64-bit floating pointbig-endian