1

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?

Thanks!

Shubham Chadokar
  • 2,520
  • 1
  • 24
  • 45

4 Answers4

1

you can try to use streams like mentioned in documentation

as following

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.

Mohamed Assem
  • 164
  • 1
  • 9
1

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 Buffer docs

NodeJS write binary buffer into a file

Protocol Buffers JavaScript SDK Docs

It should look something like this:

const buffer = messageInstance.serializeBinary()
fs.writeFile("filename.pb", buffer, "binary", callback)
AJcodez
  • 31,780
  • 20
  • 84
  • 118
1

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);
Shubham Chadokar
  • 2,520
  • 1
  • 24
  • 45
1

If using protobufjs module, finish() method returns the binary array you would save.

const protoWriter = ProtoResource.encode(ProtoResource.fromObject(someJson));
fs.writeFileSync("./resources/output", protoWriter.finish());

Of course, someJson should comply with your ProtoResource message definition.