https://github.com/evanw/node-flatbuffers
An implementation of FlatBuffers in pure JavaScript
https://github.com/evanw/node-flatbuffers
Last synced: 10 months ago
JSON representation
An implementation of FlatBuffers in pure JavaScript
- Host: GitHub
- URL: https://github.com/evanw/node-flatbuffers
- Owner: evanw
- License: other
- Created: 2016-03-08T07:44:14.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-01-24T05:58:21.000Z (almost 8 years ago)
- Last Synced: 2025-03-17T12:02:07.898Z (10 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 48
- Watchers: 3
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# FlatBuffers in JavaScript
This is an implementation of [FlatBuffers](https://github.com/google/flatbuffers) in pure JavaScript. Unlike the official compiler, this implementation generates JavaScript code to convert between JavaScript objects and FlatBuffers at run time using the JIT. It currently requires binary schemas compiled with the `flatc` compiler, which is written in C++ and has to be built (see the [build instructions](http://google.github.io/flatbuffers/flatbuffers_guide_building.html)).
### Example Usage
1. Create a schema called `example.fbs`:
```
table Example {
x: float;
y: float;
}
root_type Example;
```
2. Generate the binary schema called `example.bfbs`:
```
flatc --binary --schema example.fbs
```
3. Install this library:
```
npm install flatbuffers
```
4. Use the library:
```
var flatbuffers = require('flatbuffers');
var fs = require('fs');
var example = flatbuffers.compileSchema(fs.readFileSync('example.bfbs'));
var generated = example.generate({ x: 1, y: 2 });
var parsed = example.parse(generated);
console.log('generated:', Array.from(generated));
console.log('parsed:', parsed);
```
Running that code should print this:
```
generated: [ 12, 0, 0, 0, 8, 0, 12, 0, 8, 0, 4, 0, 8, 0, 0, 0, 0, 0, 0, 64, 0, 0, 128, 63 ]
parsed: { x: 1, y: 2 }
```