From ba54acce696e485e90164beaab4d1f2f4ac3006e Mon Sep 17 00:00:00 2001 From: c43721 Date: Sun, 27 Oct 2024 15:38:29 -0500 Subject: [PATCH 1/2] chore: use node apis --- src/rcon.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/rcon.ts b/src/rcon.ts index 64c11ed..d11f732 100644 --- a/src/rcon.ts +++ b/src/rcon.ts @@ -1,6 +1,7 @@ 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, @@ -34,7 +35,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; @@ -116,15 +117,15 @@ 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, }); From f8a4728e62f9557b984bacb34611a1804481b94b Mon Sep 17 00:00:00 2001 From: c43721 Date: Tue, 29 Oct 2024 17:30:56 -0500 Subject: [PATCH 2/2] feat(node): node compatability --- deno.json | 5 ++++- src/rcon.ts | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/deno.json b/deno.json index 94ff92b..b07ece0 100644 --- a/deno.json +++ b/deno.json @@ -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/"] + } } diff --git a/src/rcon.ts b/src/rcon.ts index d11f732..c9c84aa 100644 --- a/src/rcon.ts +++ b/src/rcon.ts @@ -1,5 +1,4 @@ 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"; @@ -75,7 +74,7 @@ export default class Rcon { */ public async authenticate(password: string): Promise { if (!this.#connected) { - await this.#connect(); + this.#connect(); } const response = await this.#send( @@ -127,6 +126,7 @@ export default class Rcon { this.#connection = createConnection({ host: this.#host, port: this.#port, + timeout: 1000, }); this.#connected = true; @@ -145,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(); @@ -191,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"); } }