update to new protodef

This commit is contained in:
Romain Beaumont 2016-02-18 19:50:40 +01:00
parent 4806e48aff
commit b2b8ad2372
2 changed files with 24 additions and 7 deletions

View file

@ -45,7 +45,7 @@
"buffer-equal": "1.0.0",
"minecraft-data": "^0.19.1",
"prismarine-nbt": "0.2.0",
"protodef": "0.2.5",
"protodef": "0.3.0",
"readable-stream": "^2.0.5",
"ursa-purejs": "0.0.3",
"uuid-1345": "^0.99.6",

View file

@ -1,4 +1,5 @@
const [readVarInt, writeVarInt, sizeOfVarInt] = require("protodef").types.varint;
const {PartialReadError} = require("protodef").utils;
const Transform = require("readable-stream").Transform;
module.exports.createSplitter = function() {
@ -47,13 +48,29 @@ class Splitter extends Transform {
}
let offset = 0;
let { value, size, error } = readVarInt(this.buffer, offset) || { error: "Not enough data" };
while (!error && this.buffer.length >= offset + size + value)
let value, size, error;
try {
({ value, size, error } = readVarInt(this.buffer, offset));
}
catch(e) {
if(!(e instanceof PartialReadError)) {
throw e;
}
}
while (this.buffer.length >= offset + size + value)
{
this.push(this.buffer.slice(offset + size, offset + size + value));
offset += size + value;
({ value, size, error } = readVarInt(this.buffer, offset) || { error: "Not enough data" });
try {
this.push(this.buffer.slice(offset + size, offset + size + value));
offset += size + value;
({value, size, error} = readVarInt(this.buffer, offset));
}
catch(e) {
if(e instanceof PartialReadError) {
break;
}
else
throw e;
}
}
this.buffer = this.buffer.slice(offset);
return cb();