add addzone and update zone in map-adapter interface

This commit is contained in:
2025-09-29 14:55:14 -04:00
parent 67c94197e1
commit db3ec6cddd
7851 changed files with 833548 additions and 18 deletions
Generated Vendored
+27
View File
@@ -0,0 +1,27 @@
Copyright (c) 2024, 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.
Generated Vendored
+293
View File
@@ -0,0 +1,293 @@
# pbf
[![Node](https://github.com/mapbox/pbf/actions/workflows/node.yml/badge.svg)](https://github.com/mapbox/pbf/actions/workflows/node.yml)
![Bundle size](https://img.shields.io/bundlephobia/minzip/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
import Pbf from 'pbf';
import {readExample, writeExample} from './example.js';
// read
var obj = readExample(new Pbf(buffer));
// write
const pbf = new Pbf();
writeExample(obj, pbf);
const buffer = pbf.finish();
```
Alternatively, you can compile a protobuf schema file directly in the code:
```js
import {compile} from 'pbf/compile';
import schema from 'protocol-buffers-schema';
const proto = schema.parse(fs.readFileSync('example.proto'));
const {readExample, writeExample} = compile(proto);
```
#### 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
Install using NPM with `npm install pbf`, then import as a module:
```js
import Pbf from 'pbf';
```
Or use as a module directly in the browser with [jsDelivr](https://www.jsdelivr.com/esm):
```html
<script type="module">
import Pbf from 'https://cdn.jsdelivr.net/npm/pbf/+esm';
</script>
```
Alternatively, there's a browser bundle with a `Pbf` global variable:
```html
<script src="https://cdn.jsdelivr.net/npm/pbf"></script>
```
## API
Create a `Pbf` object, optionally given a `Buffer` or `Uint8Array` as input data:
```js
// parse a pbf file from disk in Node
const pbf = new Pbf(fs.readFileSync('data.pbf'));
// parse a pbf file in a browser after an ajax request with responseType="arraybuffer"
const 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((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
const result = pbf.readFields(readField, {})
function readField(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
const value = pbf.readVarint();
const str = pbf.readString();
const 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
const fooPos = -1;
pbf.readFields((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
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] [--legacy]
```
The `--no-write` and `--no-read` switches remove corresponding code in the output.
The `--legacy` switch makes it generate a CommonJS module instead of ESM.
`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.
* `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 it's meant to be customized.
Generated Vendored Executable
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env node
import resolve from 'resolve-protobuf-schema';
import {compileRaw} from '../compile.js';
if (process.argv.length < 3) {
console.error('Usage: pbf [file.proto] [--no-read] [--no-write] [--legacy]');
process.exit(0);
}
const code = compileRaw(resolve.sync(process.argv[2]), {
noRead: process.argv.indexOf('--no-read') >= 0,
noWrite: process.argv.indexOf('--no-write') >= 0,
legacy: process.argv.indexOf('--legacy') >= 0
});
process.stdout.write(code);
+417
View File
@@ -0,0 +1,417 @@
import {readFileSync} from 'fs';
const version = JSON.parse(readFileSync(new URL('package.json', import.meta.url))).version;
export function compile(proto) {
return new Function(`const exports = {};\n${compileRaw(proto, {legacy: true})}\nreturn exports;`)();
}
export function compileRaw(proto, options = {}) {
const context = buildDefaults(buildContext(proto, null), proto.syntax);
return `${options.dev ? '' : `// code generated by pbf v${version}\n`}${writeContext(context, options)}`;
}
function writeContext(ctx, options) {
let code = '';
if (ctx._proto.fields) code += writeMessage(ctx, options);
if (ctx._proto.values) code += writeEnum(ctx, options);
for (let i = 0; i < ctx._children.length; i++) {
code += writeContext(ctx._children[i], options);
}
return code;
}
function writeMessage(ctx, options) {
const fields = ctx._proto.fields;
let code = '\n';
if (!options.noRead) {
const readName = `read${ctx._name}`;
code +=
`${writeFunctionExport(options, readName)}function ${readName}(pbf, end) {
return pbf.readFields(${readName}Field, ${compileDest(ctx)}, end);
}
function ${readName}Field(tag, obj, pbf) {
`;
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
const {type, name, repeated, oneof, tag} = field;
const readCode = compileFieldRead(ctx, field);
const packed = willSupportPacked(ctx, field);
let fieldRead =
type === 'map' ? compileMapRead(readCode, name) :
repeated ? (packed ? readCode : `obj.${name}.push(${readCode})`) :
`obj.${name} = ${readCode}`;
if (oneof) {
fieldRead += `; obj.${oneof} = ${JSON.stringify(name)}`;
}
fieldRead = type === 'map' || oneof ? `{ ${fieldRead}; }` : `${fieldRead};`;
code +=
` ${i ? 'else ' : ''}if (tag === ${tag}) ${fieldRead}\n`;
}
code += '}\n';
}
if (!options.noWrite) {
const writeName = `write${ctx._name}`;
code += `${writeFunctionExport(options, writeName)}function ${writeName}(obj, pbf) {\n`;
for (const field of fields) {
const writeCode =
field.repeated && !isPacked(field) ? compileRepeatedWrite(ctx, field) :
field.type === 'map' ? compileMapWrite(ctx, field) : compileFieldWrite(ctx, field, `obj.${field.name}`);
code += getDefaultWriteTest(ctx, field);
code += `${writeCode};\n`;
}
code += '}\n';
}
return code;
}
function writeFunctionExport({legacy}, name) {
return legacy ? `exports.${name} = ${name};\n` : 'export ';
}
function getEnumValues(ctx) {
const enums = {};
const ids = Object.keys(ctx._proto.values);
for (let i = 0; i < ids.length; i++) {
enums[ids[i]] = ctx._proto.values[ids[i]].value;
}
return enums;
}
function writeEnum(ctx, {legacy}) {
const enums = JSON.stringify(getEnumValues(ctx), null, 4);
const name = ctx._name;
return `\n${legacy ? `const ${name} = exports.${name}` : `export const ${name}`} = ${enums};\n`;
}
function compileDest(ctx) {
const props = new Set();
for (const {name, oneof} of ctx._proto.fields) {
props.add(`${name}: ${JSON.stringify(ctx._defaults[name])}`);
if (oneof) props.add(`${oneof }: undefined`);
}
return `{${[...props].join(', ')}}`;
}
function isEnum(type) {
return type && type._proto.values;
}
function getType(ctx, field) {
if (field.type === 'map') {
return ctx[getMapMessageName(field.tag)];
}
const path = field.type.split('.');
return path.reduce((ctx, name) => ctx && ctx[name], ctx);
}
function fieldShouldUseStringAsNumber(field) {
if (field.options.jstype === 'JS_STRING') {
switch (field.type) {
case 'float':
case 'double':
case 'uint32':
case 'uint64':
case 'int32':
case 'int64':
case 'sint32':
case 'sint64':
case 'fixed32':
case 'fixed64':
case 'sfixed32':
case 'sfixed64': return true;
default: return false;
}
}
return false;
}
function compileFieldRead(ctx, field) {
const type = getType(ctx, field);
if (type) {
if (type._proto.fields) return `read${type._name}(pbf, pbf.readVarint() + pbf.pos)`;
if (!isEnum(type)) throw new Error(`Unexpected type: ${type._name}`);
}
const fieldType = isEnum(type) ? 'enum' : field.type;
let prefix = 'pbf.read';
const signed = fieldType === 'int32' || fieldType === 'int64' ? 'true' : '';
let suffix = `(${signed})`;
if (willSupportPacked(ctx, field)) {
prefix += 'Packed';
suffix = `(obj.${field.name}${signed ? `, ${signed}` : ''})`;
}
if (fieldShouldUseStringAsNumber(field)) {
suffix += '.toString()';
}
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) {
let prefix = 'pbf.write';
if (isPacked(field)) prefix += 'Packed';
if (fieldShouldUseStringAsNumber(field)) {
if (field.type === 'float' || field.type === 'double') {
name = `parseFloat(${name})`;
} else {
name = `parseInt(${name}, 10)`;
}
}
const postfix = `${isPacked(field) ? '' : 'Field'}(${field.tag}, ${name})`;
const type = getType(ctx, field);
if (type) {
if (type._proto.fields) return `${prefix}Message(${field.tag}, write${type._name}, ${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) {
return `const {key, value} = ${readCode}; obj.${name}[key] = value`;
}
function compileRepeatedWrite(ctx, field) {
return `for (const item of obj.${field.name}) ${
compileFieldWrite(ctx, field, 'item')}`;
}
function compileMapWrite(ctx, field) {
const name = `obj.${field.name}`;
return `for (const key of Object.keys(${name})) ${
compileFieldWrite(ctx, field, `{key, value: ${name}[key]}`)}`;
}
function getMapMessageName(tag) {
return `_FieldEntry${tag}`;
}
function getMapField(name, type, tag) {
return {
name,
type,
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) {
const obj = Object.create(parent);
obj._proto = proto;
obj._children = [];
obj._defaults = {};
if (parent) {
parent[proto.name] = obj;
if (parent._name) {
obj._name = parent._name + proto.name;
} else {
obj._name = proto.name;
}
}
for (let i = 0; proto.enums && i < proto.enums.length; i++) {
obj._children.push(buildContext(proto.enums[i], obj));
}
for (let i = 0; proto.messages && i < proto.messages.length; i++) {
obj._children.push(buildContext(proto.messages[i], obj));
}
for (let 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 [];
let convertToStringIfNeeded = function (val) { return val; };
if (fieldShouldUseStringAsNumber(field)) {
convertToStringIfNeeded = function (val) { return val.toString(); };
}
switch (field.type) {
case 'float':
case 'double': return convertToStringIfNeeded(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 convertToStringIfNeeded(value ? parseInt(value, 10) : 0);
case 'string': return value || '';
case 'bool': return value === 'true';
case 'map': return {};
default: return undefined;
}
}
function willSupportPacked(ctx, field) {
const 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) {
const options = field.options;
const type = getType(ctx, field);
const enumValues = type && type._proto.values && getEnumValues(type);
// Proto3 does not support overriding defaults
const 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) {
const proto = ctx._proto;
for (let i = 0; i < ctx._children.length; i++) {
buildDefaults(ctx._children[i], syntax);
}
if (proto.fields) {
for (let 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) {
const def = ctx._defaults[field.name];
const type = getType(ctx, field);
let code = ` if (obj.${field.name}`;
if (!field.repeated && (!type || !type._proto.fields)) {
if (def === undefined || def || field.oneof) {
code += ' != null';
}
if (def) {
code += ` && obj.${field.name} !== ${JSON.stringify(def)}`;
}
}
return `${code}) `;
}
function isPacked(field) {
return field.options.packed === 'true';
}
+838
View File
@@ -0,0 +1,838 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Pbf = factory());
})(this, (function () { 'use strict';
const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
const 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)
const TEXT_DECODER_MIN_LENGTH = 12;
const utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8');
const PBF_VARINT = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum
const PBF_FIXED64 = 1; // 64-bit: double, fixed64, sfixed64
const PBF_BYTES = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields
const PBF_FIXED32 = 5; // 32-bit: float, fixed32, sfixed32
class Pbf {
/**
* @param {Uint8Array | ArrayBuffer} [buf]
*/
constructor(buf = new Uint8Array(16)) {
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.dataView = new DataView(this.buf.buffer);
this.pos = 0;
this.type = 0;
this.length = this.buf.length;
}
// === READING =================================================================
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
* @param {number} [end]
*/
readFields(readField, result, end = this.length) {
while (this.pos < end) {
const 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;
}
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
*/
readMessage(readField, result) {
return this.readFields(readField, result, this.readVarint() + this.pos);
}
readFixed32() {
const val = this.dataView.getUint32(this.pos, true);
this.pos += 4;
return val;
}
readSFixed32() {
const val = this.dataView.getInt32(this.pos, true);
this.pos += 4;
return val;
}
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
readFixed64() {
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getUint32(this.pos + 4, true) * SHIFT_LEFT_32;
this.pos += 8;
return val;
}
readSFixed64() {
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getInt32(this.pos + 4, true) * SHIFT_LEFT_32;
this.pos += 8;
return val;
}
readFloat() {
const val = this.dataView.getFloat32(this.pos, true);
this.pos += 4;
return val;
}
readDouble() {
const val = this.dataView.getFloat64(this.pos, true);
this.pos += 8;
return val;
}
/**
* @param {boolean} [isSigned]
*/
readVarint(isSigned) {
const buf = this.buf;
let 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() { // for compatibility with v2.0.1
return this.readVarint(true);
}
readSVarint() {
const num = this.readVarint();
return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding
}
readBoolean() {
return Boolean(this.readVarint());
}
readString() {
const end = this.readVarint() + this.pos;
const 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 utf8TextDecoder.decode(this.buf.subarray(pos, end));
}
// short strings are fast with our custom implementation
return readUtf8(this.buf, pos, end);
}
readBytes() {
const 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
/**
* @param {number[]} [arr]
* @param {boolean} [isSigned]
*/
readPackedVarint(arr = [], isSigned) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readVarint(isSigned));
return arr;
}
/** @param {number[]} [arr] */
readPackedSVarint(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSVarint());
return arr;
}
/** @param {boolean[]} [arr] */
readPackedBoolean(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readBoolean());
return arr;
}
/** @param {number[]} [arr] */
readPackedFloat(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFloat());
return arr;
}
/** @param {number[]} [arr] */
readPackedDouble(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readDouble());
return arr;
}
/** @param {number[]} [arr] */
readPackedFixed32(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFixed32());
return arr;
}
/** @param {number[]} [arr] */
readPackedSFixed32(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSFixed32());
return arr;
}
/** @param {number[]} [arr] */
readPackedFixed64(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFixed64());
return arr;
}
/** @param {number[]} [arr] */
readPackedSFixed64(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSFixed64());
return arr;
}
readPackedEnd() {
return this.type === PBF_BYTES ? this.readVarint() + this.pos : this.pos + 1;
}
/** @param {number} val */
skip(val) {
const 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 =================================================================
/**
* @param {number} tag
* @param {number} type
*/
writeTag(tag, type) {
this.writeVarint((tag << 3) | type);
}
/** @param {number} min */
realloc(min) {
let length = this.length || 16;
while (length < this.pos + min) length *= 2;
if (length !== this.length) {
const buf = new Uint8Array(length);
buf.set(this.buf);
this.buf = buf;
this.dataView = new DataView(buf.buffer);
this.length = length;
}
}
finish() {
this.length = this.pos;
this.pos = 0;
return this.buf.subarray(0, this.length);
}
/** @param {number} val */
writeFixed32(val) {
this.realloc(4);
this.dataView.setInt32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeSFixed32(val) {
this.realloc(4);
this.dataView.setInt32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeFixed64(val) {
this.realloc(8);
this.dataView.setInt32(this.pos, val & -1, true);
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
this.pos += 8;
}
/** @param {number} val */
writeSFixed64(val) {
this.realloc(8);
this.dataView.setInt32(this.pos, val & -1, true);
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
this.pos += 8;
}
/** @param {number} val */
writeVarint(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;
}
/** @param {number} val */
writeSVarint(val) {
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
}
/** @param {boolean} val */
writeBoolean(val) {
this.writeVarint(+val);
}
/** @param {string} str */
writeString(str) {
str = String(str);
this.realloc(str.length * 4);
this.pos++; // reserve 1 byte for short string length
const startPos = this.pos;
// write the string directly to the buffer and see how much was written
this.pos = writeUtf8(this.buf, str, this.pos);
const 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;
}
/** @param {number} val */
writeFloat(val) {
this.realloc(4);
this.dataView.setFloat32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeDouble(val) {
this.realloc(8);
this.dataView.setFloat64(this.pos, val, true);
this.pos += 8;
}
/** @param {Uint8Array} buffer */
writeBytes(buffer) {
const len = buffer.length;
this.writeVarint(len);
this.realloc(len);
for (let i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
}
/**
* @template T
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeRawMessage(fn, obj) {
this.pos++; // reserve 1 byte for short message length
// write the message directly to the buffer and see how much was written
const startPos = this.pos;
fn(obj, this);
const 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;
}
/**
* @template T
* @param {number} tag
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeMessage(tag, fn, obj) {
this.writeTag(tag, PBF_BYTES);
this.writeRawMessage(fn, obj);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedVarint(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSVarint(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
}
/**
* @param {number} tag
* @param {boolean[]} arr
*/
writePackedBoolean(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFloat(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedDouble(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed32(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed32(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed64(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed64(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
}
/**
* @param {number} tag
* @param {Uint8Array} buffer
*/
writeBytesField(tag, buffer) {
this.writeTag(tag, PBF_BYTES);
this.writeBytes(buffer);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFixed32Field(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeFixed32(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed32Field(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeSFixed32(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFixed64Field(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeFixed64(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed64Field(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeSFixed64(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeVarintField(tag, val) {
this.writeTag(tag, PBF_VARINT);
this.writeVarint(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSVarintField(tag, val) {
this.writeTag(tag, PBF_VARINT);
this.writeSVarint(val);
}
/**
* @param {number} tag
* @param {string} str
*/
writeStringField(tag, str) {
this.writeTag(tag, PBF_BYTES);
this.writeString(str);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFloatField(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeFloat(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeDoubleField(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeDouble(val);
}
/**
* @param {number} tag
* @param {boolean} val
*/
writeBooleanField(tag, val) {
this.writeVarintField(tag, +val);
}
}
/**
* @param {number} l
* @param {boolean | undefined} s
* @param {Pbf} p
*/
function readVarintRemainder(l, s, p) {
const buf = p.buf;
let 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');
}
/**
* @param {number} low
* @param {number} high
* @param {boolean} [isSigned]
*/
function toNum(low, high, isSigned) {
return isSigned ? high * 0x100000000 + (low >>> 0) : ((high >>> 0) * 0x100000000) + (low >>> 0);
}
/**
* @param {number} val
* @param {Pbf} pbf
*/
function writeBigVarint(val, pbf) {
let 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);
}
/**
* @param {number} high
* @param {number} low
* @param {Pbf} 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;
}
/**
* @param {number} high
* @param {Pbf} pbf
*/
function writeBigVarintHigh(high, pbf) {
const 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;
}
/**
* @param {number} startPos
* @param {number} len
* @param {Pbf} pbf
*/
function makeRoomForExtraLength(startPos, len, pbf) {
const 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 (let i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedVarint(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSVarint(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFloat(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedDouble(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]);
}
/**
* @param {boolean[]} arr
* @param {Pbf} pbf
*/
function writePackedBoolean(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFixed32(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSFixed32(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFixed64(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSFixed64(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]);
}
// Buffer code below from https://github.com/feross/buffer, MIT-licensed
/**
* @param {Uint8Array} buf
* @param {number} pos
* @param {number} end
*/
function readUtf8(buf, pos, end) {
let str = '';
let i = pos;
while (i < end) {
const b0 = buf[i];
let c = null; // codepoint
let bytesPerSequence =
b0 > 0xEF ? 4 :
b0 > 0xDF ? 3 :
b0 > 0xBF ? 2 : 1;
if (i + bytesPerSequence > end) break;
let 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;
}
/**
* @param {Uint8Array} buf
* @param {string} str
* @param {number} pos
*/
function writeUtf8(buf, str, pos) {
for (let 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;
}
return Pbf;
}));
+1
View File
File diff suppressed because one or more lines are too long
+206
View File
@@ -0,0 +1,206 @@
export default class Pbf {
/**
* @param {Uint8Array | ArrayBuffer} [buf]
*/
constructor(buf?: ArrayBuffer | Uint8Array | undefined);
buf: Uint8Array;
dataView: DataView;
pos: number;
type: number;
length: number;
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
* @param {number} [end]
*/
readFields<T>(readField: (tag: number, result: T, pbf: Pbf) => void, result: T, end?: number | undefined): T;
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
*/
readMessage<T>(readField: (tag: number, result: T, pbf: Pbf) => void, result: T): T;
readFixed32(): number;
readSFixed32(): number;
readFixed64(): number;
readSFixed64(): number;
readFloat(): number;
readDouble(): number;
/**
* @param {boolean} [isSigned]
*/
readVarint(isSigned?: boolean | undefined): number;
readVarint64(): number;
readSVarint(): number;
readBoolean(): boolean;
readString(): string;
readBytes(): Uint8Array;
/**
* @param {number[]} [arr]
* @param {boolean} [isSigned]
*/
readPackedVarint(arr?: number[] | undefined, isSigned?: boolean | undefined): number[];
/** @param {number[]} [arr] */
readPackedSVarint(arr?: number[] | undefined): number[];
/** @param {boolean[]} [arr] */
readPackedBoolean(arr?: boolean[] | undefined): boolean[];
/** @param {number[]} [arr] */
readPackedFloat(arr?: number[] | undefined): number[];
/** @param {number[]} [arr] */
readPackedDouble(arr?: number[] | undefined): number[];
/** @param {number[]} [arr] */
readPackedFixed32(arr?: number[] | undefined): number[];
/** @param {number[]} [arr] */
readPackedSFixed32(arr?: number[] | undefined): number[];
/** @param {number[]} [arr] */
readPackedFixed64(arr?: number[] | undefined): number[];
/** @param {number[]} [arr] */
readPackedSFixed64(arr?: number[] | undefined): number[];
readPackedEnd(): number;
/** @param {number} val */
skip(val: number): void;
/**
* @param {number} tag
* @param {number} type
*/
writeTag(tag: number, type: number): void;
/** @param {number} min */
realloc(min: number): void;
finish(): Uint8Array;
/** @param {number} val */
writeFixed32(val: number): void;
/** @param {number} val */
writeSFixed32(val: number): void;
/** @param {number} val */
writeFixed64(val: number): void;
/** @param {number} val */
writeSFixed64(val: number): void;
/** @param {number} val */
writeVarint(val: number): void;
/** @param {number} val */
writeSVarint(val: number): void;
/** @param {boolean} val */
writeBoolean(val: boolean): void;
/** @param {string} str */
writeString(str: string): void;
/** @param {number} val */
writeFloat(val: number): void;
/** @param {number} val */
writeDouble(val: number): void;
/** @param {Uint8Array} buffer */
writeBytes(buffer: Uint8Array): void;
/**
* @template T
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeRawMessage<T>(fn: (obj: T, pbf: Pbf) => void, obj: T): void;
/**
* @template T
* @param {number} tag
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeMessage<T>(tag: number, fn: (obj: T, pbf: Pbf) => void, obj: T): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedVarint(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSVarint(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {boolean[]} arr
*/
writePackedBoolean(tag: number, arr: boolean[]): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFloat(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedDouble(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed32(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed32(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed64(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed64(tag: number, arr: number[]): void;
/**
* @param {number} tag
* @param {Uint8Array} buffer
*/
writeBytesField(tag: number, buffer: Uint8Array): void;
/**
* @param {number} tag
* @param {number} val
*/
writeFixed32Field(tag: number, val: number): void;
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed32Field(tag: number, val: number): void;
/**
* @param {number} tag
* @param {number} val
*/
writeFixed64Field(tag: number, val: number): void;
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed64Field(tag: number, val: number): void;
/**
* @param {number} tag
* @param {number} val
*/
writeVarintField(tag: number, val: number): void;
/**
* @param {number} tag
* @param {number} val
*/
writeSVarintField(tag: number, val: number): void;
/**
* @param {number} tag
* @param {string} str
*/
writeStringField(tag: number, str: string): void;
/**
* @param {number} tag
* @param {number} val
*/
writeFloatField(tag: number, val: number): void;
/**
* @param {number} tag
* @param {number} val
*/
writeDoubleField(tag: number, val: number): void;
/**
* @param {number} tag
* @param {boolean} val
*/
writeBooleanField(tag: number, val: boolean): void;
}
Generated Vendored
+830
View File
@@ -0,0 +1,830 @@
const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
const 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)
const TEXT_DECODER_MIN_LENGTH = 12;
const utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8');
const PBF_VARINT = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum
const PBF_FIXED64 = 1; // 64-bit: double, fixed64, sfixed64
const PBF_BYTES = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields
const PBF_FIXED32 = 5; // 32-bit: float, fixed32, sfixed32
export default class Pbf {
/**
* @param {Uint8Array | ArrayBuffer} [buf]
*/
constructor(buf = new Uint8Array(16)) {
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.dataView = new DataView(this.buf.buffer);
this.pos = 0;
this.type = 0;
this.length = this.buf.length;
}
// === READING =================================================================
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
* @param {number} [end]
*/
readFields(readField, result, end = this.length) {
while (this.pos < end) {
const 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;
}
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
*/
readMessage(readField, result) {
return this.readFields(readField, result, this.readVarint() + this.pos);
}
readFixed32() {
const val = this.dataView.getUint32(this.pos, true);
this.pos += 4;
return val;
}
readSFixed32() {
const val = this.dataView.getInt32(this.pos, true);
this.pos += 4;
return val;
}
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
readFixed64() {
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getUint32(this.pos + 4, true) * SHIFT_LEFT_32;
this.pos += 8;
return val;
}
readSFixed64() {
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getInt32(this.pos + 4, true) * SHIFT_LEFT_32;
this.pos += 8;
return val;
}
readFloat() {
const val = this.dataView.getFloat32(this.pos, true);
this.pos += 4;
return val;
}
readDouble() {
const val = this.dataView.getFloat64(this.pos, true);
this.pos += 8;
return val;
}
/**
* @param {boolean} [isSigned]
*/
readVarint(isSigned) {
const buf = this.buf;
let 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() { // for compatibility with v2.0.1
return this.readVarint(true);
}
readSVarint() {
const num = this.readVarint();
return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding
}
readBoolean() {
return Boolean(this.readVarint());
}
readString() {
const end = this.readVarint() + this.pos;
const 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 utf8TextDecoder.decode(this.buf.subarray(pos, end));
}
// short strings are fast with our custom implementation
return readUtf8(this.buf, pos, end);
}
readBytes() {
const 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
/**
* @param {number[]} [arr]
* @param {boolean} [isSigned]
*/
readPackedVarint(arr = [], isSigned) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readVarint(isSigned));
return arr;
}
/** @param {number[]} [arr] */
readPackedSVarint(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSVarint());
return arr;
}
/** @param {boolean[]} [arr] */
readPackedBoolean(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readBoolean());
return arr;
}
/** @param {number[]} [arr] */
readPackedFloat(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFloat());
return arr;
}
/** @param {number[]} [arr] */
readPackedDouble(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readDouble());
return arr;
}
/** @param {number[]} [arr] */
readPackedFixed32(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFixed32());
return arr;
}
/** @param {number[]} [arr] */
readPackedSFixed32(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSFixed32());
return arr;
}
/** @param {number[]} [arr] */
readPackedFixed64(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFixed64());
return arr;
}
/** @param {number[]} [arr] */
readPackedSFixed64(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSFixed64());
return arr;
}
readPackedEnd() {
return this.type === PBF_BYTES ? this.readVarint() + this.pos : this.pos + 1;
}
/** @param {number} val */
skip(val) {
const 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 =================================================================
/**
* @param {number} tag
* @param {number} type
*/
writeTag(tag, type) {
this.writeVarint((tag << 3) | type);
}
/** @param {number} min */
realloc(min) {
let length = this.length || 16;
while (length < this.pos + min) length *= 2;
if (length !== this.length) {
const buf = new Uint8Array(length);
buf.set(this.buf);
this.buf = buf;
this.dataView = new DataView(buf.buffer);
this.length = length;
}
}
finish() {
this.length = this.pos;
this.pos = 0;
return this.buf.subarray(0, this.length);
}
/** @param {number} val */
writeFixed32(val) {
this.realloc(4);
this.dataView.setInt32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeSFixed32(val) {
this.realloc(4);
this.dataView.setInt32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeFixed64(val) {
this.realloc(8);
this.dataView.setInt32(this.pos, val & -1, true);
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
this.pos += 8;
}
/** @param {number} val */
writeSFixed64(val) {
this.realloc(8);
this.dataView.setInt32(this.pos, val & -1, true);
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
this.pos += 8;
}
/** @param {number} val */
writeVarint(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;
}
/** @param {number} val */
writeSVarint(val) {
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
}
/** @param {boolean} val */
writeBoolean(val) {
this.writeVarint(+val);
}
/** @param {string} str */
writeString(str) {
str = String(str);
this.realloc(str.length * 4);
this.pos++; // reserve 1 byte for short string length
const startPos = this.pos;
// write the string directly to the buffer and see how much was written
this.pos = writeUtf8(this.buf, str, this.pos);
const 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;
}
/** @param {number} val */
writeFloat(val) {
this.realloc(4);
this.dataView.setFloat32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeDouble(val) {
this.realloc(8);
this.dataView.setFloat64(this.pos, val, true);
this.pos += 8;
}
/** @param {Uint8Array} buffer */
writeBytes(buffer) {
const len = buffer.length;
this.writeVarint(len);
this.realloc(len);
for (let i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
}
/**
* @template T
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeRawMessage(fn, obj) {
this.pos++; // reserve 1 byte for short message length
// write the message directly to the buffer and see how much was written
const startPos = this.pos;
fn(obj, this);
const 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;
}
/**
* @template T
* @param {number} tag
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeMessage(tag, fn, obj) {
this.writeTag(tag, PBF_BYTES);
this.writeRawMessage(fn, obj);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedVarint(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSVarint(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
}
/**
* @param {number} tag
* @param {boolean[]} arr
*/
writePackedBoolean(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFloat(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedDouble(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed32(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed32(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed64(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed64(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
}
/**
* @param {number} tag
* @param {Uint8Array} buffer
*/
writeBytesField(tag, buffer) {
this.writeTag(tag, PBF_BYTES);
this.writeBytes(buffer);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFixed32Field(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeFixed32(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed32Field(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeSFixed32(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFixed64Field(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeFixed64(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed64Field(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeSFixed64(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeVarintField(tag, val) {
this.writeTag(tag, PBF_VARINT);
this.writeVarint(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSVarintField(tag, val) {
this.writeTag(tag, PBF_VARINT);
this.writeSVarint(val);
}
/**
* @param {number} tag
* @param {string} str
*/
writeStringField(tag, str) {
this.writeTag(tag, PBF_BYTES);
this.writeString(str);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFloatField(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeFloat(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeDoubleField(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeDouble(val);
}
/**
* @param {number} tag
* @param {boolean} val
*/
writeBooleanField(tag, val) {
this.writeVarintField(tag, +val);
}
};
/**
* @param {number} l
* @param {boolean | undefined} s
* @param {Pbf} p
*/
function readVarintRemainder(l, s, p) {
const buf = p.buf;
let 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');
}
/**
* @param {number} low
* @param {number} high
* @param {boolean} [isSigned]
*/
function toNum(low, high, isSigned) {
return isSigned ? high * 0x100000000 + (low >>> 0) : ((high >>> 0) * 0x100000000) + (low >>> 0);
}
/**
* @param {number} val
* @param {Pbf} pbf
*/
function writeBigVarint(val, pbf) {
let 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);
}
/**
* @param {number} high
* @param {number} low
* @param {Pbf} 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;
}
/**
* @param {number} high
* @param {Pbf} pbf
*/
function writeBigVarintHigh(high, pbf) {
const 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;
}
/**
* @param {number} startPos
* @param {number} len
* @param {Pbf} pbf
*/
function makeRoomForExtraLength(startPos, len, pbf) {
const 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 (let i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedVarint(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSVarint(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFloat(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedDouble(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]);
}
/**
* @param {boolean[]} arr
* @param {Pbf} pbf
*/
function writePackedBoolean(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFixed32(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSFixed32(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFixed64(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSFixed64(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]);
}
// Buffer code below from https://github.com/feross/buffer, MIT-licensed
/**
* @param {Uint8Array} buf
* @param {number} pos
* @param {number} end
*/
function readUtf8(buf, pos, end) {
let str = '';
let i = pos;
while (i < end) {
const b0 = buf[i];
let c = null; // codepoint
let bytesPerSequence =
b0 > 0xEF ? 4 :
b0 > 0xDF ? 3 :
b0 > 0xBF ? 2 : 1;
if (i + bytesPerSequence > end) break;
let 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;
}
/**
* @param {Uint8Array} buf
* @param {string} str
* @param {number} pos
*/
function writeUtf8(buf, str, pos) {
for (let 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;
}
+64
View File
@@ -0,0 +1,64 @@
{
"name": "pbf",
"version": "4.0.1",
"description": "a low-level, lightweight protocol buffers implementation in JavaScript",
"main": "index.js",
"type": "module",
"exports": {
".": "./index.js",
"./compile": "./compile.js"
},
"scripts": {
"bench": "node bench/bench.js",
"pretest": "eslint *.js compile.js test/*.js test/fixtures/*.js bin/pbf",
"test": "tsc && node --test",
"cov": "node --test --experimental-test-covetage",
"build": "rollup -c",
"prepublishOnly": "npm run test && npm run build"
},
"files": [
"bin",
"dist",
"compile.js",
"index.d.ts"
],
"types": "index.d.ts",
"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": {
"resolve-protobuf-schema": "^2.1.0"
},
"devDependencies": {
"@rollup/plugin-terser": "^0.4.4",
"benchmark": "^2.1.4",
"eslint": "^9.6.0",
"eslint-config-mourner": "^4.0.1",
"protobufjs": "^7.3.2",
"protocol-buffers": "^5.0.0",
"rollup": "^4.18.0",
"tile-stats-runner": "^1.0.0",
"typescript": "^5.5.3"
}
}