k6-docs icon indicating copy to clipboard operation
k6-docs copied to clipboard

full reproducible example with protobuf encoded/decoded messages for http and ws APIs

Open ppcano opened this issue 3 years ago • 2 comments

https://community.k6.io/t/protobuf-proto-format-payload-to-rest-endpoints/4369

https://community.k6.io/t/read-protobuf-message-from-websocket-connection/1439

by @mstoykov

ppcano avatar Aug 03 '22 14:08 ppcano

I've played with protobuf.js and can share the following:

  • grabbed the JS file from the latest release (available in the dist folder)
  • it can't be used to read .proto files from the filesystem (behind-the-scenes, it uses fs.readFile)
  • overriding fetch as suggested here would also not work as we can only open during the init context
  • Opening the .proto ahead of time and parsing it like this does appear to work (i.e. no errors):
const proto = open('./awesome.proto');

export default function () {
  const root = protobuf.parse(proto).root;

  // Obtain a message type
  var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");

  // Exemplary payload
  var payload = { awesomeField: "AwesomeString" };

  // Verify the payload if necessary (i.e. when possibly incomplete or invalid)
  var errMsg = AwesomeMessage.verify(payload);
  if (errMsg)
      throw Error(errMsg);

  // Create a new message
  var message = AwesomeMessage.create(payload); // or use .fromObject if conversion is necessary

  // Encode a message to an Uint8Array (browser) or Buffer (node)
  var buffer = AwesomeMessage.encode(message).finish();
  // ... do something with buffer

  // Decode an Uint8Array (browser) or Buffer (node) to a message
  var message = AwesomeMessage.decode(buffer);
  // ... do something with message

  // If the application uses length-delimited buffers, there is also encodeDelimited and decodeDelimited.

  // Maybe convert the message back to a plain object
  var object = AwesomeMessage.toObject(message, {
      longs: String,
      enums: String,
      bytes: String,
      // see ConversionOptions
  });
}

I'll need to find a test server to fully determine whether the messages are indeed in the expected format and that we can decode responses as well.

tom-miseur avatar Aug 03 '22 17:08 tom-miseur

Here's a repo that also includes a (simple) test server: https://github.com/tom-miseur/test-k6-protobuf.js

Things to note:

  • The verify function does not appear to work as expected (you can pass in any payload and it will never end up calling fail).
  • The use of Uint8Array was necessary to prevent trailing 0's from appearing in the payload

tom-miseur avatar Aug 04 '22 14:08 tom-miseur