diff --git a/src/datatypes/minecraft.js b/src/datatypes/minecraft.js index 83ab50e8..1b616186 100644 --- a/src/datatypes/minecraft.js +++ b/src/datatypes/minecraft.js @@ -56,7 +56,12 @@ function readCompressedNbt (buffer, offset) { const compressedNbt = buffer.slice(offset + 2, offset + 2 + length) - const nbtBuffer = zlib.gunzipSync(compressedNbt) // TODO: async + let nbtBuffer + try { + nbtBuffer = zlib.gunzipSync(compressedNbt) // TODO: async + } catch (err) { + throw new PartialReadError('zlib decompress failed: ' + err.message) + } const results = nbt.proto.read(nbtBuffer, 0, 'nbt') return { diff --git a/src/transforms/compression.js b/src/transforms/compression.js index 45b06352..718e7fed 100644 --- a/src/transforms/compression.js +++ b/src/transforms/compression.js @@ -20,14 +20,16 @@ class Compressor extends Transform { _transform (chunk, enc, cb) { if (chunk.length >= this.compressionThreshold) { - zlib.deflate(chunk, (err, newChunk) => { - if (err) { return cb(err) } + try { + const newChunk = zlib.deflateSync(chunk) const buf = Buffer.alloc(sizeOfVarInt(chunk.length) + newChunk.length) const offset = writeVarInt(chunk.length, buf, 0) newChunk.copy(buf, offset) this.push(buf) return cb() - }) + } catch (err) { + return cb(err) + } } else { const buf = Buffer.alloc(sizeOfVarInt(0) + chunk.length) const offset = writeVarInt(0, buf, 0) @@ -52,23 +54,23 @@ class Decompressor extends Transform { this.push(chunk.slice(size)) return cb() } else { - zlib.unzip(chunk.slice(size), { finishFlush: 2 /* Z_SYNC_FLUSH = 2, but when using Browserify/Webpack it doesn't exist */ }, (err, newBuf) => { /** Fix by lefela4. */ - if (err) { - if (!this.hideErrors) { - console.error('problem inflating chunk') - console.error('uncompressed length ' + value) - console.error('compressed length ' + chunk.length) - console.error('hex ' + chunk.toString('hex')) - console.log(err) - } - return cb() - } + try { + const newBuf = zlib.unzipSync(chunk.slice(size), { finishFlush: 2 }) if (newBuf.length !== value && !this.hideErrors) { console.error('uncompressed length should be ' + value + ' but is ' + newBuf.length) } this.push(newBuf) return cb() - }) + } catch (err) { + if (!this.hideErrors) { + console.error('problem inflating chunk') + console.error('uncompressed length ' + value) + console.error('compressed length ' + chunk.length) + console.error('hex ' + chunk.toString('hex')) + console.log(err) + } + return cb() + } } } }