Public
Edited
May 4, 2023
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
root = protobuf.parse(source).root
Insert cell
Insert cell
type = root.lookupType("default_package.Foo")
Insert cell
payload = ({ field: "hello" })
Insert cell
// Answer an error message if verification fails, or null if it succeeds.
type.verify(payload)
Insert cell
Insert cell
message = type.create(payload)
Insert cell
Insert cell
buffer = type.encode(message).finish()
Insert cell
Insert cell
decoded = type.decode(buffer)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
awesomeProto = protobuf.Root.fromJSON(JSON.parse(AWESOME_DESCRIPTOR))
Insert cell
protoViz(awesomeProto)
Insert cell
Insert cell
Insert cell
Insert cell
serviceProto = protobuf.parse(SERVICE_DESCRIPTOR)
Insert cell
protoViz(serviceProto.root)
Insert cell
Insert cell
Insert cell
function getEncoding(messageSyntax, messageType, payload) {
const source = `syntax = "proto2";
package default_package;
${messageSyntax}`;

const root = protobuf.parse(source).root;

const topLevelTypes = root.nested.default_package.nested;
const type = topLevelTypes[messageType];
const error = type.verify(payload);

if (error !== null) {
throw new Error(error);
}

const message = type.create(payload);
const buffer = type.encode(message).finish();
console.log(buffer);
return [...buffer].map((n) => n.toString(16).padStart(2, "0")).join(" ");
}
Insert cell
Insert cell
Insert cell
Insert cell
getEncoding(withoutOneOf, "Top", { type: 0 })
Insert cell
getEncoding(withOneOf, "Top", { case: {} })
Insert cell
Insert cell
Insert cell
getEncoding(any, "DocumentStateProto", { subtype: {} })
Insert cell
getEncoding(web2, "DocumentStateProto", { schema: "web-2" })
Insert cell
https://formats.kaitai.io/google_protobuf/
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more