Skip to content

Commit

Permalink
feat: node compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
c43721 authored Oct 29, 2024
2 parents 7a03ca2 + f8a4728 commit a58b4c3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"@std/io": "jsr:@std/io@^0.225.0"
},
"version": "0.0.2",
"exports": "./mod.ts"
"exports": "./mod.ts",
"publish": {
"include": ["README.md", "mod.ts", "src/"]
}
}
29 changes: 16 additions & 13 deletions src/rcon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import protocol from "./protocol.ts";
import { iterateReader } from "@std/io";
import { concat } from "@std/bytes";
import { createConnection, type Socket } from "node:net";
import { encode, decode } from "./packet.ts";
import {
NotAuthenticatedException,
Expand Down Expand Up @@ -34,7 +34,7 @@ import type { RconOptions } from "./types.ts";
export default class Rcon {
#host: string;
#port: number;
#connection?: Deno.Conn;
#connection?: Socket;
#connected = false;
#authenticated = false;
#maxPacketSize = 4096;
Expand Down Expand Up @@ -74,7 +74,7 @@ export default class Rcon {
*/
public async authenticate(password: string): Promise<boolean> {
if (!this.#connected) {
await this.#connect();
this.#connect();
}

const response = await this.#send(
Expand Down Expand Up @@ -116,16 +116,17 @@ export default class Rcon {
public disconnect() {
this.#authenticated = false;
this.#connected = false;
this.#connection?.close();
this.#connection?.end();
}

/**
* Connects to the SRCDS server
*/
async #connect() {
this.#connection = await Deno.connect({
hostname: this.#host,
#connect() {
this.#connection = createConnection({
host: this.#host,
port: this.#port,
timeout: 1000,
});

this.#connected = true;
Expand All @@ -144,12 +145,16 @@ export default class Rcon {
throw new PacketSizeTooBigException();
}

await this.#connection!.write(encodedPacket);
this.#connection!.write(encodedPacket);

let potentialMultiPacketResponse = new Uint8Array();

for await (const response of iterateReader(this.#connection!)) {
const decodedPacket = decode(response);
const socketIterator = this.#connection![Symbol.asyncIterator]();

while (true) {
const { value } = await socketIterator.next();

const decodedPacket = decode(value);

if (decodedPacket.size < 10) {
throw new UnableToParseResponseException();
Expand Down Expand Up @@ -190,14 +195,12 @@ export default class Rcon {
""
);

await this.#connection!.write(encodedTerminationPacket);
this.#connection!.write(encodedTerminationPacket);
} else if (decodedPacket.size <= 3700) {
// no need to check for ID_TERM here, since this packet will always be < 3700
return new TextDecoder().decode(potentialMultiPacketResponse);
}
}
}

throw new Error("Unreachable");
}
}

0 comments on commit a58b4c3

Please sign in to comment.