Example Protocol Buffers file
syntax = "proto3";
message Position {
double lat = 1;
double lon = 2;
sint32 x = 3;
sint32 y = 4;
}
- generate protobuf reader with pbf CLI tool:
> npx pbf example.proto --browser --no-write
- copy/paste generated code to handler
import Pbf from "https://cdn.skypack.dev/[email protected]";
/**
* declare non-standard `self` variable, because it's used in generated code
* @type {any} */
const self = {};
// #region code generated by pbf v3.2.1
// Position ========================================
var Position = self.Position = {};
Position.read = function (pbf, end) {
return pbf.readFields(Position._readField, { lat: 0, lon: 0, x: 0, y: 0 }, end);
};
Position._readField = function (tag, obj, pbf) {
if (tag === 1) obj.lat = pbf.readDouble();
else if (tag === 2) obj.lon = pbf.readDouble();
else if (tag === 3) obj.x = pbf.readSVarint();
else if (tag === 4) obj.y = pbf.readSVarint();
};
// #endregion
/**
* @param {string} payload base64 encoded protobuf
*/
export function process(payload) {
const { buffer } = ric.base64.decode(payload);
const { lat, lon, x, y } = Position.read(new Pbf(buffer));
return { lat, lon, x, y };
}
/**
* @test payload "CflJtU/H30tAEXrHKTqSz0JAGBQgKA=="
*/
🚧