The Struct class extends the Uint8Array to represent a structured collection of data as defined in the TLS 1.3 specification. It provides a way to create, manipulate, and manage arrays of Uint8Array or numbers while ensuring proper type validation and concatenation of values.
- Flexible Construction: Create a
Structinstance with an array ofUint8Arrayor numbers. - Automatic Concatenation: When initialized, values are automatically concatenated into a single
Uint8Array. - Member Access: Access the individual components of the
Structvia theitemsproperty. - Static Factory Method: Use
Struct.createFromorStruct.newStructto create instances.
You can create a Struct instance with either Uint8Array instances or numbers:
import{Struct}from'./struct.js';// Using the constructor directlyconststruct1=newStruct(newUint8Array([1,2]),newUint8Array([3]));conststruct2=newStruct(1,2,3);// Converts numbers to Uint8Arrayconsole.log(struct1);// Uint8Array(5) [ 1, 2, 3 ]console.log(struct2.items);// [ Uint8Array(1) [ 1 ], Uint8Array(1) [ 2 ], Uint8Array(1) [ 3 ] ]You can also create a Struct instance using the static methods:
conststruct3=Struct.createFrom(newUint8Array([4,5]),6);conststruct4=Struct.newStruct(7,8,newUint8Array([9]));console.log(struct3.items);// [ Uint8Array(1) [ 4 ], Uint8Array(1) [ 5 ], Uint8Array(1) [ 6 ] ]console.log(struct4.items);// [ Uint8Array(1) [ 7 ], Uint8Array(1) [ 8 ], Uint8Array(1) [ 9 ] ]If no arguments are passed during initialization, a default value of 1 is assigned:
constdefaultStruct=newStruct();console.log(defaultStruct.items);// [ Uint8Array(0) [ 0 ] ]The Constrained class extends the Uint8Array to represent an array with specified minimum and maximum length constraints. It ensures that the data adheres to these constraints during initialization.
- Length Constraints: Specify minimum and maximum lengths for the data.
- Type Validation: Automatically validates that the provided items are either
Uint8Arrayor numbers.
You can create a Constrained instance as follows:
import{Constrained}from'./constrained.js';classTestConstrainedextendsConstrained{}constconstrained=newTestConstrained(2,5,newUint8Array([1,2]),3);console.log(constrained.items);// [ Uint8Array(1) [ 1, 2 ], Uint8Array(1) [ 3 ] ]The constructor will throw errors if the constraints are violated:
try{constinvalidConstrained=newTestConstrained(-1,5);}catch(e){console.error(e);// RangeError: MIN length cannot be negative.}The utils.js file contains several utility functions that assist with the manipulation and validation of Uint8Array data.
ensureUint8Array(...items): Validates that all provided items areUint8Arrayinstances or converts them from numbers.import{ensureUint8Array}from'./utils.js';constresult=ensureUint8Array(newUint8Array([1,2]),3);
concatOctet(...arrays): Concatenates multipleUint8Arrayinstances into a singleUint8Array.import{concatOctet}from'./utils.js';constconcatenated=concatOctet(newUint8Array([1]),newUint8Array([2,3]));
uint8ArrayToValue(uint8array): Converts aUint8Arrayto a numeric value.import{uint8ArrayToValue}from'./utils.js';constvalue=uint8ArrayToValue(newUint8Array([0,0,1,0]));// 256
The type definitions for the Struct, Constrained, and utility functions can be found in the accompanying .d.ts files.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss changes.