Comparing version 0.1.8 to 0.1.9
{ | ||
"name": "tsbuffer", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,4 +22,3 @@ "use strict"; | ||
return this._reader.readString(); | ||
case 'Array': | ||
case 'Tuple': | ||
case 'Array': { | ||
var output = []; | ||
@@ -29,6 +28,44 @@ // 数组长度:Varint | ||
for (var i = 0; i < length_1; ++i) { | ||
var item = this._read(schema.type === 'Array' ? schema.elementType : schema.elementTypes[i]); | ||
var item = this._read(schema.elementType); | ||
output.push(item); | ||
} | ||
return output; | ||
} | ||
case 'Tuple': { | ||
if (schema.elementTypes.length > 64) { | ||
throw new Error('Elements oversized, maximum supported tuple elements is 64, now get ' + schema.elementTypes.length); | ||
} | ||
var output = []; | ||
// PayloadMask: Varint64 | ||
var payloadMask = this._reader.readVarint(); | ||
// 计算maskIndices | ||
var maskIndices = []; | ||
// Low | ||
for (var i = 0; i < 32; ++i) { | ||
if (payloadMask.uint32s[1] & 1 << i) { | ||
maskIndices.push(i); | ||
} | ||
} | ||
// High | ||
for (var i = 0; i < 32; ++i) { | ||
if (payloadMask.uint32s[0] & 1 << i) { | ||
maskIndices.push(i + 32); | ||
} | ||
} | ||
if (!maskIndices.length) { | ||
return []; | ||
} | ||
var maxIndex = maskIndices.last(); | ||
for (var i = 0, nextMaskIndex = 0, next = maskIndices[0]; i <= maxIndex; ++i) { | ||
if (i === next) { | ||
output[i] = this._read(schema.elementTypes[i]); | ||
++nextMaskIndex; | ||
next = maskIndices[nextMaskIndex]; | ||
} | ||
else { | ||
output[i] = undefined; | ||
} | ||
} | ||
return output; | ||
} | ||
case 'Enum': | ||
@@ -38,3 +75,3 @@ var enumId_1 = this._reader.readVarint().toNumber(); | ||
if (!enumItem) { | ||
throw new Error("Error enum encoding: unexpected id " + enumId_1); | ||
throw new Error("Invalid enum encoding: unexpected id " + enumId_1); | ||
} | ||
@@ -128,3 +165,3 @@ return enumItem.value; | ||
if (!flatSchema.indexSignature) { | ||
throw new Error("Error interface encoding: unexpected indexSignature at block" + i); | ||
throw new Error("Invalid interface encoding: unexpected indexSignature at block" + i); | ||
} | ||
@@ -138,3 +175,3 @@ output[fieldName] = this_1._read(flatSchema.indexSignature.type); | ||
if (!extend) { | ||
throw new Error("Error interface encoding: unexpected extendId " + extendId_1); | ||
throw new Error("Invalid interface encoding: unexpected extendId " + extendId_1); | ||
} | ||
@@ -149,3 +186,3 @@ var extendValue = this_1._read(extend.type); | ||
if (!property) { | ||
throw new Error("Error interface encoding: unexpected propertyId " + propertyId_1); | ||
throw new Error("Invalid interface encoding: unexpected propertyId " + propertyId_1); | ||
} | ||
@@ -186,3 +223,3 @@ output[property.name] = this_1._read(property.type); | ||
if (!member) { | ||
throw new Error("Error " + schema.type + " encoding: invalid member id " + id); | ||
throw new Error("Invalid " + schema.type + " encoding: invalid member id " + id); | ||
} | ||
@@ -189,0 +226,0 @@ var value = this_2._read(member.type); |
@@ -28,4 +28,3 @@ "use strict"; | ||
break; | ||
case 'Array': | ||
case 'Tuple': | ||
case 'Array': { | ||
var _v = value; | ||
@@ -36,5 +35,38 @@ // 数组长度:Varint | ||
for (var i = 0; i < _v.length; ++i) { | ||
this._write(_v[i], schema.type === 'Array' ? schema.elementType : schema.elementTypes[i]); | ||
this._write(_v[i], schema.elementType); | ||
} | ||
break; | ||
} | ||
case 'Tuple': { | ||
if (schema.elementTypes.length > 64) { | ||
throw new Error('Elements oversized, maximum supported tuple elements is 64, now get ' + schema.elementTypes.length); | ||
} | ||
var _v = value; | ||
// 计算maskPos(要编码的值的index) | ||
var maskIndices = []; | ||
for (var i = 0; i < _v.length; ++i) { | ||
if (_v[i] !== undefined) { | ||
maskIndices.push(i); | ||
} | ||
} | ||
// 生成PayloadMask:Varint64 | ||
var lo = 0; | ||
var hi = 0; | ||
for (var _i = 0, maskIndices_1 = maskIndices; _i < maskIndices_1.length; _i++) { | ||
var v = maskIndices_1[_i]; | ||
if (v < 32) { | ||
lo |= 1 << v; | ||
} | ||
else { | ||
hi |= 1 << v - 32; | ||
} | ||
} | ||
this._writer.push({ type: 'varint', value: new Varint64_1.Varint64(hi, lo) }); | ||
// Element Payload | ||
for (var _a = 0, maskIndices_2 = maskIndices; _a < maskIndices_2.length; _a++) { | ||
var i = maskIndices_2[_a]; | ||
this._write(_v[i], schema.elementTypes[i]); | ||
} | ||
break; | ||
} | ||
case 'Enum': | ||
@@ -41,0 +73,0 @@ var enumItem = schema.members.find(function (v) { return v.value === value; }); |
export declare class Varint64 { | ||
private _uint32s; | ||
uint32s: Uint32Array; | ||
constructor(high: number, low: number, byteLength?: number); | ||
@@ -4,0 +4,0 @@ static readonly Zero: Varint64; |
@@ -5,3 +5,3 @@ "use strict"; | ||
function Varint64(high, low, byteLength) { | ||
this._uint32s = new Uint32Array([high, low]); | ||
this.uint32s = new Uint32Array([high, low]); | ||
if (byteLength !== undefined) { | ||
@@ -32,4 +32,4 @@ this._byteLength = byteLength; | ||
Varint64.prototype.toNumber = function (unsigned) { | ||
if (!unsigned && this._uint32s[0] >>> 31) { | ||
var low = ~this._uint32s[1] + 1 >>> 0, high = ~this._uint32s[0] >>> 0; | ||
if (!unsigned && this.uint32s[0] >>> 31) { | ||
var low = ~this.uint32s[1] + 1 >>> 0, high = ~this.uint32s[0] >>> 0; | ||
if (!low) | ||
@@ -39,14 +39,14 @@ high = high + 1 >>> 0; | ||
} | ||
return this._uint32s[1] + this._uint32s[0] * 4294967296; | ||
return this.uint32s[1] + this.uint32s[0] * 4294967296; | ||
}; | ||
Varint64.prototype.zzEncode = function () { | ||
var mask = this._uint32s[0] >> 31; | ||
this._uint32s[0] = ((this._uint32s[0] << 1 | this._uint32s[1] >>> 31) ^ mask) >>> 0; | ||
this._uint32s[1] = (this._uint32s[1] << 1 ^ mask) >>> 0; | ||
var mask = this.uint32s[0] >> 31; | ||
this.uint32s[0] = ((this.uint32s[0] << 1 | this.uint32s[1] >>> 31) ^ mask) >>> 0; | ||
this.uint32s[1] = (this.uint32s[1] << 1 ^ mask) >>> 0; | ||
return this; | ||
}; | ||
Varint64.prototype.zzDecode = function () { | ||
var mask = -(this._uint32s[1] & 1); | ||
this._uint32s[1] = ((this._uint32s[1] >>> 1 | this._uint32s[0] << 31) ^ mask) >>> 0; | ||
this._uint32s[0] = (this._uint32s[0] >>> 1 ^ mask) >>> 0; | ||
var mask = -(this.uint32s[1] & 1); | ||
this.uint32s[1] = ((this.uint32s[1] >>> 1 | this.uint32s[0] << 31) ^ mask) >>> 0; | ||
this.uint32s[0] = (this.uint32s[0] >>> 1 ^ mask) >>> 0; | ||
return this; | ||
@@ -57,3 +57,3 @@ }; | ||
if (this._byteLength === undefined) { | ||
var part0 = this._uint32s[1], part1 = (this._uint32s[1] >>> 28 | this._uint32s[0] << 4) >>> 0, part2 = this._uint32s[0] >>> 24; | ||
var part0 = this.uint32s[1], part1 = (this.uint32s[1] >>> 28 | this.uint32s[0] << 4) >>> 0, part2 = this.uint32s[0] >>> 24; | ||
this._byteLength = part2 === 0 | ||
@@ -81,12 +81,12 @@ ? part1 === 0 | ||
Varint64.prototype.writeToBuffer = function (buf, pos) { | ||
while (this._uint32s[0]) { | ||
buf[pos++] = this._uint32s[1] & 127 | 128; | ||
this._uint32s[1] = (this._uint32s[1] >>> 7 | this._uint32s[0] << 25) >>> 0; | ||
this._uint32s[0] >>>= 7; | ||
while (this.uint32s[0]) { | ||
buf[pos++] = this.uint32s[1] & 127 | 128; | ||
this.uint32s[1] = (this.uint32s[1] >>> 7 | this.uint32s[0] << 25) >>> 0; | ||
this.uint32s[0] >>>= 7; | ||
} | ||
while (this._uint32s[1] > 127) { | ||
buf[pos++] = this._uint32s[1] & 127 | 128; | ||
this._uint32s[1] = this._uint32s[1] >>> 7; | ||
while (this.uint32s[1] > 127) { | ||
buf[pos++] = this.uint32s[1] & 127 | 128; | ||
this.uint32s[1] = this.uint32s[1] >>> 7; | ||
} | ||
buf[pos++] = this._uint32s[1]; | ||
buf[pos++] = this.uint32s[1]; | ||
return pos; | ||
@@ -93,0 +93,0 @@ }; |
@@ -43,3 +43,11 @@ "use strict"; | ||
} | ||
var value = this._decoder.decode(buf, schema); | ||
var value; | ||
try { | ||
value = this._decoder.decode(buf, schema); | ||
} | ||
catch (e) { | ||
var err = new Error('Invalid buffer encoding'); | ||
err.encodingError = e; | ||
throw err; | ||
} | ||
if (!options || !options.skipValidate) { | ||
@@ -46,0 +54,0 @@ var vRes = this._validator.validateBySchema(value, schema); |
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
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
65795
1342