By default, a 2-byte header is serialized with the payload, which is used to identify the format.
You can check headers on raw buffers using peekHeader(buf)
or peekHeaderStr(buf)
:
import { peekHeader, peekHeaderStr } from 'tinybuf'
if (peekHeader(incomingBinary) === MyMessageFormat.header) {
// Do something special.
}
if (peekHeaderStr(incomingBinary) === 'AB') {
// Do something special.
}
const User = defineFormat({
name: Type.String,
age: Type.UInt
})
const Color = defineFormat({
name: Type.String,
hex: Type.UInt
})
User.header === Color.header
// true
You can explicitly set unique headers, as an integer 0 -> 65,535, or a 2-byte string (e.g. 'AB'
).
// integer
const User = defineFormat(123, {
name: Type.String,
age: Type.UInt
})
// 2-byte string
const Color = defineFormat('Co', {
name: Type.String,
hex: Type.UInt
})
User.header === Color.header
// false
e.g. using a const enum
:
const enum Formats {
User,
Color,
}
const User = defineFormat(Formats.User, {
name: Type.String,
age: Type.UInt
})
const Color = defineFormat(Formats.Color, {
name: Type.String,
hex: Type.UInt
})
You can explicitly disable the header by passing null
:
const Color = defineFormat(null, {
name: Type.String,
hex: Type.UInt
})
Caution
Headerless formats cannot be used with bufferParser()
— there's nothing to parse