Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
flatbuffers
Advanced tools
FlatBuffers is an efficient cross-platform serialization library for C++, C#, C, Go, Java, JavaScript, TypeScript, PHP, and Python. It allows you to create and access serialized data without parsing/unpacking it, making it ideal for performance-critical applications.
Creating a FlatBuffer
This code demonstrates how to create a FlatBuffer with a string and a vector of strings. The buffer is then finished and converted to a Uint8Array.
const flatbuffers = require('flatbuffers');
const builder = new flatbuffers.Builder(1024);
// Create a string
const str = builder.createString('Hello, FlatBuffers!');
// Create a vector of strings
const strings = builder.createVectorOfStrings(['foo', 'bar', 'baz']);
// Finish the buffer
builder.finish(strings);
// Get the buffer
const buf = builder.asUint8Array();
console.log(buf);
Reading a FlatBuffer
This code demonstrates how to read a FlatBuffer. It assumes that the buffer contains data for a 'Monster' object, and it accesses the 'name' and 'hp' fields of the object.
const flatbuffers = require('flatbuffers');
const MyGame = require('./mygame_generated'); // Generated code
// Assume buf is a Uint8Array containing the FlatBuffer data
const buf = new Uint8Array([/* FlatBuffer data */]);
// Get the root object
const bb = new flatbuffers.ByteBuffer(buf);
const root = MyGame.Example.Monster.getRootAsMonster(bb);
// Access data
console.log(root.name());
console.log(root.hp());
Protobuf.js is a pure JavaScript implementation of Protocol Buffers, a language-neutral, platform-neutral, extensible mechanism for serializing structured data. Compared to FlatBuffers, Protobuf.js requires a parsing step to access data, which can be less efficient in performance-critical applications.
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON but it's faster and smaller. Compared to FlatBuffers, MessagePack is simpler but may not offer the same level of performance for complex data structures.
Avro is a data serialization system that provides rich data structures and a compact, fast, binary data format. The 'avsc' package is a pure JavaScript implementation of Avro. Compared to FlatBuffers, Avro is more focused on schema evolution and data interchange, while FlatBuffers is optimized for performance.
This is an implementation of 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).
Create a schema called example.fbs
:
table Example {
x: float;
y: float;
}
root_type Example;
Generate the binary schema called example.bfbs
:
flatc --binary --schema example.fbs
Install this library:
npm install flatbuffers
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 }
FAQs
Memory Efficient Serialization Library
The npm package flatbuffers receives a total of 183,124 weekly downloads. As such, flatbuffers popularity was classified as popular.
We found that flatbuffers demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.