Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/datatypes/minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 17 additions & 15 deletions src/transforms/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
}
}
Loading