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

https://github.com/streamich/typebase

C/C++ structs in Node.js
https://github.com/streamich/typebase

Last synced: 11 months ago
JSON representation

C/C++ structs in Node.js

Awesome Lists containing this project

README

          

# `struct` for Node.js

Read `docco` docs at [here](http://streamich.github.io/typebase/). `npm` package is [here](https://www.npmjs.com/package/typebase).

Consider a `C/C++` structure:

```c
struct address {
int port,
unsigned char ip[4],
}
```

Define the same binary `struct` in JavaScript and pack/unpack data to `Buffer`:

```js
var t = require('typebase');

var address = t.Struct.define([
['port', t.i32],
['ip', t.List.define(t.ui8, 4)]
]);

var p = new t.Pointer(new Buffer(address.size), 0);
var host = {
port: 8080,
ip: [127, 0, 0, 1]
};
address.pack(p, host);
var unpacked = address.unpack(p);

console.log(unpacked);
```