k6-docs
k6-docs copied to clipboard
full reproducible example with protobuf encoded/decoded messages for http and ws APIs
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
I've played with protobuf.js and can share the following:
- grabbed the JS file from the latest release (available in the
distfolder) - it can't be used to read .proto files from the filesystem (behind-the-scenes, it uses fs.readFile)
- overriding
fetchas suggested here would also not work as we can onlyopenduring theinitcontext - 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.
Here's a repo that also includes a (simple) test server: https://github.com/tom-miseur/test-k6-protobuf.js
Things to note:
- The
verifyfunction does not appear to work as expected (you can pass in any payload and it will never end up callingfail). - The use of
Uint8Arraywas necessary to prevent trailing 0's from appearing in the payload