From f8a4728e62f9557b984bacb34611a1804481b94b Mon Sep 17 00:00:00 2001 From: c43721 Date: Tue, 29 Oct 2024 17:30:56 -0500 Subject: [PATCH] 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"); } }