This commit is contained in:
27
node_modules/pbf/LICENSE
generated
vendored
Normal file
27
node_modules/pbf/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2017, Mapbox
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of pbf nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
452
node_modules/pbf/README.md
generated
vendored
Normal file
452
node_modules/pbf/README.md
generated
vendored
Normal file
@@ -0,0 +1,452 @@
|
||||
# pbf
|
||||
|
||||
[](http://travis-ci.org/mapbox/pbf) [](https://coveralls.io/r/mapbox/pbf)
|
||||
|
||||
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. Works both in Node and the browser. Supports lazy decoding and detailed customization of the reading/writing code.
|
||||
|
||||
## Performance
|
||||
|
||||
This library is extremely fast — much faster than native `JSON.parse`/`JSON.stringify`
|
||||
and the [protocol-buffers](https://github.com/mafintosh/protocol-buffers) module.
|
||||
Here's a result from running a real-world benchmark on Node v6.5
|
||||
(decoding and encoding a sample of 439 vector tiles, 22.6 MB total):
|
||||
|
||||
- **pbf** decode: 387ms, or 57 MB/s
|
||||
- **pbf** encode: 396ms, or 56 MB/s
|
||||
- **protocol-buffers** decode: 837ms, or 26 MB/s
|
||||
- **protocol-buffers** encode: 4197ms, or 5 MB/s
|
||||
- **JSON.parse**: 1540ms, or 125 MB/s (parsing an equivalent 77.5 MB JSON file)
|
||||
- **JSON.stringify**: 607ms, or 49 MB/s
|
||||
|
||||
## Examples
|
||||
|
||||
#### Using Compiled Code
|
||||
|
||||
Install `pbf` and compile a JavaScript module from a `.proto` file:
|
||||
|
||||
```bash
|
||||
$ npm install -g pbf
|
||||
$ pbf example.proto > example.js
|
||||
```
|
||||
|
||||
Then read and write objects using the module like this:
|
||||
|
||||
```js
|
||||
var Pbf = require('pbf');
|
||||
var Example = require('./example.js').Example;
|
||||
|
||||
// read
|
||||
var pbf = new Pbf(buffer);
|
||||
var obj = Example.read(pbf);
|
||||
|
||||
// write
|
||||
var pbf = new Pbf();
|
||||
Example.write(obj, pbf);
|
||||
var buffer = pbf.finish();
|
||||
```
|
||||
|
||||
Alternatively, you can compile a module directly in the code:
|
||||
|
||||
```js
|
||||
var compile = require('pbf/compile');
|
||||
var schema = require('protocol-buffers-schema');
|
||||
|
||||
var proto = schema.parse(fs.readFileSync('example.proto'));
|
||||
var Test = compile(proto).Test;
|
||||
```
|
||||
|
||||
If you use `webpack` as your module bundler, you can use [pbf-loader](https://github.com/trivago/pbf-loader)
|
||||
to load .proto files directly. It returns a compiled module ready to be used.
|
||||
|
||||
Given you already configured your `webpack.config.js`, the code above would look like:
|
||||
```js
|
||||
var Pbf = require('pbf');
|
||||
var proto = require('./example.proto');
|
||||
|
||||
var Test = proto.Test;
|
||||
```
|
||||
|
||||
#### Custom Reading
|
||||
|
||||
```js
|
||||
var data = new Pbf(buffer).readFields(readData, {});
|
||||
|
||||
function readData(tag, data, pbf) {
|
||||
if (tag === 1) data.name = pbf.readString();
|
||||
else if (tag === 2) data.version = pbf.readVarint();
|
||||
else if (tag === 3) data.layer = pbf.readMessage(readLayer, {});
|
||||
}
|
||||
function readLayer(tag, layer, pbf) {
|
||||
if (tag === 1) layer.name = pbf.readString();
|
||||
else if (tag === 3) layer.size = pbf.readVarint();
|
||||
}
|
||||
```
|
||||
|
||||
#### Custom Writing
|
||||
|
||||
```js
|
||||
var pbf = new Pbf();
|
||||
writeData(data, pbf);
|
||||
var buffer = pbf.finish();
|
||||
|
||||
function writeData(data, pbf) {
|
||||
pbf.writeStringField(1, data.name);
|
||||
pbf.writeVarintField(2, data.version);
|
||||
pbf.writeMessage(3, writeLayer, data.layer);
|
||||
}
|
||||
function writeLayer(layer, pbf) {
|
||||
pbf.writeStringField(1, layer.name);
|
||||
pbf.writeVarintField(2, layer.size);
|
||||
}
|
||||
```
|
||||
|
||||
## Install
|
||||
|
||||
Node and Browserify:
|
||||
|
||||
```bash
|
||||
npm install pbf
|
||||
```
|
||||
|
||||
Making a browser build:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
npm run build-dev # dist/pbf-dev.js (development build)
|
||||
npm run build-min # dist/pbf.js (minified production build)
|
||||
```
|
||||
|
||||
CDN link: https://unpkg.com/pbf@3.0.5/dist/pbf.js
|
||||
|
||||
## API
|
||||
|
||||
Create a `Pbf` object, optionally given a `Buffer` or `Uint8Array` as input data:
|
||||
|
||||
```js
|
||||
// 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));
|
||||
```
|
||||
|
||||
`Pbf` object properties:
|
||||
|
||||
```js
|
||||
pbf.length; // length of the underlying buffer
|
||||
pbf.pos; // current offset for reading or writing
|
||||
```
|
||||
|
||||
#### Reading
|
||||
|
||||
Read a sequence of fields:
|
||||
|
||||
```js
|
||||
pbf.readFields(function (tag) {
|
||||
if (tag === 1) pbf.readVarint();
|
||||
else if (tag === 2) pbf.readString();
|
||||
else ...
|
||||
});
|
||||
```
|
||||
|
||||
It optionally accepts an object that will be passed to the reading function for easier construction of decoded data,
|
||||
and also passes the `Pbf` object as a third argument:
|
||||
|
||||
```js
|
||||
var result = pbf.readFields(callback, {})
|
||||
|
||||
function callback(tag, result, pbf) {
|
||||
if (tag === 1) result.id = pbf.readVarint();
|
||||
}
|
||||
```
|
||||
|
||||
To read an embedded message, use `pbf.readMessage(fn[, obj])` (in the same way as `read`).
|
||||
|
||||
Read values:
|
||||
|
||||
```js
|
||||
var value = pbf.readVarint();
|
||||
var str = pbf.readString();
|
||||
var numbers = pbf.readPackedVarint();
|
||||
```
|
||||
|
||||
For lazy or partial decoding, simply save the position instead of reading a value,
|
||||
then later set it back to the saved value and read:
|
||||
|
||||
```js
|
||||
var fooPos = -1;
|
||||
pbf.readFields(function (tag) {
|
||||
if (tag === 1) fooPos = pbf.pos;
|
||||
});
|
||||
...
|
||||
pbf.pos = fooPos;
|
||||
pbf.readMessage(readFoo);
|
||||
```
|
||||
|
||||
Scalar reading methods:
|
||||
|
||||
* `readVarint(isSigned)` (pass `true` if you expect negative varints)
|
||||
* `readSVarint()`
|
||||
* `readFixed32()`
|
||||
* `readFixed64()`
|
||||
* `readSFixed32()`
|
||||
* `readSFixed64()`
|
||||
* `readBoolean()`
|
||||
* `readFloat()`
|
||||
* `readDouble()`
|
||||
* `readString()`
|
||||
* `readBytes()`
|
||||
* `skip(value)`
|
||||
|
||||
Packed reading methods:
|
||||
|
||||
* `readPackedVarint(arr, isSigned)` (appends read items to `arr`)
|
||||
* `readPackedSVarint(arr)`
|
||||
* `readPackedFixed32(arr)`
|
||||
* `readPackedFixed64(arr)`
|
||||
* `readPackedSFixed32(arr)`
|
||||
* `readPackedSFixed64(arr)`
|
||||
* `readPackedBoolean(arr)`
|
||||
* `readPackedFloat(arr)`
|
||||
* `readPackedDouble(arr)`
|
||||
|
||||
#### Writing
|
||||
|
||||
Write values:
|
||||
|
||||
```js
|
||||
pbf.writeVarint(123);
|
||||
pbf.writeString("Hello world");
|
||||
```
|
||||
|
||||
Write an embedded message:
|
||||
|
||||
```js
|
||||
pbf.writeMessage(1, writeObj, obj);
|
||||
|
||||
function writeObj(obj, pbf) {
|
||||
pbf.writeStringField(obj.name);
|
||||
pbf.writeVarintField(obj.version);
|
||||
}
|
||||
```
|
||||
|
||||
Field writing methods:
|
||||
|
||||
* `writeVarintField(tag, val)`
|
||||
* `writeSVarintField(tag, val)`
|
||||
* `writeFixed32Field(tag, val)`
|
||||
* `writeFixed64Field(tag, val)`
|
||||
* `writeSFixed32Field(tag, val)`
|
||||
* `writeSFixed64Field(tag, val)`
|
||||
* `writeBooleanField(tag, val)`
|
||||
* `writeFloatField(tag, val)`
|
||||
* `writeDoubleField(tag, val)`
|
||||
* `writeStringField(tag, val)`
|
||||
* `writeBytesField(tag, buffer)`
|
||||
|
||||
Packed field writing methods:
|
||||
|
||||
* `writePackedVarint(tag, val)`
|
||||
* `writePackedSVarint(tag, val)`
|
||||
* `writePackedSFixed32(tag, val)`
|
||||
* `writePackedSFixed64(tag, val)`
|
||||
* `writePackedBoolean(tag, val)`
|
||||
* `writePackedFloat(tag, val)`
|
||||
* `writePackedDouble(tag, val)`
|
||||
|
||||
Scalar writing methods:
|
||||
|
||||
* `writeVarint(val)`
|
||||
* `writeSVarint(val)`
|
||||
* `writeSFixed32(val)`
|
||||
* `writeSFixed64(val)`
|
||||
* `writeBoolean(val)`
|
||||
* `writeFloat(val)`
|
||||
* `writeDouble(val)`
|
||||
* `writeString(val)`
|
||||
* `writeBytes(buffer)`
|
||||
|
||||
Message writing methods:
|
||||
|
||||
* `writeMessage(tag, fn[, obj])`
|
||||
* `writeRawMessage(fn[, obj])`
|
||||
|
||||
Misc methods:
|
||||
|
||||
* `realloc(minBytes)` - pad the underlying buffer size to accommodate the given number of bytes;
|
||||
note that the size increases exponentially, so it won't necessarily equal the size of data written
|
||||
* `finish()` - make the current buffer ready for reading and return the data as a buffer slice
|
||||
* `destroy()` - dispose the buffer
|
||||
|
||||
For an example of a real-world usage of the library, see [vector-tile-js](https://github.com/mapbox/vector-tile-js).
|
||||
|
||||
|
||||
## 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 clean and simple, so feel free to customize it.
|
||||
|
||||
## Changelog
|
||||
|
||||
#### 3.2.1 (Oct 11, 2019)
|
||||
|
||||
- Significantly improved performance when decoding large strings in the browser.
|
||||
|
||||
#### 3.2.0 (Mar 11, 2019)
|
||||
|
||||
- Improved decoding to be able to parse repeated fields even if they were specified as packed, and vise versa.
|
||||
- Improved packed encoding to skip empty arrays (previously, it would write a tag).
|
||||
- Fixed an off-by-one data corruption bug when writing a message larger than 0x10000000 bytes.
|
||||
|
||||
#### 3.1.0 (Sep 27, 2017)
|
||||
|
||||
- Added support for Protocol Buffer 3 [maps](https://developers.google.com/protocol-buffers/docs/proto3#maps) to proto compiler.
|
||||
|
||||
#### 3.0.5 (Nov 30, 2016)
|
||||
|
||||
- Fixed an error appearing in some versions of IE11 and old Android browsers.
|
||||
|
||||
#### 3.0.4 (Nov 14, 2016)
|
||||
|
||||
- Fixed compiling repeated packed enum fields.
|
||||
|
||||
#### 3.0.3 (Nov 14, 2016)
|
||||
|
||||
- Fixed a regression that broke compiling repeated enum fields with defaults.
|
||||
|
||||
#### 3.0.2 (Sep 30, 2016)
|
||||
|
||||
- Fixed a regression that broke decoding of packed fields with a tag that didn't fit into one byte.
|
||||
|
||||
#### 3.0.1 (Sep 20, 2016)
|
||||
|
||||
- Fixed a regression that broke encoding of long strings.
|
||||
|
||||
#### 3.0.0 (Aug 30, 2016)
|
||||
|
||||
This release include tons of compatibility/robustness fixes, and a more reliable Node implementation. Decoding performance is expected to get up to ~15% slower than v2.0 in Node (browsers are unaffected), but encoding got faster by ~15% in return.
|
||||
|
||||
##### Encoder/decoder
|
||||
|
||||
- **Breaking**: changed Node implementation to use `Uint8Array` instead of `Buffer` internally (and produce corresponding result on `finish()`), making it fully match the browser implementation for consistency and simplicity.
|
||||
- Fixed `writeVarint` to write `0` when given `NaN` or other non-number to avoid producing a broken Protobuf message.
|
||||
- Changed `readPacked*` methods signature to accept an optional `arr` argument to append the results to (to support messages with repeated fields that mix packed/non-packed encoding).
|
||||
- Added an optional `isSigned` argument to `readVarint` that enables proper reading of negative varints.
|
||||
- Deprecated `readVarint64()` (it still works, but it's recommended to be changed to `readVarint(true)`).
|
||||
- Faster string encoding.
|
||||
|
||||
##### Proto compiler
|
||||
|
||||
- **Breaking:** Full support for defaults field values (both implicit and explicit); they're now included in the decoded JSON objects.
|
||||
- Fixed reading of repeated fields with mixed packed/non-packed encoding for compatibility.
|
||||
- Fixed proto3 compiler to use packed by default for repeated scalar fields.
|
||||
- Fixed reading of negative varint types.
|
||||
- Fixed packed fields to decode into `[]` if they're not present.
|
||||
- Fixed nested message references handling.
|
||||
- Fixed `packed=false` being interpreted as packed.
|
||||
- Added a comment to generated code with pbf version number.
|
||||
|
||||
#### 2.0.1 (May 28, 2016)
|
||||
|
||||
- Fixed a regression with `writeVarint` that affected certain numbers.
|
||||
|
||||
#### 2.0.0 (May 28, 2016)
|
||||
|
||||
- Significantly improved the proto compiler, which now produces a much safer reading/writing code.
|
||||
- Added the ability to compile a read/write module from a protobuf schema directly in the code.
|
||||
- Proto compiler: fixed name resolutions and collisions in schemas with nested messages.
|
||||
- Proto compiler: fixed broken top-level enums.
|
||||
|
||||
#### 1.3.7 (May 28, 2016)
|
||||
|
||||
- Fixed a regression with `writeVarint` that affected certain numbers.
|
||||
|
||||
#### 1.3.6 (May 27, 2016)
|
||||
|
||||
- Improved read and write performance (both ~15% faster).
|
||||
- Improved generated code for default values.
|
||||
|
||||
#### 1.3.5 (Oct 5, 2015)
|
||||
|
||||
- Added support for `syntax` keyword proto files (by updating `resolve-protobuf-schema` dependency).
|
||||
|
||||
#### 1.3.4 (Jul 31, 2015)
|
||||
|
||||
- Added `writeRawMessage` method for writing a message without a tag, useful for creating pbfs with multiple top-level messages.
|
||||
|
||||
#### 1.3.2 (Mar 5, 2015)
|
||||
|
||||
- Added `readVarint64` method for proper decoding of negative `int64`-encoded values.
|
||||
|
||||
#### 1.3.1 (Feb 20, 2015)
|
||||
|
||||
- Fixed pbf proto compile tool generating broken writing code.
|
||||
|
||||
#### 1.3.0 (Feb 5, 2015)
|
||||
|
||||
- Added `pbf` binary that compiles `.proto` files into `Pbf`-based JavaScript modules.
|
||||
|
||||
#### 1.2.0 (Jan 5, 2015)
|
||||
|
||||
##### Breaking API changes
|
||||
|
||||
- Changed `writeMessage` signature to `(tag, fn, obj)` (see example in the docs)
|
||||
for a huge encoding performance improvement.
|
||||
- Replaced `readPacked` and `writePacked` methods that accept type as a string
|
||||
with `readPackedVarint`, etc. for each type (better performance and simpler API).
|
||||
|
||||
##### Improvements
|
||||
|
||||
- 5x faster encoding in Node (vector tile benchmark).
|
||||
- 40x faster encoding and 3x faster decoding in the browser (vector tile benchmark).
|
||||
|
||||
#### 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`).
|
||||
Basically makes the library complete.
|
||||
|
||||
##### Improvements
|
||||
|
||||
- Improved performance of both reading and writing.
|
||||
- Made the browser build 3 times smaller.
|
||||
- Added convenience `readFields` and `readMessage` methods for a much easier reading API.
|
||||
- Added reading methods: `readFloat`, `readBoolean`, `readSFixed32`, `readSFixed64`.
|
||||
- Added writing methods: `writeUInt64`, `writeSFixed32`, `writeSFixed64`.
|
||||
- Improved `readDouble` and `readString` to use native Buffer methods under Node.
|
||||
- Improved `readString` and `writeString` to use HTML5 `TextEncoder` and `TextDecoder` where available.
|
||||
- Made `Pbf` `buffer` argument optional.
|
||||
- Added extensive docs and examples in the readme.
|
||||
- Added an extensive test suite that brings test coverage up to 100%.
|
||||
|
||||
##### Breaking API changes
|
||||
|
||||
- Renamed `readBuffer`/`writeBuffer` to `readBytes`/`writeBytes`.
|
||||
- Renamed `readUInt32`/`writeUInt32` to `readFixed32`/`writeFixed32`, etc.
|
||||
- Renamed `writeTaggedVarint` to `writeVarintField`, etc.
|
||||
- Changed `writePacked` signature from `(type, tag, items)` to `(tag, type, items)`.
|
||||
|
||||
##### Bugfixes
|
||||
|
||||
- Fixed `readVarint` to handle varints bigger than 6 bytes.
|
||||
- Fixed `readSVarint` to handle number bigger than `2^30`.
|
||||
- Fixed `writeVarint` failing on some integers.
|
||||
- Fixed `writeVarint` not throwing an error on numbers that are too big.
|
||||
- Fixed `readUInt64` always failing.
|
||||
- Fixed writing to an empty buffer always failing.
|
||||
19
node_modules/pbf/bin/pbf
generated
vendored
Executable file
19
node_modules/pbf/bin/pbf
generated
vendored
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
var resolve = require('resolve-protobuf-schema');
|
||||
var compile = require('../compile');
|
||||
|
||||
if (process.argv.length < 3) {
|
||||
console.error('Usage: pbf [file.proto] [--browser] [--no-read] [--no-write]');
|
||||
return;
|
||||
}
|
||||
|
||||
var code = compile.raw(resolve.sync(process.argv[2]), {
|
||||
exports: process.argv.indexOf('--browser') >= 0 ? 'self' : 'exports',
|
||||
noRead: process.argv.indexOf('--no-read') >= 0,
|
||||
noWrite: process.argv.indexOf('--no-write') >= 0
|
||||
});
|
||||
|
||||
process.stdout.write(code);
|
||||
390
node_modules/pbf/compile.js
generated
vendored
Normal file
390
node_modules/pbf/compile.js
generated
vendored
Normal file
@@ -0,0 +1,390 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = compile;
|
||||
|
||||
var version = require('./package.json').version;
|
||||
|
||||
function compile(proto) {
|
||||
var code = 'var exports = {};\n';
|
||||
code += compileRaw(proto) + '\n';
|
||||
code += 'return exports;\n';
|
||||
return new Function(code)();
|
||||
}
|
||||
|
||||
compile.raw = compileRaw;
|
||||
|
||||
function compileRaw(proto, options) {
|
||||
var pre = '\'use strict\'; // code generated by pbf v' + version + '\n';
|
||||
var context = buildDefaults(buildContext(proto, null), proto.syntax);
|
||||
return pre + writeContext(context, options || {});
|
||||
}
|
||||
|
||||
function writeContext(ctx, options) {
|
||||
var code = '';
|
||||
if (ctx._proto.fields) code += writeMessage(ctx, options);
|
||||
if (ctx._proto.values) code += writeEnum(ctx, options);
|
||||
|
||||
for (var i = 0; i < ctx._children.length; i++) {
|
||||
code += writeContext(ctx._children[i], options);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
function writeMessage(ctx, options) {
|
||||
var name = ctx._name;
|
||||
var fields = ctx._proto.fields;
|
||||
var numRepeated = 0;
|
||||
|
||||
var code = '\n// ' + name + ' ========================================\n\n';
|
||||
|
||||
if (!options.noRead || !options.noWrite) {
|
||||
code += compileExport(ctx, options) + ' {};\n\n';
|
||||
}
|
||||
|
||||
if (!options.noRead) {
|
||||
code += name + '.read = function (pbf, end) {\n';
|
||||
code += ' return pbf.readFields(' + name + '._readField, ' + compileDest(ctx) + ', end);\n';
|
||||
code += '};\n';
|
||||
code += name + '._readField = function (tag, obj, pbf) {\n';
|
||||
|
||||
for (var i = 0; i < fields.length; i++) {
|
||||
var field = fields[i];
|
||||
var readCode = compileFieldRead(ctx, field);
|
||||
var packed = willSupportPacked(ctx, field);
|
||||
code += ' ' + (i ? 'else if' : 'if') +
|
||||
' (tag === ' + field.tag + ') ' +
|
||||
(field.type === 'map' ? ' { ' : '') +
|
||||
(
|
||||
field.type === 'map' ? compileMapRead(readCode, field.name, numRepeated++) :
|
||||
field.repeated && !packed ? 'obj.' + field.name + '.push(' + readCode + ')' :
|
||||
field.repeated && packed ? readCode : 'obj.' + field.name + ' = ' + readCode
|
||||
);
|
||||
|
||||
if (field.oneof) {
|
||||
code += ', obj.' + field.oneof + ' = ' + JSON.stringify(field.name);
|
||||
}
|
||||
|
||||
code += ';' + (field.type === 'map' ? ' }' : '') + '\n';
|
||||
}
|
||||
code += '};\n';
|
||||
}
|
||||
|
||||
if (!options.noWrite) {
|
||||
code += name + '.write = function (obj, pbf) {\n';
|
||||
numRepeated = 0;
|
||||
for (i = 0; i < fields.length; i++) {
|
||||
field = fields[i];
|
||||
var writeCode = field.repeated && !isPacked(field) ?
|
||||
compileRepeatedWrite(ctx, field, numRepeated++) :
|
||||
field.type === 'map' ? compileMapWrite(ctx, field, numRepeated++) :
|
||||
compileFieldWrite(ctx, field, 'obj.' + field.name);
|
||||
code += getDefaultWriteTest(ctx, field);
|
||||
code += writeCode + ';\n';
|
||||
}
|
||||
code += '};\n';
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
function writeEnum(ctx, options) {
|
||||
return '\n' + compileExport(ctx, options) + ' ' +
|
||||
JSON.stringify(ctx._proto.values, null, 4) + ';\n';
|
||||
}
|
||||
|
||||
function compileExport(ctx, options) {
|
||||
var exportsVar = options.exports || 'exports';
|
||||
return (ctx._root ? 'var ' + ctx._name + ' = ' + exportsVar + '.' : '') + ctx._name + ' =';
|
||||
}
|
||||
|
||||
function compileDest(ctx) {
|
||||
var props = {};
|
||||
for (var i = 0; i < ctx._proto.fields.length; i++) {
|
||||
var field = ctx._proto.fields[i];
|
||||
props[field.name + ': ' + JSON.stringify(ctx._defaults[field.name])] = true;
|
||||
if (field.oneof) props[field.oneof + ': null'] = true;
|
||||
}
|
||||
return '{' + Object.keys(props).join(', ') + '}';
|
||||
}
|
||||
|
||||
function isEnum(type) {
|
||||
return type && type._proto.values;
|
||||
}
|
||||
|
||||
function getType(ctx, field) {
|
||||
if (field.type === 'map') {
|
||||
return ctx[getMapMessageName(field.tag)];
|
||||
}
|
||||
|
||||
var path = field.type.split('.');
|
||||
return path.reduce(function(ctx, name) { return ctx && ctx[name]; }, ctx);
|
||||
}
|
||||
|
||||
function compileFieldRead(ctx, field) {
|
||||
var type = getType(ctx, field);
|
||||
if (type) {
|
||||
if (type._proto.fields) return type._name + '.read(pbf, pbf.readVarint() + pbf.pos)';
|
||||
if (!isEnum(type)) throw new Error('Unexpected type: ' + type._name);
|
||||
}
|
||||
|
||||
var fieldType = isEnum(type) ? 'enum' : field.type;
|
||||
|
||||
var prefix = 'pbf.read';
|
||||
var signed = fieldType === 'int32' || fieldType === 'int64' ? 'true' : '';
|
||||
var suffix = '(' + signed + ')';
|
||||
|
||||
if (willSupportPacked(ctx, field)) {
|
||||
prefix += 'Packed';
|
||||
suffix = '(obj.' + field.name + (signed ? ', ' + signed : '') + ')';
|
||||
}
|
||||
|
||||
switch (fieldType) {
|
||||
case 'string': return prefix + 'String' + suffix;
|
||||
case 'float': return prefix + 'Float' + suffix;
|
||||
case 'double': return prefix + 'Double' + suffix;
|
||||
case 'bool': return prefix + 'Boolean' + suffix;
|
||||
case 'enum':
|
||||
case 'uint32':
|
||||
case 'uint64':
|
||||
case 'int32':
|
||||
case 'int64': return prefix + 'Varint' + suffix;
|
||||
case 'sint32':
|
||||
case 'sint64': return prefix + 'SVarint' + suffix;
|
||||
case 'fixed32': return prefix + 'Fixed32' + suffix;
|
||||
case 'fixed64': return prefix + 'Fixed64' + suffix;
|
||||
case 'sfixed32': return prefix + 'SFixed32' + suffix;
|
||||
case 'sfixed64': return prefix + 'SFixed64' + suffix;
|
||||
case 'bytes': return prefix + 'Bytes' + suffix;
|
||||
default: throw new Error('Unexpected type: ' + field.type);
|
||||
}
|
||||
}
|
||||
|
||||
function compileFieldWrite(ctx, field, name) {
|
||||
var prefix = 'pbf.write';
|
||||
if (isPacked(field)) prefix += 'Packed';
|
||||
|
||||
var postfix = (isPacked(field) ? '' : 'Field') + '(' + field.tag + ', ' + name + ')';
|
||||
|
||||
var type = getType(ctx, field);
|
||||
if (type) {
|
||||
if (type._proto.fields) return prefix + 'Message(' + field.tag + ', ' + type._name + '.write, ' + name + ')';
|
||||
if (type._proto.values) return prefix + 'Varint' + postfix;
|
||||
throw new Error('Unexpected type: ' + type._name);
|
||||
}
|
||||
|
||||
switch (field.type) {
|
||||
case 'string': return prefix + 'String' + postfix;
|
||||
case 'float': return prefix + 'Float' + postfix;
|
||||
case 'double': return prefix + 'Double' + postfix;
|
||||
case 'bool': return prefix + 'Boolean' + postfix;
|
||||
case 'enum':
|
||||
case 'uint32':
|
||||
case 'uint64':
|
||||
case 'int32':
|
||||
case 'int64': return prefix + 'Varint' + postfix;
|
||||
case 'sint32':
|
||||
case 'sint64': return prefix + 'SVarint' + postfix;
|
||||
case 'fixed32': return prefix + 'Fixed32' + postfix;
|
||||
case 'fixed64': return prefix + 'Fixed64' + postfix;
|
||||
case 'sfixed32': return prefix + 'SFixed32' + postfix;
|
||||
case 'sfixed64': return prefix + 'SFixed64' + postfix;
|
||||
case 'bytes': return prefix + 'Bytes' + postfix;
|
||||
default: throw new Error('Unexpected type: ' + field.type);
|
||||
}
|
||||
}
|
||||
|
||||
function compileMapRead(readCode, name, numRepeated) {
|
||||
return (numRepeated ? '' : 'var ') + 'entry = ' + readCode + '; obj.' + name + '[entry.key] = entry.value';
|
||||
}
|
||||
|
||||
function compileRepeatedWrite(ctx, field, numRepeated) {
|
||||
return 'for (' + (numRepeated ? '' : 'var ') +
|
||||
'i = 0; i < obj.' + field.name + '.length; i++) ' +
|
||||
compileFieldWrite(ctx, field, 'obj.' + field.name + '[i]');
|
||||
}
|
||||
|
||||
function compileMapWrite(ctx, field, numRepeated) {
|
||||
var name = 'obj.' + field.name;
|
||||
|
||||
return 'for (' + (numRepeated ? '' : 'var ') +
|
||||
'i in ' + name + ') if (Object.prototype.hasOwnProperty.call(' + name + ', i)) ' +
|
||||
compileFieldWrite(ctx, field, '{ key: i, value: ' + name + '[i] }');
|
||||
}
|
||||
|
||||
function getMapMessageName(tag) {
|
||||
return '_FieldEntry' + tag;
|
||||
}
|
||||
|
||||
function getMapField(name, type, tag) {
|
||||
return {
|
||||
name: name,
|
||||
type: type,
|
||||
tag: tag,
|
||||
map: null,
|
||||
oneof: null,
|
||||
required: false,
|
||||
repeated: false,
|
||||
options: {}
|
||||
};
|
||||
}
|
||||
|
||||
function getMapMessage(field) {
|
||||
return {
|
||||
name: getMapMessageName(field.tag),
|
||||
enums: [],
|
||||
messages: [],
|
||||
extensions: null,
|
||||
fields: [
|
||||
getMapField('key', field.map.from, 1),
|
||||
getMapField('value', field.map.to, 2)
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
function buildContext(proto, parent) {
|
||||
var obj = Object.create(parent);
|
||||
obj._proto = proto;
|
||||
obj._children = [];
|
||||
obj._defaults = {};
|
||||
|
||||
if (parent) {
|
||||
parent[proto.name] = obj;
|
||||
|
||||
if (parent._name) {
|
||||
obj._root = false;
|
||||
obj._name = parent._name + '.' + proto.name;
|
||||
} else {
|
||||
obj._root = true;
|
||||
obj._name = proto.name;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; proto.enums && i < proto.enums.length; i++) {
|
||||
obj._children.push(buildContext(proto.enums[i], obj));
|
||||
}
|
||||
|
||||
for (i = 0; proto.messages && i < proto.messages.length; i++) {
|
||||
obj._children.push(buildContext(proto.messages[i], obj));
|
||||
}
|
||||
|
||||
for (i = 0; proto.fields && i < proto.fields.length; i++) {
|
||||
if (proto.fields[i].type === 'map') {
|
||||
obj._children.push(buildContext(getMapMessage(proto.fields[i]), obj));
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function getDefaultValue(field, value) {
|
||||
// Defaults not supported for repeated fields
|
||||
if (field.repeated) return [];
|
||||
|
||||
switch (field.type) {
|
||||
case 'float':
|
||||
case 'double': return value ? parseFloat(value) : 0;
|
||||
case 'uint32':
|
||||
case 'uint64':
|
||||
case 'int32':
|
||||
case 'int64':
|
||||
case 'sint32':
|
||||
case 'sint64':
|
||||
case 'fixed32':
|
||||
case 'fixed64':
|
||||
case 'sfixed32':
|
||||
case 'sfixed64': return value ? parseInt(value, 10) : 0;
|
||||
case 'string': return value || '';
|
||||
case 'bool': return value === 'true';
|
||||
case 'map': return {};
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
function willSupportPacked(ctx, field) {
|
||||
var fieldType = isEnum(getType(ctx, field)) ? 'enum' : field.type;
|
||||
|
||||
switch (field.repeated && fieldType) {
|
||||
case 'float':
|
||||
case 'double':
|
||||
case 'uint32':
|
||||
case 'uint64':
|
||||
case 'int32':
|
||||
case 'int64':
|
||||
case 'sint32':
|
||||
case 'sint64':
|
||||
case 'fixed32':
|
||||
case 'fixed64':
|
||||
case 'sfixed32':
|
||||
case 'enum':
|
||||
case 'bool': return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function setPackedOption(ctx, field, syntax) {
|
||||
// No default packed in older protobuf versions
|
||||
if (syntax < 3) return;
|
||||
|
||||
// Packed option already set
|
||||
if (field.options.packed !== undefined) return;
|
||||
|
||||
// Not a packed field type
|
||||
if (!willSupportPacked(ctx, field)) return;
|
||||
|
||||
field.options.packed = 'true';
|
||||
}
|
||||
|
||||
function setDefaultValue(ctx, field, syntax) {
|
||||
var options = field.options;
|
||||
var type = getType(ctx, field);
|
||||
var enumValues = type && type._proto.values;
|
||||
|
||||
// Proto3 does not support overriding defaults
|
||||
var explicitDefault = syntax < 3 ? options.default : undefined;
|
||||
|
||||
// Set default for enum values
|
||||
if (enumValues && !field.repeated) {
|
||||
ctx._defaults[field.name] = enumValues[explicitDefault] || 0;
|
||||
|
||||
} else {
|
||||
ctx._defaults[field.name] = getDefaultValue(field, explicitDefault);
|
||||
}
|
||||
}
|
||||
|
||||
function buildDefaults(ctx, syntax) {
|
||||
var proto = ctx._proto;
|
||||
|
||||
for (var i = 0; i < ctx._children.length; i++) {
|
||||
buildDefaults(ctx._children[i], syntax);
|
||||
}
|
||||
|
||||
if (proto.fields) {
|
||||
for (i = 0; i < proto.fields.length; i++) {
|
||||
setPackedOption(ctx, proto.fields[i], syntax);
|
||||
setDefaultValue(ctx, proto.fields[i], syntax);
|
||||
}
|
||||
}
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
function getDefaultWriteTest(ctx, field) {
|
||||
var def = ctx._defaults[field.name];
|
||||
var type = getType(ctx, field);
|
||||
var code = ' if (obj.' + field.name;
|
||||
|
||||
if (!field.repeated && (!type || !type._proto.fields)) {
|
||||
if (def === undefined || def) {
|
||||
code += ' != undefined';
|
||||
}
|
||||
if (def) {
|
||||
code += ' && obj.' + field.name + ' !== ' + JSON.stringify(def);
|
||||
}
|
||||
}
|
||||
|
||||
return code + ') ';
|
||||
}
|
||||
|
||||
function isPacked(field) {
|
||||
return field.options.packed === 'true';
|
||||
}
|
||||
734
node_modules/pbf/dist/pbf-dev.js
generated
vendored
Normal file
734
node_modules/pbf/dist/pbf-dev.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
1
node_modules/pbf/dist/pbf.js
generated
vendored
Normal file
1
node_modules/pbf/dist/pbf.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
642
node_modules/pbf/index.js
generated
vendored
Normal file
642
node_modules/pbf/index.js
generated
vendored
Normal file
@@ -0,0 +1,642 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = Pbf;
|
||||
|
||||
var ieee754 = require('ieee754');
|
||||
|
||||
function Pbf(buf) {
|
||||
this.buf = ArrayBuffer.isView && ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf || 0);
|
||||
this.pos = 0;
|
||||
this.type = 0;
|
||||
this.length = this.buf.length;
|
||||
}
|
||||
|
||||
Pbf.Varint = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum
|
||||
Pbf.Fixed64 = 1; // 64-bit: double, fixed64, sfixed64
|
||||
Pbf.Bytes = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields
|
||||
Pbf.Fixed32 = 5; // 32-bit: float, fixed32, sfixed32
|
||||
|
||||
var SHIFT_LEFT_32 = (1 << 16) * (1 << 16),
|
||||
SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
|
||||
|
||||
// Threshold chosen based on both benchmarking and knowledge about browser string
|
||||
// data structures (which currently switch structure types at 12 bytes or more)
|
||||
var TEXT_DECODER_MIN_LENGTH = 12;
|
||||
var utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf8');
|
||||
|
||||
Pbf.prototype = {
|
||||
|
||||
destroy: function() {
|
||||
this.buf = null;
|
||||
},
|
||||
|
||||
// === READING =================================================================
|
||||
|
||||
readFields: function(readField, result, end) {
|
||||
end = end || this.length;
|
||||
|
||||
while (this.pos < end) {
|
||||
var val = this.readVarint(),
|
||||
tag = val >> 3,
|
||||
startPos = this.pos;
|
||||
|
||||
this.type = val & 0x7;
|
||||
readField(tag, result, this);
|
||||
|
||||
if (this.pos === startPos) this.skip(val);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
readMessage: function(readField, result) {
|
||||
return this.readFields(readField, result, this.readVarint() + this.pos);
|
||||
},
|
||||
|
||||
readFixed32: function() {
|
||||
var val = readUInt32(this.buf, this.pos);
|
||||
this.pos += 4;
|
||||
return val;
|
||||
},
|
||||
|
||||
readSFixed32: function() {
|
||||
var val = readInt32(this.buf, this.pos);
|
||||
this.pos += 4;
|
||||
return val;
|
||||
},
|
||||
|
||||
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
|
||||
|
||||
readFixed64: function() {
|
||||
var val = readUInt32(this.buf, this.pos) + readUInt32(this.buf, this.pos + 4) * SHIFT_LEFT_32;
|
||||
this.pos += 8;
|
||||
return val;
|
||||
},
|
||||
|
||||
readSFixed64: function() {
|
||||
var val = readUInt32(this.buf, this.pos) + readInt32(this.buf, this.pos + 4) * SHIFT_LEFT_32;
|
||||
this.pos += 8;
|
||||
return val;
|
||||
},
|
||||
|
||||
readFloat: function() {
|
||||
var val = ieee754.read(this.buf, this.pos, true, 23, 4);
|
||||
this.pos += 4;
|
||||
return val;
|
||||
},
|
||||
|
||||
readDouble: function() {
|
||||
var val = ieee754.read(this.buf, this.pos, true, 52, 8);
|
||||
this.pos += 8;
|
||||
return val;
|
||||
},
|
||||
|
||||
readVarint: function(isSigned) {
|
||||
var buf = this.buf,
|
||||
val, b;
|
||||
|
||||
b = buf[this.pos++]; val = b & 0x7f; if (b < 0x80) return val;
|
||||
b = buf[this.pos++]; val |= (b & 0x7f) << 7; if (b < 0x80) return val;
|
||||
b = buf[this.pos++]; val |= (b & 0x7f) << 14; if (b < 0x80) return val;
|
||||
b = buf[this.pos++]; val |= (b & 0x7f) << 21; if (b < 0x80) return val;
|
||||
b = buf[this.pos]; val |= (b & 0x0f) << 28;
|
||||
|
||||
return readVarintRemainder(val, isSigned, this);
|
||||
},
|
||||
|
||||
readVarint64: function() { // for compatibility with v2.0.1
|
||||
return this.readVarint(true);
|
||||
},
|
||||
|
||||
readSVarint: function() {
|
||||
var num = this.readVarint();
|
||||
return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding
|
||||
},
|
||||
|
||||
readBoolean: function() {
|
||||
return Boolean(this.readVarint());
|
||||
},
|
||||
|
||||
readString: function() {
|
||||
var end = this.readVarint() + this.pos;
|
||||
var pos = this.pos;
|
||||
this.pos = end;
|
||||
|
||||
if (end - pos >= TEXT_DECODER_MIN_LENGTH && utf8TextDecoder) {
|
||||
// longer strings are fast with the built-in browser TextDecoder API
|
||||
return readUtf8TextDecoder(this.buf, pos, end);
|
||||
}
|
||||
// short strings are fast with our custom implementation
|
||||
return readUtf8(this.buf, pos, end);
|
||||
},
|
||||
|
||||
readBytes: function() {
|
||||
var end = this.readVarint() + this.pos,
|
||||
buffer = this.buf.subarray(this.pos, end);
|
||||
this.pos = end;
|
||||
return buffer;
|
||||
},
|
||||
|
||||
// verbose for performance reasons; doesn't affect gzipped size
|
||||
|
||||
readPackedVarint: function(arr, isSigned) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readVarint(isSigned));
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readVarint(isSigned));
|
||||
return arr;
|
||||
},
|
||||
readPackedSVarint: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readSVarint());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readSVarint());
|
||||
return arr;
|
||||
},
|
||||
readPackedBoolean: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readBoolean());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readBoolean());
|
||||
return arr;
|
||||
},
|
||||
readPackedFloat: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readFloat());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readFloat());
|
||||
return arr;
|
||||
},
|
||||
readPackedDouble: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readDouble());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readDouble());
|
||||
return arr;
|
||||
},
|
||||
readPackedFixed32: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readFixed32());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readFixed32());
|
||||
return arr;
|
||||
},
|
||||
readPackedSFixed32: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readSFixed32());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readSFixed32());
|
||||
return arr;
|
||||
},
|
||||
readPackedFixed64: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readFixed64());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readFixed64());
|
||||
return arr;
|
||||
},
|
||||
readPackedSFixed64: function(arr) {
|
||||
if (this.type !== Pbf.Bytes) return arr.push(this.readSFixed64());
|
||||
var end = readPackedEnd(this);
|
||||
arr = arr || [];
|
||||
while (this.pos < end) arr.push(this.readSFixed64());
|
||||
return arr;
|
||||
},
|
||||
|
||||
skip: function(val) {
|
||||
var type = val & 0x7;
|
||||
if (type === Pbf.Varint) while (this.buf[this.pos++] > 0x7f) {}
|
||||
else if (type === Pbf.Bytes) this.pos = this.readVarint() + this.pos;
|
||||
else if (type === Pbf.Fixed32) this.pos += 4;
|
||||
else if (type === Pbf.Fixed64) this.pos += 8;
|
||||
else throw new Error('Unimplemented type: ' + type);
|
||||
},
|
||||
|
||||
// === WRITING =================================================================
|
||||
|
||||
writeTag: function(tag, type) {
|
||||
this.writeVarint((tag << 3) | type);
|
||||
},
|
||||
|
||||
realloc: function(min) {
|
||||
var length = this.length || 16;
|
||||
|
||||
while (length < this.pos + min) length *= 2;
|
||||
|
||||
if (length !== this.length) {
|
||||
var buf = new Uint8Array(length);
|
||||
buf.set(this.buf);
|
||||
this.buf = buf;
|
||||
this.length = length;
|
||||
}
|
||||
},
|
||||
|
||||
finish: function() {
|
||||
this.length = this.pos;
|
||||
this.pos = 0;
|
||||
return this.buf.subarray(0, this.length);
|
||||
},
|
||||
|
||||
writeFixed32: function(val) {
|
||||
this.realloc(4);
|
||||
writeInt32(this.buf, val, this.pos);
|
||||
this.pos += 4;
|
||||
},
|
||||
|
||||
writeSFixed32: function(val) {
|
||||
this.realloc(4);
|
||||
writeInt32(this.buf, val, this.pos);
|
||||
this.pos += 4;
|
||||
},
|
||||
|
||||
writeFixed64: function(val) {
|
||||
this.realloc(8);
|
||||
writeInt32(this.buf, val & -1, this.pos);
|
||||
writeInt32(this.buf, Math.floor(val * SHIFT_RIGHT_32), this.pos + 4);
|
||||
this.pos += 8;
|
||||
},
|
||||
|
||||
writeSFixed64: function(val) {
|
||||
this.realloc(8);
|
||||
writeInt32(this.buf, val & -1, this.pos);
|
||||
writeInt32(this.buf, Math.floor(val * SHIFT_RIGHT_32), this.pos + 4);
|
||||
this.pos += 8;
|
||||
},
|
||||
|
||||
writeVarint: function(val) {
|
||||
val = +val || 0;
|
||||
|
||||
if (val > 0xfffffff || val < 0) {
|
||||
writeBigVarint(val, this);
|
||||
return;
|
||||
}
|
||||
|
||||
this.realloc(4);
|
||||
|
||||
this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
|
||||
this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
|
||||
this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
|
||||
this.buf[this.pos++] = (val >>> 7) & 0x7f;
|
||||
},
|
||||
|
||||
writeSVarint: function(val) {
|
||||
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
|
||||
},
|
||||
|
||||
writeBoolean: function(val) {
|
||||
this.writeVarint(Boolean(val));
|
||||
},
|
||||
|
||||
writeString: function(str) {
|
||||
str = String(str);
|
||||
this.realloc(str.length * 4);
|
||||
|
||||
this.pos++; // reserve 1 byte for short string length
|
||||
|
||||
var startPos = this.pos;
|
||||
// write the string directly to the buffer and see how much was written
|
||||
this.pos = writeUtf8(this.buf, str, this.pos);
|
||||
var len = this.pos - startPos;
|
||||
|
||||
if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
|
||||
|
||||
// finally, write the message length in the reserved place and restore the position
|
||||
this.pos = startPos - 1;
|
||||
this.writeVarint(len);
|
||||
this.pos += len;
|
||||
},
|
||||
|
||||
writeFloat: function(val) {
|
||||
this.realloc(4);
|
||||
ieee754.write(this.buf, val, this.pos, true, 23, 4);
|
||||
this.pos += 4;
|
||||
},
|
||||
|
||||
writeDouble: function(val) {
|
||||
this.realloc(8);
|
||||
ieee754.write(this.buf, val, this.pos, true, 52, 8);
|
||||
this.pos += 8;
|
||||
},
|
||||
|
||||
writeBytes: function(buffer) {
|
||||
var len = buffer.length;
|
||||
this.writeVarint(len);
|
||||
this.realloc(len);
|
||||
for (var i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
|
||||
},
|
||||
|
||||
writeRawMessage: function(fn, obj) {
|
||||
this.pos++; // reserve 1 byte for short message length
|
||||
|
||||
// write the message directly to the buffer and see how much was written
|
||||
var startPos = this.pos;
|
||||
fn(obj, this);
|
||||
var len = this.pos - startPos;
|
||||
|
||||
if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
|
||||
|
||||
// finally, write the message length in the reserved place and restore the position
|
||||
this.pos = startPos - 1;
|
||||
this.writeVarint(len);
|
||||
this.pos += len;
|
||||
},
|
||||
|
||||
writeMessage: function(tag, fn, obj) {
|
||||
this.writeTag(tag, Pbf.Bytes);
|
||||
this.writeRawMessage(fn, obj);
|
||||
},
|
||||
|
||||
writePackedVarint: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedVarint, arr); },
|
||||
writePackedSVarint: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedSVarint, arr); },
|
||||
writePackedBoolean: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedBoolean, arr); },
|
||||
writePackedFloat: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedFloat, arr); },
|
||||
writePackedDouble: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedDouble, arr); },
|
||||
writePackedFixed32: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedFixed32, arr); },
|
||||
writePackedSFixed32: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr); },
|
||||
writePackedFixed64: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedFixed64, arr); },
|
||||
writePackedSFixed64: function(tag, arr) { if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr); },
|
||||
|
||||
writeBytesField: function(tag, buffer) {
|
||||
this.writeTag(tag, Pbf.Bytes);
|
||||
this.writeBytes(buffer);
|
||||
},
|
||||
writeFixed32Field: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Fixed32);
|
||||
this.writeFixed32(val);
|
||||
},
|
||||
writeSFixed32Field: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Fixed32);
|
||||
this.writeSFixed32(val);
|
||||
},
|
||||
writeFixed64Field: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Fixed64);
|
||||
this.writeFixed64(val);
|
||||
},
|
||||
writeSFixed64Field: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Fixed64);
|
||||
this.writeSFixed64(val);
|
||||
},
|
||||
writeVarintField: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Varint);
|
||||
this.writeVarint(val);
|
||||
},
|
||||
writeSVarintField: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Varint);
|
||||
this.writeSVarint(val);
|
||||
},
|
||||
writeStringField: function(tag, str) {
|
||||
this.writeTag(tag, Pbf.Bytes);
|
||||
this.writeString(str);
|
||||
},
|
||||
writeFloatField: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Fixed32);
|
||||
this.writeFloat(val);
|
||||
},
|
||||
writeDoubleField: function(tag, val) {
|
||||
this.writeTag(tag, Pbf.Fixed64);
|
||||
this.writeDouble(val);
|
||||
},
|
||||
writeBooleanField: function(tag, val) {
|
||||
this.writeVarintField(tag, Boolean(val));
|
||||
}
|
||||
};
|
||||
|
||||
function readVarintRemainder(l, s, p) {
|
||||
var buf = p.buf,
|
||||
h, b;
|
||||
|
||||
b = buf[p.pos++]; h = (b & 0x70) >> 4; if (b < 0x80) return toNum(l, h, s);
|
||||
b = buf[p.pos++]; h |= (b & 0x7f) << 3; if (b < 0x80) return toNum(l, h, s);
|
||||
b = buf[p.pos++]; h |= (b & 0x7f) << 10; if (b < 0x80) return toNum(l, h, s);
|
||||
b = buf[p.pos++]; h |= (b & 0x7f) << 17; if (b < 0x80) return toNum(l, h, s);
|
||||
b = buf[p.pos++]; h |= (b & 0x7f) << 24; if (b < 0x80) return toNum(l, h, s);
|
||||
b = buf[p.pos++]; h |= (b & 0x01) << 31; if (b < 0x80) return toNum(l, h, s);
|
||||
|
||||
throw new Error('Expected varint not more than 10 bytes');
|
||||
}
|
||||
|
||||
function readPackedEnd(pbf) {
|
||||
return pbf.type === Pbf.Bytes ?
|
||||
pbf.readVarint() + pbf.pos : pbf.pos + 1;
|
||||
}
|
||||
|
||||
function toNum(low, high, isSigned) {
|
||||
if (isSigned) {
|
||||
return high * 0x100000000 + (low >>> 0);
|
||||
}
|
||||
|
||||
return ((high >>> 0) * 0x100000000) + (low >>> 0);
|
||||
}
|
||||
|
||||
function writeBigVarint(val, pbf) {
|
||||
var low, high;
|
||||
|
||||
if (val >= 0) {
|
||||
low = (val % 0x100000000) | 0;
|
||||
high = (val / 0x100000000) | 0;
|
||||
} else {
|
||||
low = ~(-val % 0x100000000);
|
||||
high = ~(-val / 0x100000000);
|
||||
|
||||
if (low ^ 0xffffffff) {
|
||||
low = (low + 1) | 0;
|
||||
} else {
|
||||
low = 0;
|
||||
high = (high + 1) | 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (val >= 0x10000000000000000 || val < -0x10000000000000000) {
|
||||
throw new Error('Given varint doesn\'t fit into 10 bytes');
|
||||
}
|
||||
|
||||
pbf.realloc(10);
|
||||
|
||||
writeBigVarintLow(low, high, pbf);
|
||||
writeBigVarintHigh(high, pbf);
|
||||
}
|
||||
|
||||
function writeBigVarintLow(low, high, pbf) {
|
||||
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
||||
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
||||
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
||||
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
||||
pbf.buf[pbf.pos] = low & 0x7f;
|
||||
}
|
||||
|
||||
function writeBigVarintHigh(high, pbf) {
|
||||
var lsb = (high & 0x07) << 4;
|
||||
|
||||
pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 0x80 : 0); if (!high) return;
|
||||
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
||||
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
||||
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
||||
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
||||
pbf.buf[pbf.pos++] = high & 0x7f;
|
||||
}
|
||||
|
||||
function makeRoomForExtraLength(startPos, len, pbf) {
|
||||
var extraLen =
|
||||
len <= 0x3fff ? 1 :
|
||||
len <= 0x1fffff ? 2 :
|
||||
len <= 0xfffffff ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
|
||||
|
||||
// if 1 byte isn't enough for encoding message length, shift the data to the right
|
||||
pbf.realloc(extraLen);
|
||||
for (var i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
|
||||
}
|
||||
|
||||
function writePackedVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]); }
|
||||
function writePackedSVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]); }
|
||||
function writePackedFloat(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]); }
|
||||
function writePackedDouble(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]); }
|
||||
function writePackedBoolean(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]); }
|
||||
function writePackedFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]); }
|
||||
function writePackedSFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]); }
|
||||
function writePackedFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]); }
|
||||
function writePackedSFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]); }
|
||||
|
||||
// Buffer code below from https://github.com/feross/buffer, MIT-licensed
|
||||
|
||||
function readUInt32(buf, pos) {
|
||||
return ((buf[pos]) |
|
||||
(buf[pos + 1] << 8) |
|
||||
(buf[pos + 2] << 16)) +
|
||||
(buf[pos + 3] * 0x1000000);
|
||||
}
|
||||
|
||||
function writeInt32(buf, val, pos) {
|
||||
buf[pos] = val;
|
||||
buf[pos + 1] = (val >>> 8);
|
||||
buf[pos + 2] = (val >>> 16);
|
||||
buf[pos + 3] = (val >>> 24);
|
||||
}
|
||||
|
||||
function readInt32(buf, pos) {
|
||||
return ((buf[pos]) |
|
||||
(buf[pos + 1] << 8) |
|
||||
(buf[pos + 2] << 16)) +
|
||||
(buf[pos + 3] << 24);
|
||||
}
|
||||
|
||||
function readUtf8(buf, pos, end) {
|
||||
var str = '';
|
||||
var i = pos;
|
||||
|
||||
while (i < end) {
|
||||
var b0 = buf[i];
|
||||
var c = null; // codepoint
|
||||
var bytesPerSequence =
|
||||
b0 > 0xEF ? 4 :
|
||||
b0 > 0xDF ? 3 :
|
||||
b0 > 0xBF ? 2 : 1;
|
||||
|
||||
if (i + bytesPerSequence > end) break;
|
||||
|
||||
var b1, b2, b3;
|
||||
|
||||
if (bytesPerSequence === 1) {
|
||||
if (b0 < 0x80) {
|
||||
c = b0;
|
||||
}
|
||||
} else if (bytesPerSequence === 2) {
|
||||
b1 = buf[i + 1];
|
||||
if ((b1 & 0xC0) === 0x80) {
|
||||
c = (b0 & 0x1F) << 0x6 | (b1 & 0x3F);
|
||||
if (c <= 0x7F) {
|
||||
c = null;
|
||||
}
|
||||
}
|
||||
} else if (bytesPerSequence === 3) {
|
||||
b1 = buf[i + 1];
|
||||
b2 = buf[i + 2];
|
||||
if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80) {
|
||||
c = (b0 & 0xF) << 0xC | (b1 & 0x3F) << 0x6 | (b2 & 0x3F);
|
||||
if (c <= 0x7FF || (c >= 0xD800 && c <= 0xDFFF)) {
|
||||
c = null;
|
||||
}
|
||||
}
|
||||
} else if (bytesPerSequence === 4) {
|
||||
b1 = buf[i + 1];
|
||||
b2 = buf[i + 2];
|
||||
b3 = buf[i + 3];
|
||||
if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
|
||||
c = (b0 & 0xF) << 0x12 | (b1 & 0x3F) << 0xC | (b2 & 0x3F) << 0x6 | (b3 & 0x3F);
|
||||
if (c <= 0xFFFF || c >= 0x110000) {
|
||||
c = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (c === null) {
|
||||
c = 0xFFFD;
|
||||
bytesPerSequence = 1;
|
||||
|
||||
} else if (c > 0xFFFF) {
|
||||
c -= 0x10000;
|
||||
str += String.fromCharCode(c >>> 10 & 0x3FF | 0xD800);
|
||||
c = 0xDC00 | c & 0x3FF;
|
||||
}
|
||||
|
||||
str += String.fromCharCode(c);
|
||||
i += bytesPerSequence;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function readUtf8TextDecoder(buf, pos, end) {
|
||||
return utf8TextDecoder.decode(buf.subarray(pos, end));
|
||||
}
|
||||
|
||||
function writeUtf8(buf, str, pos) {
|
||||
for (var i = 0, c, lead; i < str.length; i++) {
|
||||
c = str.charCodeAt(i); // code point
|
||||
|
||||
if (c > 0xD7FF && c < 0xE000) {
|
||||
if (lead) {
|
||||
if (c < 0xDC00) {
|
||||
buf[pos++] = 0xEF;
|
||||
buf[pos++] = 0xBF;
|
||||
buf[pos++] = 0xBD;
|
||||
lead = c;
|
||||
continue;
|
||||
} else {
|
||||
c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
|
||||
lead = null;
|
||||
}
|
||||
} else {
|
||||
if (c > 0xDBFF || (i + 1 === str.length)) {
|
||||
buf[pos++] = 0xEF;
|
||||
buf[pos++] = 0xBF;
|
||||
buf[pos++] = 0xBD;
|
||||
} else {
|
||||
lead = c;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
} else if (lead) {
|
||||
buf[pos++] = 0xEF;
|
||||
buf[pos++] = 0xBF;
|
||||
buf[pos++] = 0xBD;
|
||||
lead = null;
|
||||
}
|
||||
|
||||
if (c < 0x80) {
|
||||
buf[pos++] = c;
|
||||
} else {
|
||||
if (c < 0x800) {
|
||||
buf[pos++] = c >> 0x6 | 0xC0;
|
||||
} else {
|
||||
if (c < 0x10000) {
|
||||
buf[pos++] = c >> 0xC | 0xE0;
|
||||
} else {
|
||||
buf[pos++] = c >> 0x12 | 0xF0;
|
||||
buf[pos++] = c >> 0xC & 0x3F | 0x80;
|
||||
}
|
||||
buf[pos++] = c >> 0x6 & 0x3F | 0x80;
|
||||
}
|
||||
buf[pos++] = c & 0x3F | 0x80;
|
||||
}
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
81
node_modules/pbf/package.json
generated
vendored
Normal file
81
node_modules/pbf/package.json
generated
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
{
|
||||
"name": "pbf",
|
||||
"version": "3.2.1",
|
||||
"description": "a low-level, lightweight protocol buffers implementation in JavaScript",
|
||||
"main": "index.js",
|
||||
"unpkg": "dist/pbf.js",
|
||||
"jsdelivr": "dist/pbf.js",
|
||||
"scripts": {
|
||||
"bench": "node bench/bench.js",
|
||||
"test": "eslint index.js compile.js test/*.js bench/bench-tiles.js bin/pbf && tap test/*.test.js && npm run build-min",
|
||||
"cov": "tap test/*.test.js --cov --coverage-report=html",
|
||||
"build-min": "mkdirp dist && browserify index.js -s Pbf | uglifyjs -c -m > dist/pbf.js",
|
||||
"build-dev": "mkdirp dist && browserify index.js -d -s Pbf > dist/pbf-dev.js",
|
||||
"prepublishOnly": "npm run build-dev && npm run build-min"
|
||||
},
|
||||
"files": [
|
||||
"bin",
|
||||
"dist",
|
||||
"compile.js"
|
||||
],
|
||||
"bin": {
|
||||
"pbf": "bin/pbf"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git@github.com:mapbox/pbf.git"
|
||||
},
|
||||
"keywords": [
|
||||
"protocol",
|
||||
"buffer",
|
||||
"pbf",
|
||||
"protobuf",
|
||||
"binary",
|
||||
"format",
|
||||
"serialization",
|
||||
"encoder",
|
||||
"decoder"
|
||||
],
|
||||
"author": "Konstantin Kaefer",
|
||||
"license": "BSD-3-Clause",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mapbox/pbf/issues"
|
||||
},
|
||||
"homepage": "https://github.com/mapbox/pbf",
|
||||
"dependencies": {
|
||||
"ieee754": "^1.1.12",
|
||||
"resolve-protobuf-schema": "^2.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"benchmark": "^2.1.4",
|
||||
"browserify": "^16.2.3",
|
||||
"eslint": "^5.15.1",
|
||||
"eslint-config-mourner": "^2.0.3",
|
||||
"mkdirp": "^0.5.1",
|
||||
"protobufjs": "^6.8.8",
|
||||
"protocol-buffers": "^4.1.0",
|
||||
"tap": "^12.6.0",
|
||||
"tile-stats-runner": "^1.0.0",
|
||||
"uglify-js": "^3.6.1"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "mourner",
|
||||
"rules": {
|
||||
"space-before-function-paren": [
|
||||
2,
|
||||
"never"
|
||||
],
|
||||
"key-spacing": 0,
|
||||
"no-empty": 0,
|
||||
"global-require": 0,
|
||||
"no-cond-assign": 0,
|
||||
"indent": [
|
||||
2,
|
||||
4,
|
||||
{
|
||||
"flatTernaryExpressions": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user