From 5b9eb5a535f158b1c203a0ef8de49ee5438307a0 Mon Sep 17 00:00:00 2001 From: woodendoors7 <60623935+woodendoors7@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:32:29 +0100 Subject: [PATCH] Cleanup, bumped version, changed argument names, fixed bugs --- CHANGELOG.md | 13 ++++++++++++- README.md | 17 ++++++++++------- deno.json | 5 +++++ jsr.json | 2 +- package.json | 6 +++--- src/better-varint.ts | 2 ++ src/index.ts | 24 +++++++++++++----------- src/packetDecoder.ts | 4 ++-- src/packetGenerator.ts | 4 +--- src/types.ts | 9 ++++----- tsconfig.json | 1 + 11 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 deno.json diff --git a/CHANGELOG.md b/CHANGELOG.md index f7aa44a..e93ee94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ # Changelog! +

v1.2.0

+ +[NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.2.0) + +- ⚙️ Changed `disableSrv` and `disableJSONParse` to `SRVLookup` and `JSONParse` respectively, changing their default values to true. +- ⚙️ Bumped the default protocol version from `764` (1.20.2) to `767` (1.21.1) +- 🛠️ Changed uncompiled `.js` imports to `.ts`, making it work with Deno. +- 🛠️ Added jsr.json file +- 🛠️ Fixed SRV lookups not being prevented when port wasn't set 25565/was an IP +- ➖ Removed (previously disfunctional) soft block on localhost addresses when looking up SRV. Code will initiate a SRV lookup with any localhost address, unless disabled with `SRVLookup`. +

v1.1.5

[NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.1.5) @@ -40,6 +51,6 @@ [NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.1.0) - 🟢 Added lookup option `disableJSONParse` to completely skip parsing JSON. - - ⚙️ Renamed primary lookup option `hostname` to `host`, `hostname` stays as an alias. + - ⚙️ Renamed primary lookup option `hostname` to `host`, `hostname` stays as an alias. [all versions](https://www.npmjs.com/package/minecraftstatuspinger?activeTab=versions) diff --git a/README.md b/README.md index 30c4388..94935f0 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,9 @@ import mc from "minecraftstatuspinger"; let result = await mc.lookup({ host: "mc.hypixel.net" }) console.log(result); -``` +``` + +

Advanced Example

@@ -48,13 +50,14 @@ let result = await mc.lookup({ protocolVersion: 764, timeout: 10000, throwOnParseError: true, - disableSRV: false, - disableJSONParse: false + SRVLookup: true, + JSONParse: true }) console.log(result); ``` +
## Docs @@ -75,10 +78,10 @@ console.log(result); > Protocol version to send to the server to simulate different Minecraft client versions. Here, you can see the [Protocol Version Numbers](https://wiki.vg/Protocol_version_numbers). The current default protocol version is for 1.20.2 (764) and will be irregularly updated to newer versions. * `throwOnParseError?:` boolean `default: true` > Whether to throw an error if the status packet fails to parse the status field. The `statusRaw` field is always included. - * `disableSrv?:` boolean `default: false` - > Whether to force skip SRV lookups. Useful when only pinging IP addresses and not hostnames (domains). - * `disableJSONParse?:` boolean `default: false` - > Whether to skip JSON parsing. Useful if you only want a raw plaintext response. If true, the `status` field will be undefined. + * `SRVLookup?:` boolean `default: true` + > Whether to perform a SRV lookup on the provided hostname. Set to `true` in order to skip. Useful to disable when you're only looking up the basic DNS records and a server with a specific port. + * `JSONParse?:` boolean `default: true` + > Whether to parse the JSON `status` field. Useful to disable when you only need the raw plaintext response. If false, the `status` field will be null. * ServerStatus * `latency?:` number > The time it takes to receive back a response after sending a small payload to a server, in milliseconds. Will be null if the `ping` option is false. diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..104c32f --- /dev/null +++ b/deno.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "strictNullChecks": false + } +} \ No newline at end of file diff --git a/jsr.json b/jsr.json index 8ad020c..e291a80 100644 --- a/jsr.json +++ b/jsr.json @@ -1,5 +1,5 @@ { "name": "@minecraft/minecraftstatuspinger", - "version": "1.1.5", + "version": "1.2.0", "exports": "./src/index.ts" } diff --git a/package.json b/package.json index 2823d44..fbe3959 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "CHANGELOG.md", "README.md" ], - "version": "1.1.5", - "description": "The best modern library for getting the ping and status of Minecraft servers, written in TypeScript with zero dependencies.", + "version": "1.2.0", + "description": "A modern library for pinging Minecraft servers and getting their status and playerlist, written in TypeScript with zero dependencies.", "keywords": [ "minecraft status", "minecraft ping", @@ -41,6 +41,6 @@ "@types/node": "^18.11.18", "@types/varint": "^6.0.1", "typedoc": "^0.23.25", - "typescript": "^4.9.4" + "typescript": "^5.7.0-beta" } } diff --git a/src/better-varint.ts b/src/better-varint.ts index bf05261..1ca0ad7 100644 --- a/src/better-varint.ts +++ b/src/better-varint.ts @@ -9,6 +9,8 @@ * I generated this with ChatGPT, let's hope it works. */ +import { Buffer } from 'node:buffer'; + function encode(num: number): Buffer { const bytes: number[] = []; do { diff --git a/src/index.ts b/src/index.ts index 1ecc7f7..a193770 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ -import packetGen from "./packetGenerator.js" -import packetDec from "./packetDecoder.js" +import packetGen from "./packetGenerator.ts" +import packetDec from "./packetDecoder.ts" import * as net from "node:net"; -import { Packet, ServerStatusOptions, ServerStatus, DynamicObject } from "./types.js"; +import { Packet, ServerStatusOptions, ServerStatus, DynamicObject } from "./types.ts"; import { promises as dns } from "node:dns"; @@ -18,8 +18,8 @@ import { promises as dns } from "node:dns"; * ping: true, * protocolVersion: 764 * throwOnParseError: false, - * disableSRV: false, - * disableJSONParse: false + * SRVLookup: true, + * JSONParse: true * }) * ``` */ @@ -33,11 +33,12 @@ async function lookup(options?: ServerStatusOptions): Promise { let port = options.port != null ? options.port : 25565; let timeout = options.timeout != null ? options.timeout : 10000; let ping = options.ping != null ? options.ping : true; - let protocolVersion = options.protocolVersion != null ? options.protocolVersion : 764; + let protocolVersion = options.protocolVersion != null ? options.protocolVersion : 767; let throwOnParseError = options.throwOnParseError != null ? options.throwOnParseError : true; - let disableSRV = options.disableSRV != null ? options.disableSRV : false; - let disableJSONParse = options.disableJSONParse != null ? options.disableJSONParse : false; - if (!disableSRV) ({ hostname, port } = await processSRV(hostname, port)) + let SRVLookup = options.SRVLookup != null ? options.SRVLookup : true; + let JSONParse = options.JSONParse != null ? options.JSONParse : true; + console.log(port) + if (SRVLookup) ({ hostname, port } = await processSRV(hostname, port)) // Default port of 25565, default timeout of 10 seconds. // Ping is sent by default. @@ -68,7 +69,7 @@ async function lookup(options?: ServerStatusOptions): Promise { if (packet.status.pingBaked || (packet.status.handshakeBaked && !ping)) { let serverStatus; try { - serverStatus = new ServerStatus(packet.crafted.data, packet.crafted.latency, throwOnParseError, disableJSONParse) + serverStatus = new ServerStatus(packet.crafted.data, packet.crafted.latency, throwOnParseError, JSONParse) } catch (error) { clearTimeout(timeoutFunc); portal.destroy(); @@ -140,7 +141,8 @@ async function processSRV(hostname: string, port: number) { * The hostname can't be localhostname, the port always has to be 25565, and the hostname cannot be an IP. */ - if (hostname == "localhostname" && port != 25565 && net.isIP(hostname) != 0) return { hostname, port } + + if (port != 25565 || net.isIP(hostname) != 0) return { hostname, port } let result = await dns.resolveSrv("_minecraft._tcp." + hostname).catch(() => { }) if (!result || result.length == 0 || !result[0].name || !result[0].port) return { hostname, port } return { hostname: result[0].name, port: result[0].port } diff --git a/src/packetDecoder.ts b/src/packetDecoder.ts index 8a42b98..fdc68d6 100644 --- a/src/packetDecoder.ts +++ b/src/packetDecoder.ts @@ -1,5 +1,5 @@ -import varint from "./better-varint.js" -import { Packet } from "./types.js"; +import varint from "./better-varint.ts" +import { Packet } from "./types.ts"; async function packetPipeline(chunk: Buffer, packet: Packet) { diff --git a/src/packetGenerator.ts b/src/packetGenerator.ts index 204d3c8..1351881 100644 --- a/src/packetGenerator.ts +++ b/src/packetGenerator.ts @@ -1,6 +1,4 @@ -import varint from "./better-varint.js" - -import {Packet, ServerStatusOptions} from "./types.js"; +import varint from "./better-varint.ts" async function craftHandshake(hostname: String, port: number, protocolVersion: number) { diff --git a/src/types.ts b/src/types.ts index 05fc32c..a8dae4e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -23,7 +23,6 @@ export class Packet { Error: Error = null; } -//export type Packet = _Packet | Error; interface PacketStatus { handshakeBaked: boolean, @@ -53,8 +52,8 @@ interface _ServerStatusOptions { ping?: boolean, protocolVersion?: number, throwOnParseError?: boolean, - disableSRV?: boolean, - disableJSONParse?: boolean, + SRVLookup?: boolean, + JSONParse?: boolean, } type NotBoth = @@ -64,8 +63,8 @@ type NotBoth = export type ServerStatusOptions = _ServerStatusOptions & NotBoth; export class ServerStatus { - constructor(statusRaw: string, latency?: number, throwOnParseError?: boolean, disableJSONParse?: boolean) { - if(!disableJSONParse){ + constructor(statusRaw: string, latency?: number, throwOnParseError?: boolean, JSONParse?: boolean) { + if(JSONParse){ try { this.status = JSON.parse(statusRaw); } catch (err) { diff --git a/tsconfig.json b/tsconfig.json index 923f0c4..d6047e2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,7 @@ "noImplicitAny": true, "strictNullChecks": false, "skipLibCheck": true, + "rewriteRelativeImportExtensions": true }, "include": [ "./src/**/*"