Public
Edited
Feb 5
Paused
Insert cell
Insert cell
Insert cell
protobuf = require("protobufjs@7.1.2/dist/protobuf.min.js")
Insert cell
Insert cell
Insert cell
Insert cell
root = protobuf.parse(SOURCE).root
Insert cell
Insert cell
User = root.lookupType("profile.User")
Insert cell
UserStatus = root.lookupEnum("profile.UserStatus")
Insert cell
Address = root.lookupType("profile.Address")
Insert cell
Insert cell
payload = ({
id: "4b2e6f31-5035-4864-8e38-e012fd9dc04d",
age: 28,
height: 1.73,
name: "Max Bo",
isVerified: true,
status: UserStatus.values.STATUS_ACTIVE,
address: {
street: "Kippax St",
city: "Sydney",
country: "Australia",
postalCode: "2010"
},
emailAddresses: ["max+observable@maxbo.me", "dontcontactme@example.com"],
metadata: {
blood: "O-"
}
})
Insert cell
Insert cell
// Answer an error message if verification fails, or null if it succeeds.
User.verify(payload)
Insert cell
Insert cell
message = User.create(payload)
Insert cell
Insert cell
buffer = User.encode(message).finish()
Insert cell
Insert cell
Insert cell
binary = new TextDecoder().decode(buffer)
Insert cell
Insert cell
decodedMessage = User.decode(buffer)
Insert cell
Insert cell
object = User.toObject(message)
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
function protoViz(root) {
const collector = [];

const drawArrows = (parent) => {
const branches = [
...Object.values(parent.nested || {}),
...Object.values(parent.fields || {}),
...Object.values(parent.values || {}),
...Object.values(parent.oneofs || {}),
...Object.values(parent.methods || {}),
...(parent.type ? [{ name: parent.type }] : []),
...(parent.requestType ? [{ name: parent.requestType }] : []),
...(parent.responseType ? [{ name: parent.responseType }] : []),

...(parent.oneof || []).map((name) => ({
name
}))
];

for (const child of branches) {
if (parent.name) {
collector.push(`${parent.name} -> ${child.name}`);
}

drawArrows(child);
}
};

drawArrows(root);

return dot`digraph {
rankdir=LR;
${collector.join(";\n")}
}`;
}
Insert cell
Insert cell
Insert cell
serviceProto = protobuf.parse(SOURCE)
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

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