An open API service indexing awesome lists of open source software.

https://github.com/incognitojam/tiny-struct

A TypeScript library for parsing binary data structures
https://github.com/incognitojam/tiny-struct

Last synced: 2 months ago
JSON representation

A TypeScript library for parsing binary data structures

Awesome Lists containing this project

README

          

# tiny-struct

A minimal TypeScript library for parsing and building binary data structures.

## Usage

```typescript
import { struct, bytes, uint32, uint64 } from "tiny-struct";

// Define a struct
const GPTHeader = struct("GPTHeader", {
signature: bytes(8),
revision: uint32(),
headerSize: uint32(),
currentLba: uint64(),
// more fields...
}, {
littleEndian: true
});

// Parse binary data
const header = GPTHeader.from(buffer);
console.log("Signature:", new TextDecoder().decode(header.signature));

// Modify it
header.revision = 0x00020000;
const modifiedBuffer = header.$toBuffer();

// Or create new data
const newBuffer = GPTHeader.to({
signature: new TextEncoder().encode("EFI PART"),
revision: 0x00010000,
headerSize: 92,
currentLba: 1n,
// more fields...
});
```

## License

MIT