From b2b8ad2372fe68dc414064630f98b18451e5c76c Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Thu, 18 Feb 2016 19:50:40 +0100 Subject: [PATCH] update to new protodef --- package.json | 2 +- src/transforms/framing.js | 29 +++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index b6b905f..2632d12 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/transforms/framing.js b/src/transforms/framing.js index 9b73a0d..4851d8b 100644 --- a/src/transforms/framing.js +++ b/src/transforms/framing.js @@ -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();