Package Exports
- pbf
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pbf) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
pbf
A low-level, lightweight protocol buffers implementation in JavaScript for Node and browsers.
Designed to be a building block for writing a customized, lazy decoder for a stable protobuf schema. If you need an easy-to-use, all-purpose protobuf JS library that does most of the work for you, take a look at protocol-buffers.
Install
Node and Browserify:
npm install pbf
Making a browser build:
npm install
npm run build-dev # pbf-dev.js (development build)
npm run build-min # pbf.js (minified production build)
Example
Reading:
var pbf = new Pbf(buffer),
name, version, layerName;
pbf.readFields(function(tag) {
if (tag === 1) name = pbf.readString();
else if (tag === 2) version = pbf.readVarint();
else if (tag === 3) {
pbf.readMessage(function(tag) {
if (tag === 1) layerName = pbf.readString();
});
}
return result;
});
Writing:
var pbf = new Pbf();
pbf.writeStringField(1, 'Hello world');
pbf.writeVarintField(2, 300);
var layer = new Pbf();
layer.writeStringField(1, 'foobar');
pbf.writeMessage(3, layer);
var buffer = pbf.finish();
API
Create a buffer:
var pbf = Protobuf(/*Buffer*/ buf);
Get buffer length:
pbf.length;
Reading
Read a sequence of fields:
pbf.readFields(function (tag) {
if (tag === 1) pbf.readVarint();
else if (tag === 2) pbf.readString();
else ...
});
To read an embedded message, use pbf.readMessage(fn)
(in the same way as read
).
Read values:
var value = pbf.readVarint();
var packed = pbf.readPacked('UInt32');
Basic reading methods:
readVarint()
readSVarint()
readFixed32()
readFixed64()
readBoolean()
readFloat()
readDouble()
readString()
readBytes()
readPacked(type)
skip(value)
Writing
Write values:
pbf.writeVarint(123);
pbf.writeString("Hello world");
Writing methods:
writeVarintField(tag, val)
writeSVarintField(tag, val)
writeFixed32Field(tag, val)
writeFixed64Field(tag, val)
writeBooleanField(tag, val)
writeFloatField(tag, val)
writeDoubleField(tag, val)
writeStringField(tag, val)
writeBytesField(tag, buffer)
writePacked(type, tag, items)
writeVarint(val)
writeSVarint(val)
writeFixed32(val)
writeFixed64(val)
writeFloat(val)
writeDouble(val)
writeString(val)
writeBytes(buffer)
writeMessage(tag, pbf)
Misc methods:
realloc(minBytes)
- pad the underlying buffer size to accommodate the given number of bytesfinish()
- make the current buffer ready for reading and return the data as a buffer slicedestroy()
- disposes the buffer
For an example of a real-world usage of the library, see vector-tile-js.