🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

pbf

Package Overview
Dependencies
Maintainers
20
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pbf - npm Package Compare versions

Comparing version

to
1.3.0

bench/vector_tile.js

115

bench/bench.js
'use strict';
var Pbf = require('../'),
Benchmark = require('benchmark'),
var Benchmark = require('benchmark'),
fs = require('fs'),
protobuf = require('protocol-buffers');
protobuf = require('protocol-buffers'),
vt = require('./vector_tile'),
Pbf = require('../'),
readTile = vt.Tile.read,
writeTile = vt.Tile.write;
var messages = protobuf(fs.readFileSync(__dirname + '/vector_tile.proto')),
var Tile = protobuf(fs.readFileSync(__dirname + '/vector_tile.proto')).Tile,
data = fs.readFileSync(__dirname + '/../test/fixtures/12665.vector.pbf'),
suite = new Benchmark.Suite();
var layers = readLayers(data),
layersJSON = JSON.stringify(layers);
var tile = readTile(new Pbf(data)),
tileJSON = JSON.stringify(tile),
tile2 = Tile.decode(data);
// var layers2 = messages.Tile.decode(data);
writeTile(tile, new Pbf());
suite
.add('decode vector tile with pbf', function() {
readLayers(data);
readTile(new Pbf(data));
})
.add('encode vector tile with pbf', function() {
var pbf = new Pbf();
writeTile(tile, pbf);
pbf.finish();
})
.add('decode vector tile with protocol-buffers', function() {
messages.Tile.decode(data);
Tile.decode(data);
})
.add('native JSON.parse of the same data', function() {
JSON.parse(layersJSON);
.add('encode vector tile with protocol-buffers', function() {
Tile.encode(tile2);
})
.add('encode vector tile with pbf', function() {
encodeLayers(layers);
.add('JSON.parse vector tile', function() {
JSON.parse(tileJSON);
})
// .add('encode vector tile with protocol-buffers', function() {
// messages.Tile.encode(layers2);
// })
.add('native JSON.stringify', function() {
JSON.stringify(layers);
.add('JSON.stringify vector tile', function() {
JSON.stringify(tile);
})

@@ -40,74 +46,1 @@ .on('cycle', function(event) {

.run();
// decoding vector tile
function readLayers(data) {
return new Pbf(data).readFields(readTile, []);
}
function readTile(tag, layers, pbf) {
if (tag === 3) layers.push(pbf.readMessage(readLayer, {features: [], keys: [], values: []}));
}
function readLayer(tag, layer, pbf) {
if (tag === 1) layer.name = pbf.readString();
else if (tag === 2) layer.features.push(pbf.readMessage(readFeature, {}));
else if (tag === 3) layer.keys.push(pbf.readString());
else if (tag === 4) layer.values.push(pbf.readMessage(readValue, {}).value);
else if (tag === 5) layer.extent = pbf.readVarint();
else if (tag === 15) layer.version = pbf.readVarint();
}
function readFeature(tag, feature, pbf) {
if (tag === 1) feature.id = pbf.readVarint();
else if (tag === 2) feature.tags = pbf.readPackedVarint();
else if (tag === 3) feature.type = pbf.readVarint();
else if (tag === 4) feature.geometry = pbf.readPackedVarint();
}
function readValue(tag, value, pbf) {
if (tag === 1) value.value = pbf.readString();
else if (tag === 2) value.value = pbf.readFloat();
else if (tag === 3) value.value = pbf.readDouble();
else if (tag === 4) value.value = pbf.readVarint();
else if (tag === 5) value.value = pbf.readVarint();
else if (tag === 6) value.value = pbf.readSVarint();
else if (tag === 7) value.value = pbf.readBoolean();
}
// encoding vector tile
function encodeLayers(layers) {
var pbf = new Pbf();
for (var i = 0; i < layers.length; i++) {
pbf.writeMessage(3, encodeLayer, layers[i]);
}
return pbf.finish();
}
function encodeLayer(layer, pbf) {
pbf.writeStringField(1, layer.name);
for (var i = 0; i < layer.features.length; i++) {
pbf.writeMessage(2, encodeFeature, layer.features[i]);
}
for (i = 0; i < layer.keys.length; i++) {
pbf.writeStringField(3, layer.keys[i]);
}
for (i = 0; i < layer.values.length; i++) {
pbf.writeMessage(4, encodeValue, layer.values[i]);
}
pbf.writeVarintField(5, layer.extent);
pbf.writeVarintField(15, layer.version);
}
function encodeFeature(feature, pbf) {
pbf.writeVarintField(1, feature.id);
pbf.writePackedVarint(2, feature.tags);
pbf.writeVarintField(3, feature.type);
pbf.writePackedVarint(4, feature.geometry);
}
function encodeValue(value, pbf) {
if (typeof value === 'string') pbf.writeStringField(1, value);
else if (typeof value === 'number') {
if (value % 1 === 0) { // integer
if (value >= 0) pbf.writeVarintField(4, value);
else pbf.writeSVarintField(6, value);
} else pbf.writeDoubleField(3, value);
} else if (typeof value === 'boolean') pbf.writeBooleanField(7, value);
}

@@ -10,2 +10,4 @@ 'use strict';

var BufferMethods;
function Buffer(length) {

@@ -39,3 +41,3 @@ var arr;

var BufferMethods = {
BufferMethods = {
readUInt32LE: function(pos) {

@@ -154,9 +156,7 @@ return ((this[pos]) |

if (c < 0x80) bytes.push(c);
else {
if (c < 0x800) bytes.push(c >> 0x6 | 0xC0, c & 0x3F | 0x80);
else if (c < 0x10000) bytes.push(c >> 0xC | 0xE0, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
else bytes.push(c >> 0x12 | 0xF0, c >> 0xC & 0x3F | 0x80, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
}
else if (c < 0x800) bytes.push(c >> 0x6 | 0xC0, c & 0x3F | 0x80);
else if (c < 0x10000) bytes.push(c >> 0xC | 0xE0, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
else bytes.push(c >> 0x12 | 0xF0, c >> 0xC & 0x3F | 0x80, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
}
return bytes;
}

@@ -180,3 +180,3 @@ 'use strict';

var type = val & 0x7;
if (type === Pbf.Varint) while (this.buf[this.pos++] > 0x7f);
if (type === Pbf.Varint) while (this.buf[this.pos++] > 0x7f) {}
else if (type === Pbf.Bytes) this.pos = this.readVarint() + this.pos;

@@ -199,3 +199,3 @@ else if (type === Pbf.Fixed32) this.pos += 4;

if (length != this.length) {
if (length !== this.length) {
var buf = new Buffer(length);

@@ -250,16 +250,16 @@ this.buf.copy(buf);

this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = (val >>> 7) & 0x7f;
this.buf[this.pos++] = ((val >>> 7) & 0x7f);
} else if (val <= 0x1fffff) {
this.realloc(3);
this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 7) & 0x7f) | 0x80;
this.buf[this.pos++] = (val >>> 14) & 0x7f;
this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 7) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 14) & 0x7f);
} else if (val <= 0xfffffff) {
this.realloc(4);
this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 7) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 7) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 14) & 0x7f) | 0x80;
this.buf[this.pos++] = (val >>> 21) & 0x7f;
this.buf[this.pos++] = ((val >>> 21) & 0x7f);

@@ -275,3 +275,3 @@ } else {

this.buf[this.pos++] = val | 0;
if (this.pos - pos > 10) throw new Error("Given varint doesn't fit into 10 bytes");
if (this.pos - pos > 10) throw new Error('Given varint doesn\'t fit into 10 bytes');
}

@@ -278,0 +278,0 @@ },

{
"name": "pbf",
"version": "1.2.0",
"version": "1.3.0",
"description": "a low-level, lightweight protocol buffers implementation in JavaScript",
"main": "index.js",
"scripts": {
"test": "jshint index.js buffer.js test/*.js && tape test/*.test.js | faucet",
"test": "eslint index.js buffer.js test/*.js bin/pbf && tape test/*.test.js | faucet",
"cov": "istanbul cover tape test/*.test.js && coveralls < ./coverage/lcov.info",
"build-min": "browserify index.js -s Pbf | uglifyjs -c -m > pbf.js",
"build-dev": "browserify index.js -d -s Pbf > pbf-dev.js"
"build-min": "mkdirp dist && browserify index.js -s Pbf | uglifyjs -c warnings=false -m > dist/pbf.js",
"build-dev": "mkdirp dist && browserify index.js -d -s Pbf > dist/pbf-dev.js"
},
"bin": {
"pbf": "bin/pbf"
},
"repository": {

@@ -34,11 +37,5 @@ "type": "git",

"dependencies": {
"ieee754": "~1.1.4"
"ieee754": "~1.1.4",
"resolve-protobuf-schema": "^1.0.2"
},
"jshintConfig": {
"trailing": true,
"undef": true,
"unused": true,
"indent": 4,
"node": true
},
"devDependencies": {

@@ -48,9 +45,47 @@ "benchmark": "^1.0.0",

"coveralls": "~2.11.2",
"eslint": "^0.13.0",
"faucet": "0.0.1",
"istanbul": "~0.3.5",
"jshint": "^2.5.11",
"protocol-buffers": "^2.4.3",
"mkdirp": "^0.5.0",
"protocol-buffers": "^2.4.6",
"tape": "~3.0.3",
"uglify-js": "^2.4.16"
},
"eslintConfig": {
"rules": {
"no-use-before-define": [
2,
"nofunc"
],
"camelcase": 2,
"space-after-function-name": 2,
"space-in-parens": 2,
"space-before-blocks": 2,
"space-after-keywords": 2,
"comma-style": 2,
"no-lonely-if": 2,
"no-else-return": 2,
"new-cap": 2,
"space-in-brackets": 2,
"quotes": [
2,
"single"
],
"no-new": 2,
"no-empty": 0,
"key-spacing": 0,
"no-multi-spaces": 0,
"no-underscore-dangle": 0,
"no-shadow": 0,
"curly": 0,
"no-constant-condition": 0
},
"env": {
"node": true,
"browser": true
},
"globals": {
"Buffer": true
}
}
}

@@ -7,11 +7,27 @@ # pbf

Designed to be a building block for writing customized decoders and encoders for a stable protobuf schema.
Designed to be a building block for writing customized decoders and encoders.
If you need an 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).
take a look at [protocol-buffers](https://github.com/mafintosh/protocol-buffers) too.
## Example
## Examples
#### Reading
#### Using Compiled Code
Install `pbf` and compile a JavaScript module from a `.proto` file:
```bash
$ npm install -g pbf
$ pbf test.proto > test.js
```
Then read and write objects using the module like this:
```js
var obj = Test.read(new Pbf(buffer)); // read
var buffer = Test.write(obj, new Pbf()); // write
```
#### Custom Reading
```js
var data = new Pbf(buffer).readFields(readData, {});

@@ -30,13 +46,13 @@

#### Writing
#### Custom Writing
```js
var buffer = writeData(data);
var pbf = new Pbf();
writeData(data, pbf);
var buffer = pbf.finish();
function writeData(data) {
var pbf = new Pbf();
function writeData(data, pbf) {
pbf.writeStringField(1, data.name);
pbf.writeVarintField(2, data.version);
pbf.writeMessage(3, writeLayer, data.layer);
return pbf.finish();
}

@@ -49,2 +65,3 @@ function writeLayer(layer, pbf) {

## Install

@@ -62,4 +79,4 @@

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

@@ -69,9 +86,13 @@

Create a Protobuf object, optionally given a `Buffer` as input data (or `Uint8Array` in browsers):
Create a `Pbf` object, optionally given a `Buffer` or `Uint8Array` as input data:
```js
var pbf = Protobuf(/*Buffer or Uint8Array*/ buf);
// parse a pbf file from disk in Node
var pbf = new Pbf(fs.readFileSync('data.pbf'));
// parse a pbf file in a browser after an ajax request with responseType="arraybuffer"
var pbf = new Pbf(new Uint8Array(xhr.response));
```
Protobuf object properties:
`Pbf` object properties:

@@ -96,3 +117,3 @@ ```js

It optionally accepts an object that will be passed to the reading function for easier construction of decoded data,
and also passes the Protobuf object as a third argument:
and also passes the `Pbf` object as a third argument:

@@ -113,3 +134,4 @@ ```js

var value = pbf.readVarint();
var packed = pbf.readPacked('UInt32');
var str = pbf.readString();
var numbers = pbf.readPackedVarint();
```

@@ -143,4 +165,2 @@

* `readBytes()`
* `readString()`
* `readBytes()`
* `skip(value)`

@@ -216,3 +236,3 @@

* `writeString(val)`
* `writeBytes(buffer[, start, end])`
* `writeBytes(buffer)`

@@ -228,4 +248,28 @@ Misc methods:

## Proto Schema to JavaScript
If installed globally, `pbf` provides a binary that compiles `proto` files into JavaScript modules. Usage:
```bash
$ pbf <proto_path> [--no-write] [--no-read] [--browser]
```
The `--no-write` and `--no-read` switches remove corresponding code in the output.
The `--browser` switch makes the module work in browsers instead of Node.
The resulting module exports each message by name with the following methods:
* `read(pbf)` - decodes an object from the given `Pbf` instance
* `write(obj, pbf)` - encodes an object into the given `Pbf` instance (usually empty)
The resulting code is pretty short and easy to understand, so you can customize it easily.
## Changelog
#### 1.3.0 (Feb 5, 2015)
- Added `pbf` binary that compiles `.proto` files into `Pbf`-based JavaScript modules.
#### 1.2.0 (Jan 5, 2015)

@@ -232,0 +276,0 @@

@@ -6,2 +6,4 @@ 'use strict';

/*eslint comma-spacing: 0*/
function toArray(buf) {

@@ -8,0 +10,0 @@ var arr = [];

@@ -5,2 +5,3 @@ 'use strict';

fs = require('fs'),
path = require('path'),
test = require('tape').test;

@@ -10,2 +11,4 @@

/*eslint comma-spacing: 0*/
function toArray(buf) {

@@ -282,3 +285,3 @@ var arr = [];

test('readFields', function (t) {
var buf = new Pbf(fs.readFileSync(__dirname + '/fixtures/12665.vector.pbf')),
var buf = new Pbf(fs.readFileSync(path.join(__dirname, '/fixtures/12665.vector.pbf'))),
layerOffsets = [],

@@ -288,3 +291,3 @@ foo = {}, res, res2, buf2;

res2 = buf.readFields(function (tag, result, buf) {
if (tag == 3) layerOffsets.push(buf.pos);
if (tag === 3) layerOffsets.push(buf.pos);
res = result;

@@ -305,3 +308,3 @@ buf2 = buf;

test('readMessage', function (t) {
var buf = new Pbf(fs.readFileSync(__dirname + '/fixtures/12665.vector.pbf')),
var buf = new Pbf(fs.readFileSync(path.join(__dirname, '/fixtures/12665.vector.pbf'))),
layerNames = [],

@@ -311,3 +314,3 @@ foo = {};

buf.readFields(function (tag) {
if (tag == 3) buf.readMessage(readLayer, foo);
if (tag === 3) buf.readMessage(readLayer, foo);
}, foo);

@@ -319,4 +322,4 @@

t.same(layerNames, ["landuse","water","barrier_line","building","tunnel","road",
"place_label","water_label","poi_label","road_label","housenum_label"]);
t.same(layerNames, ['landuse','water','barrier_line','building','tunnel','road',
'place_label','water_label','poi_label','road_label','housenum_label']);

@@ -323,0 +326,0 @@ t.end();

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet