feat(serialize, parse): Number, String, Array, ConstString, ConstArray

This commit is contained in:
2025-08-03 10:54:21 +03:00
commit 26eb3b9068
15 changed files with 2969 additions and 0 deletions

67
types/index.d.ts vendored Normal file
View File

@ -0,0 +1,67 @@
import { memcpy } from "./mem";
import { limits } from "./limits";
import { ConstString, ConstArray } from "./type";
export { memcpy, limits, ConstString, ConstArray }
import { Type } from "./type";
export type SerializableType = NumberConstructor | StringConstructor | ArrayConstructor | Type
export type Serializable = number | string | array
/**
* @param {DataView} dv destination memory
* @param {Serializable} src source object
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @throws {Error} if too small buffer
* @throws {Error} if array|string size is higher than limits.u32.MAX_VALUE
*/
export function serialize(dv: DataView, src: Serializable, ...types: SerializableType[]): void;
/**
* @param {DataView} dv source memory
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @returns {Serializable} parsed object
*/
export function parse(dv: DataView, ...types: SerializableType[]): Serializable;
/**
* some types, like Array, String, has no fixed size. So in Structure they are stored as u32 offset, which points to their beginning
* @param {Serializable} obj to check
* @returns {boolean}
*/
export function isHeadless(obj: Serializable): boolean;
/**
* some types, like Array, String, has no fixed size. So in Structure they are stored as u32 offset, which points to their beginning
* @param {SerializableType[]} ...args primary and inner types. eg: Array, Number
* @returns {boolean}
*/
export function isHeadless(...types: SerializableType[]): boolean;
/**
* if obj has no fixed size, return 4 (sizeof u32 offset)
* @param {Serializable} obj to check
* @returns {number}
*/
export function sizeofHead(obj: Serializable): number;
/**
* if obj has no fixed size, return 4 (sizeof u32 offset)
* @param {SerializableType[]} ...args primary and inner types. eg: Array, Number
* @returns {number}
*/
export function sizeofHead(...types: SerializableType[]): number;
/**
* @param {Serializable} obj to check
* @returns {number}
*/
export function sizeof(obj: Serializable): number;
/**
* @param {SerializableType[]} ...args primary and inner types. eg: Array, Number
* @returns {number}
* @throws {Error} if passed Array or String type (unknown sizeof)
*/
export function sizeof(...types: SerializableType[]): number;

32
types/limits.d.ts vendored Normal file
View File

@ -0,0 +1,32 @@
export interface IntLimit {
MIN_VALUE: number
MAX_VALUE: number
}
export interface BigIntLimit {
MIN_VALUE: bigint
MAX_VALUE: bigint
}
export interface FloatLimit {
MIN_VALUE: number
MAX_VALUE: number
MIN_NORMAL_VALUE: number
MIN_SAFE_INTEGER: number
MAX_SAFE_INTEGER: number
EPSILON: number
}
export const limits: {
i8: IntLimit
i16: IntLimit
i32: IntLimit
i64: BigIntLimit
u8: IntLimit
u16: IntLimit
u32: IntLimit
u64: BigIntLimit
bool: IntLimit
f32: FloatLimit
f64: FloatLimit
};

4
types/mem.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
/**
* copies max possible number of bytes from src to dest (min of both sizes)
*/
export function memcpy(dest: DataView, src: DataView): void;

15
types/type.d.ts vendored Normal file
View File

@ -0,0 +1,15 @@
export type Type = unknown
/**
* constructs type of utf8-string with constant byte size
* @param {number} byte_size max utf8-encoded byte size
* @returns {Type}
*/
export function ConstString(byte_size: number): Type;
/**
* constructs type of array with constant byte size
* @param {number} size number of items
* @returns {Type}
*/
export function ConstArray(size: number): Type;