How to save returned protobuf object in nodejs?
In my code, a function is returning a protobuf object and I want to save it in a file xyz.pb. When I am trying to save it using fs.writefilesync it is not saving it.
It is circular in nature. So, I tried to save it using circular-json module to confirm if there is anything inside it and it has data.
But, as I used circular-json in the first place it doesn't have the proper information(not properly formatted) and it is of no use.
How can I save this protobuf in a file using nodejs?
you can try to use streams like mentioned in documentation
const crypto = require('crypto'); const fs = require('fs'); const wstream = fs.createWriteStream('fileWithBufferInside'); // creates random Buffer of 100 bytes const buffer = crypto.randomBytes(100); wstream.write(buffer); wstream.end();
or you can convert the buffer to JSON and save it in file as following:
const crypto = require('crypto'); const fs = require('fs'); const wstream = fs.createWriteStream('myBinaryFile'); // creates random Buffer of 100 bytes const buffer = crypto.randomBytes(100); wstream.write(JSON.stringify(buffer)); wstream.end();
and if your application logic doesn't require to use sync nature you should not use
writeFileSync due to it will block your code until it will end so be careful.
try instead using
writeFile or Streams it's more convenient.
The purpose of Protocol Buffers is to serialize strongly typed messages to binary format and back into messages. If you want to write a message from memory into a file, first serialize the message into binary and then write binary to a file.
NodeJS write binary buffer into a file
It should look something like this:
const buffer = messageInstance.serializeBinary() fs.writeFile("filename.pb", buffer, "binary", callback)
I found how to easily save protobuf object in a file.
Convert the protobuf object into buffer and then save it.
const protobuf = somefunction(); // returning protobuf object const buffer = protobuf.toBuffer(); fs.writeFileSync("filename.pb", buffer);
mapbox/pbf: A low-level, lightweight protocol buffers , npm install -g pbf $ pbf example.proto > example.js. Then read and write objects using the module like this: var Pbf = require('pbf'); var Example = require('. Your go-to Node.js Toolbox. Our goal is to help you find the software and libraries you need. Made by developers for developers. The collection of libraries and resources is based on the Awesome Node.js List and direct contributions here. To add a new module, please, check the contribute section.
JSON is Not Cool Anymore: Implementing Protocol Buffers in Node.js, JSON is Not Cool Anymore: Implementing Protocol Buffers in Node.js. JSON is return next() var data =  // List of Buffer objects req.on('data',� The protocol buffer compiler generates accessors for each field in your protocol buffer message. The exact accessors depend on its type and whether it is a singular, repeated, map, or oneof field. Note that the generated accessors always use camel-case naming, even if the field name in the .proto file uses lower-case with underscores ( as it
We have two versions of our route guide example because there are two ways to generate the code needed to work with protocol buffers in Node.js - one approach uses Protobuf.js to dynamically generate the code at runtime, the other uses code statically generated using the protocol buffer compiler protoc. The examples behave identically, and
- It is throwing an error as "serializeBinary is not a function". Do I have to import any module or something for this? Thanks