{"name":"pbf","version":"4.0.2","keywords":["protocol","buffer","pbf","protobuf","binary","format","serialization","encoder","decoder"],"author":{"name":"Konstantin Kaefer"},"license":"BSD-3-Clause","_id":"pbf@4.0.2","maintainers":[{"name":"mapbox-npm-01","email":"accounts+npmjs-01@mapbox.com"},{"name":"mapbox-npm-02","email":"accounts+npmjs-02@mapbox.com"},{"name":"mapbox-npm-07","email":"accounts+npmjs-07@mapbox.com"},{"name":"mapbox-npm-03","email":"accounts+npmjs-03@mapbox.com"},{"name":"mapbox-npm-04","email":"accounts+npmjs-04@mapbox.com"},{"name":"mapbox-npm-09","email":"accounts+npmjs-09@mapbox.com"},{"name":"mapbox-npm-05","email":"accounts+npmjs-05@mapbox.com"},{"name":"mapbox-npm-06","email":"accounts+npmjs-06@mapbox.com"},{"name":"mapbox-npm-08","email":"accounts+npmjs-08@mapbox.com"},{"name":"mapbox-npm-advanced-actions","email":"accounts+npmjs-advanced-actions@mapbox.com"},{"name":"mapbox-npm-ci","email":"accounts+npmjs-npm-ci@mapbox.com"},{"name":"mapbox-npm","email":"accounts+npmjs@mapbox.com"},{"name":"mapbox-admin","email":"accounts@mapbox.com"},{"name":"mapbox-machine-user","email":"accounts+npm-mapbox-machine-user@mapbox.com"},{"name":"mbx-npm-ci-staging","email":"yannick.meeus+npm+staging+ci@mapbox.com"},{"name":"mbx-npm-ci-production","email":"yannick.meeus+npm+production+ci@mapbox.com"},{"name":"mbx-npm-01-production","email":"yannick.meeus+npm+prod+01@mapbox.com"},{"name":"mbx-npm-02-production","email":"yannick.meeus+npm+prod+02@mapbox.com"},{"name":"mbx-npm-03-production","email":"yannick.meeus+npm+prod+03@mapbox.com"},{"name":"mbx-npm-04-production","email":"yannick.meeus+npm+prod+04@mapbox.com"},{"name":"mbx-npm-05-production","email":"yannick.meeus+npm+prod+05@mapbox.com"},{"name":"mbx-npm-06-production","email":"yannick.meeus+npm+prod+06@mapbox.com"},{"name":"mbx-npm-07-production","email":"yannick.meeus+npm+prod+07@mapbox.com"},{"name":"mbx-npm-08-production","email":"yannick.meeus+npm+prod+08@mapbox.com"},{"name":"mbx-npm-09-production","email":"yannick.meeus+npm+prod+09@mapbox.com"},{"name":"mbx-npm-02-staging","email":"yannick.meeus@mapbox.com"},{"name":"mbx-npm-advanced-actions-staging","email":"yannick.meeus+npm+staging+advanced+actions@mapbox.com"},{"name":"mbx-npm-advanced-actions-production","email":"yannick.meeus+npm+prod+advanced+actions@mapbox.com"}],"homepage":"https://github.com/mapbox/pbf","bugs":{"url":"https://github.com/mapbox/pbf/issues"},"bin":{"pbf":"bin/pbf"},"dist":{"shasum":"70f71a5c4df774438c1db482630146decb03e2d9","tarball":"http://registry-npm.ywwl.com/pbf/download/pbf-4.0.2.tgz","fileCount":9,"integrity":"sha512-J0ajxARhZfpUEebxYs1vhMGMuLSXtBe1e+fFPDrf2uA2hgo+UshKfNUWOz92HJNz6/NFEXseQPddnHkTreWRqg==","signatures":[{"sig":"MEYCIQC/M4uP+XNv5p6LPgscGMZIOjmtXGGiES4bgTn3vPpOagIhAMM1Sd4VjhuJ1kA07ZmEanMtOdpFCYPjiyOfWDffX7ud","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":86576,"size":15300,"noattachment":false,"key":"/pbf/-/pbf-4.0.2.tgz"},"main":"index.js","type":"module","types":"index.d.ts","exports":{".":"./index.js","./compile":"./compile.js"},"gitHead":"aa96d917c801c1b83375ba99d0ff17acb04147f3","scripts":{"cov":"node --test --experimental-test-covetage","test":"tsc && node --test","bench":"node bench/bench.js","build":"rollup -c","pretest":"eslint *.js compile.js test/*.js test/fixtures/*.js bin/pbf","prepublishOnly":"npm run test && npm run build"},"_npmUser":{"name":"mbx-npm-06-production","email":"yannick.meeus+npm+prod+06@mapbox.com"},"repository":{"url":"git+ssh://git@github.com/mapbox/pbf.git","type":"git"},"_npmVersion":"11.3.0","description":"a low-level, lightweight protocol buffers implementation in JavaScript","directories":{},"_nodeVersion":"20.15.1","dependencies":{"resolve-protobuf-schema":"^2.1.0"},"_hasShrinkwrap":false,"devDependencies":{"eslint":"^10.4.0","rollup":"^4.60.4","benchmark":"^2.1.4","protobufjs":"^8.4.2","typescript":"^6.0.3","protocol-buffers":"^5.0.0","@rollup/plugin-terser":"^1.0.0","eslint-config-mourner":"^4.1.0"},"_npmOperationalInternal":{"tmp":"tmp/pbf_4.0.2_1779446391153_0.6634757019732391","host":"s3://npm-registry-packages-npm-production"},"readme":"# pbf\n\n[![Node](https://github.com/mapbox/pbf/actions/workflows/node.yml/badge.svg)](https://github.com/mapbox/pbf/actions/workflows/node.yml)\n![Bundle size](https://img.shields.io/bundlephobia/minzip/pbf)\n\nA low-level, fast, ultra-lightweight (2.5KB gzipped) JavaScript library for decoding and encoding [protocol buffers](https://developers.google.com/protocol-buffers), a compact binary format for structured data serialization. Works both in Node and the browser. Supports lazy decoding and detailed customization of the reading/writing code.\n\n## Performance\n\nThis library is fast — competitive with or faster than other JS protobuf implementations —\nwhile being an order of magnitude smaller. Here's a result from a real-world benchmark on Node v26\n(decoding and encoding 439 Mapbox vector tiles, 37.5 MB total; the equivalent JSON is 136 MB):\n\n|| decode | encode | size (minzip) |\n|---|---|---|---|\n| **pbf** | 195ms, 192 MB/s | 146ms, 257 MB/s | 2.6 KB |\n| [protocol-buffers](https://github.com/mafintosh/protocol-buffers) | 303ms, 124 MB/s | 612ms,  61 MB/s | 14.2 KB\n| [protobuf.js](https://github.com/protobufjs/protobuf.js/) | 192ms, 195 MB/s | 141ms, 266 MB/s | 33.9 KB |\n| JSON | 441ms, 308 MB/s | 363ms, 374 MB/s | — |\n\n`JSON` throughput is measured against the 136 MB JSON payload, not the 37.5 MB pbf payload —\non the same data, pbf is ~2× faster to decode and ~2.5× faster to encode, and produces output\nroughly a quarter the size. See `bench/bench-tiles.js`.\n\n## Examples\n\n#### Using Compiled Code\n\nInstall `pbf` and compile a JavaScript module from a `.proto` file:\n\n```bash\n$ npm install -g pbf\n$ pbf example.proto > example.js\n```\n\nThen read and write objects using the module like this:\n\n```js\nimport {PbfReader, PbfWriter} from 'pbf';\nimport {readExample, writeExample} from './example.js';\n\n// read\nconst obj = readExample(new PbfReader(buffer));\n\n// write\nconst pbf = new PbfWriter();\nwriteExample(obj, pbf);\nconst buffer = pbf.finish();\n```\n\nAlternatively, you can compile a protobuf schema file directly in the code:\n\n```js\nimport {compile} from 'pbf/compile';\nimport schema from 'protocol-buffers-schema';\n\nconst proto = schema.parse(fs.readFileSync('example.proto'));\nconst {readExample, writeExample} = compile(proto);\n```\n\n#### Custom Reading\n\n```js\nconst pbf = new PbfReader(buffer);\nconst data = readData(pbf);\n\nfunction readData(pbf, end) {\n    const data = {};\n    let field;\n    while ((field = pbf.nextField(end))) {\n        if (field === 1) data.name = pbf.readString();\n        else if (field === 2) data.version = pbf.readVarint();\n        else if (field === 3) data.layer = readLayer(pbf, pbf.readVarint() + pbf.pos);\n    }\n    return data;\n}\nfunction readLayer(pbf, end) {\n    const layer = {};\n    let field;\n    while ((field = pbf.nextField(end))) {\n        if (field === 1) layer.name = pbf.readString();\n        else if (field === 3) layer.size = pbf.readVarint();\n    }\n    return layer;\n}\n```\n\n#### Custom Writing\n\n```js\nconst pbf = new PbfWriter();\nwriteData(data, pbf);\nconst buffer = pbf.finish();\n\nfunction writeData(data, pbf) {\n    pbf.writeStringField(1, data.name);\n    pbf.writeVarintField(2, data.version);\n    pbf.writeMessage(3, writeLayer, data.layer);\n}\nfunction writeLayer(layer, pbf) {\n    pbf.writeStringField(1, layer.name);\n    pbf.writeVarintField(2, layer.size);\n}\n```\n\n## Install\n\nInstall using NPM with `npm install pbf`, then import as a module:\n\n```js\nimport {PbfReader, PbfWriter} from 'pbf';\n```\n\nOr use as a module directly in the browser with [jsDelivr](https://www.jsdelivr.com/esm):\n\n```html\n<script type=\"module\">\n    import {PbfReader, PbfWriter} from 'https://cdn.jsdelivr.net/npm/pbf/+esm';\n</script>\n```\n\nAlternatively, there's a browser bundle exposing a `Pbf` global with `PbfReader` and `PbfWriter` properties:\n\n```html\n<script src=\"https://cdn.jsdelivr.net/npm/pbf\"></script>\n```\n\n## API\n\nThe library exposes two classes: `PbfReader` for decoding and `PbfWriter` for encoding. Splitting them lets bundlers tree-shake the half you don't use.\n\nCreate a `PbfReader` from a `Buffer` or `Uint8Array`:\n\n```js\n// parse a pbf file from disk in Node\nconst pbf = new PbfReader(fs.readFileSync('data.pbf'));\n\n// parse a pbf file in a browser after an ajax request with responseType=\"arraybuffer\"\nconst pbf = new PbfReader(new Uint8Array(xhr.response));\n```\n\nBoth classes expose the following properties:\n\n```js\npbf.length; // length of the underlying buffer\npbf.pos; // current offset for reading or writing\n```\n\n#### Reading\n\nLoop over a message's fields with `nextField` and dispatch on the field number. Unrecognized or unread fields are skipped automatically on the next iteration:\n\n```js\nlet field;\nwhile ((field = pbf.nextField(end))) {\n    if (field === 1) obj.id = pbf.readVarint();\n    else if (field === 2) obj.name = pbf.readString();\n}\n```\n\nTo read an embedded message, pass `pbf.readVarint() + pbf.pos` as `end` to a nested reader:\n\n```js\nconst msg = readSubMessage(pbf, pbf.readVarint() + pbf.pos);\n```\n\nRead values:\n\n```js\nconst value = pbf.readVarint();\nconst str = pbf.readString();\nconst numbers = pbf.readPackedVarint();\n```\n\nFor lazy or partial decoding, save the position and come back to it later:\n\n```js\nlet fooPos = -1;\nlet field;\nwhile ((field = pbf.nextField())) {\n    if (field === 1) fooPos = pbf.pos;\n}\n...\npbf.pos = fooPos;\nconst foo = readFoo(pbf, pbf.readVarint() + pbf.pos);\n```\n\nA callback-based `readFields(fn, obj, end)` is also available for backward compatibility, but new code should prefer the `nextField` loop — it's significantly faster.\n\nScalar reading methods:\n\n* `readVarint(isSigned)` (pass `true` if you expect negative varints)\n* `readSVarint()`\n* `readFixed32()`\n* `readFixed64()`\n* `readSFixed32()`\n* `readSFixed64()`\n* `readBoolean()`\n* `readFloat()`\n* `readDouble()`\n* `readString()`\n* `readBytes()`\n\nField iteration methods:\n\n* `nextField(end)` — returns the next field number, or `0` at end-of-message; skips the previous field's value if it wasn't consumed\n* `skip(value)` — skips a field given its raw tag varint\n\nPacked reading methods:\n\n* `readPackedVarint(arr, isSigned)` (appends read items to `arr`)\n* `readPackedSVarint(arr)`\n* `readPackedFixed32(arr)`\n* `readPackedFixed64(arr)`\n* `readPackedSFixed32(arr)`\n* `readPackedSFixed64(arr)`\n* `readPackedBoolean(arr)`\n* `readPackedFloat(arr)`\n* `readPackedDouble(arr)`\n\n#### Writing\n\nCreate a `PbfWriter` (optionally with a pre-allocated `Buffer` or `Uint8Array`):\n\n```js\nconst pbf = new PbfWriter();\n```\n\nWrite values:\n\n```js\npbf.writeVarint(123);\npbf.writeString(\"Hello world\");\n```\n\nWrite an embedded message:\n\n```js\npbf.writeMessage(1, writeObj, obj);\n\nfunction writeObj(obj, pbf) {\n    pbf.writeStringField(obj.name);\n    pbf.writeVarintField(obj.version);\n}\n```\n\nField writing methods:\n\n* `writeVarintField(tag, val)`\n* `writeSVarintField(tag, val)`\n* `writeFixed32Field(tag, val)`\n* `writeFixed64Field(tag, val)`\n* `writeSFixed32Field(tag, val)`\n* `writeSFixed64Field(tag, val)`\n* `writeBooleanField(tag, val)`\n* `writeFloatField(tag, val)`\n* `writeDoubleField(tag, val)`\n* `writeStringField(tag, val)`\n* `writeBytesField(tag, buffer)`\n\nPacked field writing methods:\n\n* `writePackedVarint(tag, val)`\n* `writePackedSVarint(tag, val)`\n* `writePackedSFixed32(tag, val)`\n* `writePackedSFixed64(tag, val)`\n* `writePackedBoolean(tag, val)`\n* `writePackedFloat(tag, val)`\n* `writePackedDouble(tag, val)`\n\nScalar writing methods:\n\n* `writeVarint(val)`\n* `writeSVarint(val)`\n* `writeSFixed32(val)`\n* `writeSFixed64(val)`\n* `writeBoolean(val)`\n* `writeFloat(val)`\n* `writeDouble(val)`\n* `writeString(val)`\n* `writeBytes(buffer)`\n\nMessage writing methods:\n\n* `writeMessage(tag, fn[, obj])`\n* `writeRawMessage(fn[, obj])`\n\nMisc methods:\n\n* `realloc(minBytes)` - pad the underlying buffer size to accommodate the given number of bytes;\n   note that the size increases exponentially, so it won't necessarily equal the size of data written\n* `finish()` - make the current buffer ready for reading and return the data as a buffer slice\n\nFor an example of a real-world usage of the library, see [vector-tile-js](https://github.com/mapbox/vector-tile-js).\n\n\n## Proto Schema to JavaScript\n\nIf installed globally, `pbf` provides a binary that compiles `proto` files into JavaScript modules. Usage:\n\n```bash\n$ pbf <proto_path> [--no-write] [--no-read] [--legacy]\n```\n\nThe `--no-write` and `--no-read` switches remove corresponding code in the output.\nThe `--legacy` switch makes it generate a CommonJS module instead of ESM.\n\n`Pbf` will generate `read<Identifier>` and `write<Identifier>` functions for every message in the schema. For nested messages, their names will be concatenated — e.g. `Message` inside `Test` will produce `readTestMessage` and `writeTestMessage` functions.\n\n\n* `read(pbf)` - decodes an object from the given `PbfReader` instance.\n* `write(obj, pbf)` - encodes an object into the given `PbfWriter` instance (usually empty).\n\nThe resulting code is clean and simple, so it's meant to be customized.\n","publish_time":1779446391315,"_cnpm_publish_time":1779446391315,"dist-tags":{"latest":"5.2.0"}}