@protobuf-ts/runtime
Advanced tools
Comparing version 1.0.0-alpha.24 to 1.0.0-alpha.25
@@ -38,2 +38,6 @@ import { PbLong, PbULong } from "./pb-long"; | ||
* 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. | ||
*/ | ||
@@ -80,2 +84,6 @@ export interface IBinaryReader { | ||
* 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. | ||
*/ | ||
@@ -82,0 +90,0 @@ export interface IBinaryWriter { |
import { IBinaryReader, WireType } from "./binary-format-contract"; | ||
import { PbLong, PbULong } from "./pb-long"; | ||
declare type SplitVarint = [number, number]; | ||
export declare class BinaryReader implements IBinaryReader { | ||
@@ -25,10 +24,7 @@ pos: number; | ||
skipType(wireType: WireType): BinaryReader; | ||
protected varint64(): SplitVarint; | ||
protected goog_older_readSplitVarint64(): SplitVarint; | ||
private protobufjs_readLongVarint; | ||
/** | ||
* Read a `uint32` field, an unsigned 32 bit varint. | ||
*/ | ||
uint32(): number; | ||
protobufjs_uint32(): number; | ||
uint32: () => number; | ||
protected varint64: () => [number, number]; | ||
/** | ||
@@ -100,2 +96,1 @@ * Read a `int32` field, a signed 32 bit varint. | ||
} | ||
export {}; |
@@ -5,4 +5,12 @@ "use strict"; | ||
const pb_long_1 = require("./pb-long"); | ||
const protobufjs_utf8_1 = require("./protobufjs-utf8"); | ||
const protobufjs_varint_1 = require("./protobufjs-varint"); | ||
const goog_varint_1 = require("./goog-varint"); | ||
class BinaryReader { | ||
constructor(buf) { | ||
/** | ||
* Read a `uint32` field, an unsigned 32 bit varint. | ||
*/ | ||
this.uint32 = protobufjs_varint_1.varint32read; | ||
this.varint64 = goog_varint_1.varint64read; | ||
this.buf = buf; | ||
@@ -57,180 +65,3 @@ this.len = buf.length; | ||
} | ||
// TODO re-implement google copy! | ||
// https://github.com/protocolbuffers/protobuf/blob/master/js/experimental/runtime/kernel/buffer_decoder.js#L175 | ||
varint64() { | ||
let lowBits = 0; | ||
let highBits = 0; | ||
for (let shift = 0; shift < 28; shift += 7) { | ||
const b = this.buf[this.pos++]; | ||
lowBits |= (b & 0x7F) << shift; | ||
if ((b & 0x80) === 0) { | ||
return [lowBits, highBits]; | ||
} | ||
} | ||
const 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) { | ||
return [lowBits, highBits]; | ||
} | ||
for (let shift = 3; shift <= 31; shift += 7) { | ||
const b = this.buf[this.pos++]; | ||
highBits |= (b & 0x7F) << shift; | ||
if ((b & 0x80) === 0) { | ||
return [lowBits, highBits]; | ||
} | ||
} | ||
throw new Error('invalid varint'); | ||
} | ||
// TODO cleanup | ||
goog_older_readSplitVarint64() { | ||
var temp = 128; | ||
var lowBits = 0; | ||
var highBits = 0; | ||
// Read the first four bytes of the varint, stopping at the terminator if we | ||
// see it. | ||
for (var i = 0; i < 4 && temp >= 128; i++) { | ||
temp = this.buf[this.pos++]; | ||
lowBits |= (temp & 0x7F) << (i * 7); | ||
} | ||
if (temp >= 128) { | ||
// Read the fifth byte, which straddles the low and high dwords. | ||
temp = this.buf[this.pos++]; | ||
lowBits |= (temp & 0x7F) << 28; | ||
highBits |= (temp & 0x7F) >> 4; | ||
} | ||
if (temp >= 128) { | ||
// Read the sixth through tenth byte. | ||
for (var i = 0; i < 5 && temp >= 128; i++) { | ||
temp = this.buf[this.pos++]; | ||
highBits |= (temp & 0x7F) << (i * 7 + 3); | ||
} | ||
} | ||
if (temp < 128) { | ||
return [lowBits >>> 0, highBits >>> 0]; | ||
} | ||
throw new Error('invalid varint'); | ||
} | ||
// TODO cleanup | ||
protobufjs_readLongVarint() { | ||
// tends to deopt with local vars for octet etc. | ||
let lo = 0, hi = 0; | ||
let i = 0; | ||
if (this.len - this.pos > 4) { // fast route (lo) | ||
for (; i < 4; ++i) { | ||
// 1st..4th | ||
lo = (lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
// 5th | ||
lo = (lo | (this.buf[this.pos] & 127) << 28) >>> 0; | ||
hi = (hi | (this.buf[this.pos] & 127) >> 4) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
i = 0; | ||
} | ||
else { | ||
for (; i < 3; ++i) { | ||
/* istanbul ignore if */ | ||
if (this.pos >= this.len) | ||
throw new RangeError(); | ||
// 1st..3th | ||
lo = (lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
// 4th | ||
lo = (lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0; | ||
return [lo, hi]; | ||
} | ||
if (this.len - this.pos > 4) { // fast route (hi) | ||
for (; i < 5; ++i) { | ||
// 6th..10th | ||
hi = (hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
} | ||
else { | ||
for (; i < 5; ++i) { | ||
/* istanbul ignore if */ | ||
if (this.pos >= this.len) | ||
throw new RangeError(); | ||
// 6th..10th | ||
hi = (hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
} | ||
/* istanbul ignore next */ | ||
throw Error("invalid varint encoding"); | ||
} | ||
// TODO re-implement google copy! | ||
// TODO this is slightly slower than protobufjs´ | ||
/** | ||
* Read a `uint32` field, an unsigned 32 bit varint. | ||
*/ | ||
uint32() { | ||
let b = this.buf[this.pos++]; | ||
let result = b & 0x7F; | ||
if ((b & 0x80) === 0) { | ||
return result; | ||
} | ||
b = this.buf[this.pos++]; | ||
result |= (b & 0x7F) << 7; | ||
if ((b & 0x80) === 0) { | ||
return result; | ||
} | ||
b = this.buf[this.pos++]; | ||
result |= (b & 0x7F) << 14; | ||
if ((b & 0x80) === 0) { | ||
return result; | ||
} | ||
b = this.buf[this.pos++]; | ||
result |= (b & 0x7F) << 21; | ||
if ((b & 0x80) === 0) { | ||
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'); | ||
} | ||
// Result can have 32 bits, convert it to unsigned | ||
return result >>> 0; | ||
} | ||
// TODO re-implement protobufjs copy! | ||
// TODO this is slightly faster than googles´ | ||
protobufjs_uint32() { | ||
let value = (this.buf[this.pos] & 127) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 127) << 7) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 127) << 14) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 127) << 21) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 15) << 28) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
/* istanbul ignore if */ | ||
if ((this.pos += 5) > this.len) { | ||
this.pos = this.len; | ||
// throw indexOutOfRange(this, 10); | ||
throw new RangeError(`index out of range: ?? > ${this.len}`); | ||
} | ||
return value; | ||
} | ||
/** | ||
* Read a `int32` field, a signed 32 bit varint. | ||
@@ -349,46 +180,6 @@ */ | ||
let bytes = this.bytes(); | ||
return protobufjs_utf8_read(bytes, 0, bytes.length); | ||
return protobufjs_utf8_1.utf8read(bytes); | ||
} | ||
} | ||
exports.BinaryReader = BinaryReader; | ||
// TODO doch übernehmen? | ||
// TODO dann aber TextDecoder aus options ersetzen durch z.B. utf8decodeFn | ||
/** | ||
* Reads UTF8 bytes as a string. | ||
* @param {Uint8Array} buffer Source buffer | ||
* @param {number} start Source start | ||
* @param {number} end Source end | ||
* @returns {string} String read | ||
*/ | ||
function protobufjs_utf8_read(buffer, start, end) { | ||
let len = end - start; | ||
if (len < 1) | ||
return ""; | ||
let parts = [], chunk = [], i = 0, // char offset | ||
t; // temporary | ||
while (start < end) { | ||
t = buffer[start++]; | ||
if (t < 128) | ||
chunk[i++] = t; | ||
else if (t > 191 && t < 224) | ||
chunk[i++] = (t & 31) << 6 | buffer[start++] & 63; | ||
else if (t > 239 && t < 365) { | ||
t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000; | ||
chunk[i++] = 0xD800 + (t >> 10); | ||
chunk[i++] = 0xDC00 + (t & 1023); | ||
} | ||
else | ||
chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63; | ||
if (i > 8191) { | ||
parts.push(String.fromCharCode.apply(String, chunk)); | ||
i = 0; | ||
} | ||
} | ||
if (parts.length) { | ||
if (i) | ||
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i))); | ||
return parts.join(""); | ||
} | ||
return String.fromCharCode.apply(String, chunk.slice(0, i)); | ||
} | ||
//# sourceMappingURL=binary-reader.js.map |
@@ -18,3 +18,3 @@ import { IBinaryWriter, WireType } from "./binary-format-contract"; | ||
*/ | ||
private buf; | ||
protected buf: number[]; | ||
/** | ||
@@ -34,3 +34,3 @@ * Previous fork states. | ||
*/ | ||
private push; | ||
protected push(chunk: Uint8Array): void; | ||
/** | ||
@@ -75,3 +75,2 @@ * Return all bytes written and reset this writer. | ||
uint64(value: string | number | bigint): IBinaryWriter; | ||
protected varint64(lo: number, hi: number): void; | ||
} |
@@ -5,2 +5,3 @@ "use strict"; | ||
const pb_long_1 = require("./pb-long"); | ||
const goog_varint_1 = require("./goog-varint"); | ||
const MAX_UINT32 = Math.pow(2, 32) - 1; | ||
@@ -168,4 +169,2 @@ class BinaryWriter { | ||
return this.uint32(value); | ||
// TODO https://github.com/protocolbuffers/protobuf/blob/master/js/binary/encoder.js#L144 | ||
// Write nine bytes with a _signed_ right shift so we preserve the sign bit. | ||
for (let i = 0; i < 9; i++) { | ||
@@ -180,3 +179,3 @@ this.buf.push(value & 127 | 128); | ||
let long = pb_long_1.PbLong.from(value); | ||
this.varint64(long.lo, long.hi); | ||
goog_varint_1.varint64write(long.lo, long.hi, this.buf); | ||
return this; | ||
@@ -188,3 +187,3 @@ } | ||
sign = long.hi >> 31, lo = (long.lo << 1) ^ sign, hi = ((long.hi << 1) | (long.lo >>> 31)) ^ sign; | ||
this.varint64(lo, hi); | ||
goog_varint_1.varint64write(lo, hi, this.buf); | ||
return this; | ||
@@ -194,36 +193,7 @@ } | ||
let long = pb_long_1.PbULong.from(value); | ||
this.varint64(long.lo, long.hi); | ||
goog_varint_1.varint64write(long.lo, long.hi, this.buf); | ||
return this; | ||
} | ||
// TODO re-implement google ! | ||
// https://github.com/protocolbuffers/protobuf/blob/master/js/experimental/runtime/kernel/writer.js#L344 | ||
varint64(lo, hi) { | ||
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; | ||
this.buf.push(byte); | ||
if (!hasNext) { | ||
return; | ||
} | ||
} | ||
const splitBits = ((lo >>> 28) & 0x0F) | ((hi & 0x07) << 4); | ||
const hasMoreBits = !((hi >> 3) === 0); | ||
this.buf.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; | ||
this.buf.push(byte); | ||
if (!hasNext) { | ||
return; | ||
} | ||
} | ||
this.buf.push((hi >>> 31) & 0x01); | ||
} | ||
} | ||
exports.BinaryWriter = BinaryWriter; | ||
//# sourceMappingURL=binary-writer.js.map |
@@ -38,2 +38,6 @@ import { PbLong, PbULong } from "./pb-long"; | ||
* 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. | ||
*/ | ||
@@ -80,2 +84,6 @@ export interface IBinaryReader { | ||
* 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. | ||
*/ | ||
@@ -82,0 +90,0 @@ export interface IBinaryWriter { |
import { IBinaryReader, WireType } from "./binary-format-contract"; | ||
import { PbLong, PbULong } from "./pb-long"; | ||
declare type SplitVarint = [number, number]; | ||
export declare class BinaryReader implements IBinaryReader { | ||
@@ -25,10 +24,7 @@ pos: number; | ||
skipType(wireType: WireType): BinaryReader; | ||
protected varint64(): SplitVarint; | ||
protected goog_older_readSplitVarint64(): SplitVarint; | ||
private protobufjs_readLongVarint; | ||
/** | ||
* Read a `uint32` field, an unsigned 32 bit varint. | ||
*/ | ||
uint32(): number; | ||
protobufjs_uint32(): number; | ||
uint32: () => number; | ||
protected varint64: () => [number, number]; | ||
/** | ||
@@ -100,2 +96,1 @@ * Read a `int32` field, a signed 32 bit varint. | ||
} | ||
export {}; |
import { PbLong, PbULong } from "./pb-long"; | ||
import { utf8read } from "./protobufjs-utf8"; | ||
import { varint32read } from "./protobufjs-varint"; | ||
import { varint64read } from "./goog-varint"; | ||
export class BinaryReader { | ||
constructor(buf) { | ||
/** | ||
* Read a `uint32` field, an unsigned 32 bit varint. | ||
*/ | ||
this.uint32 = varint32read; | ||
this.varint64 = varint64read; | ||
this.buf = buf; | ||
@@ -53,180 +61,3 @@ this.len = buf.length; | ||
} | ||
// TODO re-implement google copy! | ||
// https://github.com/protocolbuffers/protobuf/blob/master/js/experimental/runtime/kernel/buffer_decoder.js#L175 | ||
varint64() { | ||
let lowBits = 0; | ||
let highBits = 0; | ||
for (let shift = 0; shift < 28; shift += 7) { | ||
const b = this.buf[this.pos++]; | ||
lowBits |= (b & 0x7F) << shift; | ||
if ((b & 0x80) === 0) { | ||
return [lowBits, highBits]; | ||
} | ||
} | ||
const 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) { | ||
return [lowBits, highBits]; | ||
} | ||
for (let shift = 3; shift <= 31; shift += 7) { | ||
const b = this.buf[this.pos++]; | ||
highBits |= (b & 0x7F) << shift; | ||
if ((b & 0x80) === 0) { | ||
return [lowBits, highBits]; | ||
} | ||
} | ||
throw new Error('invalid varint'); | ||
} | ||
// TODO cleanup | ||
goog_older_readSplitVarint64() { | ||
var temp = 128; | ||
var lowBits = 0; | ||
var highBits = 0; | ||
// Read the first four bytes of the varint, stopping at the terminator if we | ||
// see it. | ||
for (var i = 0; i < 4 && temp >= 128; i++) { | ||
temp = this.buf[this.pos++]; | ||
lowBits |= (temp & 0x7F) << (i * 7); | ||
} | ||
if (temp >= 128) { | ||
// Read the fifth byte, which straddles the low and high dwords. | ||
temp = this.buf[this.pos++]; | ||
lowBits |= (temp & 0x7F) << 28; | ||
highBits |= (temp & 0x7F) >> 4; | ||
} | ||
if (temp >= 128) { | ||
// Read the sixth through tenth byte. | ||
for (var i = 0; i < 5 && temp >= 128; i++) { | ||
temp = this.buf[this.pos++]; | ||
highBits |= (temp & 0x7F) << (i * 7 + 3); | ||
} | ||
} | ||
if (temp < 128) { | ||
return [lowBits >>> 0, highBits >>> 0]; | ||
} | ||
throw new Error('invalid varint'); | ||
} | ||
// TODO cleanup | ||
protobufjs_readLongVarint() { | ||
// tends to deopt with local vars for octet etc. | ||
let lo = 0, hi = 0; | ||
let i = 0; | ||
if (this.len - this.pos > 4) { // fast route (lo) | ||
for (; i < 4; ++i) { | ||
// 1st..4th | ||
lo = (lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
// 5th | ||
lo = (lo | (this.buf[this.pos] & 127) << 28) >>> 0; | ||
hi = (hi | (this.buf[this.pos] & 127) >> 4) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
i = 0; | ||
} | ||
else { | ||
for (; i < 3; ++i) { | ||
/* istanbul ignore if */ | ||
if (this.pos >= this.len) | ||
throw new RangeError(); | ||
// 1st..3th | ||
lo = (lo | (this.buf[this.pos] & 127) << i * 7) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
// 4th | ||
lo = (lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0; | ||
return [lo, hi]; | ||
} | ||
if (this.len - this.pos > 4) { // fast route (hi) | ||
for (; i < 5; ++i) { | ||
// 6th..10th | ||
hi = (hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
} | ||
else { | ||
for (; i < 5; ++i) { | ||
/* istanbul ignore if */ | ||
if (this.pos >= this.len) | ||
throw new RangeError(); | ||
// 6th..10th | ||
hi = (hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return [lo, hi]; | ||
} | ||
} | ||
/* istanbul ignore next */ | ||
throw Error("invalid varint encoding"); | ||
} | ||
// TODO re-implement google copy! | ||
// TODO this is slightly slower than protobufjs´ | ||
/** | ||
* Read a `uint32` field, an unsigned 32 bit varint. | ||
*/ | ||
uint32() { | ||
let b = this.buf[this.pos++]; | ||
let result = b & 0x7F; | ||
if ((b & 0x80) === 0) { | ||
return result; | ||
} | ||
b = this.buf[this.pos++]; | ||
result |= (b & 0x7F) << 7; | ||
if ((b & 0x80) === 0) { | ||
return result; | ||
} | ||
b = this.buf[this.pos++]; | ||
result |= (b & 0x7F) << 14; | ||
if ((b & 0x80) === 0) { | ||
return result; | ||
} | ||
b = this.buf[this.pos++]; | ||
result |= (b & 0x7F) << 21; | ||
if ((b & 0x80) === 0) { | ||
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'); | ||
} | ||
// Result can have 32 bits, convert it to unsigned | ||
return result >>> 0; | ||
} | ||
// TODO re-implement protobufjs copy! | ||
// TODO this is slightly faster than googles´ | ||
protobufjs_uint32() { | ||
let value = (this.buf[this.pos] & 127) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 127) << 7) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 127) << 14) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 127) << 21) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
value = (value | (this.buf[this.pos] & 15) << 28) >>> 0; | ||
if (this.buf[this.pos++] < 128) | ||
return value; | ||
/* istanbul ignore if */ | ||
if ((this.pos += 5) > this.len) { | ||
this.pos = this.len; | ||
// throw indexOutOfRange(this, 10); | ||
throw new RangeError(`index out of range: ?? > ${this.len}`); | ||
} | ||
return value; | ||
} | ||
/** | ||
* Read a `int32` field, a signed 32 bit varint. | ||
@@ -345,45 +176,5 @@ */ | ||
let bytes = this.bytes(); | ||
return protobufjs_utf8_read(bytes, 0, bytes.length); | ||
return utf8read(bytes); | ||
} | ||
} | ||
// TODO doch übernehmen? | ||
// TODO dann aber TextDecoder aus options ersetzen durch z.B. utf8decodeFn | ||
/** | ||
* Reads UTF8 bytes as a string. | ||
* @param {Uint8Array} buffer Source buffer | ||
* @param {number} start Source start | ||
* @param {number} end Source end | ||
* @returns {string} String read | ||
*/ | ||
function protobufjs_utf8_read(buffer, start, end) { | ||
let len = end - start; | ||
if (len < 1) | ||
return ""; | ||
let parts = [], chunk = [], i = 0, // char offset | ||
t; // temporary | ||
while (start < end) { | ||
t = buffer[start++]; | ||
if (t < 128) | ||
chunk[i++] = t; | ||
else if (t > 191 && t < 224) | ||
chunk[i++] = (t & 31) << 6 | buffer[start++] & 63; | ||
else if (t > 239 && t < 365) { | ||
t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000; | ||
chunk[i++] = 0xD800 + (t >> 10); | ||
chunk[i++] = 0xDC00 + (t & 1023); | ||
} | ||
else | ||
chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63; | ||
if (i > 8191) { | ||
parts.push(String.fromCharCode.apply(String, chunk)); | ||
i = 0; | ||
} | ||
} | ||
if (parts.length) { | ||
if (i) | ||
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i))); | ||
return parts.join(""); | ||
} | ||
return String.fromCharCode.apply(String, chunk.slice(0, i)); | ||
} | ||
//# sourceMappingURL=binary-reader.js.map |
@@ -18,3 +18,3 @@ import { IBinaryWriter, WireType } from "./binary-format-contract"; | ||
*/ | ||
private buf; | ||
protected buf: number[]; | ||
/** | ||
@@ -34,3 +34,3 @@ * Previous fork states. | ||
*/ | ||
private push; | ||
protected push(chunk: Uint8Array): void; | ||
/** | ||
@@ -75,3 +75,2 @@ * Return all bytes written and reset this writer. | ||
uint64(value: string | number | bigint): IBinaryWriter; | ||
protected varint64(lo: number, hi: number): void; | ||
} |
import { PbLong, PbULong } from "./pb-long"; | ||
import { varint64write } from "./goog-varint"; | ||
const MAX_UINT32 = Math.pow(2, 32) - 1; | ||
@@ -164,4 +165,2 @@ export class BinaryWriter { | ||
return this.uint32(value); | ||
// TODO https://github.com/protocolbuffers/protobuf/blob/master/js/binary/encoder.js#L144 | ||
// Write nine bytes with a _signed_ right shift so we preserve the sign bit. | ||
for (let i = 0; i < 9; i++) { | ||
@@ -176,3 +175,3 @@ this.buf.push(value & 127 | 128); | ||
let long = PbLong.from(value); | ||
this.varint64(long.lo, long.hi); | ||
varint64write(long.lo, long.hi, this.buf); | ||
return this; | ||
@@ -184,3 +183,3 @@ } | ||
sign = long.hi >> 31, lo = (long.lo << 1) ^ sign, hi = ((long.hi << 1) | (long.lo >>> 31)) ^ sign; | ||
this.varint64(lo, hi); | ||
varint64write(lo, hi, this.buf); | ||
return this; | ||
@@ -190,35 +189,6 @@ } | ||
let long = PbULong.from(value); | ||
this.varint64(long.lo, long.hi); | ||
varint64write(long.lo, long.hi, this.buf); | ||
return this; | ||
} | ||
// TODO re-implement google ! | ||
// https://github.com/protocolbuffers/protobuf/blob/master/js/experimental/runtime/kernel/writer.js#L344 | ||
varint64(lo, hi) { | ||
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; | ||
this.buf.push(byte); | ||
if (!hasNext) { | ||
return; | ||
} | ||
} | ||
const splitBits = ((lo >>> 28) & 0x0F) | ((hi & 0x07) << 4); | ||
const hasMoreBits = !((hi >> 3) === 0); | ||
this.buf.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; | ||
this.buf.push(byte); | ||
if (!hasNext) { | ||
return; | ||
} | ||
} | ||
this.buf.push((hi >>> 31) & 0x01); | ||
} | ||
} | ||
//# sourceMappingURL=binary-writer.js.map |
{ | ||
"name": "@protobuf-ts/runtime", | ||
"version": "1.0.0-alpha.24", | ||
"version": "1.0.0-alpha.25", | ||
"description": "Runtime library for code generated by the protobuf-ts plugin", | ||
"license": "Apache-2.0", | ||
"license": "(Apache-2.0 AND BSD-3-Clause)", | ||
"author": "Timo Stamm <ts@timostamm.com>", | ||
@@ -38,3 +38,3 @@ "homepage": "https://github.com/timostamm/protobuf-ts", | ||
}, | ||
"gitHead": "742de29442d1cb7c936eff5c8bab7a325674d504" | ||
"gitHead": "6e04d58f5bf2a78568b23cc8d23d5a035059156c" | ||
} |
@@ -15,1 +15,10 @@ @protobuf-ts/runtime | ||
For a quick overview of `protobuf-ts`, check the repository [README](https://github.com/timostamm/protobuf-ts/README.md). | ||
### Copyright | ||
- The [algorithm to decode UTF8](https://github.com/timostamm/protobuf-ts/packages/runtime/src/protobufjs-utf8.ts) is Copyright 2016 by Daniel Wirtz, licensed under BSD-3-Clause. | ||
- The [algorithm to decode 32 bit varint](https://github.com/timostamm/protobuf-ts/packages/runtime/src/protobufjs-varint.ts) is Copyright 2016 by Daniel Wirtz, licensed under BSD-3-Clause. | ||
- The [algorithm to encode and decode 64 bit varint](https://github.com/timostamm/protobuf-ts/packages/runtime/src/goog-varint.ts) is Copyright 2008 Google Inc., licensed under BSD-3-Clause. | ||
- All other files are licensed under Apache-2.0, see [LICENSE](https://github.com/timostamm/protobuf-ts/packages/runtime/LICENSE). | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
527392
159
8914
24
1