initial commit of actions

This commit is contained in:
Dominik Polakovics Polakovics 2026-01-31 18:56:04 +01:00
commit 949ece5785
44660 changed files with 12034344 additions and 0 deletions

View file

@ -0,0 +1,43 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertFloat32 = exports.assertUInt32 = exports.assertInt32 = exports.assertNever = exports.assert = void 0;
/**
* assert that condition is true or throw error (with message)
*/
function assert(condition, msg) {
if (!condition) {
throw new Error(msg);
}
}
exports.assert = assert;
/**
* assert that value cannot exist = type `never`. throw runtime error if it does.
*/
function assertNever(value, msg) {
throw new Error(msg !== null && msg !== void 0 ? msg : 'Unexpected object: ' + value);
}
exports.assertNever = assertNever;
const FLOAT32_MAX = 3.4028234663852886e+38, FLOAT32_MIN = -3.4028234663852886e+38, UINT32_MAX = 0xFFFFFFFF, INT32_MAX = 0X7FFFFFFF, INT32_MIN = -0X80000000;
function assertInt32(arg) {
if (typeof arg !== "number")
throw new Error('invalid int 32: ' + typeof arg);
if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
throw new Error('invalid int 32: ' + arg);
}
exports.assertInt32 = assertInt32;
function assertUInt32(arg) {
if (typeof arg !== "number")
throw new Error('invalid uint 32: ' + typeof arg);
if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
throw new Error('invalid uint 32: ' + arg);
}
exports.assertUInt32 = assertUInt32;
function assertFloat32(arg) {
if (typeof arg !== "number")
throw new Error('invalid float 32: ' + typeof arg);
if (!Number.isFinite(arg))
return;
if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
throw new Error('invalid float 32: ' + arg);
}
exports.assertFloat32 = assertFloat32;

View file

@ -0,0 +1,118 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.base64encode = exports.base64decode = void 0;
// lookup table from base64 character to byte
let encTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
// lookup table from base64 character *code* to byte because lookup by number is fast
let decTable = [];
for (let i = 0; i < encTable.length; i++)
decTable[encTable[i].charCodeAt(0)] = i;
// support base64url variants
decTable["-".charCodeAt(0)] = encTable.indexOf("+");
decTable["_".charCodeAt(0)] = encTable.indexOf("/");
/**
* Decodes a base64 string to a byte array.
*
* - ignores white-space, including line breaks and tabs
* - allows inner padding (can decode concatenated base64 strings)
* - does not require padding
* - understands base64url encoding:
* "-" instead of "+",
* "_" instead of "/",
* no padding
*/
function base64decode(base64Str) {
// estimate byte size, not accounting for inner padding and whitespace
let es = base64Str.length * 3 / 4;
// if (es % 3 !== 0)
// throw new Error('invalid base64 string');
if (base64Str[base64Str.length - 2] == '=')
es -= 2;
else if (base64Str[base64Str.length - 1] == '=')
es -= 1;
let bytes = new Uint8Array(es), bytePos = 0, // position in byte array
groupPos = 0, // position in base64 group
b, // current byte
p = 0 // previous byte
;
for (let i = 0; i < base64Str.length; i++) {
b = decTable[base64Str.charCodeAt(i)];
if (b === undefined) {
// noinspection FallThroughInSwitchStatementJS
switch (base64Str[i]) {
case '=':
groupPos = 0; // reset state when padding found
case '\n':
case '\r':
case '\t':
case ' ':
continue; // skip white-space, and padding
default:
throw Error(`invalid base64 string.`);
}
}
switch (groupPos) {
case 0:
p = b;
groupPos = 1;
break;
case 1:
bytes[bytePos++] = p << 2 | (b & 48) >> 4;
p = b;
groupPos = 2;
break;
case 2:
bytes[bytePos++] = (p & 15) << 4 | (b & 60) >> 2;
p = b;
groupPos = 3;
break;
case 3:
bytes[bytePos++] = (p & 3) << 6 | b;
groupPos = 0;
break;
}
}
if (groupPos == 1)
throw Error(`invalid base64 string.`);
return bytes.subarray(0, bytePos);
}
exports.base64decode = base64decode;
/**
* Encodes a byte array to a base64 string.
* Adds padding at the end.
* Does not insert newlines.
*/
function base64encode(bytes) {
let base64 = '', groupPos = 0, // position in base64 group
b, // current byte
p = 0; // carry over from previous byte
for (let i = 0; i < bytes.length; i++) {
b = bytes[i];
switch (groupPos) {
case 0:
base64 += encTable[b >> 2];
p = (b & 3) << 4;
groupPos = 1;
break;
case 1:
base64 += encTable[p | b >> 4];
p = (b & 15) << 2;
groupPos = 2;
break;
case 2:
base64 += encTable[p | b >> 6];
base64 += encTable[b & 63];
groupPos = 0;
break;
}
}
// padding required?
if (groupPos) {
base64 += encTable[p];
base64 += '=';
if (groupPos == 1)
base64 += '=';
}
return base64;
}
exports.base64encode = base64encode;

View file

@ -0,0 +1,100 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WireType = exports.mergeBinaryOptions = exports.UnknownFieldHandler = void 0;
/**
* This handler implements the default behaviour for unknown fields.
* When reading data, unknown fields are stored on the message, in a
* symbol property.
* When writing data, the symbol property is queried and unknown fields
* are serialized into the output again.
*/
var UnknownFieldHandler;
(function (UnknownFieldHandler) {
/**
* The symbol used to store unknown fields for a message.
* The property must conform to `UnknownFieldContainer`.
*/
UnknownFieldHandler.symbol = Symbol.for("protobuf-ts/unknown");
/**
* Store an unknown field during binary read directly on the message.
* This method is compatible with `BinaryReadOptions.readUnknownField`.
*/
UnknownFieldHandler.onRead = (typeName, message, fieldNo, wireType, data) => {
let container = is(message) ? message[UnknownFieldHandler.symbol] : message[UnknownFieldHandler.symbol] = [];
container.push({ no: fieldNo, wireType, data });
};
/**
* Write unknown fields stored for the message to the writer.
* This method is compatible with `BinaryWriteOptions.writeUnknownFields`.
*/
UnknownFieldHandler.onWrite = (typeName, message, writer) => {
for (let { no, wireType, data } of UnknownFieldHandler.list(message))
writer.tag(no, wireType).raw(data);
};
/**
* List unknown fields stored for the message.
* Note that there may be multiples fields with the same number.
*/
UnknownFieldHandler.list = (message, fieldNo) => {
if (is(message)) {
let all = message[UnknownFieldHandler.symbol];
return fieldNo ? all.filter(uf => uf.no == fieldNo) : all;
}
return [];
};
/**
* Returns the last unknown field by field number.
*/
UnknownFieldHandler.last = (message, fieldNo) => UnknownFieldHandler.list(message, fieldNo).slice(-1)[0];
const is = (message) => message && Array.isArray(message[UnknownFieldHandler.symbol]);
})(UnknownFieldHandler = exports.UnknownFieldHandler || (exports.UnknownFieldHandler = {}));
/**
* Merges binary write or read options. Later values override earlier values.
*/
function mergeBinaryOptions(a, b) {
return Object.assign(Object.assign({}, a), b);
}
exports.mergeBinaryOptions = mergeBinaryOptions;
/**
* Protobuf binary format wire types.
*
* A wire type provides just enough information to find the length of the
* following value.
*
* See https://developers.google.com/protocol-buffers/docs/encoding#structure
*/
var WireType;
(function (WireType) {
/**
* Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
*/
WireType[WireType["Varint"] = 0] = "Varint";
/**
* Used for fixed64, sfixed64, double.
* Always 8 bytes with little-endian byte order.
*/
WireType[WireType["Bit64"] = 1] = "Bit64";
/**
* Used for string, bytes, embedded messages, packed repeated fields
*
* Only repeated numeric types (types which use the varint, 32-bit,
* or 64-bit wire types) can be packed. In proto3, such fields are
* packed by default.
*/
WireType[WireType["LengthDelimited"] = 2] = "LengthDelimited";
/**
* Used for groups
* @deprecated
*/
WireType[WireType["StartGroup"] = 3] = "StartGroup";
/**
* Used for groups
* @deprecated
*/
WireType[WireType["EndGroup"] = 4] = "EndGroup";
/**
* Used for fixed32, sfixed32, float.
* Always 4 bytes with little-endian byte order.
*/
WireType[WireType["Bit32"] = 5] = "Bit32";
})(WireType = exports.WireType || (exports.WireType = {}));

View file

@ -0,0 +1,183 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryReader = exports.binaryReadOptions = void 0;
const binary_format_contract_1 = require("./binary-format-contract");
const pb_long_1 = require("./pb-long");
const goog_varint_1 = require("./goog-varint");
const defaultsRead = {
readUnknownField: true,
readerFactory: bytes => new BinaryReader(bytes),
};
/**
* Make options for reading binary data form partial options.
*/
function binaryReadOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsRead), options) : defaultsRead;
}
exports.binaryReadOptions = binaryReadOptions;
class BinaryReader {
constructor(buf, textDecoder) {
this.varint64 = goog_varint_1.varint64read; // dirty cast for `this`
/**
* Read a `uint32` field, an unsigned 32 bit varint.
*/
this.uint32 = goog_varint_1.varint32read; // dirty cast for `this` and access to protected `buf`
this.buf = buf;
this.len = buf.length;
this.pos = 0;
this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
this.textDecoder = textDecoder !== null && textDecoder !== void 0 ? textDecoder : new TextDecoder("utf-8", {
fatal: true,
ignoreBOM: true,
});
}
/**
* Reads a tag - field number and wire type.
*/
tag() {
let tag = this.uint32(), fieldNo = tag >>> 3, wireType = tag & 7;
if (fieldNo <= 0 || wireType < 0 || wireType > 5)
throw new Error("illegal tag: field no " + fieldNo + " wire type " + wireType);
return [fieldNo, wireType];
}
/**
* Skip one element on the wire and return the skipped data.
* Supports WireType.StartGroup since v2.0.0-alpha.23.
*/
skip(wireType) {
let start = this.pos;
// noinspection FallThroughInSwitchStatementJS
switch (wireType) {
case binary_format_contract_1.WireType.Varint:
while (this.buf[this.pos++] & 0x80) {
// ignore
}
break;
case binary_format_contract_1.WireType.Bit64:
this.pos += 4;
case binary_format_contract_1.WireType.Bit32:
this.pos += 4;
break;
case binary_format_contract_1.WireType.LengthDelimited:
let len = this.uint32();
this.pos += len;
break;
case binary_format_contract_1.WireType.StartGroup:
// From descriptor.proto: Group type is deprecated, not supported in proto3.
// But we must still be able to parse and treat as unknown.
let t;
while ((t = this.tag()[1]) !== binary_format_contract_1.WireType.EndGroup) {
this.skip(t);
}
break;
default:
throw new Error("cant skip wire type " + wireType);
}
this.assertBounds();
return this.buf.subarray(start, this.pos);
}
/**
* Throws error if position in byte array is out of range.
*/
assertBounds() {
if (this.pos > this.len)
throw new RangeError("premature EOF");
}
/**
* Read a `int32` field, a signed 32 bit varint.
*/
int32() {
return this.uint32() | 0;
}
/**
* Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
*/
sint32() {
let zze = this.uint32();
// decode zigzag
return (zze >>> 1) ^ -(zze & 1);
}
/**
* Read a `int64` field, a signed 64-bit varint.
*/
int64() {
return new pb_long_1.PbLong(...this.varint64());
}
/**
* Read a `uint64` field, an unsigned 64-bit varint.
*/
uint64() {
return new pb_long_1.PbULong(...this.varint64());
}
/**
* Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
*/
sint64() {
let [lo, hi] = this.varint64();
// decode zig zag
let s = -(lo & 1);
lo = ((lo >>> 1 | (hi & 1) << 31) ^ s);
hi = (hi >>> 1 ^ s);
return new pb_long_1.PbLong(lo, hi);
}
/**
* Read a `bool` field, a variant.
*/
bool() {
let [lo, hi] = this.varint64();
return lo !== 0 || hi !== 0;
}
/**
* Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
*/
fixed32() {
return this.view.getUint32((this.pos += 4) - 4, true);
}
/**
* Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
*/
sfixed32() {
return this.view.getInt32((this.pos += 4) - 4, true);
}
/**
* Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
*/
fixed64() {
return new pb_long_1.PbULong(this.sfixed32(), this.sfixed32());
}
/**
* Read a `fixed64` field, a signed, fixed-length 64-bit integer.
*/
sfixed64() {
return new pb_long_1.PbLong(this.sfixed32(), this.sfixed32());
}
/**
* Read a `float` field, 32-bit floating point number.
*/
float() {
return this.view.getFloat32((this.pos += 4) - 4, true);
}
/**
* Read a `double` field, a 64-bit floating point number.
*/
double() {
return this.view.getFloat64((this.pos += 8) - 8, true);
}
/**
* Read a `bytes` field, length-delimited arbitrary data.
*/
bytes() {
let len = this.uint32();
let start = this.pos;
this.pos += len;
this.assertBounds();
return this.buf.subarray(start, start + len);
}
/**
* Read a `string` field, length-delimited data converted to UTF-8 text.
*/
string() {
return this.textDecoder.decode(this.bytes());
}
}
exports.BinaryReader = BinaryReader;

View file

@ -0,0 +1,232 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryWriter = exports.binaryWriteOptions = void 0;
const pb_long_1 = require("./pb-long");
const goog_varint_1 = require("./goog-varint");
const assert_1 = require("./assert");
const defaultsWrite = {
writeUnknownFields: true,
writerFactory: () => new BinaryWriter(),
};
/**
* Make options for writing binary data form partial options.
*/
function binaryWriteOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsWrite), options) : defaultsWrite;
}
exports.binaryWriteOptions = binaryWriteOptions;
class BinaryWriter {
constructor(textEncoder) {
/**
* Previous fork states.
*/
this.stack = [];
this.textEncoder = textEncoder !== null && textEncoder !== void 0 ? textEncoder : new TextEncoder();
this.chunks = [];
this.buf = [];
}
/**
* Return all bytes written and reset this writer.
*/
finish() {
this.chunks.push(new Uint8Array(this.buf)); // flush the buffer
let len = 0;
for (let i = 0; i < this.chunks.length; i++)
len += this.chunks[i].length;
let bytes = new Uint8Array(len);
let offset = 0;
for (let i = 0; i < this.chunks.length; i++) {
bytes.set(this.chunks[i], offset);
offset += this.chunks[i].length;
}
this.chunks = [];
return bytes;
}
/**
* Start a new fork for length-delimited data like a message
* or a packed repeated field.
*
* Must be joined later with `join()`.
*/
fork() {
this.stack.push({ chunks: this.chunks, buf: this.buf });
this.chunks = [];
this.buf = [];
return this;
}
/**
* Join the last fork. Write its length and bytes, then
* return to the previous state.
*/
join() {
// get chunk of fork
let chunk = this.finish();
// restore previous state
let prev = this.stack.pop();
if (!prev)
throw new Error('invalid state, fork stack empty');
this.chunks = prev.chunks;
this.buf = prev.buf;
// write length of chunk as varint
this.uint32(chunk.byteLength);
return this.raw(chunk);
}
/**
* Writes a tag (field number and wire type).
*
* Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
*
* Generated code should compute the tag ahead of time and call `uint32()`.
*/
tag(fieldNo, type) {
return this.uint32((fieldNo << 3 | type) >>> 0);
}
/**
* Write a chunk of raw bytes.
*/
raw(chunk) {
if (this.buf.length) {
this.chunks.push(new Uint8Array(this.buf));
this.buf = [];
}
this.chunks.push(chunk);
return this;
}
/**
* Write a `uint32` value, an unsigned 32 bit varint.
*/
uint32(value) {
assert_1.assertUInt32(value);
// write value as varint 32, inlined for speed
while (value > 0x7f) {
this.buf.push((value & 0x7f) | 0x80);
value = value >>> 7;
}
this.buf.push(value);
return this;
}
/**
* Write a `int32` value, a signed 32 bit varint.
*/
int32(value) {
assert_1.assertInt32(value);
goog_varint_1.varint32write(value, this.buf);
return this;
}
/**
* Write a `bool` value, a variant.
*/
bool(value) {
this.buf.push(value ? 1 : 0);
return this;
}
/**
* Write a `bytes` value, length-delimited arbitrary data.
*/
bytes(value) {
this.uint32(value.byteLength); // write length of chunk as varint
return this.raw(value);
}
/**
* Write a `string` value, length-delimited data converted to UTF-8 text.
*/
string(value) {
let chunk = this.textEncoder.encode(value);
this.uint32(chunk.byteLength); // write length of chunk as varint
return this.raw(chunk);
}
/**
* Write a `float` value, 32-bit floating point number.
*/
float(value) {
assert_1.assertFloat32(value);
let chunk = new Uint8Array(4);
new DataView(chunk.buffer).setFloat32(0, value, true);
return this.raw(chunk);
}
/**
* Write a `double` value, a 64-bit floating point number.
*/
double(value) {
let chunk = new Uint8Array(8);
new DataView(chunk.buffer).setFloat64(0, value, true);
return this.raw(chunk);
}
/**
* Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
*/
fixed32(value) {
assert_1.assertUInt32(value);
let chunk = new Uint8Array(4);
new DataView(chunk.buffer).setUint32(0, value, true);
return this.raw(chunk);
}
/**
* Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
*/
sfixed32(value) {
assert_1.assertInt32(value);
let chunk = new Uint8Array(4);
new DataView(chunk.buffer).setInt32(0, value, true);
return this.raw(chunk);
}
/**
* Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
*/
sint32(value) {
assert_1.assertInt32(value);
// zigzag encode
value = ((value << 1) ^ (value >> 31)) >>> 0;
goog_varint_1.varint32write(value, this.buf);
return this;
}
/**
* Write a `fixed64` value, a signed, fixed-length 64-bit integer.
*/
sfixed64(value) {
let chunk = new Uint8Array(8);
let view = new DataView(chunk.buffer);
let long = pb_long_1.PbLong.from(value);
view.setInt32(0, long.lo, true);
view.setInt32(4, long.hi, true);
return this.raw(chunk);
}
/**
* Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
*/
fixed64(value) {
let chunk = new Uint8Array(8);
let view = new DataView(chunk.buffer);
let long = pb_long_1.PbULong.from(value);
view.setInt32(0, long.lo, true);
view.setInt32(4, long.hi, true);
return this.raw(chunk);
}
/**
* Write a `int64` value, a signed 64-bit varint.
*/
int64(value) {
let long = pb_long_1.PbLong.from(value);
goog_varint_1.varint64write(long.lo, long.hi, this.buf);
return this;
}
/**
* Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
*/
sint64(value) {
let long = pb_long_1.PbLong.from(value),
// zigzag encode
sign = long.hi >> 31, lo = (long.lo << 1) ^ sign, hi = ((long.hi << 1) | (long.lo >>> 31)) ^ sign;
goog_varint_1.varint64write(lo, hi, this.buf);
return this;
}
/**
* Write a `uint64` value, an unsigned 64-bit varint.
*/
uint64(value) {
let long = pb_long_1.PbULong.from(value);
goog_varint_1.varint64write(long.lo, long.hi, this.buf);
return this;
}
}
exports.BinaryWriter = BinaryWriter;

View file

@ -0,0 +1,87 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listEnumNumbers = exports.listEnumNames = exports.listEnumValues = exports.isEnumObject = void 0;
/**
* Is this a lookup object generated by Typescript, for a Typescript enum
* generated by protobuf-ts?
*
* - No `const enum` (enum must not be inlined, we need reverse mapping).
* - No string enum (we need int32 for protobuf).
* - Must have a value for 0 (otherwise, we would need to support custom default values).
*/
function isEnumObject(arg) {
if (typeof arg != 'object' || arg === null) {
return false;
}
if (!arg.hasOwnProperty(0)) {
return false;
}
for (let k of Object.keys(arg)) {
let num = parseInt(k);
if (!Number.isNaN(num)) {
// is there a name for the number?
let nam = arg[num];
if (nam === undefined)
return false;
// does the name resolve back to the number?
if (arg[nam] !== num)
return false;
}
else {
// is there a number for the name?
let num = arg[k];
if (num === undefined)
return false;
// is it a string enum?
if (typeof num !== 'number')
return false;
// do we know the number?
if (arg[num] === undefined)
return false;
}
}
return true;
}
exports.isEnumObject = isEnumObject;
/**
* Lists all values of a Typescript enum, as an array of objects with a "name"
* property and a "number" property.
*
* Note that it is possible that a number appears more than once, because it is
* possible to have aliases in an enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
function listEnumValues(enumObject) {
if (!isEnumObject(enumObject))
throw new Error("not a typescript enum object");
let values = [];
for (let [name, number] of Object.entries(enumObject))
if (typeof number == "number")
values.push({ name, number });
return values;
}
exports.listEnumValues = listEnumValues;
/**
* Lists the names of a Typescript enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
function listEnumNames(enumObject) {
return listEnumValues(enumObject).map(val => val.name);
}
exports.listEnumNames = listEnumNames;
/**
* Lists the numbers of a Typescript enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
function listEnumNumbers(enumObject) {
return listEnumValues(enumObject)
.map(val => val.number)
.filter((num, index, arr) => arr.indexOf(num) == index);
}
exports.listEnumNumbers = listEnumNumbers;

View file

@ -0,0 +1,274 @@
"use strict";
// Copyright 2008 Google Inc. 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 Google Inc. 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
// OWNER 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.
//
// Code generated by the Protocol Buffer compiler is owned by the owner
// of the input file used when generating it. This code is not
// standalone and requires a support library to be linked with it. This
// support library is itself covered by the above license.
Object.defineProperty(exports, "__esModule", { value: true });
exports.varint32read = exports.varint32write = exports.int64toString = exports.int64fromString = exports.varint64write = exports.varint64read = void 0;
/**
* Read a 64 bit varint as two JS numbers.
*
* Returns tuple:
* [0]: low bits
* [0]: high bits
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L175
*/
function varint64read() {
let lowBits = 0;
let highBits = 0;
for (let shift = 0; shift < 28; shift += 7) {
let b = this.buf[this.pos++];
lowBits |= (b & 0x7F) << shift;
if ((b & 0x80) == 0) {
this.assertBounds();
return [lowBits, highBits];
}
}
let middleByte = this.buf[this.pos++];
// last four bits of the first 32 bit number
lowBits |= (middleByte & 0x0F) << 28;
// 3 upper bits are part of the next 32 bit number
highBits = (middleByte & 0x70) >> 4;
if ((middleByte & 0x80) == 0) {
this.assertBounds();
return [lowBits, highBits];
}
for (let shift = 3; shift <= 31; shift += 7) {
let b = this.buf[this.pos++];
highBits |= (b & 0x7F) << shift;
if ((b & 0x80) == 0) {
this.assertBounds();
return [lowBits, highBits];
}
}
throw new Error('invalid varint');
}
exports.varint64read = varint64read;
/**
* Write a 64 bit varint, given as two JS numbers, to the given bytes array.
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
*/
function varint64write(lo, hi, bytes) {
for (let i = 0; i < 28; i = i + 7) {
const shift = lo >>> i;
const hasNext = !((shift >>> 7) == 0 && hi == 0);
const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
bytes.push(byte);
if (!hasNext) {
return;
}
}
const splitBits = ((lo >>> 28) & 0x0F) | ((hi & 0x07) << 4);
const hasMoreBits = !((hi >> 3) == 0);
bytes.push((hasMoreBits ? splitBits | 0x80 : splitBits) & 0xFF);
if (!hasMoreBits) {
return;
}
for (let i = 3; i < 31; i = i + 7) {
const shift = hi >>> i;
const hasNext = !((shift >>> 7) == 0);
const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
bytes.push(byte);
if (!hasNext) {
return;
}
}
bytes.push((hi >>> 31) & 0x01);
}
exports.varint64write = varint64write;
// constants for binary math
const TWO_PWR_32_DBL = (1 << 16) * (1 << 16);
/**
* Parse decimal string of 64 bit integer value as two JS numbers.
*
* Returns tuple:
* [0]: minus sign?
* [1]: low bits
* [2]: high bits
*
* Copyright 2008 Google Inc.
*/
function int64fromString(dec) {
// Check for minus sign.
let minus = dec[0] == '-';
if (minus)
dec = dec.slice(1);
// Work 6 decimal digits at a time, acting like we're converting base 1e6
// digits to binary. This is safe to do with floating point math because
// Number.isSafeInteger(ALL_32_BITS * 1e6) == true.
const base = 1e6;
let lowBits = 0;
let highBits = 0;
function add1e6digit(begin, end) {
// Note: Number('') is 0.
const digit1e6 = Number(dec.slice(begin, end));
highBits *= base;
lowBits = lowBits * base + digit1e6;
// Carry bits from lowBits to highBits
if (lowBits >= TWO_PWR_32_DBL) {
highBits = highBits + ((lowBits / TWO_PWR_32_DBL) | 0);
lowBits = lowBits % TWO_PWR_32_DBL;
}
}
add1e6digit(-24, -18);
add1e6digit(-18, -12);
add1e6digit(-12, -6);
add1e6digit(-6);
return [minus, lowBits, highBits];
}
exports.int64fromString = int64fromString;
/**
* Format 64 bit integer value (as two JS numbers) to decimal string.
*
* Copyright 2008 Google Inc.
*/
function int64toString(bitsLow, bitsHigh) {
// Skip the expensive conversion if the number is small enough to use the
// built-in conversions.
if ((bitsHigh >>> 0) <= 0x1FFFFF) {
return '' + (TWO_PWR_32_DBL * bitsHigh + (bitsLow >>> 0));
}
// What this code is doing is essentially converting the input number from
// base-2 to base-1e7, which allows us to represent the 64-bit range with
// only 3 (very large) digits. Those digits are then trivial to convert to
// a base-10 string.
// The magic numbers used here are -
// 2^24 = 16777216 = (1,6777216) in base-1e7.
// 2^48 = 281474976710656 = (2,8147497,6710656) in base-1e7.
// Split 32:32 representation into 16:24:24 representation so our
// intermediate digits don't overflow.
let low = bitsLow & 0xFFFFFF;
let mid = (((bitsLow >>> 24) | (bitsHigh << 8)) >>> 0) & 0xFFFFFF;
let high = (bitsHigh >> 16) & 0xFFFF;
// Assemble our three base-1e7 digits, ignoring carries. The maximum
// value in a digit at this step is representable as a 48-bit integer, which
// can be stored in a 64-bit floating point number.
let digitA = low + (mid * 6777216) + (high * 6710656);
let digitB = mid + (high * 8147497);
let digitC = (high * 2);
// Apply carries from A to B and from B to C.
let base = 10000000;
if (digitA >= base) {
digitB += Math.floor(digitA / base);
digitA %= base;
}
if (digitB >= base) {
digitC += Math.floor(digitB / base);
digitB %= base;
}
// Convert base-1e7 digits to base-10, with optional leading zeroes.
function decimalFrom1e7(digit1e7, needLeadingZeros) {
let partial = digit1e7 ? String(digit1e7) : '';
if (needLeadingZeros) {
return '0000000'.slice(partial.length) + partial;
}
return partial;
}
return decimalFrom1e7(digitC, /*needLeadingZeros=*/ 0) +
decimalFrom1e7(digitB, /*needLeadingZeros=*/ digitC) +
// If the final 1e7 digit didn't need leading zeros, we would have
// returned via the trivial code path at the top.
decimalFrom1e7(digitA, /*needLeadingZeros=*/ 1);
}
exports.int64toString = int64toString;
/**
* Write a 32 bit varint, signed or unsigned. Same as `varint64write(0, value, bytes)`
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/1b18833f4f2a2f681f4e4a25cdf3b0a43115ec26/js/binary/encoder.js#L144
*/
function varint32write(value, bytes) {
if (value >= 0) {
// write value as varint 32
while (value > 0x7f) {
bytes.push((value & 0x7f) | 0x80);
value = value >>> 7;
}
bytes.push(value);
}
else {
for (let i = 0; i < 9; i++) {
bytes.push(value & 127 | 128);
value = value >> 7;
}
bytes.push(1);
}
}
exports.varint32write = varint32write;
/**
* Read an unsigned 32 bit varint.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220
*/
function varint32read() {
let b = this.buf[this.pos++];
let result = b & 0x7F;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
b = this.buf[this.pos++];
result |= (b & 0x7F) << 7;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
b = this.buf[this.pos++];
result |= (b & 0x7F) << 14;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
b = this.buf[this.pos++];
result |= (b & 0x7F) << 21;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
// Extract only last 4 bits
b = this.buf[this.pos++];
result |= (b & 0x0F) << 28;
for (let readBytes = 5; ((b & 0x80) !== 0) && readBytes < 10; readBytes++)
b = this.buf[this.pos++];
if ((b & 0x80) != 0)
throw new Error('invalid varint');
this.assertBounds();
// Result can have 32 bits, convert it to unsigned
return result >>> 0;
}
exports.varint32read = varint32read;

View file

@ -0,0 +1,97 @@
"use strict";
// Public API of the protobuf-ts runtime.
// Note: we do not use `export * from ...` to help tree shakers,
// webpack verbose output hints that this should be useful
Object.defineProperty(exports, "__esModule", { value: true });
// Convenience JSON typings and corresponding type guards
var json_typings_1 = require("./json-typings");
Object.defineProperty(exports, "typeofJsonValue", { enumerable: true, get: function () { return json_typings_1.typeofJsonValue; } });
Object.defineProperty(exports, "isJsonObject", { enumerable: true, get: function () { return json_typings_1.isJsonObject; } });
// Base 64 encoding
var base64_1 = require("./base64");
Object.defineProperty(exports, "base64decode", { enumerable: true, get: function () { return base64_1.base64decode; } });
Object.defineProperty(exports, "base64encode", { enumerable: true, get: function () { return base64_1.base64encode; } });
// UTF8 encoding
var protobufjs_utf8_1 = require("./protobufjs-utf8");
Object.defineProperty(exports, "utf8read", { enumerable: true, get: function () { return protobufjs_utf8_1.utf8read; } });
// Binary format contracts, options for reading and writing, for example
var binary_format_contract_1 = require("./binary-format-contract");
Object.defineProperty(exports, "WireType", { enumerable: true, get: function () { return binary_format_contract_1.WireType; } });
Object.defineProperty(exports, "mergeBinaryOptions", { enumerable: true, get: function () { return binary_format_contract_1.mergeBinaryOptions; } });
Object.defineProperty(exports, "UnknownFieldHandler", { enumerable: true, get: function () { return binary_format_contract_1.UnknownFieldHandler; } });
// Standard IBinaryReader implementation
var binary_reader_1 = require("./binary-reader");
Object.defineProperty(exports, "BinaryReader", { enumerable: true, get: function () { return binary_reader_1.BinaryReader; } });
Object.defineProperty(exports, "binaryReadOptions", { enumerable: true, get: function () { return binary_reader_1.binaryReadOptions; } });
// Standard IBinaryWriter implementation
var binary_writer_1 = require("./binary-writer");
Object.defineProperty(exports, "BinaryWriter", { enumerable: true, get: function () { return binary_writer_1.BinaryWriter; } });
Object.defineProperty(exports, "binaryWriteOptions", { enumerable: true, get: function () { return binary_writer_1.binaryWriteOptions; } });
// Int64 and UInt64 implementations required for the binary format
var pb_long_1 = require("./pb-long");
Object.defineProperty(exports, "PbLong", { enumerable: true, get: function () { return pb_long_1.PbLong; } });
Object.defineProperty(exports, "PbULong", { enumerable: true, get: function () { return pb_long_1.PbULong; } });
// JSON format contracts, options for reading and writing, for example
var json_format_contract_1 = require("./json-format-contract");
Object.defineProperty(exports, "jsonReadOptions", { enumerable: true, get: function () { return json_format_contract_1.jsonReadOptions; } });
Object.defineProperty(exports, "jsonWriteOptions", { enumerable: true, get: function () { return json_format_contract_1.jsonWriteOptions; } });
Object.defineProperty(exports, "mergeJsonOptions", { enumerable: true, get: function () { return json_format_contract_1.mergeJsonOptions; } });
// Message type contract
var message_type_contract_1 = require("./message-type-contract");
Object.defineProperty(exports, "MESSAGE_TYPE", { enumerable: true, get: function () { return message_type_contract_1.MESSAGE_TYPE; } });
// Message type implementation via reflection
var message_type_1 = require("./message-type");
Object.defineProperty(exports, "MessageType", { enumerable: true, get: function () { return message_type_1.MessageType; } });
// Reflection info, generated by the plugin, exposed to the user, used by reflection ops
var reflection_info_1 = require("./reflection-info");
Object.defineProperty(exports, "ScalarType", { enumerable: true, get: function () { return reflection_info_1.ScalarType; } });
Object.defineProperty(exports, "LongType", { enumerable: true, get: function () { return reflection_info_1.LongType; } });
Object.defineProperty(exports, "RepeatType", { enumerable: true, get: function () { return reflection_info_1.RepeatType; } });
Object.defineProperty(exports, "normalizeFieldInfo", { enumerable: true, get: function () { return reflection_info_1.normalizeFieldInfo; } });
Object.defineProperty(exports, "readFieldOptions", { enumerable: true, get: function () { return reflection_info_1.readFieldOptions; } });
Object.defineProperty(exports, "readFieldOption", { enumerable: true, get: function () { return reflection_info_1.readFieldOption; } });
Object.defineProperty(exports, "readMessageOption", { enumerable: true, get: function () { return reflection_info_1.readMessageOption; } });
// Message operations via reflection
var reflection_type_check_1 = require("./reflection-type-check");
Object.defineProperty(exports, "ReflectionTypeCheck", { enumerable: true, get: function () { return reflection_type_check_1.ReflectionTypeCheck; } });
var reflection_create_1 = require("./reflection-create");
Object.defineProperty(exports, "reflectionCreate", { enumerable: true, get: function () { return reflection_create_1.reflectionCreate; } });
var reflection_scalar_default_1 = require("./reflection-scalar-default");
Object.defineProperty(exports, "reflectionScalarDefault", { enumerable: true, get: function () { return reflection_scalar_default_1.reflectionScalarDefault; } });
var reflection_merge_partial_1 = require("./reflection-merge-partial");
Object.defineProperty(exports, "reflectionMergePartial", { enumerable: true, get: function () { return reflection_merge_partial_1.reflectionMergePartial; } });
var reflection_equals_1 = require("./reflection-equals");
Object.defineProperty(exports, "reflectionEquals", { enumerable: true, get: function () { return reflection_equals_1.reflectionEquals; } });
var reflection_binary_reader_1 = require("./reflection-binary-reader");
Object.defineProperty(exports, "ReflectionBinaryReader", { enumerable: true, get: function () { return reflection_binary_reader_1.ReflectionBinaryReader; } });
var reflection_binary_writer_1 = require("./reflection-binary-writer");
Object.defineProperty(exports, "ReflectionBinaryWriter", { enumerable: true, get: function () { return reflection_binary_writer_1.ReflectionBinaryWriter; } });
var reflection_json_reader_1 = require("./reflection-json-reader");
Object.defineProperty(exports, "ReflectionJsonReader", { enumerable: true, get: function () { return reflection_json_reader_1.ReflectionJsonReader; } });
var reflection_json_writer_1 = require("./reflection-json-writer");
Object.defineProperty(exports, "ReflectionJsonWriter", { enumerable: true, get: function () { return reflection_json_writer_1.ReflectionJsonWriter; } });
var reflection_contains_message_type_1 = require("./reflection-contains-message-type");
Object.defineProperty(exports, "containsMessageType", { enumerable: true, get: function () { return reflection_contains_message_type_1.containsMessageType; } });
// Oneof helpers
var oneof_1 = require("./oneof");
Object.defineProperty(exports, "isOneofGroup", { enumerable: true, get: function () { return oneof_1.isOneofGroup; } });
Object.defineProperty(exports, "setOneofValue", { enumerable: true, get: function () { return oneof_1.setOneofValue; } });
Object.defineProperty(exports, "getOneofValue", { enumerable: true, get: function () { return oneof_1.getOneofValue; } });
Object.defineProperty(exports, "clearOneofValue", { enumerable: true, get: function () { return oneof_1.clearOneofValue; } });
Object.defineProperty(exports, "getSelectedOneofValue", { enumerable: true, get: function () { return oneof_1.getSelectedOneofValue; } });
// Enum object type guard and reflection util, may be interesting to the user.
var enum_object_1 = require("./enum-object");
Object.defineProperty(exports, "listEnumValues", { enumerable: true, get: function () { return enum_object_1.listEnumValues; } });
Object.defineProperty(exports, "listEnumNames", { enumerable: true, get: function () { return enum_object_1.listEnumNames; } });
Object.defineProperty(exports, "listEnumNumbers", { enumerable: true, get: function () { return enum_object_1.listEnumNumbers; } });
Object.defineProperty(exports, "isEnumObject", { enumerable: true, get: function () { return enum_object_1.isEnumObject; } });
// lowerCamelCase() is exported for plugin, rpc-runtime and other rpc packages
var lower_camel_case_1 = require("./lower-camel-case");
Object.defineProperty(exports, "lowerCamelCase", { enumerable: true, get: function () { return lower_camel_case_1.lowerCamelCase; } });
// assertion functions are exported for plugin, may also be useful to user
var assert_1 = require("./assert");
Object.defineProperty(exports, "assert", { enumerable: true, get: function () { return assert_1.assert; } });
Object.defineProperty(exports, "assertNever", { enumerable: true, get: function () { return assert_1.assertNever; } });
Object.defineProperty(exports, "assertInt32", { enumerable: true, get: function () { return assert_1.assertInt32; } });
Object.defineProperty(exports, "assertUInt32", { enumerable: true, get: function () { return assert_1.assertUInt32; } });
Object.defineProperty(exports, "assertFloat32", { enumerable: true, get: function () { return assert_1.assertFloat32; } });

View file

@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeJsonOptions = exports.jsonWriteOptions = exports.jsonReadOptions = void 0;
const defaultsWrite = {
emitDefaultValues: false,
enumAsInteger: false,
useProtoFieldName: false,
prettySpaces: 0,
}, defaultsRead = {
ignoreUnknownFields: false,
};
/**
* Make options for reading JSON data from partial options.
*/
function jsonReadOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsRead), options) : defaultsRead;
}
exports.jsonReadOptions = jsonReadOptions;
/**
* Make options for writing JSON data from partial options.
*/
function jsonWriteOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsWrite), options) : defaultsWrite;
}
exports.jsonWriteOptions = jsonWriteOptions;
/**
* Merges JSON write or read options. Later values override earlier values. Type registries are merged.
*/
function mergeJsonOptions(a, b) {
var _a, _b;
let c = Object.assign(Object.assign({}, a), b);
c.typeRegistry = [...((_a = a === null || a === void 0 ? void 0 : a.typeRegistry) !== null && _a !== void 0 ? _a : []), ...((_b = b === null || b === void 0 ? void 0 : b.typeRegistry) !== null && _b !== void 0 ? _b : [])];
return c;
}
exports.mergeJsonOptions = mergeJsonOptions;

View file

@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isJsonObject = exports.typeofJsonValue = void 0;
/**
* Get the type of a JSON value.
* Distinguishes between array, null and object.
*/
function typeofJsonValue(value) {
let t = typeof value;
if (t == "object") {
if (Array.isArray(value))
return "array";
if (value === null)
return "null";
}
return t;
}
exports.typeofJsonValue = typeofJsonValue;
/**
* Is this a JSON object (instead of an array or null)?
*/
function isJsonObject(value) {
return value !== null && typeof value == "object" && !Array.isArray(value);
}
exports.isJsonObject = isJsonObject;

View file

@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.lowerCamelCase = void 0;
/**
* Converts snake_case to lowerCamelCase.
*
* Should behave like protoc:
* https://github.com/protocolbuffers/protobuf/blob/e8ae137c96444ea313485ed1118c5e43b2099cf1/src/google/protobuf/compiler/java/java_helpers.cc#L118
*/
function lowerCamelCase(snakeCase) {
let capNext = false;
const sb = [];
for (let i = 0; i < snakeCase.length; i++) {
let next = snakeCase.charAt(i);
if (next == '_') {
capNext = true;
}
else if (/\d/.test(next)) {
sb.push(next);
capNext = true;
}
else if (capNext) {
sb.push(next.toUpperCase());
capNext = false;
}
else if (i == 0) {
sb.push(next.toLowerCase());
}
else {
sb.push(next);
}
}
return sb.join('');
}
exports.lowerCamelCase = lowerCamelCase;

View file

@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MESSAGE_TYPE = void 0;
/**
* The symbol used as a key on message objects to store the message type.
*
* Note that this is an experimental feature - it is here to stay, but
* implementation details may change without notice.
*/
exports.MESSAGE_TYPE = Symbol.for("protobuf-ts/message-type");

View file

@ -0,0 +1,175 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageType = void 0;
const message_type_contract_1 = require("./message-type-contract");
const reflection_info_1 = require("./reflection-info");
const reflection_type_check_1 = require("./reflection-type-check");
const reflection_json_reader_1 = require("./reflection-json-reader");
const reflection_json_writer_1 = require("./reflection-json-writer");
const reflection_binary_reader_1 = require("./reflection-binary-reader");
const reflection_binary_writer_1 = require("./reflection-binary-writer");
const reflection_create_1 = require("./reflection-create");
const reflection_merge_partial_1 = require("./reflection-merge-partial");
const json_typings_1 = require("./json-typings");
const json_format_contract_1 = require("./json-format-contract");
const reflection_equals_1 = require("./reflection-equals");
const binary_writer_1 = require("./binary-writer");
const binary_reader_1 = require("./binary-reader");
const baseDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf({}));
/**
* This standard message type provides reflection-based
* operations to work with a message.
*/
class MessageType {
constructor(name, fields, options) {
this.defaultCheckDepth = 16;
this.typeName = name;
this.fields = fields.map(reflection_info_1.normalizeFieldInfo);
this.options = options !== null && options !== void 0 ? options : {};
this.messagePrototype = Object.create(null, Object.assign(Object.assign({}, baseDescriptors), { [message_type_contract_1.MESSAGE_TYPE]: { value: this } }));
this.refTypeCheck = new reflection_type_check_1.ReflectionTypeCheck(this);
this.refJsonReader = new reflection_json_reader_1.ReflectionJsonReader(this);
this.refJsonWriter = new reflection_json_writer_1.ReflectionJsonWriter(this);
this.refBinReader = new reflection_binary_reader_1.ReflectionBinaryReader(this);
this.refBinWriter = new reflection_binary_writer_1.ReflectionBinaryWriter(this);
}
create(value) {
let message = reflection_create_1.reflectionCreate(this);
if (value !== undefined) {
reflection_merge_partial_1.reflectionMergePartial(this, message, value);
}
return message;
}
/**
* Clone the message.
*
* Unknown fields are discarded.
*/
clone(message) {
let copy = this.create();
reflection_merge_partial_1.reflectionMergePartial(this, copy, message);
return copy;
}
/**
* Determines whether two message of the same type have the same field values.
* Checks for deep equality, traversing repeated fields, oneof groups, maps
* and messages recursively.
* Will also return true if both messages are `undefined`.
*/
equals(a, b) {
return reflection_equals_1.reflectionEquals(this, a, b);
}
/**
* Is the given value assignable to our message type
* and contains no [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
is(arg, depth = this.defaultCheckDepth) {
return this.refTypeCheck.is(arg, depth, false);
}
/**
* Is the given value assignable to our message type,
* regardless of [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
isAssignable(arg, depth = this.defaultCheckDepth) {
return this.refTypeCheck.is(arg, depth, true);
}
/**
* Copy partial data into the target message.
*/
mergePartial(target, source) {
reflection_merge_partial_1.reflectionMergePartial(this, target, source);
}
/**
* Create a new message from binary format.
*/
fromBinary(data, options) {
let opt = binary_reader_1.binaryReadOptions(options);
return this.internalBinaryRead(opt.readerFactory(data), data.byteLength, opt);
}
/**
* Read a new message from a JSON value.
*/
fromJson(json, options) {
return this.internalJsonRead(json, json_format_contract_1.jsonReadOptions(options));
}
/**
* Read a new message from a JSON string.
* This is equivalent to `T.fromJson(JSON.parse(json))`.
*/
fromJsonString(json, options) {
let value = JSON.parse(json);
return this.fromJson(value, options);
}
/**
* Write the message to canonical JSON value.
*/
toJson(message, options) {
return this.internalJsonWrite(message, json_format_contract_1.jsonWriteOptions(options));
}
/**
* Convert the message to canonical JSON string.
* This is equivalent to `JSON.stringify(T.toJson(t))`
*/
toJsonString(message, options) {
var _a;
let value = this.toJson(message, options);
return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
}
/**
* Write the message to binary format.
*/
toBinary(message, options) {
let opt = binary_writer_1.binaryWriteOptions(options);
return this.internalBinaryWrite(message, opt.writerFactory(), opt).finish();
}
/**
* This is an internal method. If you just want to read a message from
* JSON, use `fromJson()` or `fromJsonString()`.
*
* Reads JSON value and merges the fields into the target
* according to protobuf rules. If the target is omitted,
* a new instance is created first.
*/
internalJsonRead(json, options, target) {
if (json !== null && typeof json == "object" && !Array.isArray(json)) {
let message = target !== null && target !== void 0 ? target : this.create();
this.refJsonReader.read(json, message, options);
return message;
}
throw new Error(`Unable to parse message ${this.typeName} from JSON ${json_typings_1.typeofJsonValue(json)}.`);
}
/**
* This is an internal method. If you just want to write a message
* to JSON, use `toJson()` or `toJsonString().
*
* Writes JSON value and returns it.
*/
internalJsonWrite(message, options) {
return this.refJsonWriter.write(message, options);
}
/**
* This is an internal method. If you just want to write a message
* in binary format, use `toBinary()`.
*
* Serializes the message in binary format and appends it to the given
* writer. Returns passed writer.
*/
internalBinaryWrite(message, writer, options) {
this.refBinWriter.write(message, writer, options);
return writer;
}
/**
* This is an internal method. If you just want to read a message from
* binary data, use `fromBinary()`.
*
* Reads data from binary format and merges the fields into
* the target according to protobuf rules. If the target is
* omitted, a new instance is created first.
*/
internalBinaryRead(reader, length, options, target) {
let message = target !== null && target !== void 0 ? target : this.create();
this.refBinReader.read(reader, message, options, length);
return message;
}
}
exports.MessageType = MessageType;

View file

@ -0,0 +1,114 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSelectedOneofValue = exports.clearOneofValue = exports.setUnknownOneofValue = exports.setOneofValue = exports.getOneofValue = exports.isOneofGroup = void 0;
/**
* Is the given value a valid oneof group?
*
* We represent protobuf `oneof` as algebraic data types (ADT) in generated
* code. But when working with messages of unknown type, the ADT does not
* help us.
*
* This type guard checks if the given object adheres to the ADT rules, which
* are as follows:
*
* 1) Must be an object.
*
* 2) Must have a "oneofKind" discriminator property.
*
* 3) If "oneofKind" is `undefined`, no member field is selected. The object
* must not have any other properties.
*
* 4) If "oneofKind" is a `string`, the member field with this name is
* selected.
*
* 5) If a member field is selected, the object must have a second property
* with this name. The property must not be `undefined`.
*
* 6) No extra properties are allowed. The object has either one property
* (no selection) or two properties (selection).
*
*/
function isOneofGroup(any) {
if (typeof any != 'object' || any === null || !any.hasOwnProperty('oneofKind')) {
return false;
}
switch (typeof any.oneofKind) {
case "string":
if (any[any.oneofKind] === undefined)
return false;
return Object.keys(any).length == 2;
case "undefined":
return Object.keys(any).length == 1;
default:
return false;
}
}
exports.isOneofGroup = isOneofGroup;
/**
* Returns the value of the given field in a oneof group.
*/
function getOneofValue(oneof, kind) {
return oneof[kind];
}
exports.getOneofValue = getOneofValue;
function setOneofValue(oneof, kind, value) {
if (oneof.oneofKind !== undefined) {
delete oneof[oneof.oneofKind];
}
oneof.oneofKind = kind;
if (value !== undefined) {
oneof[kind] = value;
}
}
exports.setOneofValue = setOneofValue;
function setUnknownOneofValue(oneof, kind, value) {
if (oneof.oneofKind !== undefined) {
delete oneof[oneof.oneofKind];
}
oneof.oneofKind = kind;
if (value !== undefined && kind !== undefined) {
oneof[kind] = value;
}
}
exports.setUnknownOneofValue = setUnknownOneofValue;
/**
* Removes the selected field in a oneof group.
*
* Note that the recommended way to modify a oneof group is to set
* a new object:
*
* ```ts
* message.result = { oneofKind: undefined };
* ```
*/
function clearOneofValue(oneof) {
if (oneof.oneofKind !== undefined) {
delete oneof[oneof.oneofKind];
}
oneof.oneofKind = undefined;
}
exports.clearOneofValue = clearOneofValue;
/**
* Returns the selected value of the given oneof group.
*
* Not that the recommended way to access a oneof group is to check
* the "oneofKind" property and let TypeScript narrow down the union
* type for you:
*
* ```ts
* if (message.result.oneofKind === "error") {
* message.result.error; // string
* }
* ```
*
* In the rare case you just need the value, and do not care about
* which protobuf field is selected, you can use this function
* for convenience.
*/
function getSelectedOneofValue(oneof) {
if (oneof.oneofKind === undefined) {
return undefined;
}
return oneof[oneof.oneofKind];
}
exports.getSelectedOneofValue = getSelectedOneofValue;

View file

@ -0,0 +1,238 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PbLong = exports.PbULong = exports.detectBi = void 0;
const goog_varint_1 = require("./goog-varint");
let BI;
function detectBi() {
const dv = new DataView(new ArrayBuffer(8));
const ok = globalThis.BigInt !== undefined
&& typeof dv.getBigInt64 === "function"
&& typeof dv.getBigUint64 === "function"
&& typeof dv.setBigInt64 === "function"
&& typeof dv.setBigUint64 === "function";
BI = ok ? {
MIN: BigInt("-9223372036854775808"),
MAX: BigInt("9223372036854775807"),
UMIN: BigInt("0"),
UMAX: BigInt("18446744073709551615"),
C: BigInt,
V: dv,
} : undefined;
}
exports.detectBi = detectBi;
detectBi();
function assertBi(bi) {
if (!bi)
throw new Error("BigInt unavailable, see https://github.com/timostamm/protobuf-ts/blob/v1.0.8/MANUAL.md#bigint-support");
}
// used to validate from(string) input (when bigint is unavailable)
const RE_DECIMAL_STR = /^-?[0-9]+$/;
// constants for binary math
const TWO_PWR_32_DBL = 0x100000000;
const HALF_2_PWR_32 = 0x080000000;
// base class for PbLong and PbULong provides shared code
class SharedPbLong {
/**
* Create a new instance with the given bits.
*/
constructor(lo, hi) {
this.lo = lo | 0;
this.hi = hi | 0;
}
/**
* Is this instance equal to 0?
*/
isZero() {
return this.lo == 0 && this.hi == 0;
}
/**
* Convert to a native number.
*/
toNumber() {
let result = this.hi * TWO_PWR_32_DBL + (this.lo >>> 0);
if (!Number.isSafeInteger(result))
throw new Error("cannot convert to safe number");
return result;
}
}
/**
* 64-bit unsigned integer as two 32-bit values.
* Converts between `string`, `number` and `bigint` representations.
*/
class PbULong extends SharedPbLong {
/**
* Create instance from a `string`, `number` or `bigint`.
*/
static from(value) {
if (BI)
// noinspection FallThroughInSwitchStatementJS
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
if (value == "")
throw new Error('string is no integer');
value = BI.C(value);
case "number":
if (value === 0)
return this.ZERO;
value = BI.C(value);
case "bigint":
if (!value)
return this.ZERO;
if (value < BI.UMIN)
throw new Error('signed value for ulong');
if (value > BI.UMAX)
throw new Error('ulong too large');
BI.V.setBigUint64(0, value, true);
return new PbULong(BI.V.getInt32(0, true), BI.V.getInt32(4, true));
}
else
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
value = value.trim();
if (!RE_DECIMAL_STR.test(value))
throw new Error('string is no integer');
let [minus, lo, hi] = goog_varint_1.int64fromString(value);
if (minus)
throw new Error('signed value for ulong');
return new PbULong(lo, hi);
case "number":
if (value == 0)
return this.ZERO;
if (!Number.isSafeInteger(value))
throw new Error('number is no integer');
if (value < 0)
throw new Error('signed value for ulong');
return new PbULong(value, value / TWO_PWR_32_DBL);
}
throw new Error('unknown value ' + typeof value);
}
/**
* Convert to decimal string.
*/
toString() {
return BI ? this.toBigInt().toString() : goog_varint_1.int64toString(this.lo, this.hi);
}
/**
* Convert to native bigint.
*/
toBigInt() {
assertBi(BI);
BI.V.setInt32(0, this.lo, true);
BI.V.setInt32(4, this.hi, true);
return BI.V.getBigUint64(0, true);
}
}
exports.PbULong = PbULong;
/**
* ulong 0 singleton.
*/
PbULong.ZERO = new PbULong(0, 0);
/**
* 64-bit signed integer as two 32-bit values.
* Converts between `string`, `number` and `bigint` representations.
*/
class PbLong extends SharedPbLong {
/**
* Create instance from a `string`, `number` or `bigint`.
*/
static from(value) {
if (BI)
// noinspection FallThroughInSwitchStatementJS
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
if (value == "")
throw new Error('string is no integer');
value = BI.C(value);
case "number":
if (value === 0)
return this.ZERO;
value = BI.C(value);
case "bigint":
if (!value)
return this.ZERO;
if (value < BI.MIN)
throw new Error('signed long too small');
if (value > BI.MAX)
throw new Error('signed long too large');
BI.V.setBigInt64(0, value, true);
return new PbLong(BI.V.getInt32(0, true), BI.V.getInt32(4, true));
}
else
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
value = value.trim();
if (!RE_DECIMAL_STR.test(value))
throw new Error('string is no integer');
let [minus, lo, hi] = goog_varint_1.int64fromString(value);
if (minus) {
if (hi > HALF_2_PWR_32 || (hi == HALF_2_PWR_32 && lo != 0))
throw new Error('signed long too small');
}
else if (hi >= HALF_2_PWR_32)
throw new Error('signed long too large');
let pbl = new PbLong(lo, hi);
return minus ? pbl.negate() : pbl;
case "number":
if (value == 0)
return this.ZERO;
if (!Number.isSafeInteger(value))
throw new Error('number is no integer');
return value > 0
? new PbLong(value, value / TWO_PWR_32_DBL)
: new PbLong(-value, -value / TWO_PWR_32_DBL).negate();
}
throw new Error('unknown value ' + typeof value);
}
/**
* Do we have a minus sign?
*/
isNegative() {
return (this.hi & HALF_2_PWR_32) !== 0;
}
/**
* Negate two's complement.
* Invert all the bits and add one to the result.
*/
negate() {
let hi = ~this.hi, lo = this.lo;
if (lo)
lo = ~lo + 1;
else
hi += 1;
return new PbLong(lo, hi);
}
/**
* Convert to decimal string.
*/
toString() {
if (BI)
return this.toBigInt().toString();
if (this.isNegative()) {
let n = this.negate();
return '-' + goog_varint_1.int64toString(n.lo, n.hi);
}
return goog_varint_1.int64toString(this.lo, this.hi);
}
/**
* Convert to native bigint.
*/
toBigInt() {
assertBi(BI);
BI.V.setInt32(0, this.lo, true);
BI.V.setInt32(4, this.hi, true);
return BI.V.getBigInt64(0, true);
}
}
exports.PbLong = PbLong;
/**
* long 0 singleton.
*/
PbLong.ZERO = new PbLong(0, 0);

View file

@ -0,0 +1,75 @@
"use strict";
// Copyright (c) 2016, Daniel Wirtz 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 its author, 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
// OWNER 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.
Object.defineProperty(exports, "__esModule", { value: true });
exports.utf8read = void 0;
const fromCharCodes = (chunk) => String.fromCharCode.apply(String, chunk);
/**
* @deprecated This function will no longer be exported with the next major
* release, since protobuf-ts has switch to TextDecoder API. If you need this
* function, please migrate to @protobufjs/utf8. For context, see
* https://github.com/timostamm/protobuf-ts/issues/184
*
* Reads UTF8 bytes as a string.
*
* See [protobufjs / utf8](https://github.com/protobufjs/protobuf.js/blob/9893e35b854621cce64af4bf6be2cff4fb892796/lib/utf8/index.js#L40)
*
* Copyright (c) 2016, Daniel Wirtz
*/
function utf8read(bytes) {
if (bytes.length < 1)
return "";
let pos = 0, // position in bytes
parts = [], chunk = [], i = 0, // char offset
t; // temporary
let len = bytes.length;
while (pos < len) {
t = bytes[pos++];
if (t < 128)
chunk[i++] = t;
else if (t > 191 && t < 224)
chunk[i++] = (t & 31) << 6 | bytes[pos++] & 63;
else if (t > 239 && t < 365) {
t = ((t & 7) << 18 | (bytes[pos++] & 63) << 12 | (bytes[pos++] & 63) << 6 | bytes[pos++] & 63) - 0x10000;
chunk[i++] = 0xD800 + (t >> 10);
chunk[i++] = 0xDC00 + (t & 1023);
}
else
chunk[i++] = (t & 15) << 12 | (bytes[pos++] & 63) << 6 | bytes[pos++] & 63;
if (i > 8191) {
parts.push(fromCharCodes(chunk));
i = 0;
}
}
if (parts.length) {
if (i)
parts.push(fromCharCodes(chunk.slice(0, i)));
return parts.join("");
}
return fromCharCodes(chunk.slice(0, i));
}
exports.utf8read = utf8read;

View file

@ -0,0 +1,183 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectionBinaryReader = void 0;
const binary_format_contract_1 = require("./binary-format-contract");
const reflection_info_1 = require("./reflection-info");
const reflection_long_convert_1 = require("./reflection-long-convert");
const reflection_scalar_default_1 = require("./reflection-scalar-default");
/**
* Reads proto3 messages in binary format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/encoding
*/
class ReflectionBinaryReader {
constructor(info) {
this.info = info;
}
prepare() {
var _a;
if (!this.fieldNoToField) {
const fieldsInput = (_a = this.info.fields) !== null && _a !== void 0 ? _a : [];
this.fieldNoToField = new Map(fieldsInput.map(field => [field.no, field]));
}
}
/**
* Reads a message from binary format into the target message.
*
* Repeated fields are appended. Map entries are added, overwriting
* existing keys.
*
* If a message field is already present, it will be merged with the
* new data.
*/
read(reader, message, options, length) {
this.prepare();
const end = length === undefined ? reader.len : reader.pos + length;
while (reader.pos < end) {
// read the tag and find the field
const [fieldNo, wireType] = reader.tag(), field = this.fieldNoToField.get(fieldNo);
if (!field) {
let u = options.readUnknownField;
if (u == "throw")
throw new Error(`Unknown field ${fieldNo} (wire type ${wireType}) for ${this.info.typeName}`);
let d = reader.skip(wireType);
if (u !== false)
(u === true ? binary_format_contract_1.UnknownFieldHandler.onRead : u)(this.info.typeName, message, fieldNo, wireType, d);
continue;
}
// target object for the field we are reading
let target = message, repeated = field.repeat, localName = field.localName;
// if field is member of oneof ADT, use ADT as target
if (field.oneof) {
target = target[field.oneof];
// if other oneof member selected, set new ADT
if (target.oneofKind !== localName)
target = message[field.oneof] = {
oneofKind: localName
};
}
// we have handled oneof above, we just have read the value into `target[localName]`
switch (field.kind) {
case "scalar":
case "enum":
let T = field.kind == "enum" ? reflection_info_1.ScalarType.INT32 : field.T;
let L = field.kind == "scalar" ? field.L : undefined;
if (repeated) {
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
if (wireType == binary_format_contract_1.WireType.LengthDelimited && T != reflection_info_1.ScalarType.STRING && T != reflection_info_1.ScalarType.BYTES) {
let e = reader.uint32() + reader.pos;
while (reader.pos < e)
arr.push(this.scalar(reader, T, L));
}
else
arr.push(this.scalar(reader, T, L));
}
else
target[localName] = this.scalar(reader, T, L);
break;
case "message":
if (repeated) {
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
let msg = field.T().internalBinaryRead(reader, reader.uint32(), options);
arr.push(msg);
}
else
target[localName] = field.T().internalBinaryRead(reader, reader.uint32(), options, target[localName]);
break;
case "map":
let [mapKey, mapVal] = this.mapEntry(field, reader, options);
// safe to assume presence of map object, oneof cannot contain repeated values
target[localName][mapKey] = mapVal;
break;
}
}
}
/**
* Read a map field, expecting key field = 1, value field = 2
*/
mapEntry(field, reader, options) {
let length = reader.uint32();
let end = reader.pos + length;
let key = undefined; // javascript only allows number or string for object properties
let val = undefined;
while (reader.pos < end) {
let [fieldNo, wireType] = reader.tag();
switch (fieldNo) {
case 1:
if (field.K == reflection_info_1.ScalarType.BOOL)
key = reader.bool().toString();
else
// long types are read as string, number types are okay as number
key = this.scalar(reader, field.K, reflection_info_1.LongType.STRING);
break;
case 2:
switch (field.V.kind) {
case "scalar":
val = this.scalar(reader, field.V.T, field.V.L);
break;
case "enum":
val = reader.int32();
break;
case "message":
val = field.V.T().internalBinaryRead(reader, reader.uint32(), options);
break;
}
break;
default:
throw new Error(`Unknown field ${fieldNo} (wire type ${wireType}) in map entry for ${this.info.typeName}#${field.name}`);
}
}
if (key === undefined) {
let keyRaw = reflection_scalar_default_1.reflectionScalarDefault(field.K);
key = field.K == reflection_info_1.ScalarType.BOOL ? keyRaw.toString() : keyRaw;
}
if (val === undefined)
switch (field.V.kind) {
case "scalar":
val = reflection_scalar_default_1.reflectionScalarDefault(field.V.T, field.V.L);
break;
case "enum":
val = 0;
break;
case "message":
val = field.V.T().create();
break;
}
return [key, val];
}
scalar(reader, type, longType) {
switch (type) {
case reflection_info_1.ScalarType.INT32:
return reader.int32();
case reflection_info_1.ScalarType.STRING:
return reader.string();
case reflection_info_1.ScalarType.BOOL:
return reader.bool();
case reflection_info_1.ScalarType.DOUBLE:
return reader.double();
case reflection_info_1.ScalarType.FLOAT:
return reader.float();
case reflection_info_1.ScalarType.INT64:
return reflection_long_convert_1.reflectionLongConvert(reader.int64(), longType);
case reflection_info_1.ScalarType.UINT64:
return reflection_long_convert_1.reflectionLongConvert(reader.uint64(), longType);
case reflection_info_1.ScalarType.FIXED64:
return reflection_long_convert_1.reflectionLongConvert(reader.fixed64(), longType);
case reflection_info_1.ScalarType.FIXED32:
return reader.fixed32();
case reflection_info_1.ScalarType.BYTES:
return reader.bytes();
case reflection_info_1.ScalarType.UINT32:
return reader.uint32();
case reflection_info_1.ScalarType.SFIXED32:
return reader.sfixed32();
case reflection_info_1.ScalarType.SFIXED64:
return reflection_long_convert_1.reflectionLongConvert(reader.sfixed64(), longType);
case reflection_info_1.ScalarType.SINT32:
return reader.sint32();
case reflection_info_1.ScalarType.SINT64:
return reflection_long_convert_1.reflectionLongConvert(reader.sint64(), longType);
}
}
}
exports.ReflectionBinaryReader = ReflectionBinaryReader;

View file

@ -0,0 +1,233 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectionBinaryWriter = void 0;
const binary_format_contract_1 = require("./binary-format-contract");
const reflection_info_1 = require("./reflection-info");
const assert_1 = require("./assert");
const pb_long_1 = require("./pb-long");
/**
* Writes proto3 messages in binary format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/encoding
*/
class ReflectionBinaryWriter {
constructor(info) {
this.info = info;
}
prepare() {
if (!this.fields) {
const fieldsInput = this.info.fields ? this.info.fields.concat() : [];
this.fields = fieldsInput.sort((a, b) => a.no - b.no);
}
}
/**
* Writes the message to binary format.
*/
write(message, writer, options) {
this.prepare();
for (const field of this.fields) {
let value, // this will be our field value, whether it is member of a oneof or not
emitDefault, // whether we emit the default value (only true for oneof members)
repeated = field.repeat, localName = field.localName;
// handle oneof ADT
if (field.oneof) {
const group = message[field.oneof];
if (group.oneofKind !== localName)
continue; // if field is not selected, skip
value = group[localName];
emitDefault = true;
}
else {
value = message[localName];
emitDefault = false;
}
// we have handled oneof above. we just have to honor `emitDefault`.
switch (field.kind) {
case "scalar":
case "enum":
let T = field.kind == "enum" ? reflection_info_1.ScalarType.INT32 : field.T;
if (repeated) {
assert_1.assert(Array.isArray(value));
if (repeated == reflection_info_1.RepeatType.PACKED)
this.packed(writer, T, field.no, value);
else
for (const item of value)
this.scalar(writer, T, field.no, item, true);
}
else if (value === undefined)
assert_1.assert(field.opt);
else
this.scalar(writer, T, field.no, value, emitDefault || field.opt);
break;
case "message":
if (repeated) {
assert_1.assert(Array.isArray(value));
for (const item of value)
this.message(writer, options, field.T(), field.no, item);
}
else {
this.message(writer, options, field.T(), field.no, value);
}
break;
case "map":
assert_1.assert(typeof value == 'object' && value !== null);
for (const [key, val] of Object.entries(value))
this.mapEntry(writer, options, field, key, val);
break;
}
}
let u = options.writeUnknownFields;
if (u !== false)
(u === true ? binary_format_contract_1.UnknownFieldHandler.onWrite : u)(this.info.typeName, message, writer);
}
mapEntry(writer, options, field, key, value) {
writer.tag(field.no, binary_format_contract_1.WireType.LengthDelimited);
writer.fork();
// javascript only allows number or string for object properties
// we convert from our representation to the protobuf type
let keyValue = key;
switch (field.K) {
case reflection_info_1.ScalarType.INT32:
case reflection_info_1.ScalarType.FIXED32:
case reflection_info_1.ScalarType.UINT32:
case reflection_info_1.ScalarType.SFIXED32:
case reflection_info_1.ScalarType.SINT32:
keyValue = Number.parseInt(key);
break;
case reflection_info_1.ScalarType.BOOL:
assert_1.assert(key == 'true' || key == 'false');
keyValue = key == 'true';
break;
}
// write key, expecting key field number = 1
this.scalar(writer, field.K, 1, keyValue, true);
// write value, expecting value field number = 2
switch (field.V.kind) {
case 'scalar':
this.scalar(writer, field.V.T, 2, value, true);
break;
case 'enum':
this.scalar(writer, reflection_info_1.ScalarType.INT32, 2, value, true);
break;
case 'message':
this.message(writer, options, field.V.T(), 2, value);
break;
}
writer.join();
}
message(writer, options, handler, fieldNo, value) {
if (value === undefined)
return;
handler.internalBinaryWrite(value, writer.tag(fieldNo, binary_format_contract_1.WireType.LengthDelimited).fork(), options);
writer.join();
}
/**
* Write a single scalar value.
*/
scalar(writer, type, fieldNo, value, emitDefault) {
let [wireType, method, isDefault] = this.scalarInfo(type, value);
if (!isDefault || emitDefault) {
writer.tag(fieldNo, wireType);
writer[method](value);
}
}
/**
* Write an array of scalar values in packed format.
*/
packed(writer, type, fieldNo, value) {
if (!value.length)
return;
assert_1.assert(type !== reflection_info_1.ScalarType.BYTES && type !== reflection_info_1.ScalarType.STRING);
// write tag
writer.tag(fieldNo, binary_format_contract_1.WireType.LengthDelimited);
// begin length-delimited
writer.fork();
// write values without tags
let [, method,] = this.scalarInfo(type);
for (let i = 0; i < value.length; i++)
writer[method](value[i]);
// end length delimited
writer.join();
}
/**
* Get information for writing a scalar value.
*
* Returns tuple:
* [0]: appropriate WireType
* [1]: name of the appropriate method of IBinaryWriter
* [2]: whether the given value is a default value
*
* If argument `value` is omitted, [2] is always false.
*/
scalarInfo(type, value) {
let t = binary_format_contract_1.WireType.Varint;
let m;
let i = value === undefined;
let d = value === 0;
switch (type) {
case reflection_info_1.ScalarType.INT32:
m = "int32";
break;
case reflection_info_1.ScalarType.STRING:
d = i || !value.length;
t = binary_format_contract_1.WireType.LengthDelimited;
m = "string";
break;
case reflection_info_1.ScalarType.BOOL:
d = value === false;
m = "bool";
break;
case reflection_info_1.ScalarType.UINT32:
m = "uint32";
break;
case reflection_info_1.ScalarType.DOUBLE:
t = binary_format_contract_1.WireType.Bit64;
m = "double";
break;
case reflection_info_1.ScalarType.FLOAT:
t = binary_format_contract_1.WireType.Bit32;
m = "float";
break;
case reflection_info_1.ScalarType.INT64:
d = i || pb_long_1.PbLong.from(value).isZero();
m = "int64";
break;
case reflection_info_1.ScalarType.UINT64:
d = i || pb_long_1.PbULong.from(value).isZero();
m = "uint64";
break;
case reflection_info_1.ScalarType.FIXED64:
d = i || pb_long_1.PbULong.from(value).isZero();
t = binary_format_contract_1.WireType.Bit64;
m = "fixed64";
break;
case reflection_info_1.ScalarType.BYTES:
d = i || !value.byteLength;
t = binary_format_contract_1.WireType.LengthDelimited;
m = "bytes";
break;
case reflection_info_1.ScalarType.FIXED32:
t = binary_format_contract_1.WireType.Bit32;
m = "fixed32";
break;
case reflection_info_1.ScalarType.SFIXED32:
t = binary_format_contract_1.WireType.Bit32;
m = "sfixed32";
break;
case reflection_info_1.ScalarType.SFIXED64:
d = i || pb_long_1.PbLong.from(value).isZero();
t = binary_format_contract_1.WireType.Bit64;
m = "sfixed64";
break;
case reflection_info_1.ScalarType.SINT32:
m = "sint32";
break;
case reflection_info_1.ScalarType.SINT64:
d = i || pb_long_1.PbLong.from(value).isZero();
m = "sint64";
break;
}
return [t, m, i || d];
}
}
exports.ReflectionBinaryWriter = ReflectionBinaryWriter;

View file

@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.containsMessageType = void 0;
const message_type_contract_1 = require("./message-type-contract");
/**
* Check if the provided object is a proto message.
*
* Note that this is an experimental feature - it is here to stay, but
* implementation details may change without notice.
*/
function containsMessageType(msg) {
return msg[message_type_contract_1.MESSAGE_TYPE] != null;
}
exports.containsMessageType = containsMessageType;

View file

@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reflectionCreate = void 0;
const reflection_scalar_default_1 = require("./reflection-scalar-default");
const message_type_contract_1 = require("./message-type-contract");
/**
* Creates an instance of the generic message, using the field
* information.
*/
function reflectionCreate(type) {
/**
* This ternary can be removed in the next major version.
* The `Object.create()` code path utilizes a new `messagePrototype`
* property on the `IMessageType` which has this same `MESSAGE_TYPE`
* non-enumerable property on it. Doing it this way means that we only
* pay the cost of `Object.defineProperty()` once per `IMessageType`
* class of once per "instance". The falsy code path is only provided
* for backwards compatibility in cases where the runtime library is
* updated without also updating the generated code.
*/
const msg = type.messagePrototype
? Object.create(type.messagePrototype)
: Object.defineProperty({}, message_type_contract_1.MESSAGE_TYPE, { value: type });
for (let field of type.fields) {
let name = field.localName;
if (field.opt)
continue;
if (field.oneof)
msg[field.oneof] = { oneofKind: undefined };
else if (field.repeat)
msg[name] = [];
else
switch (field.kind) {
case "scalar":
msg[name] = reflection_scalar_default_1.reflectionScalarDefault(field.T, field.L);
break;
case "enum":
// we require 0 to be default value for all enums
msg[name] = 0;
break;
case "map":
msg[name] = {};
break;
}
}
return msg;
}
exports.reflectionCreate = reflectionCreate;

View file

@ -0,0 +1,77 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reflectionEquals = void 0;
const reflection_info_1 = require("./reflection-info");
/**
* Determines whether two message of the same type have the same field values.
* Checks for deep equality, traversing repeated fields, oneof groups, maps
* and messages recursively.
* Will also return true if both messages are `undefined`.
*/
function reflectionEquals(info, a, b) {
if (a === b)
return true;
if (!a || !b)
return false;
for (let field of info.fields) {
let localName = field.localName;
let val_a = field.oneof ? a[field.oneof][localName] : a[localName];
let val_b = field.oneof ? b[field.oneof][localName] : b[localName];
switch (field.kind) {
case "enum":
case "scalar":
let t = field.kind == "enum" ? reflection_info_1.ScalarType.INT32 : field.T;
if (!(field.repeat
? repeatedPrimitiveEq(t, val_a, val_b)
: primitiveEq(t, val_a, val_b)))
return false;
break;
case "map":
if (!(field.V.kind == "message"
? repeatedMsgEq(field.V.T(), objectValues(val_a), objectValues(val_b))
: repeatedPrimitiveEq(field.V.kind == "enum" ? reflection_info_1.ScalarType.INT32 : field.V.T, objectValues(val_a), objectValues(val_b))))
return false;
break;
case "message":
let T = field.T();
if (!(field.repeat
? repeatedMsgEq(T, val_a, val_b)
: T.equals(val_a, val_b)))
return false;
break;
}
}
return true;
}
exports.reflectionEquals = reflectionEquals;
const objectValues = Object.values;
function primitiveEq(type, a, b) {
if (a === b)
return true;
if (type !== reflection_info_1.ScalarType.BYTES)
return false;
let ba = a;
let bb = b;
if (ba.length !== bb.length)
return false;
for (let i = 0; i < ba.length; i++)
if (ba[i] != bb[i])
return false;
return true;
}
function repeatedPrimitiveEq(type, a, b) {
if (a.length !== b.length)
return false;
for (let i = 0; i < a.length; i++)
if (!primitiveEq(type, a[i], b[i]))
return false;
return true;
}
function repeatedMsgEq(type, a, b) {
if (a.length !== b.length)
return false;
for (let i = 0; i < a.length; i++)
if (!type.equals(a[i], b[i]))
return false;
return true;
}

View file

@ -0,0 +1,158 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readMessageOption = exports.readFieldOption = exports.readFieldOptions = exports.normalizeFieldInfo = exports.RepeatType = exports.LongType = exports.ScalarType = void 0;
const lower_camel_case_1 = require("./lower-camel-case");
/**
* Scalar value types. This is a subset of field types declared by protobuf
* enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
* are omitted, but the numerical values are identical.
*/
var ScalarType;
(function (ScalarType) {
// 0 is reserved for errors.
// Order is weird for historical reasons.
ScalarType[ScalarType["DOUBLE"] = 1] = "DOUBLE";
ScalarType[ScalarType["FLOAT"] = 2] = "FLOAT";
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
// negative values are likely.
ScalarType[ScalarType["INT64"] = 3] = "INT64";
ScalarType[ScalarType["UINT64"] = 4] = "UINT64";
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
// negative values are likely.
ScalarType[ScalarType["INT32"] = 5] = "INT32";
ScalarType[ScalarType["FIXED64"] = 6] = "FIXED64";
ScalarType[ScalarType["FIXED32"] = 7] = "FIXED32";
ScalarType[ScalarType["BOOL"] = 8] = "BOOL";
ScalarType[ScalarType["STRING"] = 9] = "STRING";
// Tag-delimited aggregate.
// Group type is deprecated and not supported in proto3. However, Proto3
// implementations should still be able to parse the group wire format and
// treat group fields as unknown fields.
// TYPE_GROUP = 10,
// TYPE_MESSAGE = 11, // Length-delimited aggregate.
// New in version 2.
ScalarType[ScalarType["BYTES"] = 12] = "BYTES";
ScalarType[ScalarType["UINT32"] = 13] = "UINT32";
// TYPE_ENUM = 14,
ScalarType[ScalarType["SFIXED32"] = 15] = "SFIXED32";
ScalarType[ScalarType["SFIXED64"] = 16] = "SFIXED64";
ScalarType[ScalarType["SINT32"] = 17] = "SINT32";
ScalarType[ScalarType["SINT64"] = 18] = "SINT64";
})(ScalarType = exports.ScalarType || (exports.ScalarType = {}));
/**
* JavaScript representation of 64 bit integral types. Equivalent to the
* field option "jstype".
*
* By default, protobuf-ts represents 64 bit types as `bigint`.
*
* You can change the default behaviour by enabling the plugin parameter
* `long_type_string`, which will represent 64 bit types as `string`.
*
* Alternatively, you can change the behaviour for individual fields
* with the field option "jstype":
*
* ```protobuf
* uint64 my_field = 1 [jstype = JS_STRING];
* uint64 other_field = 2 [jstype = JS_NUMBER];
* ```
*/
var LongType;
(function (LongType) {
/**
* Use JavaScript `bigint`.
*
* Field option `[jstype = JS_NORMAL]`.
*/
LongType[LongType["BIGINT"] = 0] = "BIGINT";
/**
* Use JavaScript `string`.
*
* Field option `[jstype = JS_STRING]`.
*/
LongType[LongType["STRING"] = 1] = "STRING";
/**
* Use JavaScript `number`.
*
* Large values will loose precision.
*
* Field option `[jstype = JS_NUMBER]`.
*/
LongType[LongType["NUMBER"] = 2] = "NUMBER";
})(LongType = exports.LongType || (exports.LongType = {}));
/**
* Protobuf 2.1.0 introduced packed repeated fields.
* Setting the field option `[packed = true]` enables packing.
*
* In proto3, all repeated fields are packed by default.
* Setting the field option `[packed = false]` disables packing.
*
* Packed repeated fields are encoded with a single tag,
* then a length-delimiter, then the element values.
*
* Unpacked repeated fields are encoded with a tag and
* value for each element.
*
* `bytes` and `string` cannot be packed.
*/
var RepeatType;
(function (RepeatType) {
/**
* The field is not repeated.
*/
RepeatType[RepeatType["NO"] = 0] = "NO";
/**
* The field is repeated and should be packed.
* Invalid for `bytes` and `string`, they cannot be packed.
*/
RepeatType[RepeatType["PACKED"] = 1] = "PACKED";
/**
* The field is repeated but should not be packed.
* The only valid repeat type for repeated `bytes` and `string`.
*/
RepeatType[RepeatType["UNPACKED"] = 2] = "UNPACKED";
})(RepeatType = exports.RepeatType || (exports.RepeatType = {}));
/**
* Turns PartialFieldInfo into FieldInfo.
*/
function normalizeFieldInfo(field) {
var _a, _b, _c, _d;
field.localName = (_a = field.localName) !== null && _a !== void 0 ? _a : lower_camel_case_1.lowerCamelCase(field.name);
field.jsonName = (_b = field.jsonName) !== null && _b !== void 0 ? _b : lower_camel_case_1.lowerCamelCase(field.name);
field.repeat = (_c = field.repeat) !== null && _c !== void 0 ? _c : RepeatType.NO;
field.opt = (_d = field.opt) !== null && _d !== void 0 ? _d : (field.repeat ? false : field.oneof ? false : field.kind == "message");
return field;
}
exports.normalizeFieldInfo = normalizeFieldInfo;
/**
* Read custom field options from a generated message type.
*
* @deprecated use readFieldOption()
*/
function readFieldOptions(messageType, fieldName, extensionName, extensionType) {
var _a;
const options = (_a = messageType.fields.find((m, i) => m.localName == fieldName || i == fieldName)) === null || _a === void 0 ? void 0 : _a.options;
return options && options[extensionName] ? extensionType.fromJson(options[extensionName]) : undefined;
}
exports.readFieldOptions = readFieldOptions;
function readFieldOption(messageType, fieldName, extensionName, extensionType) {
var _a;
const options = (_a = messageType.fields.find((m, i) => m.localName == fieldName || i == fieldName)) === null || _a === void 0 ? void 0 : _a.options;
if (!options) {
return undefined;
}
const optionVal = options[extensionName];
if (optionVal === undefined) {
return optionVal;
}
return extensionType ? extensionType.fromJson(optionVal) : optionVal;
}
exports.readFieldOption = readFieldOption;
function readMessageOption(messageType, extensionName, extensionType) {
const options = messageType.options;
const optionVal = options[extensionName];
if (optionVal === undefined) {
return optionVal;
}
return extensionType ? extensionType.fromJson(optionVal) : optionVal;
}
exports.readMessageOption = readMessageOption;

View file

@ -0,0 +1,313 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectionJsonReader = void 0;
const json_typings_1 = require("./json-typings");
const base64_1 = require("./base64");
const reflection_info_1 = require("./reflection-info");
const pb_long_1 = require("./pb-long");
const assert_1 = require("./assert");
const reflection_long_convert_1 = require("./reflection-long-convert");
/**
* Reads proto3 messages in canonical JSON format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/proto3#json
*/
class ReflectionJsonReader {
constructor(info) {
this.info = info;
}
prepare() {
var _a;
if (this.fMap === undefined) {
this.fMap = {};
const fieldsInput = (_a = this.info.fields) !== null && _a !== void 0 ? _a : [];
for (const field of fieldsInput) {
this.fMap[field.name] = field;
this.fMap[field.jsonName] = field;
this.fMap[field.localName] = field;
}
}
}
// Cannot parse JSON <type of jsonValue> for <type name>#<fieldName>.
assert(condition, fieldName, jsonValue) {
if (!condition) {
let what = json_typings_1.typeofJsonValue(jsonValue);
if (what == "number" || what == "boolean")
what = jsonValue.toString();
throw new Error(`Cannot parse JSON ${what} for ${this.info.typeName}#${fieldName}`);
}
}
/**
* Reads a message from canonical JSON format into the target message.
*
* Repeated fields are appended. Map entries are added, overwriting
* existing keys.
*
* If a message field is already present, it will be merged with the
* new data.
*/
read(input, message, options) {
this.prepare();
const oneofsHandled = [];
for (const [jsonKey, jsonValue] of Object.entries(input)) {
const field = this.fMap[jsonKey];
if (!field) {
if (!options.ignoreUnknownFields)
throw new Error(`Found unknown field while reading ${this.info.typeName} from JSON format. JSON key: ${jsonKey}`);
continue;
}
const localName = field.localName;
// handle oneof ADT
let target; // this will be the target for the field value, whether it is member of a oneof or not
if (field.oneof) {
if (jsonValue === null && (field.kind !== 'enum' || field.T()[0] !== 'google.protobuf.NullValue')) {
continue;
}
// since json objects are unordered by specification, it is not possible to take the last of multiple oneofs
if (oneofsHandled.includes(field.oneof))
throw new Error(`Multiple members of the oneof group "${field.oneof}" of ${this.info.typeName} are present in JSON.`);
oneofsHandled.push(field.oneof);
target = message[field.oneof] = {
oneofKind: localName
};
}
else {
target = message;
}
// we have handled oneof above. we just have read the value into `target`.
if (field.kind == 'map') {
if (jsonValue === null) {
continue;
}
// check input
this.assert(json_typings_1.isJsonObject(jsonValue), field.name, jsonValue);
// our target to put map entries into
const fieldObj = target[localName];
// read entries
for (const [jsonObjKey, jsonObjValue] of Object.entries(jsonValue)) {
this.assert(jsonObjValue !== null, field.name + " map value", null);
// read value
let val;
switch (field.V.kind) {
case "message":
val = field.V.T().internalJsonRead(jsonObjValue, options);
break;
case "enum":
val = this.enum(field.V.T(), jsonObjValue, field.name, options.ignoreUnknownFields);
if (val === false)
continue;
break;
case "scalar":
val = this.scalar(jsonObjValue, field.V.T, field.V.L, field.name);
break;
}
this.assert(val !== undefined, field.name + " map value", jsonObjValue);
// read key
let key = jsonObjKey;
if (field.K == reflection_info_1.ScalarType.BOOL)
key = key == "true" ? true : key == "false" ? false : key;
key = this.scalar(key, field.K, reflection_info_1.LongType.STRING, field.name).toString();
fieldObj[key] = val;
}
}
else if (field.repeat) {
if (jsonValue === null)
continue;
// check input
this.assert(Array.isArray(jsonValue), field.name, jsonValue);
// our target to put array entries into
const fieldArr = target[localName];
// read array entries
for (const jsonItem of jsonValue) {
this.assert(jsonItem !== null, field.name, null);
let val;
switch (field.kind) {
case "message":
val = field.T().internalJsonRead(jsonItem, options);
break;
case "enum":
val = this.enum(field.T(), jsonItem, field.name, options.ignoreUnknownFields);
if (val === false)
continue;
break;
case "scalar":
val = this.scalar(jsonItem, field.T, field.L, field.name);
break;
}
this.assert(val !== undefined, field.name, jsonValue);
fieldArr.push(val);
}
}
else {
switch (field.kind) {
case "message":
if (jsonValue === null && field.T().typeName != 'google.protobuf.Value') {
this.assert(field.oneof === undefined, field.name + " (oneof member)", null);
continue;
}
target[localName] = field.T().internalJsonRead(jsonValue, options, target[localName]);
break;
case "enum":
let val = this.enum(field.T(), jsonValue, field.name, options.ignoreUnknownFields);
if (val === false)
continue;
target[localName] = val;
break;
case "scalar":
target[localName] = this.scalar(jsonValue, field.T, field.L, field.name);
break;
}
}
}
}
/**
* Returns `false` for unrecognized string representations.
*
* google.protobuf.NullValue accepts only JSON `null` (or the old `"NULL_VALUE"`).
*/
enum(type, json, fieldName, ignoreUnknownFields) {
if (type[0] == 'google.protobuf.NullValue')
assert_1.assert(json === null || json === "NULL_VALUE", `Unable to parse field ${this.info.typeName}#${fieldName}, enum ${type[0]} only accepts null.`);
if (json === null)
// we require 0 to be default value for all enums
return 0;
switch (typeof json) {
case "number":
assert_1.assert(Number.isInteger(json), `Unable to parse field ${this.info.typeName}#${fieldName}, enum can only be integral number, got ${json}.`);
return json;
case "string":
let localEnumName = json;
if (type[2] && json.substring(0, type[2].length) === type[2])
// lookup without the shared prefix
localEnumName = json.substring(type[2].length);
let enumNumber = type[1][localEnumName];
if (typeof enumNumber === 'undefined' && ignoreUnknownFields) {
return false;
}
assert_1.assert(typeof enumNumber == "number", `Unable to parse field ${this.info.typeName}#${fieldName}, enum ${type[0]} has no value for "${json}".`);
return enumNumber;
}
assert_1.assert(false, `Unable to parse field ${this.info.typeName}#${fieldName}, cannot parse enum value from ${typeof json}".`);
}
scalar(json, type, longType, fieldName) {
let e;
try {
switch (type) {
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
// Either numbers or strings are accepted. Exponent notation is also accepted.
case reflection_info_1.ScalarType.DOUBLE:
case reflection_info_1.ScalarType.FLOAT:
if (json === null)
return .0;
if (json === "NaN")
return Number.NaN;
if (json === "Infinity")
return Number.POSITIVE_INFINITY;
if (json === "-Infinity")
return Number.NEGATIVE_INFINITY;
if (json === "") {
e = "empty string";
break;
}
if (typeof json == "string" && json.trim().length !== json.length) {
e = "extra whitespace";
break;
}
if (typeof json != "string" && typeof json != "number") {
break;
}
let float = Number(json);
if (Number.isNaN(float)) {
e = "not a number";
break;
}
if (!Number.isFinite(float)) {
// infinity and -infinity are handled by string representation above, so this is an error
e = "too large or small";
break;
}
if (type == reflection_info_1.ScalarType.FLOAT)
assert_1.assertFloat32(float);
return float;
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
case reflection_info_1.ScalarType.INT32:
case reflection_info_1.ScalarType.FIXED32:
case reflection_info_1.ScalarType.SFIXED32:
case reflection_info_1.ScalarType.SINT32:
case reflection_info_1.ScalarType.UINT32:
if (json === null)
return 0;
let int32;
if (typeof json == "number")
int32 = json;
else if (json === "")
e = "empty string";
else if (typeof json == "string") {
if (json.trim().length !== json.length)
e = "extra whitespace";
else
int32 = Number(json);
}
if (int32 === undefined)
break;
if (type == reflection_info_1.ScalarType.UINT32)
assert_1.assertUInt32(int32);
else
assert_1.assertInt32(int32);
return int32;
// int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
case reflection_info_1.ScalarType.INT64:
case reflection_info_1.ScalarType.SFIXED64:
case reflection_info_1.ScalarType.SINT64:
if (json === null)
return reflection_long_convert_1.reflectionLongConvert(pb_long_1.PbLong.ZERO, longType);
if (typeof json != "number" && typeof json != "string")
break;
return reflection_long_convert_1.reflectionLongConvert(pb_long_1.PbLong.from(json), longType);
case reflection_info_1.ScalarType.FIXED64:
case reflection_info_1.ScalarType.UINT64:
if (json === null)
return reflection_long_convert_1.reflectionLongConvert(pb_long_1.PbULong.ZERO, longType);
if (typeof json != "number" && typeof json != "string")
break;
return reflection_long_convert_1.reflectionLongConvert(pb_long_1.PbULong.from(json), longType);
// bool:
case reflection_info_1.ScalarType.BOOL:
if (json === null)
return false;
if (typeof json !== "boolean")
break;
return json;
// string:
case reflection_info_1.ScalarType.STRING:
if (json === null)
return "";
if (typeof json !== "string") {
e = "extra whitespace";
break;
}
try {
encodeURIComponent(json);
}
catch (e) {
e = "invalid UTF8";
break;
}
return json;
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
case reflection_info_1.ScalarType.BYTES:
if (json === null || json === "")
return new Uint8Array(0);
if (typeof json !== 'string')
break;
return base64_1.base64decode(json);
}
}
catch (error) {
e = error.message;
}
this.assert(false, fieldName + (e ? " - " + e : ""), json);
}
}
exports.ReflectionJsonReader = ReflectionJsonReader;

View file

@ -0,0 +1,230 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectionJsonWriter = void 0;
const base64_1 = require("./base64");
const pb_long_1 = require("./pb-long");
const reflection_info_1 = require("./reflection-info");
const assert_1 = require("./assert");
/**
* Writes proto3 messages in canonical JSON format using reflection
* information.
*
* https://developers.google.com/protocol-buffers/docs/proto3#json
*/
class ReflectionJsonWriter {
constructor(info) {
var _a;
this.fields = (_a = info.fields) !== null && _a !== void 0 ? _a : [];
}
/**
* Converts the message to a JSON object, based on the field descriptors.
*/
write(message, options) {
const json = {}, source = message;
for (const field of this.fields) {
// field is not part of a oneof, simply write as is
if (!field.oneof) {
let jsonValue = this.field(field, source[field.localName], options);
if (jsonValue !== undefined)
json[options.useProtoFieldName ? field.name : field.jsonName] = jsonValue;
continue;
}
// field is part of a oneof
const group = source[field.oneof];
if (group.oneofKind !== field.localName)
continue; // not selected, skip
const opt = field.kind == 'scalar' || field.kind == 'enum'
? Object.assign(Object.assign({}, options), { emitDefaultValues: true }) : options;
let jsonValue = this.field(field, group[field.localName], opt);
assert_1.assert(jsonValue !== undefined);
json[options.useProtoFieldName ? field.name : field.jsonName] = jsonValue;
}
return json;
}
field(field, value, options) {
let jsonValue = undefined;
if (field.kind == 'map') {
assert_1.assert(typeof value == "object" && value !== null);
const jsonObj = {};
switch (field.V.kind) {
case "scalar":
for (const [entryKey, entryValue] of Object.entries(value)) {
const val = this.scalar(field.V.T, entryValue, field.name, false, true);
assert_1.assert(val !== undefined);
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
}
break;
case "message":
const messageType = field.V.T();
for (const [entryKey, entryValue] of Object.entries(value)) {
const val = this.message(messageType, entryValue, field.name, options);
assert_1.assert(val !== undefined);
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
}
break;
case "enum":
const enumInfo = field.V.T();
for (const [entryKey, entryValue] of Object.entries(value)) {
assert_1.assert(entryValue === undefined || typeof entryValue == 'number');
const val = this.enum(enumInfo, entryValue, field.name, false, true, options.enumAsInteger);
assert_1.assert(val !== undefined);
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
}
break;
}
if (options.emitDefaultValues || Object.keys(jsonObj).length > 0)
jsonValue = jsonObj;
}
else if (field.repeat) {
assert_1.assert(Array.isArray(value));
const jsonArr = [];
switch (field.kind) {
case "scalar":
for (let i = 0; i < value.length; i++) {
const val = this.scalar(field.T, value[i], field.name, field.opt, true);
assert_1.assert(val !== undefined);
jsonArr.push(val);
}
break;
case "enum":
const enumInfo = field.T();
for (let i = 0; i < value.length; i++) {
assert_1.assert(value[i] === undefined || typeof value[i] == 'number');
const val = this.enum(enumInfo, value[i], field.name, field.opt, true, options.enumAsInteger);
assert_1.assert(val !== undefined);
jsonArr.push(val);
}
break;
case "message":
const messageType = field.T();
for (let i = 0; i < value.length; i++) {
const val = this.message(messageType, value[i], field.name, options);
assert_1.assert(val !== undefined);
jsonArr.push(val);
}
break;
}
// add converted array to json output
if (options.emitDefaultValues || jsonArr.length > 0 || options.emitDefaultValues)
jsonValue = jsonArr;
}
else {
switch (field.kind) {
case "scalar":
jsonValue = this.scalar(field.T, value, field.name, field.opt, options.emitDefaultValues);
break;
case "enum":
jsonValue = this.enum(field.T(), value, field.name, field.opt, options.emitDefaultValues, options.enumAsInteger);
break;
case "message":
jsonValue = this.message(field.T(), value, field.name, options);
break;
}
}
return jsonValue;
}
/**
* Returns `null` as the default for google.protobuf.NullValue.
*/
enum(type, value, fieldName, optional, emitDefaultValues, enumAsInteger) {
if (type[0] == 'google.protobuf.NullValue')
return !emitDefaultValues && !optional ? undefined : null;
if (value === undefined) {
assert_1.assert(optional);
return undefined;
}
if (value === 0 && !emitDefaultValues && !optional)
// we require 0 to be default value for all enums
return undefined;
assert_1.assert(typeof value == 'number');
assert_1.assert(Number.isInteger(value));
if (enumAsInteger || !type[1].hasOwnProperty(value))
// if we don't now the enum value, just return the number
return value;
if (type[2])
// restore the dropped prefix
return type[2] + type[1][value];
return type[1][value];
}
message(type, value, fieldName, options) {
if (value === undefined)
return options.emitDefaultValues ? null : undefined;
return type.internalJsonWrite(value, options);
}
scalar(type, value, fieldName, optional, emitDefaultValues) {
if (value === undefined) {
assert_1.assert(optional);
return undefined;
}
const ed = emitDefaultValues || optional;
// noinspection FallThroughInSwitchStatementJS
switch (type) {
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
case reflection_info_1.ScalarType.INT32:
case reflection_info_1.ScalarType.SFIXED32:
case reflection_info_1.ScalarType.SINT32:
if (value === 0)
return ed ? 0 : undefined;
assert_1.assertInt32(value);
return value;
case reflection_info_1.ScalarType.FIXED32:
case reflection_info_1.ScalarType.UINT32:
if (value === 0)
return ed ? 0 : undefined;
assert_1.assertUInt32(value);
return value;
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
// Either numbers or strings are accepted. Exponent notation is also accepted.
case reflection_info_1.ScalarType.FLOAT:
assert_1.assertFloat32(value);
case reflection_info_1.ScalarType.DOUBLE:
if (value === 0)
return ed ? 0 : undefined;
assert_1.assert(typeof value == 'number');
if (Number.isNaN(value))
return 'NaN';
if (value === Number.POSITIVE_INFINITY)
return 'Infinity';
if (value === Number.NEGATIVE_INFINITY)
return '-Infinity';
return value;
// string:
case reflection_info_1.ScalarType.STRING:
if (value === "")
return ed ? '' : undefined;
assert_1.assert(typeof value == 'string');
return value;
// bool:
case reflection_info_1.ScalarType.BOOL:
if (value === false)
return ed ? false : undefined;
assert_1.assert(typeof value == 'boolean');
return value;
// JSON value will be a decimal string. Either numbers or strings are accepted.
case reflection_info_1.ScalarType.UINT64:
case reflection_info_1.ScalarType.FIXED64:
assert_1.assert(typeof value == 'number' || typeof value == 'string' || typeof value == 'bigint');
let ulong = pb_long_1.PbULong.from(value);
if (ulong.isZero() && !ed)
return undefined;
return ulong.toString();
// JSON value will be a decimal string. Either numbers or strings are accepted.
case reflection_info_1.ScalarType.INT64:
case reflection_info_1.ScalarType.SFIXED64:
case reflection_info_1.ScalarType.SINT64:
assert_1.assert(typeof value == 'number' || typeof value == 'string' || typeof value == 'bigint');
let long = pb_long_1.PbLong.from(value);
if (long.isZero() && !ed)
return undefined;
return long.toString();
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
case reflection_info_1.ScalarType.BYTES:
assert_1.assert(value instanceof Uint8Array);
if (!value.byteLength)
return ed ? "" : undefined;
return base64_1.base64encode(value);
}
}
}
exports.ReflectionJsonWriter = ReflectionJsonWriter;

View file

@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reflectionLongConvert = void 0;
const reflection_info_1 = require("./reflection-info");
/**
* Utility method to convert a PbLong or PbUlong to a JavaScript
* representation during runtime.
*
* Works with generated field information, `undefined` is equivalent
* to `STRING`.
*/
function reflectionLongConvert(long, type) {
switch (type) {
case reflection_info_1.LongType.BIGINT:
return long.toBigInt();
case reflection_info_1.LongType.NUMBER:
return long.toNumber();
default:
// case undefined:
// case LongType.STRING:
return long.toString();
}
}
exports.reflectionLongConvert = reflectionLongConvert;

View file

@ -0,0 +1,90 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reflectionMergePartial = void 0;
/**
* Copy partial data into the target message.
*
* If a singular scalar or enum field is present in the source, it
* replaces the field in the target.
*
* If a singular message field is present in the source, it is merged
* with the target field by calling mergePartial() of the responsible
* message type.
*
* If a repeated field is present in the source, its values replace
* all values in the target array, removing extraneous values.
* Repeated message fields are copied, not merged.
*
* If a map field is present in the source, entries are added to the
* target map, replacing entries with the same key. Entries that only
* exist in the target remain. Entries with message values are copied,
* not merged.
*
* Note that this function differs from protobuf merge semantics,
* which appends repeated fields.
*/
function reflectionMergePartial(info, target, source) {
let fieldValue, // the field value we are working with
input = source, output; // where we want our field value to go
for (let field of info.fields) {
let name = field.localName;
if (field.oneof) {
const group = input[field.oneof]; // this is the oneof`s group in the source
if ((group === null || group === void 0 ? void 0 : group.oneofKind) == undefined) { // the user is free to omit
continue; // we skip this field, and all other members too
}
fieldValue = group[name]; // our value comes from the the oneof group of the source
output = target[field.oneof]; // and our output is the oneof group of the target
output.oneofKind = group.oneofKind; // always update discriminator
if (fieldValue == undefined) {
delete output[name]; // remove any existing value
continue; // skip further work on field
}
}
else {
fieldValue = input[name]; // we are using the source directly
output = target; // we want our field value to go directly into the target
if (fieldValue == undefined) {
continue; // skip further work on field, existing value is used as is
}
}
if (field.repeat)
output[name].length = fieldValue.length; // resize target array to match source array
// now we just work with `fieldValue` and `output` to merge the value
switch (field.kind) {
case "scalar":
case "enum":
if (field.repeat)
for (let i = 0; i < fieldValue.length; i++)
output[name][i] = fieldValue[i]; // not a reference type
else
output[name] = fieldValue; // not a reference type
break;
case "message":
let T = field.T();
if (field.repeat)
for (let i = 0; i < fieldValue.length; i++)
output[name][i] = T.create(fieldValue[i]);
else if (output[name] === undefined)
output[name] = T.create(fieldValue); // nothing to merge with
else
T.mergePartial(output[name], fieldValue);
break;
case "map":
// Map and repeated fields are simply overwritten, not appended or merged
switch (field.V.kind) {
case "scalar":
case "enum":
Object.assign(output[name], fieldValue); // elements are not reference types
break;
case "message":
let T = field.V.T();
for (let k of Object.keys(fieldValue))
output[name][k] = T.create(fieldValue[k]);
break;
}
break;
}
}
}
exports.reflectionMergePartial = reflectionMergePartial;

View file

@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reflectionScalarDefault = void 0;
const reflection_info_1 = require("./reflection-info");
const reflection_long_convert_1 = require("./reflection-long-convert");
const pb_long_1 = require("./pb-long");
/**
* Creates the default value for a scalar type.
*/
function reflectionScalarDefault(type, longType = reflection_info_1.LongType.STRING) {
switch (type) {
case reflection_info_1.ScalarType.BOOL:
return false;
case reflection_info_1.ScalarType.UINT64:
case reflection_info_1.ScalarType.FIXED64:
return reflection_long_convert_1.reflectionLongConvert(pb_long_1.PbULong.ZERO, longType);
case reflection_info_1.ScalarType.INT64:
case reflection_info_1.ScalarType.SFIXED64:
case reflection_info_1.ScalarType.SINT64:
return reflection_long_convert_1.reflectionLongConvert(pb_long_1.PbLong.ZERO, longType);
case reflection_info_1.ScalarType.DOUBLE:
case reflection_info_1.ScalarType.FLOAT:
return 0.0;
case reflection_info_1.ScalarType.BYTES:
return new Uint8Array(0);
case reflection_info_1.ScalarType.STRING:
return "";
default:
// case ScalarType.INT32:
// case ScalarType.UINT32:
// case ScalarType.SINT32:
// case ScalarType.FIXED32:
// case ScalarType.SFIXED32:
return 0;
}
}
exports.reflectionScalarDefault = reflectionScalarDefault;

View file

@ -0,0 +1,230 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectionTypeCheck = void 0;
const reflection_info_1 = require("./reflection-info");
const oneof_1 = require("./oneof");
// noinspection JSMethodCanBeStatic
class ReflectionTypeCheck {
constructor(info) {
var _a;
this.fields = (_a = info.fields) !== null && _a !== void 0 ? _a : [];
}
prepare() {
if (this.data)
return;
const req = [], known = [], oneofs = [];
for (let field of this.fields) {
if (field.oneof) {
if (!oneofs.includes(field.oneof)) {
oneofs.push(field.oneof);
req.push(field.oneof);
known.push(field.oneof);
}
}
else {
known.push(field.localName);
switch (field.kind) {
case "scalar":
case "enum":
if (!field.opt || field.repeat)
req.push(field.localName);
break;
case "message":
if (field.repeat)
req.push(field.localName);
break;
case "map":
req.push(field.localName);
break;
}
}
}
this.data = { req, known, oneofs: Object.values(oneofs) };
}
/**
* Is the argument a valid message as specified by the
* reflection information?
*
* Checks all field types recursively. The `depth`
* specifies how deep into the structure the check will be.
*
* With a depth of 0, only the presence of fields
* is checked.
*
* With a depth of 1 or more, the field types are checked.
*
* With a depth of 2 or more, the members of map, repeated
* and message fields are checked.
*
* Message fields will be checked recursively with depth - 1.
*
* The number of map entries / repeated values being checked
* is < depth.
*/
is(message, depth, allowExcessProperties = false) {
if (depth < 0)
return true;
if (message === null || message === undefined || typeof message != 'object')
return false;
this.prepare();
let keys = Object.keys(message), data = this.data;
// if a required field is missing in arg, this cannot be a T
if (keys.length < data.req.length || data.req.some(n => !keys.includes(n)))
return false;
if (!allowExcessProperties) {
// if the arg contains a key we dont know, this is not a literal T
if (keys.some(k => !data.known.includes(k)))
return false;
}
// "With a depth of 0, only the presence and absence of fields is checked."
// "With a depth of 1 or more, the field types are checked."
if (depth < 1) {
return true;
}
// check oneof group
for (const name of data.oneofs) {
const group = message[name];
if (!oneof_1.isOneofGroup(group))
return false;
if (group.oneofKind === undefined)
continue;
const field = this.fields.find(f => f.localName === group.oneofKind);
if (!field)
return false; // we found no field, but have a kind, something is wrong
if (!this.field(group[group.oneofKind], field, allowExcessProperties, depth))
return false;
}
// check types
for (const field of this.fields) {
if (field.oneof !== undefined)
continue;
if (!this.field(message[field.localName], field, allowExcessProperties, depth))
return false;
}
return true;
}
field(arg, field, allowExcessProperties, depth) {
let repeated = field.repeat;
switch (field.kind) {
case "scalar":
if (arg === undefined)
return field.opt;
if (repeated)
return this.scalars(arg, field.T, depth, field.L);
return this.scalar(arg, field.T, field.L);
case "enum":
if (arg === undefined)
return field.opt;
if (repeated)
return this.scalars(arg, reflection_info_1.ScalarType.INT32, depth);
return this.scalar(arg, reflection_info_1.ScalarType.INT32);
case "message":
if (arg === undefined)
return true;
if (repeated)
return this.messages(arg, field.T(), allowExcessProperties, depth);
return this.message(arg, field.T(), allowExcessProperties, depth);
case "map":
if (typeof arg != 'object' || arg === null)
return false;
if (depth < 2)
return true;
if (!this.mapKeys(arg, field.K, depth))
return false;
switch (field.V.kind) {
case "scalar":
return this.scalars(Object.values(arg), field.V.T, depth, field.V.L);
case "enum":
return this.scalars(Object.values(arg), reflection_info_1.ScalarType.INT32, depth);
case "message":
return this.messages(Object.values(arg), field.V.T(), allowExcessProperties, depth);
}
break;
}
return true;
}
message(arg, type, allowExcessProperties, depth) {
if (allowExcessProperties) {
return type.isAssignable(arg, depth);
}
return type.is(arg, depth);
}
messages(arg, type, allowExcessProperties, depth) {
if (!Array.isArray(arg))
return false;
if (depth < 2)
return true;
if (allowExcessProperties) {
for (let i = 0; i < arg.length && i < depth; i++)
if (!type.isAssignable(arg[i], depth - 1))
return false;
}
else {
for (let i = 0; i < arg.length && i < depth; i++)
if (!type.is(arg[i], depth - 1))
return false;
}
return true;
}
scalar(arg, type, longType) {
let argType = typeof arg;
switch (type) {
case reflection_info_1.ScalarType.UINT64:
case reflection_info_1.ScalarType.FIXED64:
case reflection_info_1.ScalarType.INT64:
case reflection_info_1.ScalarType.SFIXED64:
case reflection_info_1.ScalarType.SINT64:
switch (longType) {
case reflection_info_1.LongType.BIGINT:
return argType == "bigint";
case reflection_info_1.LongType.NUMBER:
return argType == "number" && !isNaN(arg);
default:
return argType == "string";
}
case reflection_info_1.ScalarType.BOOL:
return argType == 'boolean';
case reflection_info_1.ScalarType.STRING:
return argType == 'string';
case reflection_info_1.ScalarType.BYTES:
return arg instanceof Uint8Array;
case reflection_info_1.ScalarType.DOUBLE:
case reflection_info_1.ScalarType.FLOAT:
return argType == 'number' && !isNaN(arg);
default:
// case ScalarType.UINT32:
// case ScalarType.FIXED32:
// case ScalarType.INT32:
// case ScalarType.SINT32:
// case ScalarType.SFIXED32:
return argType == 'number' && Number.isInteger(arg);
}
}
scalars(arg, type, depth, longType) {
if (!Array.isArray(arg))
return false;
if (depth < 2)
return true;
if (Array.isArray(arg))
for (let i = 0; i < arg.length && i < depth; i++)
if (!this.scalar(arg[i], type, longType))
return false;
return true;
}
mapKeys(map, type, depth) {
let keys = Object.keys(map);
switch (type) {
case reflection_info_1.ScalarType.INT32:
case reflection_info_1.ScalarType.FIXED32:
case reflection_info_1.ScalarType.SFIXED32:
case reflection_info_1.ScalarType.SINT32:
case reflection_info_1.ScalarType.UINT32:
return this.scalars(keys.slice(0, depth).map(k => parseInt(k)), type, depth);
case reflection_info_1.ScalarType.BOOL:
return this.scalars(keys.slice(0, depth).map(k => k == 'true' ? true : k == 'false' ? false : k), type, depth);
default:
return this.scalars(keys, type, depth, reflection_info_1.LongType.STRING);
}
}
}
exports.ReflectionTypeCheck = ReflectionTypeCheck;

View file

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View file

@ -0,0 +1,35 @@
/**
* assert that condition is true or throw error (with message)
*/
export function assert(condition, msg) {
if (!condition) {
throw new Error(msg);
}
}
/**
* assert that value cannot exist = type `never`. throw runtime error if it does.
*/
export function assertNever(value, msg) {
throw new Error(msg !== null && msg !== void 0 ? msg : 'Unexpected object: ' + value);
}
const FLOAT32_MAX = 3.4028234663852886e+38, FLOAT32_MIN = -3.4028234663852886e+38, UINT32_MAX = 0xFFFFFFFF, INT32_MAX = 0X7FFFFFFF, INT32_MIN = -0X80000000;
export function assertInt32(arg) {
if (typeof arg !== "number")
throw new Error('invalid int 32: ' + typeof arg);
if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
throw new Error('invalid int 32: ' + arg);
}
export function assertUInt32(arg) {
if (typeof arg !== "number")
throw new Error('invalid uint 32: ' + typeof arg);
if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
throw new Error('invalid uint 32: ' + arg);
}
export function assertFloat32(arg) {
if (typeof arg !== "number")
throw new Error('invalid float 32: ' + typeof arg);
if (!Number.isFinite(arg))
return;
if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
throw new Error('invalid float 32: ' + arg);
}

View file

@ -0,0 +1,113 @@
// lookup table from base64 character to byte
let encTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
// lookup table from base64 character *code* to byte because lookup by number is fast
let decTable = [];
for (let i = 0; i < encTable.length; i++)
decTable[encTable[i].charCodeAt(0)] = i;
// support base64url variants
decTable["-".charCodeAt(0)] = encTable.indexOf("+");
decTable["_".charCodeAt(0)] = encTable.indexOf("/");
/**
* Decodes a base64 string to a byte array.
*
* - ignores white-space, including line breaks and tabs
* - allows inner padding (can decode concatenated base64 strings)
* - does not require padding
* - understands base64url encoding:
* "-" instead of "+",
* "_" instead of "/",
* no padding
*/
export function base64decode(base64Str) {
// estimate byte size, not accounting for inner padding and whitespace
let es = base64Str.length * 3 / 4;
// if (es % 3 !== 0)
// throw new Error('invalid base64 string');
if (base64Str[base64Str.length - 2] == '=')
es -= 2;
else if (base64Str[base64Str.length - 1] == '=')
es -= 1;
let bytes = new Uint8Array(es), bytePos = 0, // position in byte array
groupPos = 0, // position in base64 group
b, // current byte
p = 0 // previous byte
;
for (let i = 0; i < base64Str.length; i++) {
b = decTable[base64Str.charCodeAt(i)];
if (b === undefined) {
// noinspection FallThroughInSwitchStatementJS
switch (base64Str[i]) {
case '=':
groupPos = 0; // reset state when padding found
case '\n':
case '\r':
case '\t':
case ' ':
continue; // skip white-space, and padding
default:
throw Error(`invalid base64 string.`);
}
}
switch (groupPos) {
case 0:
p = b;
groupPos = 1;
break;
case 1:
bytes[bytePos++] = p << 2 | (b & 48) >> 4;
p = b;
groupPos = 2;
break;
case 2:
bytes[bytePos++] = (p & 15) << 4 | (b & 60) >> 2;
p = b;
groupPos = 3;
break;
case 3:
bytes[bytePos++] = (p & 3) << 6 | b;
groupPos = 0;
break;
}
}
if (groupPos == 1)
throw Error(`invalid base64 string.`);
return bytes.subarray(0, bytePos);
}
/**
* Encodes a byte array to a base64 string.
* Adds padding at the end.
* Does not insert newlines.
*/
export function base64encode(bytes) {
let base64 = '', groupPos = 0, // position in base64 group
b, // current byte
p = 0; // carry over from previous byte
for (let i = 0; i < bytes.length; i++) {
b = bytes[i];
switch (groupPos) {
case 0:
base64 += encTable[b >> 2];
p = (b & 3) << 4;
groupPos = 1;
break;
case 1:
base64 += encTable[p | b >> 4];
p = (b & 15) << 2;
groupPos = 2;
break;
case 2:
base64 += encTable[p | b >> 6];
base64 += encTable[b & 63];
groupPos = 0;
break;
}
}
// padding required?
if (groupPos) {
base64 += encTable[p];
base64 += '=';
if (groupPos == 1)
base64 += '=';
}
return base64;
}

View file

@ -0,0 +1,96 @@
/**
* This handler implements the default behaviour for unknown fields.
* When reading data, unknown fields are stored on the message, in a
* symbol property.
* When writing data, the symbol property is queried and unknown fields
* are serialized into the output again.
*/
export var UnknownFieldHandler;
(function (UnknownFieldHandler) {
/**
* The symbol used to store unknown fields for a message.
* The property must conform to `UnknownFieldContainer`.
*/
UnknownFieldHandler.symbol = Symbol.for("protobuf-ts/unknown");
/**
* Store an unknown field during binary read directly on the message.
* This method is compatible with `BinaryReadOptions.readUnknownField`.
*/
UnknownFieldHandler.onRead = (typeName, message, fieldNo, wireType, data) => {
let container = is(message) ? message[UnknownFieldHandler.symbol] : message[UnknownFieldHandler.symbol] = [];
container.push({ no: fieldNo, wireType, data });
};
/**
* Write unknown fields stored for the message to the writer.
* This method is compatible with `BinaryWriteOptions.writeUnknownFields`.
*/
UnknownFieldHandler.onWrite = (typeName, message, writer) => {
for (let { no, wireType, data } of UnknownFieldHandler.list(message))
writer.tag(no, wireType).raw(data);
};
/**
* List unknown fields stored for the message.
* Note that there may be multiples fields with the same number.
*/
UnknownFieldHandler.list = (message, fieldNo) => {
if (is(message)) {
let all = message[UnknownFieldHandler.symbol];
return fieldNo ? all.filter(uf => uf.no == fieldNo) : all;
}
return [];
};
/**
* Returns the last unknown field by field number.
*/
UnknownFieldHandler.last = (message, fieldNo) => UnknownFieldHandler.list(message, fieldNo).slice(-1)[0];
const is = (message) => message && Array.isArray(message[UnknownFieldHandler.symbol]);
})(UnknownFieldHandler || (UnknownFieldHandler = {}));
/**
* Merges binary write or read options. Later values override earlier values.
*/
export function mergeBinaryOptions(a, b) {
return Object.assign(Object.assign({}, a), b);
}
/**
* Protobuf binary format wire types.
*
* A wire type provides just enough information to find the length of the
* following value.
*
* See https://developers.google.com/protocol-buffers/docs/encoding#structure
*/
export var WireType;
(function (WireType) {
/**
* Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
*/
WireType[WireType["Varint"] = 0] = "Varint";
/**
* Used for fixed64, sfixed64, double.
* Always 8 bytes with little-endian byte order.
*/
WireType[WireType["Bit64"] = 1] = "Bit64";
/**
* Used for string, bytes, embedded messages, packed repeated fields
*
* Only repeated numeric types (types which use the varint, 32-bit,
* or 64-bit wire types) can be packed. In proto3, such fields are
* packed by default.
*/
WireType[WireType["LengthDelimited"] = 2] = "LengthDelimited";
/**
* Used for groups
* @deprecated
*/
WireType[WireType["StartGroup"] = 3] = "StartGroup";
/**
* Used for groups
* @deprecated
*/
WireType[WireType["EndGroup"] = 4] = "EndGroup";
/**
* Used for fixed32, sfixed32, float.
* Always 4 bytes with little-endian byte order.
*/
WireType[WireType["Bit32"] = 5] = "Bit32";
})(WireType || (WireType = {}));

View file

@ -0,0 +1,178 @@
import { WireType } from "./binary-format-contract";
import { PbLong, PbULong } from "./pb-long";
import { varint32read, varint64read } from "./goog-varint";
const defaultsRead = {
readUnknownField: true,
readerFactory: bytes => new BinaryReader(bytes),
};
/**
* Make options for reading binary data form partial options.
*/
export function binaryReadOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsRead), options) : defaultsRead;
}
export class BinaryReader {
constructor(buf, textDecoder) {
this.varint64 = varint64read; // dirty cast for `this`
/**
* Read a `uint32` field, an unsigned 32 bit varint.
*/
this.uint32 = varint32read; // dirty cast for `this` and access to protected `buf`
this.buf = buf;
this.len = buf.length;
this.pos = 0;
this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
this.textDecoder = textDecoder !== null && textDecoder !== void 0 ? textDecoder : new TextDecoder("utf-8", {
fatal: true,
ignoreBOM: true,
});
}
/**
* Reads a tag - field number and wire type.
*/
tag() {
let tag = this.uint32(), fieldNo = tag >>> 3, wireType = tag & 7;
if (fieldNo <= 0 || wireType < 0 || wireType > 5)
throw new Error("illegal tag: field no " + fieldNo + " wire type " + wireType);
return [fieldNo, wireType];
}
/**
* Skip one element on the wire and return the skipped data.
* Supports WireType.StartGroup since v2.0.0-alpha.23.
*/
skip(wireType) {
let start = this.pos;
// noinspection FallThroughInSwitchStatementJS
switch (wireType) {
case WireType.Varint:
while (this.buf[this.pos++] & 0x80) {
// ignore
}
break;
case WireType.Bit64:
this.pos += 4;
case WireType.Bit32:
this.pos += 4;
break;
case WireType.LengthDelimited:
let len = this.uint32();
this.pos += len;
break;
case WireType.StartGroup:
// From descriptor.proto: Group type is deprecated, not supported in proto3.
// But we must still be able to parse and treat as unknown.
let t;
while ((t = this.tag()[1]) !== WireType.EndGroup) {
this.skip(t);
}
break;
default:
throw new Error("cant skip wire type " + wireType);
}
this.assertBounds();
return this.buf.subarray(start, this.pos);
}
/**
* Throws error if position in byte array is out of range.
*/
assertBounds() {
if (this.pos > this.len)
throw new RangeError("premature EOF");
}
/**
* Read a `int32` field, a signed 32 bit varint.
*/
int32() {
return this.uint32() | 0;
}
/**
* Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
*/
sint32() {
let zze = this.uint32();
// decode zigzag
return (zze >>> 1) ^ -(zze & 1);
}
/**
* Read a `int64` field, a signed 64-bit varint.
*/
int64() {
return new PbLong(...this.varint64());
}
/**
* Read a `uint64` field, an unsigned 64-bit varint.
*/
uint64() {
return new PbULong(...this.varint64());
}
/**
* Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
*/
sint64() {
let [lo, hi] = this.varint64();
// decode zig zag
let s = -(lo & 1);
lo = ((lo >>> 1 | (hi & 1) << 31) ^ s);
hi = (hi >>> 1 ^ s);
return new PbLong(lo, hi);
}
/**
* Read a `bool` field, a variant.
*/
bool() {
let [lo, hi] = this.varint64();
return lo !== 0 || hi !== 0;
}
/**
* Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
*/
fixed32() {
return this.view.getUint32((this.pos += 4) - 4, true);
}
/**
* Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
*/
sfixed32() {
return this.view.getInt32((this.pos += 4) - 4, true);
}
/**
* Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
*/
fixed64() {
return new PbULong(this.sfixed32(), this.sfixed32());
}
/**
* Read a `fixed64` field, a signed, fixed-length 64-bit integer.
*/
sfixed64() {
return new PbLong(this.sfixed32(), this.sfixed32());
}
/**
* Read a `float` field, 32-bit floating point number.
*/
float() {
return this.view.getFloat32((this.pos += 4) - 4, true);
}
/**
* Read a `double` field, a 64-bit floating point number.
*/
double() {
return this.view.getFloat64((this.pos += 8) - 8, true);
}
/**
* Read a `bytes` field, length-delimited arbitrary data.
*/
bytes() {
let len = this.uint32();
let start = this.pos;
this.pos += len;
this.assertBounds();
return this.buf.subarray(start, start + len);
}
/**
* Read a `string` field, length-delimited data converted to UTF-8 text.
*/
string() {
return this.textDecoder.decode(this.bytes());
}
}

View file

@ -0,0 +1,227 @@
import { PbLong, PbULong } from "./pb-long";
import { varint32write, varint64write } from "./goog-varint";
import { assertFloat32, assertInt32, assertUInt32 } from "./assert";
const defaultsWrite = {
writeUnknownFields: true,
writerFactory: () => new BinaryWriter(),
};
/**
* Make options for writing binary data form partial options.
*/
export function binaryWriteOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsWrite), options) : defaultsWrite;
}
export class BinaryWriter {
constructor(textEncoder) {
/**
* Previous fork states.
*/
this.stack = [];
this.textEncoder = textEncoder !== null && textEncoder !== void 0 ? textEncoder : new TextEncoder();
this.chunks = [];
this.buf = [];
}
/**
* Return all bytes written and reset this writer.
*/
finish() {
this.chunks.push(new Uint8Array(this.buf)); // flush the buffer
let len = 0;
for (let i = 0; i < this.chunks.length; i++)
len += this.chunks[i].length;
let bytes = new Uint8Array(len);
let offset = 0;
for (let i = 0; i < this.chunks.length; i++) {
bytes.set(this.chunks[i], offset);
offset += this.chunks[i].length;
}
this.chunks = [];
return bytes;
}
/**
* Start a new fork for length-delimited data like a message
* or a packed repeated field.
*
* Must be joined later with `join()`.
*/
fork() {
this.stack.push({ chunks: this.chunks, buf: this.buf });
this.chunks = [];
this.buf = [];
return this;
}
/**
* Join the last fork. Write its length and bytes, then
* return to the previous state.
*/
join() {
// get chunk of fork
let chunk = this.finish();
// restore previous state
let prev = this.stack.pop();
if (!prev)
throw new Error('invalid state, fork stack empty');
this.chunks = prev.chunks;
this.buf = prev.buf;
// write length of chunk as varint
this.uint32(chunk.byteLength);
return this.raw(chunk);
}
/**
* Writes a tag (field number and wire type).
*
* Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
*
* Generated code should compute the tag ahead of time and call `uint32()`.
*/
tag(fieldNo, type) {
return this.uint32((fieldNo << 3 | type) >>> 0);
}
/**
* Write a chunk of raw bytes.
*/
raw(chunk) {
if (this.buf.length) {
this.chunks.push(new Uint8Array(this.buf));
this.buf = [];
}
this.chunks.push(chunk);
return this;
}
/**
* Write a `uint32` value, an unsigned 32 bit varint.
*/
uint32(value) {
assertUInt32(value);
// write value as varint 32, inlined for speed
while (value > 0x7f) {
this.buf.push((value & 0x7f) | 0x80);
value = value >>> 7;
}
this.buf.push(value);
return this;
}
/**
* Write a `int32` value, a signed 32 bit varint.
*/
int32(value) {
assertInt32(value);
varint32write(value, this.buf);
return this;
}
/**
* Write a `bool` value, a variant.
*/
bool(value) {
this.buf.push(value ? 1 : 0);
return this;
}
/**
* Write a `bytes` value, length-delimited arbitrary data.
*/
bytes(value) {
this.uint32(value.byteLength); // write length of chunk as varint
return this.raw(value);
}
/**
* Write a `string` value, length-delimited data converted to UTF-8 text.
*/
string(value) {
let chunk = this.textEncoder.encode(value);
this.uint32(chunk.byteLength); // write length of chunk as varint
return this.raw(chunk);
}
/**
* Write a `float` value, 32-bit floating point number.
*/
float(value) {
assertFloat32(value);
let chunk = new Uint8Array(4);
new DataView(chunk.buffer).setFloat32(0, value, true);
return this.raw(chunk);
}
/**
* Write a `double` value, a 64-bit floating point number.
*/
double(value) {
let chunk = new Uint8Array(8);
new DataView(chunk.buffer).setFloat64(0, value, true);
return this.raw(chunk);
}
/**
* Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
*/
fixed32(value) {
assertUInt32(value);
let chunk = new Uint8Array(4);
new DataView(chunk.buffer).setUint32(0, value, true);
return this.raw(chunk);
}
/**
* Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
*/
sfixed32(value) {
assertInt32(value);
let chunk = new Uint8Array(4);
new DataView(chunk.buffer).setInt32(0, value, true);
return this.raw(chunk);
}
/**
* Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
*/
sint32(value) {
assertInt32(value);
// zigzag encode
value = ((value << 1) ^ (value >> 31)) >>> 0;
varint32write(value, this.buf);
return this;
}
/**
* Write a `fixed64` value, a signed, fixed-length 64-bit integer.
*/
sfixed64(value) {
let chunk = new Uint8Array(8);
let view = new DataView(chunk.buffer);
let long = PbLong.from(value);
view.setInt32(0, long.lo, true);
view.setInt32(4, long.hi, true);
return this.raw(chunk);
}
/**
* Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
*/
fixed64(value) {
let chunk = new Uint8Array(8);
let view = new DataView(chunk.buffer);
let long = PbULong.from(value);
view.setInt32(0, long.lo, true);
view.setInt32(4, long.hi, true);
return this.raw(chunk);
}
/**
* Write a `int64` value, a signed 64-bit varint.
*/
int64(value) {
let long = PbLong.from(value);
varint64write(long.lo, long.hi, this.buf);
return this;
}
/**
* Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
*/
sint64(value) {
let long = PbLong.from(value),
// zigzag encode
sign = long.hi >> 31, lo = (long.lo << 1) ^ sign, hi = ((long.hi << 1) | (long.lo >>> 31)) ^ sign;
varint64write(lo, hi, this.buf);
return this;
}
/**
* Write a `uint64` value, an unsigned 64-bit varint.
*/
uint64(value) {
let long = PbULong.from(value);
varint64write(long.lo, long.hi, this.buf);
return this;
}
}

View file

@ -0,0 +1,80 @@
/**
* Is this a lookup object generated by Typescript, for a Typescript enum
* generated by protobuf-ts?
*
* - No `const enum` (enum must not be inlined, we need reverse mapping).
* - No string enum (we need int32 for protobuf).
* - Must have a value for 0 (otherwise, we would need to support custom default values).
*/
export function isEnumObject(arg) {
if (typeof arg != 'object' || arg === null) {
return false;
}
if (!arg.hasOwnProperty(0)) {
return false;
}
for (let k of Object.keys(arg)) {
let num = parseInt(k);
if (!Number.isNaN(num)) {
// is there a name for the number?
let nam = arg[num];
if (nam === undefined)
return false;
// does the name resolve back to the number?
if (arg[nam] !== num)
return false;
}
else {
// is there a number for the name?
let num = arg[k];
if (num === undefined)
return false;
// is it a string enum?
if (typeof num !== 'number')
return false;
// do we know the number?
if (arg[num] === undefined)
return false;
}
}
return true;
}
/**
* Lists all values of a Typescript enum, as an array of objects with a "name"
* property and a "number" property.
*
* Note that it is possible that a number appears more than once, because it is
* possible to have aliases in an enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
export function listEnumValues(enumObject) {
if (!isEnumObject(enumObject))
throw new Error("not a typescript enum object");
let values = [];
for (let [name, number] of Object.entries(enumObject))
if (typeof number == "number")
values.push({ name, number });
return values;
}
/**
* Lists the names of a Typescript enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
export function listEnumNames(enumObject) {
return listEnumValues(enumObject).map(val => val.name);
}
/**
* Lists the numbers of a Typescript enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
export function listEnumNumbers(enumObject) {
return listEnumValues(enumObject)
.map(val => val.number)
.filter((num, index, arr) => arr.indexOf(num) == index);
}

View file

@ -0,0 +1,265 @@
// Copyright 2008 Google Inc. 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 Google Inc. 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
// OWNER 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.
//
// Code generated by the Protocol Buffer compiler is owned by the owner
// of the input file used when generating it. This code is not
// standalone and requires a support library to be linked with it. This
// support library is itself covered by the above license.
/**
* Read a 64 bit varint as two JS numbers.
*
* Returns tuple:
* [0]: low bits
* [0]: high bits
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L175
*/
export function varint64read() {
let lowBits = 0;
let highBits = 0;
for (let shift = 0; shift < 28; shift += 7) {
let b = this.buf[this.pos++];
lowBits |= (b & 0x7F) << shift;
if ((b & 0x80) == 0) {
this.assertBounds();
return [lowBits, highBits];
}
}
let middleByte = this.buf[this.pos++];
// last four bits of the first 32 bit number
lowBits |= (middleByte & 0x0F) << 28;
// 3 upper bits are part of the next 32 bit number
highBits = (middleByte & 0x70) >> 4;
if ((middleByte & 0x80) == 0) {
this.assertBounds();
return [lowBits, highBits];
}
for (let shift = 3; shift <= 31; shift += 7) {
let b = this.buf[this.pos++];
highBits |= (b & 0x7F) << shift;
if ((b & 0x80) == 0) {
this.assertBounds();
return [lowBits, highBits];
}
}
throw new Error('invalid varint');
}
/**
* Write a 64 bit varint, given as two JS numbers, to the given bytes array.
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
*/
export function varint64write(lo, hi, bytes) {
for (let i = 0; i < 28; i = i + 7) {
const shift = lo >>> i;
const hasNext = !((shift >>> 7) == 0 && hi == 0);
const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
bytes.push(byte);
if (!hasNext) {
return;
}
}
const splitBits = ((lo >>> 28) & 0x0F) | ((hi & 0x07) << 4);
const hasMoreBits = !((hi >> 3) == 0);
bytes.push((hasMoreBits ? splitBits | 0x80 : splitBits) & 0xFF);
if (!hasMoreBits) {
return;
}
for (let i = 3; i < 31; i = i + 7) {
const shift = hi >>> i;
const hasNext = !((shift >>> 7) == 0);
const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
bytes.push(byte);
if (!hasNext) {
return;
}
}
bytes.push((hi >>> 31) & 0x01);
}
// constants for binary math
const TWO_PWR_32_DBL = (1 << 16) * (1 << 16);
/**
* Parse decimal string of 64 bit integer value as two JS numbers.
*
* Returns tuple:
* [0]: minus sign?
* [1]: low bits
* [2]: high bits
*
* Copyright 2008 Google Inc.
*/
export function int64fromString(dec) {
// Check for minus sign.
let minus = dec[0] == '-';
if (minus)
dec = dec.slice(1);
// Work 6 decimal digits at a time, acting like we're converting base 1e6
// digits to binary. This is safe to do with floating point math because
// Number.isSafeInteger(ALL_32_BITS * 1e6) == true.
const base = 1e6;
let lowBits = 0;
let highBits = 0;
function add1e6digit(begin, end) {
// Note: Number('') is 0.
const digit1e6 = Number(dec.slice(begin, end));
highBits *= base;
lowBits = lowBits * base + digit1e6;
// Carry bits from lowBits to highBits
if (lowBits >= TWO_PWR_32_DBL) {
highBits = highBits + ((lowBits / TWO_PWR_32_DBL) | 0);
lowBits = lowBits % TWO_PWR_32_DBL;
}
}
add1e6digit(-24, -18);
add1e6digit(-18, -12);
add1e6digit(-12, -6);
add1e6digit(-6);
return [minus, lowBits, highBits];
}
/**
* Format 64 bit integer value (as two JS numbers) to decimal string.
*
* Copyright 2008 Google Inc.
*/
export function int64toString(bitsLow, bitsHigh) {
// Skip the expensive conversion if the number is small enough to use the
// built-in conversions.
if ((bitsHigh >>> 0) <= 0x1FFFFF) {
return '' + (TWO_PWR_32_DBL * bitsHigh + (bitsLow >>> 0));
}
// What this code is doing is essentially converting the input number from
// base-2 to base-1e7, which allows us to represent the 64-bit range with
// only 3 (very large) digits. Those digits are then trivial to convert to
// a base-10 string.
// The magic numbers used here are -
// 2^24 = 16777216 = (1,6777216) in base-1e7.
// 2^48 = 281474976710656 = (2,8147497,6710656) in base-1e7.
// Split 32:32 representation into 16:24:24 representation so our
// intermediate digits don't overflow.
let low = bitsLow & 0xFFFFFF;
let mid = (((bitsLow >>> 24) | (bitsHigh << 8)) >>> 0) & 0xFFFFFF;
let high = (bitsHigh >> 16) & 0xFFFF;
// Assemble our three base-1e7 digits, ignoring carries. The maximum
// value in a digit at this step is representable as a 48-bit integer, which
// can be stored in a 64-bit floating point number.
let digitA = low + (mid * 6777216) + (high * 6710656);
let digitB = mid + (high * 8147497);
let digitC = (high * 2);
// Apply carries from A to B and from B to C.
let base = 10000000;
if (digitA >= base) {
digitB += Math.floor(digitA / base);
digitA %= base;
}
if (digitB >= base) {
digitC += Math.floor(digitB / base);
digitB %= base;
}
// Convert base-1e7 digits to base-10, with optional leading zeroes.
function decimalFrom1e7(digit1e7, needLeadingZeros) {
let partial = digit1e7 ? String(digit1e7) : '';
if (needLeadingZeros) {
return '0000000'.slice(partial.length) + partial;
}
return partial;
}
return decimalFrom1e7(digitC, /*needLeadingZeros=*/ 0) +
decimalFrom1e7(digitB, /*needLeadingZeros=*/ digitC) +
// If the final 1e7 digit didn't need leading zeros, we would have
// returned via the trivial code path at the top.
decimalFrom1e7(digitA, /*needLeadingZeros=*/ 1);
}
/**
* Write a 32 bit varint, signed or unsigned. Same as `varint64write(0, value, bytes)`
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/1b18833f4f2a2f681f4e4a25cdf3b0a43115ec26/js/binary/encoder.js#L144
*/
export function varint32write(value, bytes) {
if (value >= 0) {
// write value as varint 32
while (value > 0x7f) {
bytes.push((value & 0x7f) | 0x80);
value = value >>> 7;
}
bytes.push(value);
}
else {
for (let i = 0; i < 9; i++) {
bytes.push(value & 127 | 128);
value = value >> 7;
}
bytes.push(1);
}
}
/**
* Read an unsigned 32 bit varint.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220
*/
export function varint32read() {
let b = this.buf[this.pos++];
let result = b & 0x7F;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
b = this.buf[this.pos++];
result |= (b & 0x7F) << 7;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
b = this.buf[this.pos++];
result |= (b & 0x7F) << 14;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
b = this.buf[this.pos++];
result |= (b & 0x7F) << 21;
if ((b & 0x80) == 0) {
this.assertBounds();
return result;
}
// Extract only last 4 bits
b = this.buf[this.pos++];
result |= (b & 0x0F) << 28;
for (let readBytes = 5; ((b & 0x80) !== 0) && readBytes < 10; readBytes++)
b = this.buf[this.pos++];
if ((b & 0x80) != 0)
throw new Error('invalid varint');
this.assertBounds();
// Result can have 32 bits, convert it to unsigned
return result >>> 0;
}

View file

@ -0,0 +1,44 @@
// Public API of the protobuf-ts runtime.
// Note: we do not use `export * from ...` to help tree shakers,
// webpack verbose output hints that this should be useful
// Convenience JSON typings and corresponding type guards
export { typeofJsonValue, isJsonObject } from './json-typings';
// Base 64 encoding
export { base64decode, base64encode } from './base64';
// UTF8 encoding
export { utf8read } from './protobufjs-utf8';
// Binary format contracts, options for reading and writing, for example
export { WireType, mergeBinaryOptions, UnknownFieldHandler, } from './binary-format-contract';
// Standard IBinaryReader implementation
export { BinaryReader, binaryReadOptions } from './binary-reader';
// Standard IBinaryWriter implementation
export { BinaryWriter, binaryWriteOptions } from './binary-writer';
// Int64 and UInt64 implementations required for the binary format
export { PbLong, PbULong } from './pb-long';
// JSON format contracts, options for reading and writing, for example
export { jsonReadOptions, jsonWriteOptions, mergeJsonOptions } from './json-format-contract';
// Message type contract
export { MESSAGE_TYPE } from './message-type-contract';
// Message type implementation via reflection
export { MessageType } from './message-type';
// Reflection info, generated by the plugin, exposed to the user, used by reflection ops
export { ScalarType, LongType, RepeatType, normalizeFieldInfo, readFieldOptions, readFieldOption, readMessageOption } from './reflection-info';
// Message operations via reflection
export { ReflectionTypeCheck } from './reflection-type-check';
export { reflectionCreate } from './reflection-create';
export { reflectionScalarDefault } from './reflection-scalar-default';
export { reflectionMergePartial } from './reflection-merge-partial';
export { reflectionEquals } from './reflection-equals';
export { ReflectionBinaryReader } from './reflection-binary-reader';
export { ReflectionBinaryWriter } from './reflection-binary-writer';
export { ReflectionJsonReader } from './reflection-json-reader';
export { ReflectionJsonWriter } from './reflection-json-writer';
export { containsMessageType } from './reflection-contains-message-type';
// Oneof helpers
export { isOneofGroup, setOneofValue, getOneofValue, clearOneofValue, getSelectedOneofValue } from './oneof';
// Enum object type guard and reflection util, may be interesting to the user.
export { listEnumValues, listEnumNames, listEnumNumbers, isEnumObject } from './enum-object';
// lowerCamelCase() is exported for plugin, rpc-runtime and other rpc packages
export { lowerCamelCase } from './lower-camel-case';
// assertion functions are exported for plugin, may also be useful to user
export { assert, assertNever, assertInt32, assertUInt32, assertFloat32 } from './assert';

View file

@ -0,0 +1,29 @@
const defaultsWrite = {
emitDefaultValues: false,
enumAsInteger: false,
useProtoFieldName: false,
prettySpaces: 0,
}, defaultsRead = {
ignoreUnknownFields: false,
};
/**
* Make options for reading JSON data from partial options.
*/
export function jsonReadOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsRead), options) : defaultsRead;
}
/**
* Make options for writing JSON data from partial options.
*/
export function jsonWriteOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsWrite), options) : defaultsWrite;
}
/**
* Merges JSON write or read options. Later values override earlier values. Type registries are merged.
*/
export function mergeJsonOptions(a, b) {
var _a, _b;
let c = Object.assign(Object.assign({}, a), b);
c.typeRegistry = [...((_a = a === null || a === void 0 ? void 0 : a.typeRegistry) !== null && _a !== void 0 ? _a : []), ...((_b = b === null || b === void 0 ? void 0 : b.typeRegistry) !== null && _b !== void 0 ? _b : [])];
return c;
}

View file

@ -0,0 +1,20 @@
/**
* Get the type of a JSON value.
* Distinguishes between array, null and object.
*/
export function typeofJsonValue(value) {
let t = typeof value;
if (t == "object") {
if (Array.isArray(value))
return "array";
if (value === null)
return "null";
}
return t;
}
/**
* Is this a JSON object (instead of an array or null)?
*/
export function isJsonObject(value) {
return value !== null && typeof value == "object" && !Array.isArray(value);
}

View file

@ -0,0 +1,31 @@
/**
* Converts snake_case to lowerCamelCase.
*
* Should behave like protoc:
* https://github.com/protocolbuffers/protobuf/blob/e8ae137c96444ea313485ed1118c5e43b2099cf1/src/google/protobuf/compiler/java/java_helpers.cc#L118
*/
export function lowerCamelCase(snakeCase) {
let capNext = false;
const sb = [];
for (let i = 0; i < snakeCase.length; i++) {
let next = snakeCase.charAt(i);
if (next == '_') {
capNext = true;
}
else if (/\d/.test(next)) {
sb.push(next);
capNext = true;
}
else if (capNext) {
sb.push(next.toUpperCase());
capNext = false;
}
else if (i == 0) {
sb.push(next.toLowerCase());
}
else {
sb.push(next);
}
}
return sb.join('');
}

View file

@ -0,0 +1,7 @@
/**
* The symbol used as a key on message objects to store the message type.
*
* Note that this is an experimental feature - it is here to stay, but
* implementation details may change without notice.
*/
export const MESSAGE_TYPE = Symbol.for("protobuf-ts/message-type");

View file

@ -0,0 +1,171 @@
import { MESSAGE_TYPE } from "./message-type-contract";
import { normalizeFieldInfo } from "./reflection-info";
import { ReflectionTypeCheck } from "./reflection-type-check";
import { ReflectionJsonReader } from "./reflection-json-reader";
import { ReflectionJsonWriter } from "./reflection-json-writer";
import { ReflectionBinaryReader } from "./reflection-binary-reader";
import { ReflectionBinaryWriter } from "./reflection-binary-writer";
import { reflectionCreate } from "./reflection-create";
import { reflectionMergePartial } from "./reflection-merge-partial";
import { typeofJsonValue } from "./json-typings";
import { jsonReadOptions, jsonWriteOptions, } from "./json-format-contract";
import { reflectionEquals } from "./reflection-equals";
import { binaryWriteOptions } from "./binary-writer";
import { binaryReadOptions } from "./binary-reader";
const baseDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf({}));
/**
* This standard message type provides reflection-based
* operations to work with a message.
*/
export class MessageType {
constructor(name, fields, options) {
this.defaultCheckDepth = 16;
this.typeName = name;
this.fields = fields.map(normalizeFieldInfo);
this.options = options !== null && options !== void 0 ? options : {};
this.messagePrototype = Object.create(null, Object.assign(Object.assign({}, baseDescriptors), { [MESSAGE_TYPE]: { value: this } }));
this.refTypeCheck = new ReflectionTypeCheck(this);
this.refJsonReader = new ReflectionJsonReader(this);
this.refJsonWriter = new ReflectionJsonWriter(this);
this.refBinReader = new ReflectionBinaryReader(this);
this.refBinWriter = new ReflectionBinaryWriter(this);
}
create(value) {
let message = reflectionCreate(this);
if (value !== undefined) {
reflectionMergePartial(this, message, value);
}
return message;
}
/**
* Clone the message.
*
* Unknown fields are discarded.
*/
clone(message) {
let copy = this.create();
reflectionMergePartial(this, copy, message);
return copy;
}
/**
* Determines whether two message of the same type have the same field values.
* Checks for deep equality, traversing repeated fields, oneof groups, maps
* and messages recursively.
* Will also return true if both messages are `undefined`.
*/
equals(a, b) {
return reflectionEquals(this, a, b);
}
/**
* Is the given value assignable to our message type
* and contains no [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
is(arg, depth = this.defaultCheckDepth) {
return this.refTypeCheck.is(arg, depth, false);
}
/**
* Is the given value assignable to our message type,
* regardless of [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
isAssignable(arg, depth = this.defaultCheckDepth) {
return this.refTypeCheck.is(arg, depth, true);
}
/**
* Copy partial data into the target message.
*/
mergePartial(target, source) {
reflectionMergePartial(this, target, source);
}
/**
* Create a new message from binary format.
*/
fromBinary(data, options) {
let opt = binaryReadOptions(options);
return this.internalBinaryRead(opt.readerFactory(data), data.byteLength, opt);
}
/**
* Read a new message from a JSON value.
*/
fromJson(json, options) {
return this.internalJsonRead(json, jsonReadOptions(options));
}
/**
* Read a new message from a JSON string.
* This is equivalent to `T.fromJson(JSON.parse(json))`.
*/
fromJsonString(json, options) {
let value = JSON.parse(json);
return this.fromJson(value, options);
}
/**
* Write the message to canonical JSON value.
*/
toJson(message, options) {
return this.internalJsonWrite(message, jsonWriteOptions(options));
}
/**
* Convert the message to canonical JSON string.
* This is equivalent to `JSON.stringify(T.toJson(t))`
*/
toJsonString(message, options) {
var _a;
let value = this.toJson(message, options);
return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
}
/**
* Write the message to binary format.
*/
toBinary(message, options) {
let opt = binaryWriteOptions(options);
return this.internalBinaryWrite(message, opt.writerFactory(), opt).finish();
}
/**
* This is an internal method. If you just want to read a message from
* JSON, use `fromJson()` or `fromJsonString()`.
*
* Reads JSON value and merges the fields into the target
* according to protobuf rules. If the target is omitted,
* a new instance is created first.
*/
internalJsonRead(json, options, target) {
if (json !== null && typeof json == "object" && !Array.isArray(json)) {
let message = target !== null && target !== void 0 ? target : this.create();
this.refJsonReader.read(json, message, options);
return message;
}
throw new Error(`Unable to parse message ${this.typeName} from JSON ${typeofJsonValue(json)}.`);
}
/**
* This is an internal method. If you just want to write a message
* to JSON, use `toJson()` or `toJsonString().
*
* Writes JSON value and returns it.
*/
internalJsonWrite(message, options) {
return this.refJsonWriter.write(message, options);
}
/**
* This is an internal method. If you just want to write a message
* in binary format, use `toBinary()`.
*
* Serializes the message in binary format and appends it to the given
* writer. Returns passed writer.
*/
internalBinaryWrite(message, writer, options) {
this.refBinWriter.write(message, writer, options);
return writer;
}
/**
* This is an internal method. If you just want to read a message from
* binary data, use `fromBinary()`.
*
* Reads data from binary format and merges the fields into
* the target according to protobuf rules. If the target is
* omitted, a new instance is created first.
*/
internalBinaryRead(reader, length, options, target) {
let message = target !== null && target !== void 0 ? target : this.create();
this.refBinReader.read(reader, message, options, length);
return message;
}
}

View file

@ -0,0 +1,105 @@
/**
* Is the given value a valid oneof group?
*
* We represent protobuf `oneof` as algebraic data types (ADT) in generated
* code. But when working with messages of unknown type, the ADT does not
* help us.
*
* This type guard checks if the given object adheres to the ADT rules, which
* are as follows:
*
* 1) Must be an object.
*
* 2) Must have a "oneofKind" discriminator property.
*
* 3) If "oneofKind" is `undefined`, no member field is selected. The object
* must not have any other properties.
*
* 4) If "oneofKind" is a `string`, the member field with this name is
* selected.
*
* 5) If a member field is selected, the object must have a second property
* with this name. The property must not be `undefined`.
*
* 6) No extra properties are allowed. The object has either one property
* (no selection) or two properties (selection).
*
*/
export function isOneofGroup(any) {
if (typeof any != 'object' || any === null || !any.hasOwnProperty('oneofKind')) {
return false;
}
switch (typeof any.oneofKind) {
case "string":
if (any[any.oneofKind] === undefined)
return false;
return Object.keys(any).length == 2;
case "undefined":
return Object.keys(any).length == 1;
default:
return false;
}
}
/**
* Returns the value of the given field in a oneof group.
*/
export function getOneofValue(oneof, kind) {
return oneof[kind];
}
export function setOneofValue(oneof, kind, value) {
if (oneof.oneofKind !== undefined) {
delete oneof[oneof.oneofKind];
}
oneof.oneofKind = kind;
if (value !== undefined) {
oneof[kind] = value;
}
}
export function setUnknownOneofValue(oneof, kind, value) {
if (oneof.oneofKind !== undefined) {
delete oneof[oneof.oneofKind];
}
oneof.oneofKind = kind;
if (value !== undefined && kind !== undefined) {
oneof[kind] = value;
}
}
/**
* Removes the selected field in a oneof group.
*
* Note that the recommended way to modify a oneof group is to set
* a new object:
*
* ```ts
* message.result = { oneofKind: undefined };
* ```
*/
export function clearOneofValue(oneof) {
if (oneof.oneofKind !== undefined) {
delete oneof[oneof.oneofKind];
}
oneof.oneofKind = undefined;
}
/**
* Returns the selected value of the given oneof group.
*
* Not that the recommended way to access a oneof group is to check
* the "oneofKind" property and let TypeScript narrow down the union
* type for you:
*
* ```ts
* if (message.result.oneofKind === "error") {
* message.result.error; // string
* }
* ```
*
* In the rare case you just need the value, and do not care about
* which protobuf field is selected, you can use this function
* for convenience.
*/
export function getSelectedOneofValue(oneof) {
if (oneof.oneofKind === undefined) {
return undefined;
}
return oneof[oneof.oneofKind];
}

View file

@ -0,0 +1,232 @@
import { int64fromString, int64toString } from "./goog-varint";
let BI;
export function detectBi() {
const dv = new DataView(new ArrayBuffer(8));
const ok = globalThis.BigInt !== undefined
&& typeof dv.getBigInt64 === "function"
&& typeof dv.getBigUint64 === "function"
&& typeof dv.setBigInt64 === "function"
&& typeof dv.setBigUint64 === "function";
BI = ok ? {
MIN: BigInt("-9223372036854775808"),
MAX: BigInt("9223372036854775807"),
UMIN: BigInt("0"),
UMAX: BigInt("18446744073709551615"),
C: BigInt,
V: dv,
} : undefined;
}
detectBi();
function assertBi(bi) {
if (!bi)
throw new Error("BigInt unavailable, see https://github.com/timostamm/protobuf-ts/blob/v1.0.8/MANUAL.md#bigint-support");
}
// used to validate from(string) input (when bigint is unavailable)
const RE_DECIMAL_STR = /^-?[0-9]+$/;
// constants for binary math
const TWO_PWR_32_DBL = 0x100000000;
const HALF_2_PWR_32 = 0x080000000;
// base class for PbLong and PbULong provides shared code
class SharedPbLong {
/**
* Create a new instance with the given bits.
*/
constructor(lo, hi) {
this.lo = lo | 0;
this.hi = hi | 0;
}
/**
* Is this instance equal to 0?
*/
isZero() {
return this.lo == 0 && this.hi == 0;
}
/**
* Convert to a native number.
*/
toNumber() {
let result = this.hi * TWO_PWR_32_DBL + (this.lo >>> 0);
if (!Number.isSafeInteger(result))
throw new Error("cannot convert to safe number");
return result;
}
}
/**
* 64-bit unsigned integer as two 32-bit values.
* Converts between `string`, `number` and `bigint` representations.
*/
export class PbULong extends SharedPbLong {
/**
* Create instance from a `string`, `number` or `bigint`.
*/
static from(value) {
if (BI)
// noinspection FallThroughInSwitchStatementJS
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
if (value == "")
throw new Error('string is no integer');
value = BI.C(value);
case "number":
if (value === 0)
return this.ZERO;
value = BI.C(value);
case "bigint":
if (!value)
return this.ZERO;
if (value < BI.UMIN)
throw new Error('signed value for ulong');
if (value > BI.UMAX)
throw new Error('ulong too large');
BI.V.setBigUint64(0, value, true);
return new PbULong(BI.V.getInt32(0, true), BI.V.getInt32(4, true));
}
else
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
value = value.trim();
if (!RE_DECIMAL_STR.test(value))
throw new Error('string is no integer');
let [minus, lo, hi] = int64fromString(value);
if (minus)
throw new Error('signed value for ulong');
return new PbULong(lo, hi);
case "number":
if (value == 0)
return this.ZERO;
if (!Number.isSafeInteger(value))
throw new Error('number is no integer');
if (value < 0)
throw new Error('signed value for ulong');
return new PbULong(value, value / TWO_PWR_32_DBL);
}
throw new Error('unknown value ' + typeof value);
}
/**
* Convert to decimal string.
*/
toString() {
return BI ? this.toBigInt().toString() : int64toString(this.lo, this.hi);
}
/**
* Convert to native bigint.
*/
toBigInt() {
assertBi(BI);
BI.V.setInt32(0, this.lo, true);
BI.V.setInt32(4, this.hi, true);
return BI.V.getBigUint64(0, true);
}
}
/**
* ulong 0 singleton.
*/
PbULong.ZERO = new PbULong(0, 0);
/**
* 64-bit signed integer as two 32-bit values.
* Converts between `string`, `number` and `bigint` representations.
*/
export class PbLong extends SharedPbLong {
/**
* Create instance from a `string`, `number` or `bigint`.
*/
static from(value) {
if (BI)
// noinspection FallThroughInSwitchStatementJS
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
if (value == "")
throw new Error('string is no integer');
value = BI.C(value);
case "number":
if (value === 0)
return this.ZERO;
value = BI.C(value);
case "bigint":
if (!value)
return this.ZERO;
if (value < BI.MIN)
throw new Error('signed long too small');
if (value > BI.MAX)
throw new Error('signed long too large');
BI.V.setBigInt64(0, value, true);
return new PbLong(BI.V.getInt32(0, true), BI.V.getInt32(4, true));
}
else
switch (typeof value) {
case "string":
if (value == "0")
return this.ZERO;
value = value.trim();
if (!RE_DECIMAL_STR.test(value))
throw new Error('string is no integer');
let [minus, lo, hi] = int64fromString(value);
if (minus) {
if (hi > HALF_2_PWR_32 || (hi == HALF_2_PWR_32 && lo != 0))
throw new Error('signed long too small');
}
else if (hi >= HALF_2_PWR_32)
throw new Error('signed long too large');
let pbl = new PbLong(lo, hi);
return minus ? pbl.negate() : pbl;
case "number":
if (value == 0)
return this.ZERO;
if (!Number.isSafeInteger(value))
throw new Error('number is no integer');
return value > 0
? new PbLong(value, value / TWO_PWR_32_DBL)
: new PbLong(-value, -value / TWO_PWR_32_DBL).negate();
}
throw new Error('unknown value ' + typeof value);
}
/**
* Do we have a minus sign?
*/
isNegative() {
return (this.hi & HALF_2_PWR_32) !== 0;
}
/**
* Negate two's complement.
* Invert all the bits and add one to the result.
*/
negate() {
let hi = ~this.hi, lo = this.lo;
if (lo)
lo = ~lo + 1;
else
hi += 1;
return new PbLong(lo, hi);
}
/**
* Convert to decimal string.
*/
toString() {
if (BI)
return this.toBigInt().toString();
if (this.isNegative()) {
let n = this.negate();
return '-' + int64toString(n.lo, n.hi);
}
return int64toString(this.lo, this.hi);
}
/**
* Convert to native bigint.
*/
toBigInt() {
assertBi(BI);
BI.V.setInt32(0, this.lo, true);
BI.V.setInt32(4, this.hi, true);
return BI.V.getBigInt64(0, true);
}
}
/**
* long 0 singleton.
*/
PbLong.ZERO = new PbLong(0, 0);

View file

@ -0,0 +1,71 @@
// Copyright (c) 2016, Daniel Wirtz 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 its author, 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
// OWNER 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.
const fromCharCodes = (chunk) => String.fromCharCode.apply(String, chunk);
/**
* @deprecated This function will no longer be exported with the next major
* release, since protobuf-ts has switch to TextDecoder API. If you need this
* function, please migrate to @protobufjs/utf8. For context, see
* https://github.com/timostamm/protobuf-ts/issues/184
*
* Reads UTF8 bytes as a string.
*
* See [protobufjs / utf8](https://github.com/protobufjs/protobuf.js/blob/9893e35b854621cce64af4bf6be2cff4fb892796/lib/utf8/index.js#L40)
*
* Copyright (c) 2016, Daniel Wirtz
*/
export function utf8read(bytes) {
if (bytes.length < 1)
return "";
let pos = 0, // position in bytes
parts = [], chunk = [], i = 0, // char offset
t; // temporary
let len = bytes.length;
while (pos < len) {
t = bytes[pos++];
if (t < 128)
chunk[i++] = t;
else if (t > 191 && t < 224)
chunk[i++] = (t & 31) << 6 | bytes[pos++] & 63;
else if (t > 239 && t < 365) {
t = ((t & 7) << 18 | (bytes[pos++] & 63) << 12 | (bytes[pos++] & 63) << 6 | bytes[pos++] & 63) - 0x10000;
chunk[i++] = 0xD800 + (t >> 10);
chunk[i++] = 0xDC00 + (t & 1023);
}
else
chunk[i++] = (t & 15) << 12 | (bytes[pos++] & 63) << 6 | bytes[pos++] & 63;
if (i > 8191) {
parts.push(fromCharCodes(chunk));
i = 0;
}
}
if (parts.length) {
if (i)
parts.push(fromCharCodes(chunk.slice(0, i)));
return parts.join("");
}
return fromCharCodes(chunk.slice(0, i));
}

View file

@ -0,0 +1,179 @@
import { UnknownFieldHandler, WireType } from "./binary-format-contract";
import { LongType, ScalarType } from "./reflection-info";
import { reflectionLongConvert } from "./reflection-long-convert";
import { reflectionScalarDefault } from "./reflection-scalar-default";
/**
* Reads proto3 messages in binary format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/encoding
*/
export class ReflectionBinaryReader {
constructor(info) {
this.info = info;
}
prepare() {
var _a;
if (!this.fieldNoToField) {
const fieldsInput = (_a = this.info.fields) !== null && _a !== void 0 ? _a : [];
this.fieldNoToField = new Map(fieldsInput.map(field => [field.no, field]));
}
}
/**
* Reads a message from binary format into the target message.
*
* Repeated fields are appended. Map entries are added, overwriting
* existing keys.
*
* If a message field is already present, it will be merged with the
* new data.
*/
read(reader, message, options, length) {
this.prepare();
const end = length === undefined ? reader.len : reader.pos + length;
while (reader.pos < end) {
// read the tag and find the field
const [fieldNo, wireType] = reader.tag(), field = this.fieldNoToField.get(fieldNo);
if (!field) {
let u = options.readUnknownField;
if (u == "throw")
throw new Error(`Unknown field ${fieldNo} (wire type ${wireType}) for ${this.info.typeName}`);
let d = reader.skip(wireType);
if (u !== false)
(u === true ? UnknownFieldHandler.onRead : u)(this.info.typeName, message, fieldNo, wireType, d);
continue;
}
// target object for the field we are reading
let target = message, repeated = field.repeat, localName = field.localName;
// if field is member of oneof ADT, use ADT as target
if (field.oneof) {
target = target[field.oneof];
// if other oneof member selected, set new ADT
if (target.oneofKind !== localName)
target = message[field.oneof] = {
oneofKind: localName
};
}
// we have handled oneof above, we just have read the value into `target[localName]`
switch (field.kind) {
case "scalar":
case "enum":
let T = field.kind == "enum" ? ScalarType.INT32 : field.T;
let L = field.kind == "scalar" ? field.L : undefined;
if (repeated) {
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
if (wireType == WireType.LengthDelimited && T != ScalarType.STRING && T != ScalarType.BYTES) {
let e = reader.uint32() + reader.pos;
while (reader.pos < e)
arr.push(this.scalar(reader, T, L));
}
else
arr.push(this.scalar(reader, T, L));
}
else
target[localName] = this.scalar(reader, T, L);
break;
case "message":
if (repeated) {
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
let msg = field.T().internalBinaryRead(reader, reader.uint32(), options);
arr.push(msg);
}
else
target[localName] = field.T().internalBinaryRead(reader, reader.uint32(), options, target[localName]);
break;
case "map":
let [mapKey, mapVal] = this.mapEntry(field, reader, options);
// safe to assume presence of map object, oneof cannot contain repeated values
target[localName][mapKey] = mapVal;
break;
}
}
}
/**
* Read a map field, expecting key field = 1, value field = 2
*/
mapEntry(field, reader, options) {
let length = reader.uint32();
let end = reader.pos + length;
let key = undefined; // javascript only allows number or string for object properties
let val = undefined;
while (reader.pos < end) {
let [fieldNo, wireType] = reader.tag();
switch (fieldNo) {
case 1:
if (field.K == ScalarType.BOOL)
key = reader.bool().toString();
else
// long types are read as string, number types are okay as number
key = this.scalar(reader, field.K, LongType.STRING);
break;
case 2:
switch (field.V.kind) {
case "scalar":
val = this.scalar(reader, field.V.T, field.V.L);
break;
case "enum":
val = reader.int32();
break;
case "message":
val = field.V.T().internalBinaryRead(reader, reader.uint32(), options);
break;
}
break;
default:
throw new Error(`Unknown field ${fieldNo} (wire type ${wireType}) in map entry for ${this.info.typeName}#${field.name}`);
}
}
if (key === undefined) {
let keyRaw = reflectionScalarDefault(field.K);
key = field.K == ScalarType.BOOL ? keyRaw.toString() : keyRaw;
}
if (val === undefined)
switch (field.V.kind) {
case "scalar":
val = reflectionScalarDefault(field.V.T, field.V.L);
break;
case "enum":
val = 0;
break;
case "message":
val = field.V.T().create();
break;
}
return [key, val];
}
scalar(reader, type, longType) {
switch (type) {
case ScalarType.INT32:
return reader.int32();
case ScalarType.STRING:
return reader.string();
case ScalarType.BOOL:
return reader.bool();
case ScalarType.DOUBLE:
return reader.double();
case ScalarType.FLOAT:
return reader.float();
case ScalarType.INT64:
return reflectionLongConvert(reader.int64(), longType);
case ScalarType.UINT64:
return reflectionLongConvert(reader.uint64(), longType);
case ScalarType.FIXED64:
return reflectionLongConvert(reader.fixed64(), longType);
case ScalarType.FIXED32:
return reader.fixed32();
case ScalarType.BYTES:
return reader.bytes();
case ScalarType.UINT32:
return reader.uint32();
case ScalarType.SFIXED32:
return reader.sfixed32();
case ScalarType.SFIXED64:
return reflectionLongConvert(reader.sfixed64(), longType);
case ScalarType.SINT32:
return reader.sint32();
case ScalarType.SINT64:
return reflectionLongConvert(reader.sint64(), longType);
}
}
}

View file

@ -0,0 +1,229 @@
import { UnknownFieldHandler, WireType } from "./binary-format-contract";
import { RepeatType, ScalarType } from "./reflection-info";
import { assert } from "./assert";
import { PbLong, PbULong } from "./pb-long";
/**
* Writes proto3 messages in binary format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/encoding
*/
export class ReflectionBinaryWriter {
constructor(info) {
this.info = info;
}
prepare() {
if (!this.fields) {
const fieldsInput = this.info.fields ? this.info.fields.concat() : [];
this.fields = fieldsInput.sort((a, b) => a.no - b.no);
}
}
/**
* Writes the message to binary format.
*/
write(message, writer, options) {
this.prepare();
for (const field of this.fields) {
let value, // this will be our field value, whether it is member of a oneof or not
emitDefault, // whether we emit the default value (only true for oneof members)
repeated = field.repeat, localName = field.localName;
// handle oneof ADT
if (field.oneof) {
const group = message[field.oneof];
if (group.oneofKind !== localName)
continue; // if field is not selected, skip
value = group[localName];
emitDefault = true;
}
else {
value = message[localName];
emitDefault = false;
}
// we have handled oneof above. we just have to honor `emitDefault`.
switch (field.kind) {
case "scalar":
case "enum":
let T = field.kind == "enum" ? ScalarType.INT32 : field.T;
if (repeated) {
assert(Array.isArray(value));
if (repeated == RepeatType.PACKED)
this.packed(writer, T, field.no, value);
else
for (const item of value)
this.scalar(writer, T, field.no, item, true);
}
else if (value === undefined)
assert(field.opt);
else
this.scalar(writer, T, field.no, value, emitDefault || field.opt);
break;
case "message":
if (repeated) {
assert(Array.isArray(value));
for (const item of value)
this.message(writer, options, field.T(), field.no, item);
}
else {
this.message(writer, options, field.T(), field.no, value);
}
break;
case "map":
assert(typeof value == 'object' && value !== null);
for (const [key, val] of Object.entries(value))
this.mapEntry(writer, options, field, key, val);
break;
}
}
let u = options.writeUnknownFields;
if (u !== false)
(u === true ? UnknownFieldHandler.onWrite : u)(this.info.typeName, message, writer);
}
mapEntry(writer, options, field, key, value) {
writer.tag(field.no, WireType.LengthDelimited);
writer.fork();
// javascript only allows number or string for object properties
// we convert from our representation to the protobuf type
let keyValue = key;
switch (field.K) {
case ScalarType.INT32:
case ScalarType.FIXED32:
case ScalarType.UINT32:
case ScalarType.SFIXED32:
case ScalarType.SINT32:
keyValue = Number.parseInt(key);
break;
case ScalarType.BOOL:
assert(key == 'true' || key == 'false');
keyValue = key == 'true';
break;
}
// write key, expecting key field number = 1
this.scalar(writer, field.K, 1, keyValue, true);
// write value, expecting value field number = 2
switch (field.V.kind) {
case 'scalar':
this.scalar(writer, field.V.T, 2, value, true);
break;
case 'enum':
this.scalar(writer, ScalarType.INT32, 2, value, true);
break;
case 'message':
this.message(writer, options, field.V.T(), 2, value);
break;
}
writer.join();
}
message(writer, options, handler, fieldNo, value) {
if (value === undefined)
return;
handler.internalBinaryWrite(value, writer.tag(fieldNo, WireType.LengthDelimited).fork(), options);
writer.join();
}
/**
* Write a single scalar value.
*/
scalar(writer, type, fieldNo, value, emitDefault) {
let [wireType, method, isDefault] = this.scalarInfo(type, value);
if (!isDefault || emitDefault) {
writer.tag(fieldNo, wireType);
writer[method](value);
}
}
/**
* Write an array of scalar values in packed format.
*/
packed(writer, type, fieldNo, value) {
if (!value.length)
return;
assert(type !== ScalarType.BYTES && type !== ScalarType.STRING);
// write tag
writer.tag(fieldNo, WireType.LengthDelimited);
// begin length-delimited
writer.fork();
// write values without tags
let [, method,] = this.scalarInfo(type);
for (let i = 0; i < value.length; i++)
writer[method](value[i]);
// end length delimited
writer.join();
}
/**
* Get information for writing a scalar value.
*
* Returns tuple:
* [0]: appropriate WireType
* [1]: name of the appropriate method of IBinaryWriter
* [2]: whether the given value is a default value
*
* If argument `value` is omitted, [2] is always false.
*/
scalarInfo(type, value) {
let t = WireType.Varint;
let m;
let i = value === undefined;
let d = value === 0;
switch (type) {
case ScalarType.INT32:
m = "int32";
break;
case ScalarType.STRING:
d = i || !value.length;
t = WireType.LengthDelimited;
m = "string";
break;
case ScalarType.BOOL:
d = value === false;
m = "bool";
break;
case ScalarType.UINT32:
m = "uint32";
break;
case ScalarType.DOUBLE:
t = WireType.Bit64;
m = "double";
break;
case ScalarType.FLOAT:
t = WireType.Bit32;
m = "float";
break;
case ScalarType.INT64:
d = i || PbLong.from(value).isZero();
m = "int64";
break;
case ScalarType.UINT64:
d = i || PbULong.from(value).isZero();
m = "uint64";
break;
case ScalarType.FIXED64:
d = i || PbULong.from(value).isZero();
t = WireType.Bit64;
m = "fixed64";
break;
case ScalarType.BYTES:
d = i || !value.byteLength;
t = WireType.LengthDelimited;
m = "bytes";
break;
case ScalarType.FIXED32:
t = WireType.Bit32;
m = "fixed32";
break;
case ScalarType.SFIXED32:
t = WireType.Bit32;
m = "sfixed32";
break;
case ScalarType.SFIXED64:
d = i || PbLong.from(value).isZero();
t = WireType.Bit64;
m = "sfixed64";
break;
case ScalarType.SINT32:
m = "sint32";
break;
case ScalarType.SINT64:
d = i || PbLong.from(value).isZero();
m = "sint64";
break;
}
return [t, m, i || d];
}
}

View file

@ -0,0 +1,10 @@
import { MESSAGE_TYPE } from './message-type-contract';
/**
* Check if the provided object is a proto message.
*
* Note that this is an experimental feature - it is here to stay, but
* implementation details may change without notice.
*/
export function containsMessageType(msg) {
return msg[MESSAGE_TYPE] != null;
}

View file

@ -0,0 +1,44 @@
import { reflectionScalarDefault } from "./reflection-scalar-default";
import { MESSAGE_TYPE } from './message-type-contract';
/**
* Creates an instance of the generic message, using the field
* information.
*/
export function reflectionCreate(type) {
/**
* This ternary can be removed in the next major version.
* The `Object.create()` code path utilizes a new `messagePrototype`
* property on the `IMessageType` which has this same `MESSAGE_TYPE`
* non-enumerable property on it. Doing it this way means that we only
* pay the cost of `Object.defineProperty()` once per `IMessageType`
* class of once per "instance". The falsy code path is only provided
* for backwards compatibility in cases where the runtime library is
* updated without also updating the generated code.
*/
const msg = type.messagePrototype
? Object.create(type.messagePrototype)
: Object.defineProperty({}, MESSAGE_TYPE, { value: type });
for (let field of type.fields) {
let name = field.localName;
if (field.opt)
continue;
if (field.oneof)
msg[field.oneof] = { oneofKind: undefined };
else if (field.repeat)
msg[name] = [];
else
switch (field.kind) {
case "scalar":
msg[name] = reflectionScalarDefault(field.T, field.L);
break;
case "enum":
// we require 0 to be default value for all enums
msg[name] = 0;
break;
case "map":
msg[name] = {};
break;
}
}
return msg;
}

View file

@ -0,0 +1,73 @@
import { ScalarType } from "./reflection-info";
/**
* Determines whether two message of the same type have the same field values.
* Checks for deep equality, traversing repeated fields, oneof groups, maps
* and messages recursively.
* Will also return true if both messages are `undefined`.
*/
export function reflectionEquals(info, a, b) {
if (a === b)
return true;
if (!a || !b)
return false;
for (let field of info.fields) {
let localName = field.localName;
let val_a = field.oneof ? a[field.oneof][localName] : a[localName];
let val_b = field.oneof ? b[field.oneof][localName] : b[localName];
switch (field.kind) {
case "enum":
case "scalar":
let t = field.kind == "enum" ? ScalarType.INT32 : field.T;
if (!(field.repeat
? repeatedPrimitiveEq(t, val_a, val_b)
: primitiveEq(t, val_a, val_b)))
return false;
break;
case "map":
if (!(field.V.kind == "message"
? repeatedMsgEq(field.V.T(), objectValues(val_a), objectValues(val_b))
: repeatedPrimitiveEq(field.V.kind == "enum" ? ScalarType.INT32 : field.V.T, objectValues(val_a), objectValues(val_b))))
return false;
break;
case "message":
let T = field.T();
if (!(field.repeat
? repeatedMsgEq(T, val_a, val_b)
: T.equals(val_a, val_b)))
return false;
break;
}
}
return true;
}
const objectValues = Object.values;
function primitiveEq(type, a, b) {
if (a === b)
return true;
if (type !== ScalarType.BYTES)
return false;
let ba = a;
let bb = b;
if (ba.length !== bb.length)
return false;
for (let i = 0; i < ba.length; i++)
if (ba[i] != bb[i])
return false;
return true;
}
function repeatedPrimitiveEq(type, a, b) {
if (a.length !== b.length)
return false;
for (let i = 0; i < a.length; i++)
if (!primitiveEq(type, a[i], b[i]))
return false;
return true;
}
function repeatedMsgEq(type, a, b) {
if (a.length !== b.length)
return false;
for (let i = 0; i < a.length; i++)
if (!type.equals(a[i], b[i]))
return false;
return true;
}

View file

@ -0,0 +1,151 @@
import { lowerCamelCase } from "./lower-camel-case";
/**
* Scalar value types. This is a subset of field types declared by protobuf
* enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
* are omitted, but the numerical values are identical.
*/
export var ScalarType;
(function (ScalarType) {
// 0 is reserved for errors.
// Order is weird for historical reasons.
ScalarType[ScalarType["DOUBLE"] = 1] = "DOUBLE";
ScalarType[ScalarType["FLOAT"] = 2] = "FLOAT";
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
// negative values are likely.
ScalarType[ScalarType["INT64"] = 3] = "INT64";
ScalarType[ScalarType["UINT64"] = 4] = "UINT64";
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
// negative values are likely.
ScalarType[ScalarType["INT32"] = 5] = "INT32";
ScalarType[ScalarType["FIXED64"] = 6] = "FIXED64";
ScalarType[ScalarType["FIXED32"] = 7] = "FIXED32";
ScalarType[ScalarType["BOOL"] = 8] = "BOOL";
ScalarType[ScalarType["STRING"] = 9] = "STRING";
// Tag-delimited aggregate.
// Group type is deprecated and not supported in proto3. However, Proto3
// implementations should still be able to parse the group wire format and
// treat group fields as unknown fields.
// TYPE_GROUP = 10,
// TYPE_MESSAGE = 11, // Length-delimited aggregate.
// New in version 2.
ScalarType[ScalarType["BYTES"] = 12] = "BYTES";
ScalarType[ScalarType["UINT32"] = 13] = "UINT32";
// TYPE_ENUM = 14,
ScalarType[ScalarType["SFIXED32"] = 15] = "SFIXED32";
ScalarType[ScalarType["SFIXED64"] = 16] = "SFIXED64";
ScalarType[ScalarType["SINT32"] = 17] = "SINT32";
ScalarType[ScalarType["SINT64"] = 18] = "SINT64";
})(ScalarType || (ScalarType = {}));
/**
* JavaScript representation of 64 bit integral types. Equivalent to the
* field option "jstype".
*
* By default, protobuf-ts represents 64 bit types as `bigint`.
*
* You can change the default behaviour by enabling the plugin parameter
* `long_type_string`, which will represent 64 bit types as `string`.
*
* Alternatively, you can change the behaviour for individual fields
* with the field option "jstype":
*
* ```protobuf
* uint64 my_field = 1 [jstype = JS_STRING];
* uint64 other_field = 2 [jstype = JS_NUMBER];
* ```
*/
export var LongType;
(function (LongType) {
/**
* Use JavaScript `bigint`.
*
* Field option `[jstype = JS_NORMAL]`.
*/
LongType[LongType["BIGINT"] = 0] = "BIGINT";
/**
* Use JavaScript `string`.
*
* Field option `[jstype = JS_STRING]`.
*/
LongType[LongType["STRING"] = 1] = "STRING";
/**
* Use JavaScript `number`.
*
* Large values will loose precision.
*
* Field option `[jstype = JS_NUMBER]`.
*/
LongType[LongType["NUMBER"] = 2] = "NUMBER";
})(LongType || (LongType = {}));
/**
* Protobuf 2.1.0 introduced packed repeated fields.
* Setting the field option `[packed = true]` enables packing.
*
* In proto3, all repeated fields are packed by default.
* Setting the field option `[packed = false]` disables packing.
*
* Packed repeated fields are encoded with a single tag,
* then a length-delimiter, then the element values.
*
* Unpacked repeated fields are encoded with a tag and
* value for each element.
*
* `bytes` and `string` cannot be packed.
*/
export var RepeatType;
(function (RepeatType) {
/**
* The field is not repeated.
*/
RepeatType[RepeatType["NO"] = 0] = "NO";
/**
* The field is repeated and should be packed.
* Invalid for `bytes` and `string`, they cannot be packed.
*/
RepeatType[RepeatType["PACKED"] = 1] = "PACKED";
/**
* The field is repeated but should not be packed.
* The only valid repeat type for repeated `bytes` and `string`.
*/
RepeatType[RepeatType["UNPACKED"] = 2] = "UNPACKED";
})(RepeatType || (RepeatType = {}));
/**
* Turns PartialFieldInfo into FieldInfo.
*/
export function normalizeFieldInfo(field) {
var _a, _b, _c, _d;
field.localName = (_a = field.localName) !== null && _a !== void 0 ? _a : lowerCamelCase(field.name);
field.jsonName = (_b = field.jsonName) !== null && _b !== void 0 ? _b : lowerCamelCase(field.name);
field.repeat = (_c = field.repeat) !== null && _c !== void 0 ? _c : RepeatType.NO;
field.opt = (_d = field.opt) !== null && _d !== void 0 ? _d : (field.repeat ? false : field.oneof ? false : field.kind == "message");
return field;
}
/**
* Read custom field options from a generated message type.
*
* @deprecated use readFieldOption()
*/
export function readFieldOptions(messageType, fieldName, extensionName, extensionType) {
var _a;
const options = (_a = messageType.fields.find((m, i) => m.localName == fieldName || i == fieldName)) === null || _a === void 0 ? void 0 : _a.options;
return options && options[extensionName] ? extensionType.fromJson(options[extensionName]) : undefined;
}
export function readFieldOption(messageType, fieldName, extensionName, extensionType) {
var _a;
const options = (_a = messageType.fields.find((m, i) => m.localName == fieldName || i == fieldName)) === null || _a === void 0 ? void 0 : _a.options;
if (!options) {
return undefined;
}
const optionVal = options[extensionName];
if (optionVal === undefined) {
return optionVal;
}
return extensionType ? extensionType.fromJson(optionVal) : optionVal;
}
export function readMessageOption(messageType, extensionName, extensionType) {
const options = messageType.options;
const optionVal = options[extensionName];
if (optionVal === undefined) {
return optionVal;
}
return extensionType ? extensionType.fromJson(optionVal) : optionVal;
}

View file

@ -0,0 +1,309 @@
import { isJsonObject, typeofJsonValue } from "./json-typings";
import { base64decode } from "./base64";
import { LongType, ScalarType } from "./reflection-info";
import { PbLong, PbULong } from "./pb-long";
import { assert, assertFloat32, assertInt32, assertUInt32 } from "./assert";
import { reflectionLongConvert } from "./reflection-long-convert";
/**
* Reads proto3 messages in canonical JSON format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/proto3#json
*/
export class ReflectionJsonReader {
constructor(info) {
this.info = info;
}
prepare() {
var _a;
if (this.fMap === undefined) {
this.fMap = {};
const fieldsInput = (_a = this.info.fields) !== null && _a !== void 0 ? _a : [];
for (const field of fieldsInput) {
this.fMap[field.name] = field;
this.fMap[field.jsonName] = field;
this.fMap[field.localName] = field;
}
}
}
// Cannot parse JSON <type of jsonValue> for <type name>#<fieldName>.
assert(condition, fieldName, jsonValue) {
if (!condition) {
let what = typeofJsonValue(jsonValue);
if (what == "number" || what == "boolean")
what = jsonValue.toString();
throw new Error(`Cannot parse JSON ${what} for ${this.info.typeName}#${fieldName}`);
}
}
/**
* Reads a message from canonical JSON format into the target message.
*
* Repeated fields are appended. Map entries are added, overwriting
* existing keys.
*
* If a message field is already present, it will be merged with the
* new data.
*/
read(input, message, options) {
this.prepare();
const oneofsHandled = [];
for (const [jsonKey, jsonValue] of Object.entries(input)) {
const field = this.fMap[jsonKey];
if (!field) {
if (!options.ignoreUnknownFields)
throw new Error(`Found unknown field while reading ${this.info.typeName} from JSON format. JSON key: ${jsonKey}`);
continue;
}
const localName = field.localName;
// handle oneof ADT
let target; // this will be the target for the field value, whether it is member of a oneof or not
if (field.oneof) {
if (jsonValue === null && (field.kind !== 'enum' || field.T()[0] !== 'google.protobuf.NullValue')) {
continue;
}
// since json objects are unordered by specification, it is not possible to take the last of multiple oneofs
if (oneofsHandled.includes(field.oneof))
throw new Error(`Multiple members of the oneof group "${field.oneof}" of ${this.info.typeName} are present in JSON.`);
oneofsHandled.push(field.oneof);
target = message[field.oneof] = {
oneofKind: localName
};
}
else {
target = message;
}
// we have handled oneof above. we just have read the value into `target`.
if (field.kind == 'map') {
if (jsonValue === null) {
continue;
}
// check input
this.assert(isJsonObject(jsonValue), field.name, jsonValue);
// our target to put map entries into
const fieldObj = target[localName];
// read entries
for (const [jsonObjKey, jsonObjValue] of Object.entries(jsonValue)) {
this.assert(jsonObjValue !== null, field.name + " map value", null);
// read value
let val;
switch (field.V.kind) {
case "message":
val = field.V.T().internalJsonRead(jsonObjValue, options);
break;
case "enum":
val = this.enum(field.V.T(), jsonObjValue, field.name, options.ignoreUnknownFields);
if (val === false)
continue;
break;
case "scalar":
val = this.scalar(jsonObjValue, field.V.T, field.V.L, field.name);
break;
}
this.assert(val !== undefined, field.name + " map value", jsonObjValue);
// read key
let key = jsonObjKey;
if (field.K == ScalarType.BOOL)
key = key == "true" ? true : key == "false" ? false : key;
key = this.scalar(key, field.K, LongType.STRING, field.name).toString();
fieldObj[key] = val;
}
}
else if (field.repeat) {
if (jsonValue === null)
continue;
// check input
this.assert(Array.isArray(jsonValue), field.name, jsonValue);
// our target to put array entries into
const fieldArr = target[localName];
// read array entries
for (const jsonItem of jsonValue) {
this.assert(jsonItem !== null, field.name, null);
let val;
switch (field.kind) {
case "message":
val = field.T().internalJsonRead(jsonItem, options);
break;
case "enum":
val = this.enum(field.T(), jsonItem, field.name, options.ignoreUnknownFields);
if (val === false)
continue;
break;
case "scalar":
val = this.scalar(jsonItem, field.T, field.L, field.name);
break;
}
this.assert(val !== undefined, field.name, jsonValue);
fieldArr.push(val);
}
}
else {
switch (field.kind) {
case "message":
if (jsonValue === null && field.T().typeName != 'google.protobuf.Value') {
this.assert(field.oneof === undefined, field.name + " (oneof member)", null);
continue;
}
target[localName] = field.T().internalJsonRead(jsonValue, options, target[localName]);
break;
case "enum":
let val = this.enum(field.T(), jsonValue, field.name, options.ignoreUnknownFields);
if (val === false)
continue;
target[localName] = val;
break;
case "scalar":
target[localName] = this.scalar(jsonValue, field.T, field.L, field.name);
break;
}
}
}
}
/**
* Returns `false` for unrecognized string representations.
*
* google.protobuf.NullValue accepts only JSON `null` (or the old `"NULL_VALUE"`).
*/
enum(type, json, fieldName, ignoreUnknownFields) {
if (type[0] == 'google.protobuf.NullValue')
assert(json === null || json === "NULL_VALUE", `Unable to parse field ${this.info.typeName}#${fieldName}, enum ${type[0]} only accepts null.`);
if (json === null)
// we require 0 to be default value for all enums
return 0;
switch (typeof json) {
case "number":
assert(Number.isInteger(json), `Unable to parse field ${this.info.typeName}#${fieldName}, enum can only be integral number, got ${json}.`);
return json;
case "string":
let localEnumName = json;
if (type[2] && json.substring(0, type[2].length) === type[2])
// lookup without the shared prefix
localEnumName = json.substring(type[2].length);
let enumNumber = type[1][localEnumName];
if (typeof enumNumber === 'undefined' && ignoreUnknownFields) {
return false;
}
assert(typeof enumNumber == "number", `Unable to parse field ${this.info.typeName}#${fieldName}, enum ${type[0]} has no value for "${json}".`);
return enumNumber;
}
assert(false, `Unable to parse field ${this.info.typeName}#${fieldName}, cannot parse enum value from ${typeof json}".`);
}
scalar(json, type, longType, fieldName) {
let e;
try {
switch (type) {
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
// Either numbers or strings are accepted. Exponent notation is also accepted.
case ScalarType.DOUBLE:
case ScalarType.FLOAT:
if (json === null)
return .0;
if (json === "NaN")
return Number.NaN;
if (json === "Infinity")
return Number.POSITIVE_INFINITY;
if (json === "-Infinity")
return Number.NEGATIVE_INFINITY;
if (json === "") {
e = "empty string";
break;
}
if (typeof json == "string" && json.trim().length !== json.length) {
e = "extra whitespace";
break;
}
if (typeof json != "string" && typeof json != "number") {
break;
}
let float = Number(json);
if (Number.isNaN(float)) {
e = "not a number";
break;
}
if (!Number.isFinite(float)) {
// infinity and -infinity are handled by string representation above, so this is an error
e = "too large or small";
break;
}
if (type == ScalarType.FLOAT)
assertFloat32(float);
return float;
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
case ScalarType.INT32:
case ScalarType.FIXED32:
case ScalarType.SFIXED32:
case ScalarType.SINT32:
case ScalarType.UINT32:
if (json === null)
return 0;
let int32;
if (typeof json == "number")
int32 = json;
else if (json === "")
e = "empty string";
else if (typeof json == "string") {
if (json.trim().length !== json.length)
e = "extra whitespace";
else
int32 = Number(json);
}
if (int32 === undefined)
break;
if (type == ScalarType.UINT32)
assertUInt32(int32);
else
assertInt32(int32);
return int32;
// int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
case ScalarType.INT64:
case ScalarType.SFIXED64:
case ScalarType.SINT64:
if (json === null)
return reflectionLongConvert(PbLong.ZERO, longType);
if (typeof json != "number" && typeof json != "string")
break;
return reflectionLongConvert(PbLong.from(json), longType);
case ScalarType.FIXED64:
case ScalarType.UINT64:
if (json === null)
return reflectionLongConvert(PbULong.ZERO, longType);
if (typeof json != "number" && typeof json != "string")
break;
return reflectionLongConvert(PbULong.from(json), longType);
// bool:
case ScalarType.BOOL:
if (json === null)
return false;
if (typeof json !== "boolean")
break;
return json;
// string:
case ScalarType.STRING:
if (json === null)
return "";
if (typeof json !== "string") {
e = "extra whitespace";
break;
}
try {
encodeURIComponent(json);
}
catch (e) {
e = "invalid UTF8";
break;
}
return json;
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
case ScalarType.BYTES:
if (json === null || json === "")
return new Uint8Array(0);
if (typeof json !== 'string')
break;
return base64decode(json);
}
}
catch (error) {
e = error.message;
}
this.assert(false, fieldName + (e ? " - " + e : ""), json);
}
}

View file

@ -0,0 +1,226 @@
import { base64encode } from "./base64";
import { PbLong, PbULong } from "./pb-long";
import { ScalarType } from "./reflection-info";
import { assert, assertFloat32, assertInt32, assertUInt32 } from "./assert";
/**
* Writes proto3 messages in canonical JSON format using reflection
* information.
*
* https://developers.google.com/protocol-buffers/docs/proto3#json
*/
export class ReflectionJsonWriter {
constructor(info) {
var _a;
this.fields = (_a = info.fields) !== null && _a !== void 0 ? _a : [];
}
/**
* Converts the message to a JSON object, based on the field descriptors.
*/
write(message, options) {
const json = {}, source = message;
for (const field of this.fields) {
// field is not part of a oneof, simply write as is
if (!field.oneof) {
let jsonValue = this.field(field, source[field.localName], options);
if (jsonValue !== undefined)
json[options.useProtoFieldName ? field.name : field.jsonName] = jsonValue;
continue;
}
// field is part of a oneof
const group = source[field.oneof];
if (group.oneofKind !== field.localName)
continue; // not selected, skip
const opt = field.kind == 'scalar' || field.kind == 'enum'
? Object.assign(Object.assign({}, options), { emitDefaultValues: true }) : options;
let jsonValue = this.field(field, group[field.localName], opt);
assert(jsonValue !== undefined);
json[options.useProtoFieldName ? field.name : field.jsonName] = jsonValue;
}
return json;
}
field(field, value, options) {
let jsonValue = undefined;
if (field.kind == 'map') {
assert(typeof value == "object" && value !== null);
const jsonObj = {};
switch (field.V.kind) {
case "scalar":
for (const [entryKey, entryValue] of Object.entries(value)) {
const val = this.scalar(field.V.T, entryValue, field.name, false, true);
assert(val !== undefined);
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
}
break;
case "message":
const messageType = field.V.T();
for (const [entryKey, entryValue] of Object.entries(value)) {
const val = this.message(messageType, entryValue, field.name, options);
assert(val !== undefined);
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
}
break;
case "enum":
const enumInfo = field.V.T();
for (const [entryKey, entryValue] of Object.entries(value)) {
assert(entryValue === undefined || typeof entryValue == 'number');
const val = this.enum(enumInfo, entryValue, field.name, false, true, options.enumAsInteger);
assert(val !== undefined);
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
}
break;
}
if (options.emitDefaultValues || Object.keys(jsonObj).length > 0)
jsonValue = jsonObj;
}
else if (field.repeat) {
assert(Array.isArray(value));
const jsonArr = [];
switch (field.kind) {
case "scalar":
for (let i = 0; i < value.length; i++) {
const val = this.scalar(field.T, value[i], field.name, field.opt, true);
assert(val !== undefined);
jsonArr.push(val);
}
break;
case "enum":
const enumInfo = field.T();
for (let i = 0; i < value.length; i++) {
assert(value[i] === undefined || typeof value[i] == 'number');
const val = this.enum(enumInfo, value[i], field.name, field.opt, true, options.enumAsInteger);
assert(val !== undefined);
jsonArr.push(val);
}
break;
case "message":
const messageType = field.T();
for (let i = 0; i < value.length; i++) {
const val = this.message(messageType, value[i], field.name, options);
assert(val !== undefined);
jsonArr.push(val);
}
break;
}
// add converted array to json output
if (options.emitDefaultValues || jsonArr.length > 0 || options.emitDefaultValues)
jsonValue = jsonArr;
}
else {
switch (field.kind) {
case "scalar":
jsonValue = this.scalar(field.T, value, field.name, field.opt, options.emitDefaultValues);
break;
case "enum":
jsonValue = this.enum(field.T(), value, field.name, field.opt, options.emitDefaultValues, options.enumAsInteger);
break;
case "message":
jsonValue = this.message(field.T(), value, field.name, options);
break;
}
}
return jsonValue;
}
/**
* Returns `null` as the default for google.protobuf.NullValue.
*/
enum(type, value, fieldName, optional, emitDefaultValues, enumAsInteger) {
if (type[0] == 'google.protobuf.NullValue')
return !emitDefaultValues && !optional ? undefined : null;
if (value === undefined) {
assert(optional);
return undefined;
}
if (value === 0 && !emitDefaultValues && !optional)
// we require 0 to be default value for all enums
return undefined;
assert(typeof value == 'number');
assert(Number.isInteger(value));
if (enumAsInteger || !type[1].hasOwnProperty(value))
// if we don't now the enum value, just return the number
return value;
if (type[2])
// restore the dropped prefix
return type[2] + type[1][value];
return type[1][value];
}
message(type, value, fieldName, options) {
if (value === undefined)
return options.emitDefaultValues ? null : undefined;
return type.internalJsonWrite(value, options);
}
scalar(type, value, fieldName, optional, emitDefaultValues) {
if (value === undefined) {
assert(optional);
return undefined;
}
const ed = emitDefaultValues || optional;
// noinspection FallThroughInSwitchStatementJS
switch (type) {
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
case ScalarType.INT32:
case ScalarType.SFIXED32:
case ScalarType.SINT32:
if (value === 0)
return ed ? 0 : undefined;
assertInt32(value);
return value;
case ScalarType.FIXED32:
case ScalarType.UINT32:
if (value === 0)
return ed ? 0 : undefined;
assertUInt32(value);
return value;
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
// Either numbers or strings are accepted. Exponent notation is also accepted.
case ScalarType.FLOAT:
assertFloat32(value);
case ScalarType.DOUBLE:
if (value === 0)
return ed ? 0 : undefined;
assert(typeof value == 'number');
if (Number.isNaN(value))
return 'NaN';
if (value === Number.POSITIVE_INFINITY)
return 'Infinity';
if (value === Number.NEGATIVE_INFINITY)
return '-Infinity';
return value;
// string:
case ScalarType.STRING:
if (value === "")
return ed ? '' : undefined;
assert(typeof value == 'string');
return value;
// bool:
case ScalarType.BOOL:
if (value === false)
return ed ? false : undefined;
assert(typeof value == 'boolean');
return value;
// JSON value will be a decimal string. Either numbers or strings are accepted.
case ScalarType.UINT64:
case ScalarType.FIXED64:
assert(typeof value == 'number' || typeof value == 'string' || typeof value == 'bigint');
let ulong = PbULong.from(value);
if (ulong.isZero() && !ed)
return undefined;
return ulong.toString();
// JSON value will be a decimal string. Either numbers or strings are accepted.
case ScalarType.INT64:
case ScalarType.SFIXED64:
case ScalarType.SINT64:
assert(typeof value == 'number' || typeof value == 'string' || typeof value == 'bigint');
let long = PbLong.from(value);
if (long.isZero() && !ed)
return undefined;
return long.toString();
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
case ScalarType.BYTES:
assert(value instanceof Uint8Array);
if (!value.byteLength)
return ed ? "" : undefined;
return base64encode(value);
}
}
}

View file

@ -0,0 +1,20 @@
import { LongType } from "./reflection-info";
/**
* Utility method to convert a PbLong or PbUlong to a JavaScript
* representation during runtime.
*
* Works with generated field information, `undefined` is equivalent
* to `STRING`.
*/
export function reflectionLongConvert(long, type) {
switch (type) {
case LongType.BIGINT:
return long.toBigInt();
case LongType.NUMBER:
return long.toNumber();
default:
// case undefined:
// case LongType.STRING:
return long.toString();
}
}

View file

@ -0,0 +1,86 @@
/**
* Copy partial data into the target message.
*
* If a singular scalar or enum field is present in the source, it
* replaces the field in the target.
*
* If a singular message field is present in the source, it is merged
* with the target field by calling mergePartial() of the responsible
* message type.
*
* If a repeated field is present in the source, its values replace
* all values in the target array, removing extraneous values.
* Repeated message fields are copied, not merged.
*
* If a map field is present in the source, entries are added to the
* target map, replacing entries with the same key. Entries that only
* exist in the target remain. Entries with message values are copied,
* not merged.
*
* Note that this function differs from protobuf merge semantics,
* which appends repeated fields.
*/
export function reflectionMergePartial(info, target, source) {
let fieldValue, // the field value we are working with
input = source, output; // where we want our field value to go
for (let field of info.fields) {
let name = field.localName;
if (field.oneof) {
const group = input[field.oneof]; // this is the oneof`s group in the source
if ((group === null || group === void 0 ? void 0 : group.oneofKind) == undefined) { // the user is free to omit
continue; // we skip this field, and all other members too
}
fieldValue = group[name]; // our value comes from the the oneof group of the source
output = target[field.oneof]; // and our output is the oneof group of the target
output.oneofKind = group.oneofKind; // always update discriminator
if (fieldValue == undefined) {
delete output[name]; // remove any existing value
continue; // skip further work on field
}
}
else {
fieldValue = input[name]; // we are using the source directly
output = target; // we want our field value to go directly into the target
if (fieldValue == undefined) {
continue; // skip further work on field, existing value is used as is
}
}
if (field.repeat)
output[name].length = fieldValue.length; // resize target array to match source array
// now we just work with `fieldValue` and `output` to merge the value
switch (field.kind) {
case "scalar":
case "enum":
if (field.repeat)
for (let i = 0; i < fieldValue.length; i++)
output[name][i] = fieldValue[i]; // not a reference type
else
output[name] = fieldValue; // not a reference type
break;
case "message":
let T = field.T();
if (field.repeat)
for (let i = 0; i < fieldValue.length; i++)
output[name][i] = T.create(fieldValue[i]);
else if (output[name] === undefined)
output[name] = T.create(fieldValue); // nothing to merge with
else
T.mergePartial(output[name], fieldValue);
break;
case "map":
// Map and repeated fields are simply overwritten, not appended or merged
switch (field.V.kind) {
case "scalar":
case "enum":
Object.assign(output[name], fieldValue); // elements are not reference types
break;
case "message":
let T = field.V.T();
for (let k of Object.keys(fieldValue))
output[name][k] = T.create(fieldValue[k]);
break;
}
break;
}
}
}

View file

@ -0,0 +1,33 @@
import { LongType, ScalarType } from "./reflection-info";
import { reflectionLongConvert } from "./reflection-long-convert";
import { PbLong, PbULong } from "./pb-long";
/**
* Creates the default value for a scalar type.
*/
export function reflectionScalarDefault(type, longType = LongType.STRING) {
switch (type) {
case ScalarType.BOOL:
return false;
case ScalarType.UINT64:
case ScalarType.FIXED64:
return reflectionLongConvert(PbULong.ZERO, longType);
case ScalarType.INT64:
case ScalarType.SFIXED64:
case ScalarType.SINT64:
return reflectionLongConvert(PbLong.ZERO, longType);
case ScalarType.DOUBLE:
case ScalarType.FLOAT:
return 0.0;
case ScalarType.BYTES:
return new Uint8Array(0);
case ScalarType.STRING:
return "";
default:
// case ScalarType.INT32:
// case ScalarType.UINT32:
// case ScalarType.SINT32:
// case ScalarType.FIXED32:
// case ScalarType.SFIXED32:
return 0;
}
}

View file

@ -0,0 +1,226 @@
import { LongType, ScalarType } from "./reflection-info";
import { isOneofGroup } from "./oneof";
// noinspection JSMethodCanBeStatic
export class ReflectionTypeCheck {
constructor(info) {
var _a;
this.fields = (_a = info.fields) !== null && _a !== void 0 ? _a : [];
}
prepare() {
if (this.data)
return;
const req = [], known = [], oneofs = [];
for (let field of this.fields) {
if (field.oneof) {
if (!oneofs.includes(field.oneof)) {
oneofs.push(field.oneof);
req.push(field.oneof);
known.push(field.oneof);
}
}
else {
known.push(field.localName);
switch (field.kind) {
case "scalar":
case "enum":
if (!field.opt || field.repeat)
req.push(field.localName);
break;
case "message":
if (field.repeat)
req.push(field.localName);
break;
case "map":
req.push(field.localName);
break;
}
}
}
this.data = { req, known, oneofs: Object.values(oneofs) };
}
/**
* Is the argument a valid message as specified by the
* reflection information?
*
* Checks all field types recursively. The `depth`
* specifies how deep into the structure the check will be.
*
* With a depth of 0, only the presence of fields
* is checked.
*
* With a depth of 1 or more, the field types are checked.
*
* With a depth of 2 or more, the members of map, repeated
* and message fields are checked.
*
* Message fields will be checked recursively with depth - 1.
*
* The number of map entries / repeated values being checked
* is < depth.
*/
is(message, depth, allowExcessProperties = false) {
if (depth < 0)
return true;
if (message === null || message === undefined || typeof message != 'object')
return false;
this.prepare();
let keys = Object.keys(message), data = this.data;
// if a required field is missing in arg, this cannot be a T
if (keys.length < data.req.length || data.req.some(n => !keys.includes(n)))
return false;
if (!allowExcessProperties) {
// if the arg contains a key we dont know, this is not a literal T
if (keys.some(k => !data.known.includes(k)))
return false;
}
// "With a depth of 0, only the presence and absence of fields is checked."
// "With a depth of 1 or more, the field types are checked."
if (depth < 1) {
return true;
}
// check oneof group
for (const name of data.oneofs) {
const group = message[name];
if (!isOneofGroup(group))
return false;
if (group.oneofKind === undefined)
continue;
const field = this.fields.find(f => f.localName === group.oneofKind);
if (!field)
return false; // we found no field, but have a kind, something is wrong
if (!this.field(group[group.oneofKind], field, allowExcessProperties, depth))
return false;
}
// check types
for (const field of this.fields) {
if (field.oneof !== undefined)
continue;
if (!this.field(message[field.localName], field, allowExcessProperties, depth))
return false;
}
return true;
}
field(arg, field, allowExcessProperties, depth) {
let repeated = field.repeat;
switch (field.kind) {
case "scalar":
if (arg === undefined)
return field.opt;
if (repeated)
return this.scalars(arg, field.T, depth, field.L);
return this.scalar(arg, field.T, field.L);
case "enum":
if (arg === undefined)
return field.opt;
if (repeated)
return this.scalars(arg, ScalarType.INT32, depth);
return this.scalar(arg, ScalarType.INT32);
case "message":
if (arg === undefined)
return true;
if (repeated)
return this.messages(arg, field.T(), allowExcessProperties, depth);
return this.message(arg, field.T(), allowExcessProperties, depth);
case "map":
if (typeof arg != 'object' || arg === null)
return false;
if (depth < 2)
return true;
if (!this.mapKeys(arg, field.K, depth))
return false;
switch (field.V.kind) {
case "scalar":
return this.scalars(Object.values(arg), field.V.T, depth, field.V.L);
case "enum":
return this.scalars(Object.values(arg), ScalarType.INT32, depth);
case "message":
return this.messages(Object.values(arg), field.V.T(), allowExcessProperties, depth);
}
break;
}
return true;
}
message(arg, type, allowExcessProperties, depth) {
if (allowExcessProperties) {
return type.isAssignable(arg, depth);
}
return type.is(arg, depth);
}
messages(arg, type, allowExcessProperties, depth) {
if (!Array.isArray(arg))
return false;
if (depth < 2)
return true;
if (allowExcessProperties) {
for (let i = 0; i < arg.length && i < depth; i++)
if (!type.isAssignable(arg[i], depth - 1))
return false;
}
else {
for (let i = 0; i < arg.length && i < depth; i++)
if (!type.is(arg[i], depth - 1))
return false;
}
return true;
}
scalar(arg, type, longType) {
let argType = typeof arg;
switch (type) {
case ScalarType.UINT64:
case ScalarType.FIXED64:
case ScalarType.INT64:
case ScalarType.SFIXED64:
case ScalarType.SINT64:
switch (longType) {
case LongType.BIGINT:
return argType == "bigint";
case LongType.NUMBER:
return argType == "number" && !isNaN(arg);
default:
return argType == "string";
}
case ScalarType.BOOL:
return argType == 'boolean';
case ScalarType.STRING:
return argType == 'string';
case ScalarType.BYTES:
return arg instanceof Uint8Array;
case ScalarType.DOUBLE:
case ScalarType.FLOAT:
return argType == 'number' && !isNaN(arg);
default:
// case ScalarType.UINT32:
// case ScalarType.FIXED32:
// case ScalarType.INT32:
// case ScalarType.SINT32:
// case ScalarType.SFIXED32:
return argType == 'number' && Number.isInteger(arg);
}
}
scalars(arg, type, depth, longType) {
if (!Array.isArray(arg))
return false;
if (depth < 2)
return true;
if (Array.isArray(arg))
for (let i = 0; i < arg.length && i < depth; i++)
if (!this.scalar(arg[i], type, longType))
return false;
return true;
}
mapKeys(map, type, depth) {
let keys = Object.keys(map);
switch (type) {
case ScalarType.INT32:
case ScalarType.FIXED32:
case ScalarType.SFIXED32:
case ScalarType.SINT32:
case ScalarType.UINT32:
return this.scalars(keys.slice(0, depth).map(k => parseInt(k)), type, depth);
case ScalarType.BOOL:
return this.scalars(keys.slice(0, depth).map(k => k == 'true' ? true : k == 'false' ? false : k), type, depth);
default:
return this.scalars(keys, type, depth, LongType.STRING);
}
}
}

View file

@ -0,0 +1,11 @@
/**
* assert that condition is true or throw error (with message)
*/
export declare function assert(condition: any, msg?: string): asserts condition;
/**
* assert that value cannot exist = type `never`. throw runtime error if it does.
*/
export declare function assertNever(value: never, msg?: string): never;
export declare function assertInt32(arg: any): asserts arg is number;
export declare function assertUInt32(arg: any): asserts arg is number;
export declare function assertFloat32(arg: any): asserts arg is number;

View file

@ -0,0 +1,18 @@
/**
* Decodes a base64 string to a byte array.
*
* - ignores white-space, including line breaks and tabs
* - allows inner padding (can decode concatenated base64 strings)
* - does not require padding
* - understands base64url encoding:
* "-" instead of "+",
* "_" instead of "/",
* no padding
*/
export declare function base64decode(base64Str: string): Uint8Array;
/**
* Encodes a byte array to a base64 string.
* Adds padding at the end.
* Does not insert newlines.
*/
export declare function base64encode(bytes: Uint8Array): string;

View file

@ -0,0 +1,323 @@
import type { PbLong, PbULong } from "./pb-long";
/**
* Options for writing binary data.
*/
export interface BinaryWriteOptions {
/**
* Shall unknown fields be written back on wire?
*
* `true`: unknown fields stored in a symbol property of the message
* are written back. This is the default behaviour.
*
* `false`: unknown fields are not written.
*
* `UnknownFieldWriter`: Your own behaviour for unknown fields.
*/
writeUnknownFields: boolean | UnknownFieldWriter;
/**
* Allows to use a custom implementation to encode binary data.
*/
writerFactory: () => IBinaryWriter;
}
/**
* Options for reading binary data.
*/
export interface BinaryReadOptions {
/**
* Shall unknown fields be read, ignored or raise an error?
*
* `true`: stores the unknown field on a symbol property of the
* message. This is the default behaviour.
*
* `false`: ignores the unknown field.
*
* `"throw"`: throws an error.
*
* `UnknownFieldReader`: Your own behaviour for unknown fields.
*/
readUnknownField: boolean | 'throw' | UnknownFieldReader;
/**
* Allows to use a custom implementation to parse binary data.
*/
readerFactory: (bytes: Uint8Array) => IBinaryReader;
}
/**
* Store an unknown field for a message somewhere.
*/
declare type UnknownFieldReader = (typeName: string, message: any, fieldNo: number, wireType: WireType, data: Uint8Array) => void;
/**
* Write unknown fields stored for the message to the writer.
*/
declare type UnknownFieldWriter = (typeName: string, message: any, writer: IBinaryWriter) => void;
/**
* This handler implements the default behaviour for unknown fields.
* When reading data, unknown fields are stored on the message, in a
* symbol property.
* When writing data, the symbol property is queried and unknown fields
* are serialized into the output again.
*/
export declare namespace UnknownFieldHandler {
/**
* The symbol used to store unknown fields for a message.
* The property must conform to `UnknownFieldContainer`.
*/
const symbol: unique symbol;
/**
* Store an unknown field during binary read directly on the message.
* This method is compatible with `BinaryReadOptions.readUnknownField`.
*/
const onRead: UnknownFieldReader;
/**
* Write unknown fields stored for the message to the writer.
* This method is compatible with `BinaryWriteOptions.writeUnknownFields`.
*/
const onWrite: UnknownFieldWriter;
/**
* List unknown fields stored for the message.
* Note that there may be multiples fields with the same number.
*/
const list: (message: any, fieldNo?: number | undefined) => UnknownField[];
/**
* Returns the last unknown field by field number.
*/
const last: (message: any, fieldNo: number) => UnknownField | undefined;
}
interface UnknownField {
no: number;
wireType: WireType;
data: Uint8Array;
}
export interface UnknownFieldContainer {
[UnknownFieldHandler.symbol]: UnknownField[];
}
/**
* Merges binary write or read options. Later values override earlier values.
*/
export declare function mergeBinaryOptions<T extends BinaryWriteOptions | BinaryReadOptions>(a?: Partial<T>, b?: Partial<T>): Partial<T>;
/**
* This interface is used throughout @protobuf-ts to read
* protobuf binary format.
*
* While not completely compatible, this interface is closely aligned
* with the `Reader` class of `protobufjs` to make it easier to swap
* the implementation.
*/
export interface IBinaryReader {
/**
* Current position.
*/
readonly pos: number;
/**
* Number of bytes available in this reader.
*/
readonly len: number;
/**
* Reads a tag - field number and wire type.
*/
tag(): [number, WireType];
/**
* Skip one element on the wire and return the skipped data.
*/
skip(wireType: WireType): Uint8Array;
/**
* Read a `int32` field, a signed 32 bit varint.
*/
uint32(): number;
/**
* Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
*/
int32(): number;
/**
* Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
*/
sint32(): number;
/**
* Read a `int64` field, a signed 64-bit varint.
*/
int64(): PbLong;
/**
* Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
*/
sint64(): PbLong;
/**
* Read a `fixed64` field, a signed, fixed-length 64-bit integer.
*/
sfixed64(): PbLong;
/**
* Read a `uint64` field, an unsigned 64-bit varint.
*/
uint64(): PbULong;
/**
* Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
*/
fixed64(): PbULong;
/**
* Read a `bool` field, a variant.
*/
bool(): boolean;
/**
* Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
*/
fixed32(): number;
/**
* Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
*/
sfixed32(): number;
/**
* Read a `float` field, 32-bit floating point number.
*/
float(): number;
/**
* Read a `double` field, a 64-bit floating point number.
*/
double(): number;
/**
* Read a `bytes` field, length-delimited arbitrary data.
*/
bytes(): Uint8Array;
/**
* Read a `string` field, length-delimited data converted to UTF-8 text.
*/
string(): string;
}
/**
* This interface is used throughout @protobuf-ts to write
* protobuf binary format.
*
* While not completely compatible, this interface is closely aligned
* with the `Writer` class of `protobufjs` to make it easier to swap
* the implementation.
*/
export interface IBinaryWriter {
/**
* Return all bytes written and reset this writer.
*/
finish(): Uint8Array;
/**
* Start a new fork for length-delimited data like a message
* or a packed repeated field.
*
* Must be joined later with `join()`.
*/
fork(): IBinaryWriter;
/**
* Join the last fork. Write its length and bytes, then
* return to the previous state.
*/
join(): IBinaryWriter;
/**
* Writes a tag (field number and wire type).
*
* Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`
*
* Generated code should compute the tag ahead of time and call `uint32()`.
*/
tag(fieldNo: number, type: WireType): IBinaryWriter;
/**
* Write a chunk of raw bytes.
*/
raw(chunk: Uint8Array): IBinaryWriter;
/**
* Write a `uint32` value, an unsigned 32 bit varint.
*/
uint32(value: number): IBinaryWriter;
/**
* Write a `int32` value, a signed 32 bit varint.
*/
int32(value: number): IBinaryWriter;
/**
* Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
*/
sint32(value: number): IBinaryWriter;
/**
* Write a `int64` value, a signed 64-bit varint.
*/
int64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `uint64` value, an unsigned 64-bit varint.
*/
uint64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
*/
sint64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
*/
fixed64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `fixed64` value, a signed, fixed-length 64-bit integer.
*/
sfixed64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `bool` value, a variant.
*/
bool(value: boolean): IBinaryWriter;
/**
* Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
*/
fixed32(value: number): IBinaryWriter;
/**
* Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
*/
sfixed32(value: number): IBinaryWriter;
/**
* Write a `float` value, 32-bit floating point number.
*/
float(value: number): IBinaryWriter;
/**
* Write a `double` value, a 64-bit floating point number.
*/
double(value: number): IBinaryWriter;
/**
* Write a `bytes` value, length-delimited arbitrary data.
*/
bytes(value: Uint8Array): IBinaryWriter;
/**
* Write a `string` value, length-delimited data converted to UTF-8 text.
*/
string(value: string): IBinaryWriter;
}
/**
* Protobuf binary format wire types.
*
* A wire type provides just enough information to find the length of the
* following value.
*
* See https://developers.google.com/protocol-buffers/docs/encoding#structure
*/
export declare enum WireType {
/**
* Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
*/
Varint = 0,
/**
* Used for fixed64, sfixed64, double.
* Always 8 bytes with little-endian byte order.
*/
Bit64 = 1,
/**
* Used for string, bytes, embedded messages, packed repeated fields
*
* Only repeated numeric types (types which use the varint, 32-bit,
* or 64-bit wire types) can be packed. In proto3, such fields are
* packed by default.
*/
LengthDelimited = 2,
/**
* Used for groups
* @deprecated
*/
StartGroup = 3,
/**
* Used for groups
* @deprecated
*/
EndGroup = 4,
/**
* Used for fixed32, sfixed32, float.
* Always 4 bytes with little-endian byte order.
*/
Bit32 = 5
}
export {};

View file

@ -0,0 +1,102 @@
import type { BinaryReadOptions, IBinaryReader } from "./binary-format-contract";
import { WireType } from "./binary-format-contract";
import { PbLong, PbULong } from "./pb-long";
/**
* Make options for reading binary data form partial options.
*/
export declare function binaryReadOptions(options?: Partial<BinaryReadOptions>): Readonly<BinaryReadOptions>;
/**
* TextDecoderLike is the subset of the TextDecoder API required by protobuf-ts.
*/
interface TextDecoderLike {
decode(input?: Uint8Array): string;
}
export declare class BinaryReader implements IBinaryReader {
/**
* Current position.
*/
pos: number;
/**
* Number of bytes available in this reader.
*/
readonly len: number;
private readonly buf;
private readonly view;
private readonly textDecoder;
constructor(buf: Uint8Array, textDecoder?: TextDecoderLike);
/**
* Reads a tag - field number and wire type.
*/
tag(): [number, WireType];
/**
* Skip one element on the wire and return the skipped data.
* Supports WireType.StartGroup since v2.0.0-alpha.23.
*/
skip(wireType: WireType): Uint8Array;
protected varint64: () => [number, number];
/**
* Throws error if position in byte array is out of range.
*/
protected assertBounds(): void;
/**
* Read a `uint32` field, an unsigned 32 bit varint.
*/
uint32: () => number;
/**
* Read a `int32` field, a signed 32 bit varint.
*/
int32(): number;
/**
* Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
*/
sint32(): number;
/**
* Read a `int64` field, a signed 64-bit varint.
*/
int64(): PbLong;
/**
* Read a `uint64` field, an unsigned 64-bit varint.
*/
uint64(): PbULong;
/**
* Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
*/
sint64(): PbLong;
/**
* Read a `bool` field, a variant.
*/
bool(): boolean;
/**
* Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
*/
fixed32(): number;
/**
* Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
*/
sfixed32(): number;
/**
* Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
*/
fixed64(): PbULong;
/**
* Read a `fixed64` field, a signed, fixed-length 64-bit integer.
*/
sfixed64(): PbLong;
/**
* Read a `float` field, 32-bit floating point number.
*/
float(): number;
/**
* Read a `double` field, a 64-bit floating point number.
*/
double(): number;
/**
* Read a `bytes` field, length-delimited arbitrary data.
*/
bytes(): Uint8Array;
/**
* Read a `string` field, length-delimited data converted to UTF-8 text.
*/
string(): string;
}
export {};

View file

@ -0,0 +1,128 @@
import type { BinaryWriteOptions, IBinaryWriter, WireType } from "./binary-format-contract";
/**
* Make options for writing binary data form partial options.
*/
export declare function binaryWriteOptions(options?: Partial<BinaryWriteOptions>): Readonly<BinaryWriteOptions>;
/**
* TextEncoderLike is the subset of the TextEncoder API required by protobuf-ts.
*/
interface TextEncoderLike {
encode(input?: string): Uint8Array;
}
export declare class BinaryWriter implements IBinaryWriter {
/**
* We cannot allocate a buffer for the entire output
* because we don't know it's size.
*
* So we collect smaller chunks of known size and
* concat them later.
*
* Use `raw()` to push data to this array. It will flush
* `buf` first.
*/
private chunks;
/**
* A growing buffer for byte values. If you don't know
* the size of the data you are writing, push to this
* array.
*/
protected buf: number[];
/**
* Previous fork states.
*/
private stack;
/**
* Text encoder instance to convert UTF-8 to bytes.
*/
private readonly textEncoder;
constructor(textEncoder?: TextEncoderLike);
/**
* Return all bytes written and reset this writer.
*/
finish(): Uint8Array;
/**
* Start a new fork for length-delimited data like a message
* or a packed repeated field.
*
* Must be joined later with `join()`.
*/
fork(): IBinaryWriter;
/**
* Join the last fork. Write its length and bytes, then
* return to the previous state.
*/
join(): IBinaryWriter;
/**
* Writes a tag (field number and wire type).
*
* Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
*
* Generated code should compute the tag ahead of time and call `uint32()`.
*/
tag(fieldNo: number, type: WireType): IBinaryWriter;
/**
* Write a chunk of raw bytes.
*/
raw(chunk: Uint8Array): IBinaryWriter;
/**
* Write a `uint32` value, an unsigned 32 bit varint.
*/
uint32(value: number): IBinaryWriter;
/**
* Write a `int32` value, a signed 32 bit varint.
*/
int32(value: number): IBinaryWriter;
/**
* Write a `bool` value, a variant.
*/
bool(value: boolean): IBinaryWriter;
/**
* Write a `bytes` value, length-delimited arbitrary data.
*/
bytes(value: Uint8Array): IBinaryWriter;
/**
* Write a `string` value, length-delimited data converted to UTF-8 text.
*/
string(value: string): IBinaryWriter;
/**
* Write a `float` value, 32-bit floating point number.
*/
float(value: number): IBinaryWriter;
/**
* Write a `double` value, a 64-bit floating point number.
*/
double(value: number): IBinaryWriter;
/**
* Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
*/
fixed32(value: number): IBinaryWriter;
/**
* Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
*/
sfixed32(value: number): IBinaryWriter;
/**
* Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
*/
sint32(value: number): IBinaryWriter;
/**
* Write a `fixed64` value, a signed, fixed-length 64-bit integer.
*/
sfixed64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
*/
fixed64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `int64` value, a signed 64-bit varint.
*/
int64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
*/
sint64(value: string | number | bigint): IBinaryWriter;
/**
* Write a `uint64` value, an unsigned 64-bit varint.
*/
uint64(value: string | number | bigint): IBinaryWriter;
}
export {};

View file

@ -0,0 +1,42 @@
import type { EnumInfo } from "./reflection-info";
/**
* Is this a lookup object generated by Typescript, for a Typescript enum
* generated by protobuf-ts?
*
* - No `const enum` (enum must not be inlined, we need reverse mapping).
* - No string enum (we need int32 for protobuf).
* - Must have a value for 0 (otherwise, we would need to support custom default values).
*/
export declare function isEnumObject(arg: any): arg is EnumInfo[1];
/**
* Lists all values of a Typescript enum, as an array of objects with a "name"
* property and a "number" property.
*
* Note that it is possible that a number appears more than once, because it is
* possible to have aliases in an enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
export declare function listEnumValues(enumObject: any): EnumObjectValue[];
/**
* A value of a Typescript enum generated by protobuf-ts.
*/
export declare type EnumObjectValue = {
name: string;
number: number;
};
/**
* Lists the names of a Typescript enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
export declare function listEnumNames(enumObject: any): Array<string>;
/**
* Lists the numbers of a Typescript enum.
*
* Throws if the enum does not adhere to the rules of enums generated by
* protobuf-ts. See `isEnumObject()`.
*/
export declare function listEnumNumbers(enumObject: any): Array<number>;

View file

@ -0,0 +1,58 @@
/**
* Read a 64 bit varint as two JS numbers.
*
* Returns tuple:
* [0]: low bits
* [0]: high bits
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L175
*/
export declare function varint64read(this: ReaderLike): [number, number];
/**
* Write a 64 bit varint, given as two JS numbers, to the given bytes array.
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
*/
export declare function varint64write(lo: number, hi: number, bytes: number[]): void;
/**
* Parse decimal string of 64 bit integer value as two JS numbers.
*
* Returns tuple:
* [0]: minus sign?
* [1]: low bits
* [2]: high bits
*
* Copyright 2008 Google Inc.
*/
export declare function int64fromString(dec: string): [boolean, number, number];
/**
* Format 64 bit integer value (as two JS numbers) to decimal string.
*
* Copyright 2008 Google Inc.
*/
export declare function int64toString(bitsLow: number, bitsHigh: number): string;
/**
* Write a 32 bit varint, signed or unsigned. Same as `varint64write(0, value, bytes)`
*
* Copyright 2008 Google Inc. All rights reserved.
*
* See https://github.com/protocolbuffers/protobuf/blob/1b18833f4f2a2f681f4e4a25cdf3b0a43115ec26/js/binary/encoder.js#L144
*/
export declare function varint32write(value: number, bytes: number[]): void;
/**
* Read an unsigned 32 bit varint.
*
* See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220
*/
export declare function varint32read(this: ReaderLike): number;
declare type ReaderLike = {
buf: Uint8Array;
pos: number;
len: number;
assertBounds(): void;
};
export {};

View file

@ -0,0 +1,26 @@
export { JsonValue, JsonObject, typeofJsonValue, isJsonObject } from './json-typings';
export { base64decode, base64encode } from './base64';
export { utf8read } from './protobufjs-utf8';
export { WireType, BinaryReadOptions, BinaryWriteOptions, mergeBinaryOptions, IBinaryWriter, IBinaryReader, UnknownFieldHandler, } from './binary-format-contract';
export { BinaryReader, binaryReadOptions } from './binary-reader';
export { BinaryWriter, binaryWriteOptions } from './binary-writer';
export { PbLong, PbULong } from './pb-long';
export { JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions, jsonReadOptions, jsonWriteOptions, mergeJsonOptions } from './json-format-contract';
export { IMessageType, PartialMessage, MESSAGE_TYPE } from './message-type-contract';
export { MessageType } from './message-type';
export { ScalarType, LongType, RepeatType, MessageInfo, EnumInfo, FieldInfo, PartialFieldInfo, normalizeFieldInfo, readFieldOptions, readFieldOption, readMessageOption } from './reflection-info';
export { UnknownEnum, UnknownMap, UnknownMessage, UnknownOneofGroup, UnknownScalar } from './unknown-types';
export { ReflectionTypeCheck } from './reflection-type-check';
export { reflectionCreate } from './reflection-create';
export { reflectionScalarDefault } from './reflection-scalar-default';
export { reflectionMergePartial } from './reflection-merge-partial';
export { reflectionEquals } from './reflection-equals';
export { ReflectionBinaryReader } from './reflection-binary-reader';
export { ReflectionBinaryWriter } from './reflection-binary-writer';
export { ReflectionJsonReader } from './reflection-json-reader';
export { ReflectionJsonWriter } from './reflection-json-writer';
export { containsMessageType, MessageTypeContainer } from './reflection-contains-message-type';
export { isOneofGroup, setOneofValue, getOneofValue, clearOneofValue, getSelectedOneofValue } from './oneof';
export { EnumObjectValue, listEnumValues, listEnumNames, listEnumNumbers, isEnumObject } from './enum-object';
export { lowerCamelCase } from './lower-camel-case';
export { assert, assertNever, assertInt32, assertUInt32, assertFloat32 } from './assert';

View file

@ -0,0 +1,69 @@
import type { IMessageType } from "./message-type-contract";
/**
* Options for parsing JSON data.
* All boolean options default to `false`.
*/
export interface JsonReadOptions {
/**
* Ignore unknown fields: Proto3 JSON parser should reject unknown fields
* by default. This option ignores unknown fields in parsing, as well as
* unrecognized enum string representations.
*/
ignoreUnknownFields: boolean;
/**
* This option is required to read `google.protobuf.Any`
* from JSON format.
*/
typeRegistry?: readonly IMessageType<any>[];
}
/**
* Options for serializing to JSON object.
* All boolean options default to `false`.
*/
export interface JsonWriteOptions {
/**
* Emit fields with default values: Fields with default values are omitted
* by default in proto3 JSON output. This option overrides this behavior
* and outputs fields with their default values.
*/
emitDefaultValues: boolean;
/**
* Emit enum values as integers instead of strings: The name of an enum
* value is used by default in JSON output. An option may be provided to
* use the numeric value of the enum value instead.
*/
enumAsInteger: boolean;
/**
* Use proto field name instead of lowerCamelCase name: By default proto3
* JSON printer should convert the field name to lowerCamelCase and use
* that as the JSON name. An implementation may provide an option to use
* proto field name as the JSON name instead. Proto3 JSON parsers are
* required to accept both the converted lowerCamelCase name and the proto
* field name.
*/
useProtoFieldName: boolean;
/**
* This option is required to write `google.protobuf.Any`
* to JSON format.
*/
typeRegistry?: readonly IMessageType<any>[];
}
/**
* Options for serializing to JSON string.
* All options default to `false` or `0`.
*/
export interface JsonWriteStringOptions extends JsonWriteOptions {
prettySpaces: number;
}
/**
* Make options for reading JSON data from partial options.
*/
export declare function jsonReadOptions(options?: Partial<JsonReadOptions>): Readonly<JsonReadOptions>;
/**
* Make options for writing JSON data from partial options.
*/
export declare function jsonWriteOptions(options?: Partial<JsonWriteStringOptions>): JsonWriteStringOptions;
/**
* Merges JSON write or read options. Later values override earlier values. Type registries are merged.
*/
export declare function mergeJsonOptions<T extends JsonWriteStringOptions | JsonReadOptions>(a?: Partial<T>, b?: Partial<T>): Partial<T>;

View file

@ -0,0 +1,28 @@
/**
* Represents any possible JSON value:
* - number
* - string
* - boolean
* - null
* - object (with any JSON value as property)
* - array (with any JSON value as element)
*/
export declare type JsonValue = number | string | boolean | null | JsonObject | JsonArray;
/**
* Represents a JSON object.
*/
export declare type JsonObject = {
[k: string]: JsonValue;
};
interface JsonArray extends Array<JsonValue> {
}
/**
* Get the type of a JSON value.
* Distinguishes between array, null and object.
*/
export declare function typeofJsonValue(value: JsonValue | undefined): 'string' | 'number' | 'object' | 'array' | 'null' | 'boolean' | 'undefined';
/**
* Is this a JSON object (instead of an array or null)?
*/
export declare function isJsonObject(value: JsonValue): value is JsonObject;
export {};

View file

@ -0,0 +1,7 @@
/**
* Converts snake_case to lowerCamelCase.
*
* Should behave like protoc:
* https://github.com/protocolbuffers/protobuf/blob/e8ae137c96444ea313485ed1118c5e43b2099cf1/src/google/protobuf/compiler/java/java_helpers.cc#L118
*/
export declare function lowerCamelCase(snakeCase: string): string;

View file

@ -0,0 +1,178 @@
import type { FieldInfo, MessageInfo } from "./reflection-info";
import type { BinaryReadOptions, BinaryWriteOptions, IBinaryReader, IBinaryWriter } from "./binary-format-contract";
import type { JsonValue } from "./json-typings";
import type { JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions } from "./json-format-contract";
/**
* The symbol used as a key on message objects to store the message type.
*
* Note that this is an experimental feature - it is here to stay, but
* implementation details may change without notice.
*/
export declare const MESSAGE_TYPE: unique symbol;
/**
* Similar to `Partial<T>`, but recursive, and keeps `oneof` groups
* intact.
*/
export declare type PartialMessage<T extends object> = {
[K in keyof T]?: PartialField<T[K]>;
};
declare type PartialField<T> = T extends (Date | Uint8Array | bigint | boolean | string | number) ? T : T extends Array<infer U> ? Array<PartialField<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : T extends {
oneofKind: string;
} ? T : T extends {
oneofKind: undefined;
} ? T : T extends object ? PartialMessage<T> : T;
/**
* A message type provides an API to work with messages of a specific type.
* It also exposes reflection information that can be used to work with a
* message of unknown type.
*/
export interface IMessageType<T extends object> extends MessageInfo {
/**
* The protobuf type name of the message, including package and
* parent types if present.
*
* Examples:
* 'MyNamespaceLessMessage'
* 'my_package.MyMessage'
* 'my_package.ParentMessage.ChildMessage'
*/
readonly typeName: string;
/**
* Simple information for each message field, in the order
* of declaration in the .proto.
*/
readonly fields: readonly FieldInfo[];
/**
* Contains custom message options from the .proto source in JSON format.
*/
readonly options: {
[extensionName: string]: JsonValue;
};
/**
* Contains the prototype for messages returned by create() which
* includes the `MESSAGE_TYPE` symbol pointing back to `this`.
*/
readonly messagePrototype?: Readonly<{}> | undefined;
/**
* Create a new message with default values.
*
* For example, a protobuf `string name = 1;` has the default value `""`.
*/
create(): T;
/**
* Create a new message from partial data.
*
* Unknown fields are discarded.
*
* `PartialMessage<T>` is similar to `Partial<T>`,
* but it is recursive, and it keeps `oneof` groups
* intact.
*/
create(value: PartialMessage<T>): T;
/**
* Create a new message from binary format.
*/
fromBinary(data: Uint8Array, options?: Partial<BinaryReadOptions>): T;
/**
* Write the message to binary format.
*/
toBinary(message: T, options?: Partial<BinaryWriteOptions>): Uint8Array;
/**
* Read a new message from a JSON value.
*/
fromJson(json: JsonValue, options?: Partial<JsonReadOptions>): T;
/**
* Read a new message from a JSON string.
* This is equivalent to `T.fromJson(JSON.parse(json))`.
*/
fromJsonString(json: string, options?: Partial<JsonReadOptions>): T;
/**
* Convert the message to canonical JSON value.
*/
toJson(message: T, options?: Partial<JsonWriteOptions>): JsonValue;
/**
* Convert the message to canonical JSON string.
* This is equivalent to `JSON.stringify(T.toJson(t))`
*/
toJsonString(message: T, options?: Partial<JsonWriteStringOptions>): string;
/**
* Clone the message.
*
* Unknown fields are discarded.
*/
clone(message: T): T;
/**
* Copy partial data into the target message.
*
* If a singular scalar or enum field is present in the source, it
* replaces the field in the target.
*
* If a singular message field is present in the source, it is merged
* with the target field by calling mergePartial() of the responsible
* message type.
*
* If a repeated field is present in the source, its values replace
* all values in the target array, removing extraneous values.
* Repeated message fields are copied, not merged.
*
* If a map field is present in the source, entries are added to the
* target map, replacing entries with the same key. Entries that only
* exist in the target remain. Entries with message values are copied,
* not merged.
*
* Note that this function differs from protobuf merge semantics,
* which appends repeated fields.
*/
mergePartial(target: T, source: PartialMessage<T>): void;
/**
* Determines whether two message of the same type have the same field values.
* Checks for deep equality, traversing repeated fields, oneof groups, maps
* and messages recursively.
* Will also return true if both messages are `undefined`.
*/
equals(a: T | undefined, b: T | undefined): boolean;
/**
* Is the given value assignable to our message type
* and contains no [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
is(arg: any, depth?: number): arg is T;
/**
* Is the given value assignable to our message type,
* regardless of [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
isAssignable(arg: any, depth?: number): arg is T;
/**
* This is an internal method. If you just want to read a message from
* JSON, use `fromJson()` or `fromJsonString()`.
*
* Reads JSON value and merges the fields into the target
* according to protobuf rules. If the target is omitted,
* a new instance is created first.
*/
internalJsonRead(json: JsonValue, options: JsonReadOptions, target?: T): T;
/**
* This is an internal method. If you just want to write a message
* to JSON, use `toJson()` or `toJsonString().
*
* Writes JSON value and returns it.
*/
internalJsonWrite(message: T, options: JsonWriteOptions): JsonValue;
/**
* This is an internal method. If you just want to write a message
* in binary format, use `toBinary()`.
*
* Serializes the message in binary format and appends it to the given
* writer. Returns passed writer.
*/
internalBinaryWrite(message: T, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter;
/**
* This is an internal method. If you just want to read a message from
* binary data, use `fromBinary()`.
*
* Reads data from binary format and merges the fields into
* the target according to protobuf rules. If the target is
* omitted, a new instance is created first.
*/
internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: T): T;
}
export {};

View file

@ -0,0 +1,157 @@
import type { IMessageType, PartialMessage } from "./message-type-contract";
import type { FieldInfo, PartialFieldInfo } from "./reflection-info";
import { ReflectionTypeCheck } from "./reflection-type-check";
import { ReflectionJsonReader } from "./reflection-json-reader";
import { ReflectionJsonWriter } from "./reflection-json-writer";
import { ReflectionBinaryReader } from "./reflection-binary-reader";
import { ReflectionBinaryWriter } from "./reflection-binary-writer";
import type { JsonValue } from "./json-typings";
import type { JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions } from "./json-format-contract";
import type { BinaryReadOptions, BinaryWriteOptions, IBinaryReader, IBinaryWriter } from "./binary-format-contract";
/**
* This standard message type provides reflection-based
* operations to work with a message.
*/
export declare class MessageType<T extends object> implements IMessageType<T> {
/**
* The protobuf type name of the message, including package and
* parent types if present.
*
* If the .proto file included a `package` statement,
* the type name will always start with a '.'.
*
* Examples:
* 'MyNamespaceLessMessage'
* '.my_package.MyMessage'
* '.my_package.ParentMessage.ChildMessage'
*/
readonly typeName: string;
/**
* Simple information for each message field, in the order
* of declaration in the .proto.
*/
readonly fields: readonly FieldInfo[];
/**
* Contains custom service options from the .proto source in JSON format.
*/
readonly options: JsonOptionsMap;
/**
* Contains the prototype for messages returned by create() which
* includes the `MESSAGE_TYPE` symbol pointing back to `this`.
*/
readonly messagePrototype?: Readonly<{}> | undefined;
protected readonly defaultCheckDepth = 16;
protected readonly refTypeCheck: ReflectionTypeCheck;
protected readonly refJsonReader: ReflectionJsonReader;
protected readonly refJsonWriter: ReflectionJsonWriter;
protected readonly refBinReader: ReflectionBinaryReader;
protected readonly refBinWriter: ReflectionBinaryWriter;
constructor(name: string, fields: readonly PartialFieldInfo[], options?: JsonOptionsMap);
/**
* Create a new message with default values.
*
* For example, a protobuf `string name = 1;` has the default value `""`.
*/
create(): T;
/**
* Create a new message from partial data.
* Where a field is omitted, the default value is used.
*
* Unknown fields are discarded.
*
* `PartialMessage<T>` is similar to `Partial<T>`,
* but it is recursive, and it keeps `oneof` groups
* intact.
*/
create(value: PartialMessage<T>): T;
/**
* Clone the message.
*
* Unknown fields are discarded.
*/
clone(message: T): T;
/**
* Determines whether two message of the same type have the same field values.
* Checks for deep equality, traversing repeated fields, oneof groups, maps
* and messages recursively.
* Will also return true if both messages are `undefined`.
*/
equals(a: T | undefined, b: T | undefined): boolean;
/**
* Is the given value assignable to our message type
* and contains no [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
is(arg: any, depth?: number): arg is T;
/**
* Is the given value assignable to our message type,
* regardless of [excess properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks)?
*/
isAssignable(arg: any, depth?: number): arg is T;
/**
* Copy partial data into the target message.
*/
mergePartial(target: T, source: PartialMessage<T>): void;
/**
* Create a new message from binary format.
*/
fromBinary(data: Uint8Array, options?: Partial<BinaryReadOptions>): T;
/**
* Read a new message from a JSON value.
*/
fromJson(json: JsonValue, options?: Partial<JsonReadOptions>): T;
/**
* Read a new message from a JSON string.
* This is equivalent to `T.fromJson(JSON.parse(json))`.
*/
fromJsonString(json: string, options?: Partial<JsonReadOptions>): T;
/**
* Write the message to canonical JSON value.
*/
toJson(message: T, options?: Partial<JsonWriteOptions>): JsonValue;
/**
* Convert the message to canonical JSON string.
* This is equivalent to `JSON.stringify(T.toJson(t))`
*/
toJsonString(message: T, options?: Partial<JsonWriteStringOptions>): string;
/**
* Write the message to binary format.
*/
toBinary(message: T, options?: Partial<BinaryWriteOptions>): Uint8Array;
/**
* This is an internal method. If you just want to read a message from
* JSON, use `fromJson()` or `fromJsonString()`.
*
* Reads JSON value and merges the fields into the target
* according to protobuf rules. If the target is omitted,
* a new instance is created first.
*/
internalJsonRead(json: JsonValue, options: JsonReadOptions, target?: T): T;
/**
* This is an internal method. If you just want to write a message
* to JSON, use `toJson()` or `toJsonString().
*
* Writes JSON value and returns it.
*/
internalJsonWrite(message: T, options: JsonWriteOptions): JsonValue;
/**
* This is an internal method. If you just want to write a message
* in binary format, use `toBinary()`.
*
* Serializes the message in binary format and appends it to the given
* writer. Returns passed writer.
*/
internalBinaryWrite(message: T, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter;
/**
* This is an internal method. If you just want to read a message from
* binary data, use `fromBinary()`.
*
* Reads data from binary format and merges the fields into
* the target according to protobuf rules. If the target is
* omitted, a new instance is created first.
*/
internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: T): T;
}
declare type JsonOptionsMap = {
[extensionName: string]: JsonValue;
};
export {};

View file

@ -0,0 +1,93 @@
import type { UnknownEnum, UnknownMessage, UnknownOneofGroup, UnknownScalar } from "./unknown-types";
/**
* Is the given value a valid oneof group?
*
* We represent protobuf `oneof` as algebraic data types (ADT) in generated
* code. But when working with messages of unknown type, the ADT does not
* help us.
*
* This type guard checks if the given object adheres to the ADT rules, which
* are as follows:
*
* 1) Must be an object.
*
* 2) Must have a "oneofKind" discriminator property.
*
* 3) If "oneofKind" is `undefined`, no member field is selected. The object
* must not have any other properties.
*
* 4) If "oneofKind" is a `string`, the member field with this name is
* selected.
*
* 5) If a member field is selected, the object must have a second property
* with this name. The property must not be `undefined`.
*
* 6) No extra properties are allowed. The object has either one property
* (no selection) or two properties (selection).
*
*/
export declare function isOneofGroup(any: any): any is UnknownOneofGroup;
/**
* Returns the value of the given field in a oneof group.
*/
export declare function getOneofValue<T extends UnknownOneofGroup, K extends T extends {
oneofKind: keyof T;
} ? T["oneofKind"] : never, V extends T extends {
oneofKind: K;
} ? T[K] : never>(oneof: T, kind: K): V | undefined;
/**
* Selects the given field in a oneof group.
*
* Note that the recommended way to modify a oneof group is to set
* a new object:
*
* ```ts
* message.result = {
* oneofKind: "error",
* error: "foo"
* };
* ```
*/
export declare function setOneofValue<T extends UnknownOneofGroup, K extends T extends {
oneofKind: keyof T;
} ? T["oneofKind"] : never, V extends T extends {
oneofKind: K;
} ? T[K] : never>(oneof: T, kind: K, value: V): void;
export declare function setOneofValue<T extends UnknownOneofGroup>(oneof: T, kind: undefined, value?: undefined): void;
/**
* Selects the given field in a oneof group, just like `setOneofValue()`,
* but works with unknown oneof groups.
*/
export declare function setUnknownOneofValue(oneof: UnknownOneofGroup, kind: string, value: UnknownScalar | UnknownEnum | UnknownMessage): void;
export declare function setUnknownOneofValue(oneof: UnknownOneofGroup, kind: undefined, value?: undefined): void;
/**
* Removes the selected field in a oneof group.
*
* Note that the recommended way to modify a oneof group is to set
* a new object:
*
* ```ts
* message.result = { oneofKind: undefined };
* ```
*/
export declare function clearOneofValue<T extends UnknownOneofGroup>(oneof: T): void;
/**
* Returns the selected value of the given oneof group.
*
* Not that the recommended way to access a oneof group is to check
* the "oneofKind" property and let TypeScript narrow down the union
* type for you:
*
* ```ts
* if (message.result.oneofKind === "error") {
* message.result.error; // string
* }
* ```
*
* In the rare case you just need the value, and do not care about
* which protobuf field is selected, you can use this function
* for convenience.
*/
export declare function getSelectedOneofValue<T extends UnknownOneofGroup, V extends string extends keyof T ? UnknownOneofGroup[string] : T extends {
oneofKind: keyof T;
} ? T[T["oneofKind"]] : never>(oneof: T): V | undefined;

View file

@ -0,0 +1,85 @@
export declare function detectBi(): void;
declare abstract class SharedPbLong {
/**
* Low 32 bits.
*/
readonly lo: number;
/**
* High 32 bits.
*/
readonly hi: number;
/**
* Create a new instance with the given bits.
*/
constructor(lo: number, hi: number);
/**
* Is this instance equal to 0?
*/
isZero(): boolean;
/**
* Convert to a native number.
*/
toNumber(): number;
/**
* Convert to decimal string.
*/
abstract toString(): string;
/**
* Convert to native bigint.
*/
abstract toBigInt(): bigint;
}
/**
* 64-bit unsigned integer as two 32-bit values.
* Converts between `string`, `number` and `bigint` representations.
*/
export declare class PbULong extends SharedPbLong {
/**
* ulong 0 singleton.
*/
static ZERO: PbULong;
/**
* Create instance from a `string`, `number` or `bigint`.
*/
static from(value: string | number | bigint): PbULong;
/**
* Convert to decimal string.
*/
toString(): string;
/**
* Convert to native bigint.
*/
toBigInt(): bigint;
}
/**
* 64-bit signed integer as two 32-bit values.
* Converts between `string`, `number` and `bigint` representations.
*/
export declare class PbLong extends SharedPbLong {
/**
* long 0 singleton.
*/
static ZERO: PbLong;
/**
* Create instance from a `string`, `number` or `bigint`.
*/
static from(value: string | number | bigint): PbLong;
/**
* Do we have a minus sign?
*/
isNegative(): boolean;
/**
* Negate two's complement.
* Invert all the bits and add one to the result.
*/
negate(): PbLong;
/**
* Convert to decimal string.
*/
toString(): string;
/**
* Convert to native bigint.
*/
toBigInt(): bigint;
}
export {};

View file

@ -0,0 +1,13 @@
/**
* @deprecated This function will no longer be exported with the next major
* release, since protobuf-ts has switch to TextDecoder API. If you need this
* function, please migrate to @protobufjs/utf8. For context, see
* https://github.com/timostamm/protobuf-ts/issues/184
*
* Reads UTF8 bytes as a string.
*
* See [protobufjs / utf8](https://github.com/protobufjs/protobuf.js/blob/9893e35b854621cce64af4bf6be2cff4fb892796/lib/utf8/index.js#L40)
*
* Copyright (c) 2016, Daniel Wirtz
*/
export declare function utf8read(bytes: Uint8Array): string;

View file

@ -0,0 +1,32 @@
import type { BinaryReadOptions, IBinaryReader } from "./binary-format-contract";
import type { FieldInfo, PartialMessageInfo } from "./reflection-info";
import { LongType, ScalarType } from "./reflection-info";
import type { UnknownMap, UnknownScalar } from "./unknown-types";
/**
* Reads proto3 messages in binary format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/encoding
*/
export declare class ReflectionBinaryReader {
private readonly info;
protected fieldNoToField?: ReadonlyMap<number, FieldInfo>;
constructor(info: PartialMessageInfo);
protected prepare(): void;
/**
* Reads a message from binary format into the target message.
*
* Repeated fields are appended. Map entries are added, overwriting
* existing keys.
*
* If a message field is already present, it will be merged with the
* new data.
*/
read<T extends object>(reader: IBinaryReader, message: T, options: BinaryReadOptions, length?: number): void;
/**
* Read a map field, expecting key field = 1, value field = 2
*/
protected mapEntry(field: FieldInfo & {
kind: "map";
}, reader: IBinaryReader, options: BinaryReadOptions): [string | number, UnknownMap[string]];
protected scalar(reader: IBinaryReader, type: ScalarType, longType: LongType | undefined): UnknownScalar;
}

View file

@ -0,0 +1,43 @@
import type { BinaryWriteOptions, IBinaryWriter } from "./binary-format-contract";
import { WireType } from "./binary-format-contract";
import type { FieldInfo } from "./reflection-info";
import { PartialMessageInfo, ScalarType } from "./reflection-info";
import type { IMessageType } from "./message-type-contract";
/**
* Writes proto3 messages in binary format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/encoding
*/
export declare class ReflectionBinaryWriter {
private readonly info;
protected fields?: readonly FieldInfo[];
constructor(info: PartialMessageInfo);
protected prepare(): void;
/**
* Writes the message to binary format.
*/
write<T extends object>(message: T, writer: IBinaryWriter, options: BinaryWriteOptions): void;
protected mapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
kind: 'map';
}, key: any, value: any): void;
protected message(writer: IBinaryWriter, options: BinaryWriteOptions, handler: IMessageType<any>, fieldNo: number, value: any): void;
/**
* Write a single scalar value.
*/
protected scalar(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any, emitDefault: boolean): void;
/**
* Write an array of scalar values in packed format.
*/
protected packed(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any[]): void;
/**
* Get information for writing a scalar value.
*
* Returns tuple:
* [0]: appropriate WireType
* [1]: name of the appropriate method of IBinaryWriter
* [2]: whether the given value is a default value
*
* If argument `value` is omitted, [2] is always false.
*/
protected scalarInfo(type: ScalarType, value?: any): [WireType, "int32" | "string" | "bool" | "uint32" | "double" | "float" | "int64" | "uint64" | "fixed64" | "bytes" | "fixed32" | "sfixed32" | "sfixed64" | "sint32" | "sint64", boolean];
}

View file

@ -0,0 +1,17 @@
import { IMessageType, MESSAGE_TYPE } from './message-type-contract';
/**
* The interface that models storing type in a symbol property.
*
* Note that this is an experimental feature - it is here to stay, but
* implementation details may change without notice.
*/
export interface MessageTypeContainer<T extends object> {
[MESSAGE_TYPE]: IMessageType<T>;
}
/**
* Check if the provided object is a proto message.
*
* Note that this is an experimental feature - it is here to stay, but
* implementation details may change without notice.
*/
export declare function containsMessageType<T extends object>(msg: T): msg is (T & MessageTypeContainer<T>);

View file

@ -0,0 +1,6 @@
import type { IMessageType } from './message-type-contract';
/**
* Creates an instance of the generic message, using the field
* information.
*/
export declare function reflectionCreate<T extends object>(type: IMessageType<T>): T;

View file

@ -0,0 +1,9 @@
import type { MessageInfo } from "./reflection-info";
import type { UnknownMessage } from "./unknown-types";
/**
* Determines whether two message of the same type have the same field values.
* Checks for deep equality, traversing repeated fields, oneof groups, maps
* and messages recursively.
* Will also return true if both messages are `undefined`.
*/
export declare function reflectionEquals(info: MessageInfo, a: UnknownMessage | undefined, b: UnknownMessage | undefined): boolean;

View file

@ -0,0 +1,419 @@
import type { IMessageType } from "./message-type-contract";
import type { JsonValue } from "./json-typings";
/**
* Describes a protobuf enum for runtime reflection.
*
* The tuple consists of:
*
*
* [0] the protobuf type name
*
* The type name follows the same rules as message type names.
* See `MessageInfo` for details.
*
*
* [1] the enum object generated by Typescript
*
* We generate standard Typescript enums for protobuf enums. They are compiled
* to lookup objects that map from numerical value to name strings and vice
* versa and can also contain alias names.
*
* See https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
*
* We use this lookup feature to when encoding / decoding JSON format. The
* enum is guaranteed to have a value for 0. We generate an entry for 0 if
* none was declared in .proto because we would need to support custom default
* values if we didn't.
*
*
* [2] the prefix shared by all original enum values (optional)
*
* If all values of a protobuf enum share a prefix, it is dropped in the
* generated enum. For example, the protobuf enum `enum My { MY_FOO, MY_BAR }`
* becomes the typescript enum `enum My { FOO, BAR }`.
*
* Because the JSON format requires the original value name, we store the
* dropped prefix here, so that the JSON format implementation can restore
* the original value names.
*/
export declare type EnumInfo = readonly [
/**
* The protobuf type name of the enum
*/
string,
/**
* The enum object generated by Typescript
*/
{
[key: number]: string;
[k: string]: number | string;
},
/**
* The prefix shared by all original enum values
*/
string?];
/**
* Describes a protobuf message for runtime reflection.
*/
export interface MessageInfo {
/**
* The protobuf type name of the message, including package and
* parent types if present.
*
* If the .proto file included a `package` statement, the type name
* starts with '.'.
*
* Examples:
* 'MyNamespaceLessMessage'
* '.my_package.MyMessage'
* '.my_package.ParentMessage.ChildMessage'
*/
readonly typeName: string;
/**
* Simple information for each message field, in the order
* of declaration in the source .proto.
*/
readonly fields: readonly FieldInfo[];
/**
* Contains custom message options from the .proto source in JSON format.
*/
readonly options: {
[extensionName: string]: JsonValue;
};
}
/**
* Version of `MessageInfo` that allows the following properties
* to be omitted:
* - "fields": omitting means the message has no fields
* - "options": omitting means the message has no options
*/
export declare type PartialMessageInfo = PartialPartial<MessageInfo, "fields" | "options">;
declare type PartialPartial<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
/**
* Describes a field of a protobuf message for runtime
* reflection. We distinguish between the following
* kinds of fields:
*
* "scalar": string, bool, float, int32, etc.
* See https://developers.google.com/protocol-buffers/docs/proto3#scalar
*
* "enum": field was declared with an enum type.
*
* "message": field was declared with a message type.
*
* "map": field was declared with map<K,V>.
*
*
* Every field, regardless of it's kind, always has the following properties:
*
* "no": The field number of the .proto field.
* "name": The original name of the .proto field.
* "localName": The name of the field as used in generated code.
* "jsonName": The name for JSON serialization / deserialization.
* "options": Custom field options from the .proto source in JSON format.
*
*
* Other properties:
*
* - Fields of kind "scalar", "enum" and "message" can have a "repeat" type.
* - Fields of kind "scalar" and "enum" can have a "repeat" type.
* - Fields of kind "scalar", "enum" and "message" can be member of a "oneof".
*
* A field can be only have one of the above properties set.
*
* Options for "scalar" fields:
*
* - 64 bit integral types can provide "L" - the JavaScript representation
* type.
*
*/
export declare type FieldInfo = fiRules<fiScalar> | fiRules<fiEnum> | fiRules<fiMessage> | fiRules<fiMap>;
/**
* Version of `FieldInfo` that allows the following properties
* to be omitted:
* - "localName", "jsonName": can be omitted if equal to lowerCamelCase(name)
* - "opt": can be omitted if false
* - "repeat", can be omitted if RepeatType.NO
*
* Use `normalizeFieldInfo()` to fill the omitted fields with
* their standard values.
*/
export declare type PartialFieldInfo = fiPartialRules<fiScalar> | fiPartialRules<fiEnum> | fiPartialRules<fiMessage> | fiPartialRules<fiMap>;
interface fiShared {
/**
* The field number of the .proto field.
*/
no: number;
/**
* The original name of the .proto field.
*/
name: string;
/**
* The name of the field as used in generated code.
*/
localName: string;
/**
* The name for JSON serialization / deserialization.
*/
jsonName: string;
/**
* The name of the `oneof` group, if this field belongs to one.
*/
oneof: string | undefined;
/**
* Contains custom field options from the .proto source in JSON format.
*/
options?: {
[extensionName: string]: JsonValue;
};
}
interface fiScalar extends fiShared {
kind: 'scalar';
/**
* Scalar type of the field.
*/
T: ScalarType;
/**
* Representation of 64 bit integral types (int64, uint64, sint64,
* fixed64, sfixed64).
*
* If this option is set for other scalar types, it is ignored.
* Omitting this option is equivalent to `STRING`.
*/
L?: LongType;
/**
* Is the field repeated?
*/
repeat: RepeatType;
/**
* Is the field optional?
*/
opt: boolean;
}
interface fiMessage extends fiShared {
kind: 'message';
/**
* Message handler for the field.
*/
T: () => IMessageType<any>;
/**
* Is the field repeated?
*/
repeat: RepeatType;
}
interface fiEnum extends fiShared {
kind: 'enum';
/**
* Enum type information for the field.
*/
T: () => EnumInfo;
/**
* Is the field repeated?
*/
repeat: RepeatType;
/**
* Is the field optional?
*/
opt: boolean;
}
interface fiMap extends fiShared {
kind: 'map';
/**
* Map key type.
*
* The key_type can be any integral or string type
* (so, any scalar type except for floating point
* types and bytes)
*/
K: Exclude<ScalarType, ScalarType.FLOAT | ScalarType.DOUBLE | ScalarType.BYTES>;
/**
* Map value type. Can be a `ScalarType`, enum type information,
* or type handler for a message.
*/
V: {
kind: 'scalar';
T: ScalarType;
L?: LongType;
} | {
kind: 'enum';
T: () => EnumInfo;
} | {
kind: 'message';
T: () => IMessageType<any>;
};
}
declare type fiRules<T> = Omit<T, 'oneof' | 'repeat' | 'opt'> & ({
repeat: RepeatType.NO;
opt: false;
oneof: undefined;
} | {
repeat: RepeatType.NO;
opt: true;
oneof: undefined;
} | {
repeat: RepeatType.PACKED | RepeatType.UNPACKED;
opt: false;
oneof: undefined;
} | {
repeat: RepeatType.NO;
opt: false;
oneof: string;
});
declare type fiPartialRules<T> = Omit<T, 'jsonName' | 'localName' | 'oneof' | 'repeat' | 'opt'> & ({
localName?: string;
jsonName?: string;
repeat?: RepeatType.NO;
opt?: false;
oneof?: undefined;
} | {
localName?: string;
jsonName?: string;
repeat?: RepeatType.NO;
opt: true;
oneof?: undefined;
} | {
localName?: string;
jsonName?: string;
repeat: RepeatType.PACKED | RepeatType.UNPACKED;
opt?: false;
oneof?: undefined;
} | {
localName?: string;
jsonName?: string;
repeat?: RepeatType.NO;
opt?: false;
oneof: string;
});
/**
* Scalar value types. This is a subset of field types declared by protobuf
* enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
* are omitted, but the numerical values are identical.
*/
export declare enum ScalarType {
DOUBLE = 1,
FLOAT = 2,
INT64 = 3,
UINT64 = 4,
INT32 = 5,
FIXED64 = 6,
FIXED32 = 7,
BOOL = 8,
STRING = 9,
BYTES = 12,
UINT32 = 13,
SFIXED32 = 15,
SFIXED64 = 16,
SINT32 = 17,
SINT64 = 18
}
/**
* JavaScript representation of 64 bit integral types. Equivalent to the
* field option "jstype".
*
* By default, protobuf-ts represents 64 bit types as `bigint`.
*
* You can change the default behaviour by enabling the plugin parameter
* `long_type_string`, which will represent 64 bit types as `string`.
*
* Alternatively, you can change the behaviour for individual fields
* with the field option "jstype":
*
* ```protobuf
* uint64 my_field = 1 [jstype = JS_STRING];
* uint64 other_field = 2 [jstype = JS_NUMBER];
* ```
*/
export declare enum LongType {
/**
* Use JavaScript `bigint`.
*
* Field option `[jstype = JS_NORMAL]`.
*/
BIGINT = 0,
/**
* Use JavaScript `string`.
*
* Field option `[jstype = JS_STRING]`.
*/
STRING = 1,
/**
* Use JavaScript `number`.
*
* Large values will loose precision.
*
* Field option `[jstype = JS_NUMBER]`.
*/
NUMBER = 2
}
/**
* Protobuf 2.1.0 introduced packed repeated fields.
* Setting the field option `[packed = true]` enables packing.
*
* In proto3, all repeated fields are packed by default.
* Setting the field option `[packed = false]` disables packing.
*
* Packed repeated fields are encoded with a single tag,
* then a length-delimiter, then the element values.
*
* Unpacked repeated fields are encoded with a tag and
* value for each element.
*
* `bytes` and `string` cannot be packed.
*/
export declare enum RepeatType {
/**
* The field is not repeated.
*/
NO = 0,
/**
* The field is repeated and should be packed.
* Invalid for `bytes` and `string`, they cannot be packed.
*/
PACKED = 1,
/**
* The field is repeated but should not be packed.
* The only valid repeat type for repeated `bytes` and `string`.
*/
UNPACKED = 2
}
/**
* Turns PartialFieldInfo into FieldInfo.
*/
export declare function normalizeFieldInfo(field: PartialFieldInfo): FieldInfo;
/**
* Read custom field options from a generated message type.
*
* @deprecated use readFieldOption()
*/
export declare function readFieldOptions<T extends object>(messageType: MessageInfo, fieldName: string | number, extensionName: string, extensionType: IMessageType<T>): T | undefined;
/**
* Read a custom field option.
*
* ```proto
* message MyMessage {
* int32 my_field = 1 [(acme.field_opt) = true];
* }
* ```
*
* ```typescript
* let val = readFieldOption(MyMessage, 'myField', 'acme.field_opt')
* ```
*/
export declare function readFieldOption<T extends object>(messageType: MessageInfo, fieldName: string | number, extensionName: string): JsonValue | undefined;
export declare function readFieldOption<T extends object>(messageType: MessageInfo, fieldName: string | number, extensionName: string, extensionType: IMessageType<T>): T | undefined;
/**
* Read a custom message option.
*
* ```proto
* message MyMessage {
* option acme.message_opt = true;
* }
* ```
*
* ```typescript
* let val = readMessageOption(MyMessage, 'acme.message_opt')
* ```
*/
export declare function readMessageOption<T extends object>(messageType: MessageInfo, extensionName: string): JsonValue | undefined;
export declare function readMessageOption<T extends object>(messageType: MessageInfo, extensionName: string, extensionType: IMessageType<T>): T | undefined;
export {};

View file

@ -0,0 +1,39 @@
import type { JsonObject, JsonValue } from "./json-typings";
import type { JsonReadOptions } from "./json-format-contract";
import type { EnumInfo } from "./reflection-info";
import { LongType, PartialMessageInfo, ScalarType } from "./reflection-info";
import type { UnknownEnum, UnknownScalar } from "./unknown-types";
/**
* Reads proto3 messages in canonical JSON format using reflection information.
*
* https://developers.google.com/protocol-buffers/docs/proto3#json
*/
export declare class ReflectionJsonReader {
private readonly info;
/**
* JSON key to field.
* Accepts the original proto field name in the .proto, the
* lowerCamelCase name, or the name specified by the json_name option.
*/
private fMap?;
constructor(info: PartialMessageInfo);
protected prepare(): void;
assert(condition: any, fieldName: string, jsonValue: JsonValue): asserts condition;
/**
* Reads a message from canonical JSON format into the target message.
*
* Repeated fields are appended. Map entries are added, overwriting
* existing keys.
*
* If a message field is already present, it will be merged with the
* new data.
*/
read<T extends object>(input: JsonObject, message: T, options: JsonReadOptions): void;
/**
* Returns `false` for unrecognized string representations.
*
* google.protobuf.NullValue accepts only JSON `null` (or the old `"NULL_VALUE"`).
*/
enum(type: EnumInfo, json: unknown, fieldName: string, ignoreUnknownFields: boolean): UnknownEnum | false;
scalar(json: JsonValue, type: ScalarType, longType: LongType | undefined, fieldName: string): UnknownScalar;
}

View file

@ -0,0 +1,27 @@
import type { JsonValue } from "./json-typings";
import type { JsonWriteOptions } from "./json-format-contract";
import type { EnumInfo, FieldInfo, PartialMessageInfo } from "./reflection-info";
import { ScalarType } from "./reflection-info";
import type { IMessageType } from "./message-type-contract";
/**
* Writes proto3 messages in canonical JSON format using reflection
* information.
*
* https://developers.google.com/protocol-buffers/docs/proto3#json
*/
export declare class ReflectionJsonWriter {
private readonly fields;
constructor(info: PartialMessageInfo);
/**
* Converts the message to a JSON object, based on the field descriptors.
*/
write<T extends object>(message: T, options: JsonWriteOptions): JsonValue;
field(field: FieldInfo, value: unknown, options: JsonWriteOptions): JsonValue | undefined;
/**
* Returns `null` as the default for google.protobuf.NullValue.
*/
enum(type: EnumInfo, value: unknown, fieldName: string, optional: boolean, emitDefaultValues: boolean, enumAsInteger: boolean): JsonValue | undefined;
message(type: IMessageType<any>, value: unknown, fieldName: string, options: JsonWriteOptions): JsonValue | undefined;
scalar(type: ScalarType, value: unknown, fieldName: string, optional: false, emitDefaultValues: boolean): JsonValue;
scalar(type: ScalarType, value: unknown, fieldName: string, optional: boolean, emitDefaultValues: boolean): JsonValue | undefined;
}

View file

@ -0,0 +1,10 @@
import type { PbLong, PbULong } from "./pb-long";
import { LongType } from "./reflection-info";
/**
* Utility method to convert a PbLong or PbUlong to a JavaScript
* representation during runtime.
*
* Works with generated field information, `undefined` is equivalent
* to `STRING`.
*/
export declare function reflectionLongConvert(long: PbLong | PbULong, type: LongType | undefined): string | number | bigint;

View file

@ -0,0 +1,25 @@
import type { MessageInfo } from "./reflection-info";
import type { PartialMessage } from "./message-type-contract";
/**
* Copy partial data into the target message.
*
* If a singular scalar or enum field is present in the source, it
* replaces the field in the target.
*
* If a singular message field is present in the source, it is merged
* with the target field by calling mergePartial() of the responsible
* message type.
*
* If a repeated field is present in the source, its values replace
* all values in the target array, removing extraneous values.
* Repeated message fields are copied, not merged.
*
* If a map field is present in the source, entries are added to the
* target map, replacing entries with the same key. Entries that only
* exist in the target remain. Entries with message values are copied,
* not merged.
*
* Note that this function differs from protobuf merge semantics,
* which appends repeated fields.
*/
export declare function reflectionMergePartial<T extends object>(info: MessageInfo, target: T, source: PartialMessage<T>): void;

View file

@ -0,0 +1,5 @@
import { LongType, ScalarType } from "./reflection-info";
/**
* Creates the default value for a scalar type.
*/
export declare function reflectionScalarDefault(type: ScalarType, longType?: LongType): string | number | bigint | boolean | Uint8Array;

View file

@ -0,0 +1,34 @@
import type { PartialMessageInfo } from "./reflection-info";
export declare class ReflectionTypeCheck {
private readonly fields;
private data;
constructor(info: PartialMessageInfo);
private prepare;
/**
* Is the argument a valid message as specified by the
* reflection information?
*
* Checks all field types recursively. The `depth`
* specifies how deep into the structure the check will be.
*
* With a depth of 0, only the presence of fields
* is checked.
*
* With a depth of 1 or more, the field types are checked.
*
* With a depth of 2 or more, the members of map, repeated
* and message fields are checked.
*
* Message fields will be checked recursively with depth - 1.
*
* The number of map entries / repeated values being checked
* is < depth.
*/
is(message: any, depth: number, allowExcessProperties?: boolean): boolean;
private field;
private message;
private messages;
private scalar;
private scalars;
private mapKeys;
}

View file

@ -0,0 +1,27 @@
/**
* A message of unknown type.
*/
export interface UnknownMessage {
[k: string]: UnknownScalar | UnknownEnum | UnknownMessage | UnknownOneofGroup | UnknownMap | UnknownScalar[] | UnknownMessage[] | UnknownEnum[] | undefined;
}
/**
* A map field of unknown type.
*/
export declare type UnknownMap<T = UnknownMessage | UnknownScalar | UnknownEnum> = {
[key: string]: T;
};
/**
* A scalar field of unknown type.
*/
export declare type UnknownScalar = string | number | bigint | boolean | Uint8Array;
/**
* A enum field of unknown type.
*/
export declare type UnknownEnum = number;
/**
* A unknown oneof group. See `isOneofGroup()` for details.
*/
export declare type UnknownOneofGroup = {
oneofKind: undefined | string;
[k: string]: UnknownScalar | UnknownEnum | UnknownMessage | undefined;
};