<commit_before># pbf

[![build status](https://secure.travis-ci.org/mapbox/pbf.png)](http://travis-ci.org/mapbox/pbf) [![Coverage Status](https://coveralls.io/repos/mapbox/pbf/badge.png)](https://coveralls.io/r/mapbox/pbf)

A low-level, lightweight [protocol buffers](https://developers.google.com/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](https://github.com/mafintosh/protocol-buffers).

## Install

Node and Browserify:

```bash
npm install pbf
```

Making a browser build:

```bash
npm install
npm run build-dev # pbf-dev.js (development build)
npm run build-min # pbf.js (minified production build)
```

## Example

```js
var Pbf = require('pbf');
var pbf = new Pbf(buffer);

while (pbf.pos < pbf.length) {
    var val = pbf.readVarint(),
        tag = val >> 3,
        name, version;

    if (tag === 1) name = pbf.readString();
    else if (tag === 2) version = pbf.readVarint();
    else pbf.skip(val);
}
```

## API

Create a buffer:

```js
var pbf = Protobuf(/*Buffer*/ buf);
```

Get buffer length:

```js
pbf.length;
```

#### Reading

Read values:

```js
var value = pbf.readVarint();
var packed = pbf.readPacked('UInt32');
```

Reading methods:

* `readVarint()`
* `readSVarint()`<commit_msg>add read & readMessage docs<commit_after># pbf

[![build status](https://secure.travis-ci.org/mapbox/pbf.png)](http://travis-ci.org/mapbox/pbf) [![Coverage Status](https://coveralls.io/repos/mapbox/pbf/badge.png)](https://coveralls.io/r/mapbox/pbf)

A low-level, lightweight [protocol buffers](https://developers.google.com/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](https://github.com/mafintosh/protocol-buffers).

## Install

Node and Browserify:

```bash
npm install pbf
```

Making a browser build:

```bash
npm install
npm run build-dev # pbf-dev.js (development build)
npm run build-min # pbf.js (minified production build)
```

## Example

```js
var pbf = new Pbf(buffer),
    name, version, layerName;

pbf.read(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;
});
```

## API

Create a buffer:

```js
var pbf = Protobuf(/*Buffer*/ buf);
```

Get buffer length:

```js
pbf.length;
```

#### Reading

Read a sequence of fields:

```js
pbf.read(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:

```js
var value = pbf.readVarint();
var packed = pbf.readPacked('UInt32');
```

Basic reading methods:

* `readVarint()`
* `readSVarint()`
{'fraction_non_alphanumeric': 0.16195451751690226, 'fraction_numerical': 0.003995082974800246, 'mean_word_length': 3.8582089552238807, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4833592', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1115, 'n_words': 326}