Skip to content

component/struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

struct

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

Installation

$ component install component/struct

Example

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

About

C style structs for reading and writing to Uint8Arrays

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published