Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

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 1.1.3 to 1.1.4

28

buffer.js

@@ -11,6 +11,14 @@ 'use strict';

function Buffer(length) {
var buf = new Uint8Array(length);
var arr;
if (length && length.length) {
arr = length;
length = arr.length;
}
var buf = new Uint8Array(length || 0);
if (arr) buf.set(arr);
for (var i in BufferMethods) {
buf[i] = BufferMethods[i];
}
buf._isBuffer = true;
return buf;

@@ -41,4 +49,4 @@ }

readFloatLE: function(pos) { return ieee754.read(this, pos, true, 23, 4); },
readDoubleLE: function(pos) { return ieee754.read(this, pos, true, 52, 8); },
readFloatLE: function(pos) { return ieee754.read(this, pos, true, 23, 4); },
readDoubleLE: function(pos) { return ieee754.read(this, pos, true, 52, 8); },

@@ -91,8 +99,2 @@ writeFloatLE: function(val, pos) { return ieee754.write(this, val, pos, true, 23, 4); },

Buffer.wrap = function(arr) {
var buf = Buffer(arr.length);
buf.set(arr);
return buf;
};
Buffer.byteLength = function(str) {

@@ -104,2 +106,6 @@ Buffer._lastStr = str;

Buffer.isBuffer = function(buf) {
return !!(buf && buf._isBuffer);
};
function encodeString(str) {

@@ -123,4 +129,4 @@ if (typeof TextEncoder !== 'undefined') return new TextEncoder('utf8').encode(str);

} else {
c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
lead = null;
c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
lead = null;
}

@@ -127,0 +133,0 @@

@@ -8,6 +8,3 @@ 'use strict';

function Protobuf(buf) {
this.buf = !buf ? new Buffer(0) :
!global.Buffer ? Buffer.wrap(buf) :
buf instanceof Uint8Array ? new Buffer(buf) : buf;
this.buf = !Buffer.isBuffer(buf) ? new Buffer(buf || 0) : buf;
this.pos = 0;

@@ -40,3 +37,5 @@ this.length = this.buf.length;

startPos = this.pos;
readField(tag, result, this);
if (this.pos === startPos) this.skip(val);

@@ -48,4 +47,3 @@ }

readMessage: function(readField, result) {
var bytes = this.readVarint();
return this.readFields(readField, result, this.pos + bytes);
return this.readFields(readField, result, this.readVarint() + this.pos);
},

@@ -122,5 +120,5 @@

readString: function() {
var bytes = this.readVarint(),
str = this.buf.toString('utf8', this.pos, this.pos + bytes);
this.pos += bytes;
var end = this.readVarint() + this.pos,
str = this.buf.toString('utf8', this.pos, end);
this.pos = end;
return str;

@@ -130,5 +128,5 @@ },

readBytes: function() {
var bytes = this.readVarint();
var buffer = this.buf.slice(this.pos, this.pos + bytes);
this.pos += bytes;
var end = this.readVarint() + this.pos,
buffer = this.buf.slice(this.pos, end);
this.pos = end;
return buffer;

@@ -138,10 +136,13 @@ },

readPacked: function(type) {
var bytes = this.readVarint();
var end = this.pos + bytes;
var array = [];
var read = this['read' + type];
while (this.pos < end) {
array.push(read.call(this));
}
return array;
var end = this.readVarint() + this.pos,
arr = [];
if (type === 'Varint') while (this.pos < end) arr.push(this.readVarint());
else if (type === 'SVarint') while (this.pos < end) arr.push(this.readSVarint());
else if (type === 'Float') while (this.pos < end) arr.push(this.readFloat());
else if (type === 'Double') while (this.pos < end) arr.push(this.readDouble());
else if (type === 'Fixed32') while (this.pos < end) arr.push(this.readFixed32());
else if (type === 'SFixed32') while (this.pos < end) arr.push(this.readSFixed32());
else if (type === 'Fixed64') while (this.pos < end) arr.push(this.readFixed64());
else if (type === 'SFixed64') while (this.pos < end) arr.push(this.readSFixed64());
return arr;
},

@@ -151,12 +152,5 @@

var type = val & 0x7;
if (type === Protobuf.Varint) {
var buf = this.buf;
while (buf[this.pos++] > 0x7f);
} else if (type === Protobuf.Bytes) {
var bytes = this.readVarint();
this.pos += bytes;
} else if (type === Protobuf.Fixed32) this.pos += 4;
if (type === Protobuf.Varint) while (this.buf[this.pos++] > 0x7f);
else if (type === Protobuf.Bytes) this.pos = this.readVarint() + this.pos;
else if (type === Protobuf.Fixed32) this.pos += 4;
else if (type === Protobuf.Fixed64) this.pos += 8;

@@ -194,11 +188,16 @@ else throw new Error('Unimplemented type: ' + type);

var message = new Protobuf();
var write = message['write' + type];
for (var i = 0; i < items.length; i++) {
write.call(message, items[i]);
}
var data = message.finish();
var message = new Protobuf(),
len = items.length,
i = 0;
this.writeTag(tag, Protobuf.Bytes);
this.writeBytes(data);
if (type === 'Varint') for (; i < len; i++) message.writeVarint(items[i]);
else if (type === 'SVarint') for (; i < len; i++) message.writeSVarint(items[i]);
else if (type === 'Float') for (; i < len; i++) message.writeFloat(items[i]);
else if (type === 'Double') for (; i < len; i++) message.writeDouble(items[i]);
else if (type === 'Fixed32') for (; i < len; i++) message.writeFixed32(items[i]);
else if (type === 'SFixed32') for (; i < len; i++) message.writeSFixed32(items[i]);
else if (type === 'Fixed64') for (; i < len; i++) message.writeFixed64(items[i]);
else if (type === 'SFixed64') for (; i < len; i++) message.writeSFixed64(items[i]);
this.writeMessage(tag, message);
},

@@ -305,9 +304,8 @@

writeBytesField: function(tag, buffer) {
writeMessage: function(tag, protobuf) {
this.writeTag(tag, Protobuf.Bytes);
this.writeBytes(buffer);
this.writeBytes(protobuf.finish());
},
writeMessage: function(tag, protobuf) {
var buffer = protobuf.finish();
writeBytesField: function(tag, buffer) {
this.writeTag(tag, Protobuf.Bytes);

@@ -314,0 +312,0 @@ this.writeBytes(buffer);

{
"name": "pbf",
"version": "1.1.3",
"version": "1.1.4",
"description": "a low-level, lightweight protocol buffers implementation in JavaScript",

@@ -21,3 +21,7 @@ "main": "index.js",

"protobuf",
"binary"
"binary",
"format",
"serialization",
"encoder",
"decoder"
],

@@ -47,6 +51,6 @@ "author": "Konstantin Kaefer",

"jshint": "^2.5.11",
"protocol-buffers": "^2.4.3",
"tape": "~3.0.3",
"uglify-js": "^2.4.16",
"vector-tile": "^0.1.3"
"uglify-js": "^2.4.16"
}
}

@@ -5,24 +5,8 @@ # pbf

A low-level, ultra-lightweight (2.9KB gzipped) [protocol buffers](https://developers.google.com/protocol-buffers) implementation in JavaScript for browsers and Node.
A 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).
Designed to be a building block for writing a customized, lazy decoder for a stable protobuf schema.
Designed to be a building block for writing customized decoders and encoders for a stable protobuf schema.
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).
## 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

@@ -39,7 +23,5 @@

else if (tag === 2) version = pbf.readVarint();
else if (tag === 3) {
pbf.readMessage(function(tag) {
if (tag === 1) layerName = pbf.readString();
});
}
else if (tag === 3) pbf.readMessage(function(tag) {
if (tag === 1) layerName = pbf.readString();
});
});

@@ -64,2 +46,18 @@ ```

## 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)
```
## API

@@ -188,4 +186,8 @@

#### 1.1.3 (Dec 26 2014)
#### 1.1.4 (Jan 2, 2015)
- Significantly improved `readPacked` and `writePacked` performance (the tile reading benchmark is now 70% faster).
#### 1.1.3 (Dec 26, 2014)
Brings tons of improvements and fixes over the previous version (`0.0.2`).

@@ -192,0 +194,0 @@ Basically makes the library complete.

var Pbf = require('../'),
VectorTile = require('vector-tile').VectorTile,
Benchmark = require('benchmark'),
fs = require('fs');
var protobuf = require('protocol-buffers'),
messages = protobuf(fs.readFileSync('../vector-tile-js/proto/vector_tile.proto'));
var suite = new Benchmark.Suite(),
data = fs.readFileSync(__dirname + '/fixtures/12665.vector.pbf');
readTile(); // output any errors before running the suite
readTile(false, true);
var layersJSON = JSON.stringify(readData(data));
suite
.add('read tile with geometries', function() {
readTile(true);
.add('decode vector tile with pbf', function() {
readData(data);
})
.add('read tile without geometries', function() {
readTile();
.add('decode vector tile with protocol-buffers', function() {
messages.tile.decode(data);
})
.add('read tile with packed geometries load', function() {
readTile(false, true);
.add('native JSON.parse of the same data', function() {
JSON.parse(layersJSON);
})

@@ -33,18 +34,34 @@ .add('write varints', function () {

function readData(data) {
return new Pbf(data).readFields(readTile, []);
}
function readTile(loadGeom, loadPacked) {
var buf = new Pbf(data),
vt = new VectorTile(buf);
function readTile(tag, layers, pbf) {
if (tag === 3) layers.push(pbf.readMessage(readLayer, {features: [], keys: [], values: []}));
}
for (var id in vt.layers) {
var layer = vt.layers[id];
for (var i = 0; i < layer.length; i++) {
var feature = layer.feature(i);
if (loadGeom) feature.loadGeometry();
if (loadPacked) {
buf.pos = feature._geometry;
buf.readPacked('SVarint');
}
}
}
function readLayer(tag, layer, pbf) {
if (tag === 15) layer.version = pbf.readVarint();
else 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() || 4096;
}
function readFeature(tag, feature, pbf) {
if (tag === 1) feature.id = pbf.readVarint();
else if (tag === 2) feature.tags = pbf.readPacked('Varint');
else if (tag === 3) feature.type = pbf.readVarint();
else if (tag === 4) feature.geometry = pbf.readPacked('Varint');
}
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();
}

@@ -0,1 +1,3 @@

'use strict';
var BufferShim = require('../buffer'),

@@ -12,2 +14,16 @@ test = require('tape');

test('empty buffer', function (t) {
var buf = new BufferShim();
t.equal(buf.length, 0);
t.end();
});
test('isBuffer', function (t) {
var buf = new BufferShim();
t.equal(BufferShim.isBuffer(buf), true);
t.equal(BufferShim.isBuffer([1, 2, 3]), false);
t.equal(BufferShim.isBuffer(new BufferShim([1, 2, 3])), true);
t.end();
});
test('writeUInt32LE', function (t) {

@@ -105,3 +121,3 @@ var shim = new BufferShim(8);

var arr = new Uint8Array(testBytes);
var shim = BufferShim.wrap(arr);
var shim = new BufferShim(arr);

@@ -118,3 +134,3 @@ t.same(shim.toString(), testStr);

test('copy', function (t) {
var shim = BufferShim.wrap(new Uint8Array(testBytes));
var shim = new BufferShim(new Uint8Array(testBytes));
var shim2 = new BufferShim(22);

@@ -129,5 +145,5 @@

test('slice', function (t) {
var shim = BufferShim.wrap(new Uint8Array(testBytes));
var shim = new BufferShim(new Uint8Array(testBytes));
t.same(toArray(shim.slice(0, 2)), [208, 159]);
t.end();
});

@@ -0,1 +1,3 @@

'use strict';
var Pbf = require('../'),

@@ -116,3 +118,3 @@ fs = require('fs'),

var buf = new Pbf([8, 1, 2, 3, 4, 5, 6, 7, 8]);
t.same(buf.readBytes(), [1, 2, 3, 4, 5, 6, 7, 8]);
t.same(toArray(buf.readBytes()), [1, 2, 3, 4, 5, 6, 7, 8]);
t.end();

@@ -141,8 +143,15 @@ });

t.equal(buf.pos, 0);
buf.writePacked(1, 'Varint', testNumbers);
buf.finish();
buf.readFields(function (tag) {
if (tag === 1) t.same(buf.readPacked('Varint'), testNumbers);
else t.fail('wrong tag encountered: ' + tag);
var testNumbers2 = testNumbers.slice(0, 10);
['Varint', 'SVarint', 'Float', 'Double', 'Fixed32', 'SFixed32', 'Fixed64', 'SFixed64'].forEach(function (type) {
var buf = new Pbf();
buf.writePacked(1, type, testNumbers2);
buf.finish();
buf.readFields(function readField(tag) {
if (tag === 1) t.same(buf.readPacked(type), testNumbers2, 'packed ' + type);
else t.fail('wrong tag encountered: ' + tag);
});
});
t.end();

@@ -149,0 +158,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc