Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/component/struct

C style structs for reading and writing to Uint8Arrays
https://github.com/component/struct

Last synced: 13 days ago
JSON representation

C style structs for reading and writing to Uint8Arrays

Awesome Lists containing this project

README

        

# struct

C struct style buffer views for reading and writing structures of varying types.

## Installation

$ component install component/struct

## Example

```js
var struct = require('struct');

// buffer big enough for all 3 structs
var buf = new Uint8Array(3 * (32 + 5 + 16));

var Pet = struct({
id: 'uint32',
name: 'string',
age: 'uint16'
});

// write

var tobi = new Pet({
id: 1,
name: 'Tobi',
age: 2
});

var loki = new Pet({
id: 2,
name: 'Loki',
age: 2
});

var jane = new Pet({
id: 3,
name: 'Jane',
age: 6
});

var off = 0;
var pets = [tobi, loki, jane];
for (var i = 0; i < pets.length; i++) {
off = pets[i].write(buf, off);
}

// read

var n = 3;
var off = 0;
var pet = new Pet;
while (n--) {
off = pet.read(buf, off);
console.log('%s (%s) is %s years old', pet.name, pet.id, pet.age);
}
```

## API

...

## License

MIT