{"name":"pbf","version":"4.0.0","keywords":["protocol","buffer","pbf","protobuf","binary","format","serialization","encoder","decoder"],"author":{"name":"Konstantin Kaefer"},"license":"BSD-3-Clause","_id":"pbf@4.0.0","homepage":"https://github.com/mapbox/pbf","bugs":{"url":"https://github.com/mapbox/pbf/issues"},"bin":{"pbf":"bin/pbf"},"dist":{"shasum":"5c4e940d14e84316c6bb1e04ea7088f2d9fb0e83","tarball":"http://registry-npm.ywwl.com/pbf/download/pbf-4.0.0.tgz","fileCount":9,"integrity":"sha512-KrI+xXUZhu0ImmIdT5iiuMAb7nxe3T5eOhlWixYXSStckIDpW7dTYwJhRTmK31R2xTXSIzAuPRREa+PXIPv8nA==","signatures":[{"sig":"MEUCICRdEOOGXVm9YLSruyxkUgZW7TBXAFGyfs131AA0wvoJAiEAkkWxIPf+R4z/W0a26ko8UiV/d6WB7Dx07yc50QRAjNY=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":85802,"size":15008,"noattachment":false,"key":"/pbf/-/pbf-4.0.0.tgz"},"main":"index.js","type":"module","types":"index.d.ts","exports":{".":"./index.js","./compile":"./compile.js"},"gitHead":"6dcee27ffbf9ea58c282e59bb575c5f0e591c0f7","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":"mapbox-npm-07","email":"accounts+npmjs-07@mapbox.com"},"repository":{"url":"git+ssh://git@github.com/mapbox/pbf.git","type":"git"},"_npmVersion":"10.5.1","description":"a low-level, lightweight protocol buffers implementation in JavaScript","directories":{},"_nodeVersion":"20.12.1","dependencies":{"resolve-protobuf-schema":"^2.1.0"},"_hasShrinkwrap":false,"devDependencies":{"eslint":"^9.6.0","rollup":"^4.18.0","benchmark":"^2.1.4","protobufjs":"^7.3.2","typescript":"^5.5.3","protocol-buffers":"^5.0.0","tile-stats-runner":"^1.0.0","@rollup/plugin-terser":"^0.4.4","eslint-config-mourner":"^4.0.1"},"_npmOperationalInternal":{"tmp":"tmp/pbf_4.0.0_1720428293619_0.5257168712828142","host":"s3://npm-registry-packages"},"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 (3KB 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 extremely fast — much faster than native `JSON.parse`/`JSON.stringify`\nand the [protocol-buffers](https://github.com/mafintosh/protocol-buffers) module.\nHere's a result from running a real-world benchmark on Node v6.5\n(decoding and encoding a sample of 439 vector tiles, 22.6 MB total):\n\n- **pbf** decode: 387ms, or 57 MB/s\n- **pbf** encode: 396ms, or 56 MB/s\n- **protocol-buffers** decode: 837ms, or 26 MB/s\n- **protocol-buffers** encode: 4197ms, or 5 MB/s\n- **JSON.parse**: 1540ms, or 125 MB/s (parsing an equivalent 77.5 MB JSON file)\n- **JSON.stringify**: 607ms, or 49 MB/s\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 Pbf from 'pbf';\nimport {readExample, writeExample} from './example.js';\n\n// read\nvar obj = readExample(new Pbf(buffer));\n\n// write\nconst pbf = new Pbf();\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\nvar data = new Pbf(buffer).readFields(readData, {});\n\nfunction readData(tag, data, pbf) {\n    if (tag === 1) data.name = pbf.readString();\n    else if (tag === 2) data.version = pbf.readVarint();\n    else if (tag === 3) data.layer = pbf.readMessage(readLayer, {});\n}\nfunction readLayer(tag, layer, pbf) {\n    if (tag === 1) layer.name = pbf.readString();\n    else if (tag === 3) layer.size = pbf.readVarint();\n}\n```\n\n#### Custom Writing\n\n```js\nvar pbf = new Pbf();\nwriteData(data, pbf);\nvar 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 Pbf 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 Pbf from 'https://cdn.jsdelivr.net/npm/pbf/+esm';\n</script>\n```\n\nAlternatively, there's a browser bundle with a `Pbf` global variable:\n\n```html\n<script src=\"https://cdn.jsdelivr.net/npm/pbf\"></script>\n```\n\n## API\n\nCreate a `Pbf` object, optionally given a `Buffer` or `Uint8Array` as input data:\n\n```js\n// parse a pbf file from disk in Node\nconst pbf = new Pbf(fs.readFileSync('data.pbf'));\n\n// parse a pbf file in a browser after an ajax request with responseType=\"arraybuffer\"\nconst pbf = new Pbf(new Uint8Array(xhr.response));\n```\n\n`Pbf` object properties:\n\n```js\npbf.length; // length of the underlying buffer\npbf.pos; // current offset for reading or writing\n```\n\n#### Reading\n\nRead a sequence of fields:\n\n```js\npbf.readFields((tag) => {\n    if (tag === 1) pbf.readVarint();\n    else if (tag === 2) pbf.readString();\n    else ...\n});\n```\n\nIt optionally accepts an object that will be passed to the reading function for easier construction of decoded data,\nand also passes the `Pbf` object as a third argument:\n\n```js\nconst result = pbf.readFields(readField, {})\n\nfunction readField(tag, result, pbf) {\n    if (tag === 1) result.id = pbf.readVarint();\n}\n```\n\nTo read an embedded message, use `pbf.readMessage(fn[, obj])` (in the same way as `read`).\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, simply save the position instead of reading a value,\nthen later set it back to the saved value and read:\n\n```js\nconst fooPos = -1;\npbf.readFields((tag) => {\n    if (tag === 1) fooPos = pbf.pos;\n});\n...\npbf.pos = fooPos;\npbf.readMessage(readFoo);\n```\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* `skip(value)`\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\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 `Pbf` instance.\n* `write(obj, pbf)` - encodes an object into the given `Pbf` instance (usually empty).\n\nThe resulting code is clean and simple, so it's meant to be customized.\n","publish_time":1720428293750,"maintainers":[{"name":"mbx-npm-ci-production","email":"yannick.meeus+npm+production+ci@mapbox.com"},{"name":"mbx-npm-ci-staging","email":"yannick.meeus+npm+staging+ci@mapbox.com"},{"name":"mbx-npm-advanced-actions-production","email":"yannick.meeus+npm+prod+advanced+actions@mapbox.com"},{"name":"mbx-npm-advanced-actions-staging","email":"yannick.meeus+npm+staging+advanced+actions@mapbox.com"},{"name":"mbx-npm-09-production","email":"yannick.meeus+npm+prod+09@mapbox.com"},{"name":"mbx-npm-08-production","email":"yannick.meeus+npm+prod+08@mapbox.com"},{"name":"mbx-npm-07-production","email":"yannick.meeus+npm+prod+07@mapbox.com"},{"name":"mbx-npm-06-production","email":"yannick.meeus+npm+prod+06@mapbox.com"},{"name":"mbx-npm-05-production","email":"yannick.meeus+npm+prod+05@mapbox.com"},{"name":"mbx-npm-04-production","email":"yannick.meeus+npm+prod+04@mapbox.com"},{"name":"mbx-npm-03-production","email":"yannick.meeus+npm+prod+03@mapbox.com"},{"name":"mbx-npm-02-production","email":"yannick.meeus+npm+prod+02@mapbox.com"},{"name":"mbx-npm-01-production","email":"yannick.meeus+npm+prod+01@mapbox.com"},{"name":"mbx-npm-02-staging","email":"yannick.meeus@mapbox.com"},{"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"}],"_cnpm_publish_time":1720428293750,"dist-tags":{"latest":"5.2.0"}}