Skip to content

Commit

Permalink
Cleanup, bumped version, changed argument names, fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
woodendoors7 committed Nov 11, 2024
1 parent 5fa5235 commit 5b9eb5a
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 33 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Changelog!


<h2>v1.2.0</h2>

[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`.

<h2>v1.1.5</h2>

[NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.1.5)
Expand Down Expand Up @@ -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)
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import mc from "minecraftstatuspinger";

let result = await mc.lookup({ host: "mc.hypixel.net" })
console.log(result);
```
```



<details>
<summary><h3>Advanced Example</h3></summary>
Expand All @@ -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);
```


</details>

## Docs
Expand All @@ -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.
* <b>`throwOnParseError?:`</b> boolean <i>`default: true`</i>
> Whether to throw an error if the status packet fails to parse the status field. The `statusRaw` field is always included.
* <b>`disableSrv?:`</b> boolean <i>`default: false`</i>
> Whether to force skip SRV lookups. Useful when only pinging IP addresses and not hostnames (domains).
* <b>`disableJSONParse?:`</b> boolean <i>`default: false`</i>
> Whether to skip JSON parsing. Useful if you only want a raw plaintext response. If true, the `status` field will be undefined.
* <b>`SRVLookup?:`</b> boolean <i>`default: true`</i>
> 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.
* <b>`JSONParse?:`</b> boolean <i>`default: true`</i>
> 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
* <b>`latency?:`</b> 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.
Expand Down
5 changes: 5 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strictNullChecks": false
}
}
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@minecraft/minecraftstatuspinger",
"version": "1.1.5",
"version": "1.2.0",
"exports": "./src/index.ts"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
2 changes: 2 additions & 0 deletions src/better-varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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
* })
* ```
*/
Expand All @@ -33,11 +33,12 @@ async function lookup(options?: ServerStatusOptions): Promise<ServerStatus> {
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.

Expand Down Expand Up @@ -68,7 +69,7 @@ async function lookup(options?: ServerStatusOptions): Promise<ServerStatus> {
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();
Expand Down Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions src/packetDecoder.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions src/packetGenerator.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export class Packet {
Error: Error = null;
}

//export type Packet = _Packet | Error;

interface PacketStatus {
handshakeBaked: boolean,
Expand Down Expand Up @@ -53,8 +52,8 @@ interface _ServerStatusOptions {
ping?: boolean,
protocolVersion?: number,
throwOnParseError?: boolean,
disableSRV?: boolean,
disableJSONParse?: boolean,
SRVLookup?: boolean,
JSONParse?: boolean,
}

type NotBoth =
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"noImplicitAny": true,
"strictNullChecks": false,
"skipLibCheck": true,
"rewriteRelativeImportExtensions": true
},
"include": [
"./src/**/*"
Expand Down

0 comments on commit 5b9eb5a

Please sign in to comment.