EnriqCG / rcon-srcds

A zero-dependency Typescript library for the Source/Minecraft RCON Protocol
https://www.npmjs.com/package/rcon-srcds
MIT License
61 stars 20 forks source link

Fix types and response not resolving with the server's response #10

Closed c43721 closed 3 years ago

c43721 commented 3 years ago

This PR fixes an issue authenticate not building and the response never resolving the server's response.

//rcon.ts
 this.write(protocol.SERVERDATA_AUTH, protocol.ID_AUTH, password)
                .then((data) => {
                    if (data === true) {
                        this.authenticated =  true
                        resolve() //Returns undefined upon successful auth!
                    } else {

The issue with the types is that when resolve() was called, it would return void, which you couldn't test if it was authenticated. To fix this, we return resolve(true), returning that the server was authenticated.

//rcon.ts
 this.write(protocol.SERVERDATA_AUTH, protocol.ID_AUTH, password)
                .then((data) => {
                    if (data === true) {
                        this.authenticated =  true
                        resolve(true) //Returns true upon successful auth
                    } else {
//rcon.ts
 response = response.concat(decodedPacket.body.replace(/\n$/, '\n')) // remove last line break
                    if (response.includes(`command "${body}"`)) { // Never returns true, the response will not have command prefixed
                        this.connection.removeListener('data', onData)
                        resolve(response)
                    }

The response never resolving was an issue with checking if the string response contained the prefixed word command. This is no longer the case as it seems with my testing, and just returns the raw response. To fix this, we just check if there is a response, and to go ahead with the resolving of the response.

Also, added a comment to explain why we're doing this check so it makes sense why it's being used.

//rcon.ts
 response = response.concat(decodedPacket.body.replace(/\n$/, '\n')) // remove last line break
                    if (response) { // Will resove if there's a response length
                        this.connection.removeListener('data', onData)
                        resolve(response)
                    }

I will be testing this actively as I rely on this package to do all my rcon communication, and since it'll be written in typescript, types are a must.