Add hideErrors option

fix #431
This commit is contained in:
Romain Beaumont 2018-08-03 21:11:33 +02:00
parent 62d595937b
commit 9cb5810597
No known key found for this signature in database
GPG key ID: DB60E388B3BCF286
6 changed files with 24 additions and 15 deletions

View file

@ -24,6 +24,7 @@ automatically logged in and validated against mojang's auth.
* stream : a stream to use as connection
* connect : a function taking the client as parameter and that should client.setSocket(socket)
and client.emit('connect') when appropriate (see the proxy examples for an example of use)
* hideErrors : do not display errors, default to false
## mc.Server(version,[customPackets])
@ -82,6 +83,7 @@ Returns a `Client` instance and perform login.
* checkTimeoutInterval : default to `30*1000` (30s), check if keepalive received at that period, disconnect otherwise.
* version : 1.8 or 1.9 or false (to auto-negotiate): default to 1.8
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
* hideErrors : do not display errors, default to false
## mc.Client(isServer,version,[customPackets])

View file

@ -11,7 +11,7 @@ const createSerializer = require('./transforms/serializer').createSerializer
const createDeserializer = require('./transforms/serializer').createDeserializer
class Client extends EventEmitter {
constructor (isServer, version, customPackets) {
constructor (isServer, version, customPackets, hideErrors = false) {
super()
this.customPackets = customPackets
this.version = version
@ -25,6 +25,7 @@ class Client extends EventEmitter {
this.decompressor = null
this.ended = true
this.latency = 0
this.hideErrors = hideErrors
this.state = states.HANDSHAKING
}
@ -183,7 +184,7 @@ class Client extends EventEmitter {
this.compressor.on('error', (err) => this.emit('error', err))
this.serializer.unpipe(this.framer)
this.serializer.pipe(this.compressor).pipe(this.framer)
this.decompressor = compression.createDecompressor(threshold)
this.decompressor = compression.createDecompressor(threshold, this.hideErrors)
this.decompressor.on('error', (err) => this.emit('error', err))
this.splitter.unpipe(this.deserializer)
this.splitter.pipe(this.decompressor).pipe(this.deserializer)

View file

@ -28,8 +28,9 @@ function createClient (options) {
const version = mcData.version
options.majorVersion = version.majorVersion
options.protocolVersion = version.version
const hideErrors = options.hideErrors || false
const client = new Client(false, version.minecraftVersion, options.customPackets)
const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors)
tcpDns(client, options)
auth(client, options)

View file

@ -27,8 +27,9 @@ function createServer (options = {}) {
const mcData = require('minecraft-data')(optVersion)
const mcversion = mcData.version
const hideErrors = options.hideErrors || false
const server = new Server(mcversion.minecraftVersion, customPackets)
const server = new Server(mcversion.minecraftVersion, customPackets, hideErrors)
server.mcversion = mcversion
server.motd = motd
server.maxPlayers = maxPlayers

View file

@ -6,7 +6,7 @@ const Client = require('./client')
const states = require('./states')
class Server extends EventEmitter {
constructor (version, customPackets) {
constructor (version, customPackets, hideErrors = false) {
super()
this.version = version
this.socketServer = null
@ -14,6 +14,7 @@ class Server extends EventEmitter {
this.decipher = null
this.clients = {}
this.customPackets = customPackets
this.hideErrors = hideErrors
}
listen (port, host) {
@ -21,7 +22,7 @@ class Server extends EventEmitter {
let nextId = 0
self.socketServer = net.createServer()
self.socketServer.on('connection', socket => {
const client = new Client(true, this.version, this.customPackets)
const client = new Client(true, this.version, this.customPackets, this.hideErrors)
client._end = client.end
client.end = function end (endReason) {
endReason = '{"text":"' + endReason + '"}'

View file

@ -8,8 +8,8 @@ module.exports.createCompressor = function (threshold) {
return new Compressor(threshold)
}
module.exports.createDecompressor = function (threshold) {
return new Decompressor(threshold)
module.exports.createDecompressor = function (threshold, hideErrors) {
return new Decompressor(threshold, hideErrors)
}
class Compressor extends Transform {
@ -39,9 +39,10 @@ class Compressor extends Transform {
}
class Decompressor extends Transform {
constructor (compressionThreshold = -1) {
constructor (compressionThreshold = -1, hideErrors = false) {
super()
this.compressionThreshold = compressionThreshold
this.hideErrors = hideErrors
}
_transform (chunk, enc, cb) {
@ -53,14 +54,16 @@ class Decompressor extends Transform {
} else {
zlib.inflate(chunk.slice(size), (err, newBuf) => {
if (err) {
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)
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()
}
if (newBuf.length !== value) {
if (newBuf.length !== value && !this.hideErrors) {
console.error('uncompressed length should be ' + value + ' but is ' + newBuf.length)
}
this.push(newBuf)