Skip to content

Commit 5698064

Browse files
committed
1.2.0: Switched from Buffer to Uint8Array, changed argument names, multiple changes
- ⚙️ Changed `disableSrv` and `disableJSONParse` to `SRVLookup` and `JSONParse` respectively, changing their default values to true. - ⚙️ Changed all `Buffer`s to `Uint8Array`s, improving compatibility throughout runtimes. - ⚙️ Bumped the default protocol version from `764` (1.20.2) to `767` (1.21.1) - 🛠️ Added hard limit on port property - 🛠️ 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 dysfunctional) soft block on localhost addresses when looking up SRV. Code will initiate a SRV lookup with any local address, has to be disabled with `SRVLookup`,
1 parent 5b9eb5a commit 5698064

File tree

11 files changed

+165
-104
lines changed

11 files changed

+165
-104
lines changed

.github/workflows/publish.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Publish
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
id-token: write # The OIDC ID token is used for authentication with JSR.
14+
steps:
15+
- uses: actions/checkout@v4
16+
- run: npx jsr publish

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33

44
<h2>v1.2.0</h2>
55

6-
[NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.2.0)
6+
<sup>[NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.2.0) | [JSR](https://jsr.io/@minecraft/[email protected])</sup>
77

88
- ⚙️ Changed `disableSrv` and `disableJSONParse` to `SRVLookup` and `JSONParse` respectively, changing their default values to true.
9+
- ⚙️ Changed all `Buffer`s to `Uint8Array`s, improving compatibility throughout runtimes.
910
- ⚙️ Bumped the default protocol version from `764` (1.20.2) to `767` (1.21.1)
11+
- 🛠️ Added hard limit on port property
1012
- 🛠️ Changed uncompiled `.js` imports to `.ts`, making it work with Deno.
1113
- 🛠️ Added jsr.json file
1214
- 🛠️ Fixed SRV lookups not being prevented when port wasn't set 25565/was an IP
13-
- ➖ 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`.
15+
- ➖ Removed (previously dysfunctional) soft block on localhost addresses when looking up SRV. Code will initiate a SRV lookup with any local address, has to be disabled with `SRVLookup`.
1416

1517
<h2>v1.1.5</h2>
1618

17-
[NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.1.5)
19+
<sup>[NPM](https://www.npmjs.com/package/minecraftstatuspinger/v/1.1.5) | [JSR](https://jsr.io/@minecraft/[email protected])</sup>
1820

1921
- 🛠️ Fixed latency being returned as `null` instead of zero.
2022

@@ -53,4 +55,4 @@
5355
- 🟢 Added lookup option `disableJSONParse` to completely skip parsing JSON.
5456
- ⚙️ Renamed primary lookup option `hostname` to `host`, `hostname` stays as an alias.
5557

56-
[all versions](https://www.npmjs.com/package/minecraftstatuspinger?activeTab=versions)
58+
All versions: [NPM](https://www.npmjs.com/package/minecraftstatuspinger?activeTab=versions) | [JSR](https://jsr.io/@minecraft/minecraftstatuspinger/versions)

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
[![GitHub Link badge](https://badgen.net/badge/icon/GitHub?icon=github&label&color=010409)](https://github.com/woodendoors7/minecraftstatuspinger)
2+
[![NPM link badge](https://badgen.net/badge/icon/NPM?icon=JS&label&color=a00a0b)](https://www.npmjs.com/package/minecraftstatuspinger)
3+
[![JSR link badge](https://badgen.net/badge/color/JSR/?label=&color=d2a404)](https://jsr.io/@minecraft/minecraftstatuspinger)
4+
[![Web link badge](https://badgen.net/badge/icon/Web?icon=JS&label)](https://pinger.floppa.hair)
5+
6+
7+
8+
19

210

311
<div align="center">
@@ -7,6 +15,13 @@
715

816
<hr>
917

18+
19+
![NPM Version](https://badgen.net/npm/v/minecraftstatuspinger)
20+
![TS Types](https://badgen.net/npm/types/minecraftstatuspinger)
21+
![License](https://badgen.net/npm/license/minecraftstatuspinger)
22+
![NPM Downloads](https://badgen.net/npm/dy/minecraftstatuspinger/?color=00a600)
23+
![Maintained?](https://badgen.net/static/Maintained%3F/Yes/00a600)
24+
1025
### What can this be used for?
1126

1227
- Getting server **Latency (ping)**
@@ -34,8 +49,6 @@ let result = await mc.lookup({ host: "mc.hypixel.net" })
3449
console.log(result);
3550
```
3651

37-
38-
3952
<details>
4053
<summary><h3>Advanced Example</h3></summary>
4154
<br>
@@ -47,7 +60,7 @@ let result = await mc.lookup({
4760
host: "mc.hypixel.net",
4861
port: 25565,
4962
ping: true,
50-
protocolVersion: 764,
63+
protocolVersion: 767,
5164
timeout: 10000,
5265
throwOnParseError: true,
5366
SRVLookup: true,
@@ -57,7 +70,6 @@ let result = await mc.lookup({
5770
console.log(result);
5871
```
5972

60-
6173
</details>
6274

6375
## Docs
@@ -109,7 +121,7 @@ console.log(result);
109121
### Changelog
110122

111123
**[View Changelog](https://pinger.floppa.hair/changelog/)**,
112-
Latest version: <b><i><code>v1.1.5</code></i></b>
124+
Latest version: <b><i><code>v1.2.0</code></i></b>
113125

114126

115127

deno.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

jsr.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@minecraft/minecraftstatuspinger",
33
"version": "1.2.0",
4-
"exports": "./src/index.ts"
4+
"exports": "./src/index.ts",
5+
"compilerOptions":{"strictNullChecks": false}
56
}

src/better-varint.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,42 @@ async function lookup(options?: ServerStatusOptions): Promise<ServerStatus> {
2828
return new Promise<ServerStatus>(async (resolve, reject) => {
2929

3030
let hostname = options.host || options.hostname;
31-
if (!hostname) return reject(new Error("No hostname was provided!"))
31+
if (!hostname) return reject(new Error("No hostname was provided!"));
3232

3333
let port = options.port != null ? options.port : 25565;
34+
if(port < 0 || port > 65535) return reject(new Error("Port number must be between 0 and 25565!"));
35+
3436
let timeout = options.timeout != null ? options.timeout : 10000;
3537
let ping = options.ping != null ? options.ping : true;
3638
let protocolVersion = options.protocolVersion != null ? options.protocolVersion : 767;
3739
let throwOnParseError = options.throwOnParseError != null ? options.throwOnParseError : true;
3840
let SRVLookup = options.SRVLookup != null ? options.SRVLookup : true;
3941
let JSONParse = options.JSONParse != null ? options.JSONParse : true;
40-
console.log(port)
4142
if (SRVLookup) ({ hostname, port } = await processSRV(hostname, port))
4243
// Default port of 25565, default timeout of 10 seconds.
4344
// Ping is sent by default.
4445

4546
let portal = net.createConnection({ port: port, host: hostname, lookup: customLookup }, async () => {
4647
// Send first the handshake, and then the status request to the server.
4748
let handshake = await packetGen.craftHandshake(hostname, port, protocolVersion);
49+
4850
let statusRequest = await packetGen.craftEmptyPacket(0);
51+
4952
portal.write(handshake);
5053
portal.write(statusRequest);
54+
5155
})
5256

5357
let packet = new Packet();
54-
portal.on("data", async (chunk) => {
58+
portal.on("data", async (_chunk) => {
5559

5660
/*
5761
Pass every new chunk of data sent from the server into the pipeline,
5862
and also pass the packet object in, which holds the current state of the request.
5963
The pipeline returns the packet object, with changes made from processing the data chunk.
6064
*/
6165

66+
let chunk = new Uint8Array(<any>_chunk);
6267
packet = await packetDec.packetPipeline(chunk, packet)
6368
if (packet.Error) {
6469
clearTimeout(timeoutFunc)

src/packetDecoder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import varint from "./better-varint.ts"
1+
import varint from "./utils-varint.ts"
22
import { Packet } from "./types.ts";
33

44

5-
async function packetPipeline(chunk: Buffer, packet: Packet) {
5+
async function packetPipeline(chunk: Uint8Array, packet: Packet) {
66
// Wait for and Collect all the data coming from the server.
77
if (packet.status.pingSent) return await craftLatency(packet);
88

9-
packet.dataBuffer = await Buffer.concat([
9+
packet.dataBuffer = await varint.concatUI8([
1010
packet.dataBuffer,
1111
chunk
1212
])
1313

14-
if (Buffer.byteLength(packet.dataBuffer) > 102400) {
14+
if (packet.dataBuffer.length > 102400) {
1515
packet.Error = new Error("Maximum buffer size of 100 Kilobytes reached.\nThe status packet should be smaller than 20 Kilobytes.")
1616
return packet;
1717
}
@@ -41,7 +41,7 @@ async function craftData(packet: Packet) {
4141
packet.fieldsBuffer = packet.dataBuffer.slice(packet.meta.metaLength)
4242
let fieldLength = varint.decode(packet.fieldsBuffer)
4343
packet.fieldsBuffer = packet.fieldsBuffer.slice(varint.encodingLength(fieldLength));
44-
packet.crafted.data = packet.fieldsBuffer.toString();
44+
packet.crafted.data = new TextDecoder().decode(packet.fieldsBuffer);
4545
packet.status.handshakeBaked = true;
4646
return packet;
4747
}

src/packetGenerator.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
import varint from "./better-varint.ts"
1+
import varint from "./utils-varint.ts"
22

33

4-
async function craftHandshake(hostname: String, port: number, protocolVersion: number) {
4+
async function craftHandshake(hostname: string, port: number, protocolVersion: number) {
55
let packetBody = await craftHandshakeBody(hostname, port, protocolVersion);
66

77
// Field 1: Length of the entire object, (VarInt)
88
// Field 2: PacketID, (VarInt)
99
// Field 3: The body of the request
1010
const packetID = 0;
1111

12-
let packetLengthBuffer = Buffer.from(varint.encode(varint.encodingLength(packetID) + packetBody.length));
13-
let packetIDBuffer = Buffer.from(varint.encode(packetID));
12+
let packetLengthBuffer = varint.encode(varint.encodingLength(packetID) + packetBody.length);
13+
let packetIDBuffer = varint.encode(packetID);
1414

15-
let craftedHandshake = Buffer.concat([
15+
let craftedHandshake = varint.concatUI8([
1616
packetLengthBuffer,
1717
packetIDBuffer,
18-
packetBody,
18+
packetBody
1919
])
2020

2121
return craftedHandshake
2222
}
2323

24-
async function craftHandshakeBody(hostname: String, port: number, protocolVersion: number) {
24+
async function craftHandshakeBody(hostname: string, port: number, protocolVersion: number) {
2525
// Field 1: The Protocol Version, (VarInt)
2626
// Field 2: The hostname of the server, (String) prefixed with it's length (VarInt)
2727
// Field 3: The port of the server, (UInt16)
2828
// Field 4: Next expected state, whether to get the status (1) or login (2), (VarInt)
29-
let protocolVersionBuffer = Buffer.from(varint.encode(protocolVersion));
30-
let hostnamePrefixBuffer = Buffer.from(varint.encode(hostname.length));
31-
let hostnameBuffer = Buffer.from(hostname, "utf8");
32-
let portBuffer = Buffer.allocUnsafe(2)
33-
portBuffer.writeUInt16BE(port, 0)
34-
let nextStateBuffer = Buffer.from(varint.encode(1))
35-
36-
let packetBody = Buffer.concat([
29+
let protocolVersionBuffer = varint.encode(protocolVersion);
30+
let hostnamePrefixBuffer = varint.encode(hostname.length);
31+
let hostnameBuffer = new TextEncoder().encode(hostname);
32+
let portBuffer = varint.craftUInt16BE(port);
33+
let nextStateBuffer = varint.encode(1)
34+
35+
let packetBody = varint.concatUI8([
3736
protocolVersionBuffer,
3837
hostnamePrefixBuffer,
3938
hostnameBuffer,
@@ -46,10 +45,10 @@ async function craftHandshakeBody(hostname: String, port: number, protocolVersio
4645

4746
async function craftEmptyPacket(packetID: number) {
4847

49-
let packetLengthBuffer = Buffer.from(varint.encode(varint.encodingLength(packetID)));
50-
let packetIDBuffer = Buffer.from(varint.encode(packetID));
48+
let packetLengthBuffer = varint.encode(varint.encodingLength(packetID));
49+
let packetIDBuffer = varint.encode(packetID);
5150

52-
let craftedPacket = Buffer.concat([
51+
let craftedPacket = varint.concatUI8([
5352
packetLengthBuffer,
5453
packetIDBuffer
5554
])
@@ -66,12 +65,12 @@ async function craftPingPacket() {
6665
// The time of when the ping request was sent is stored in a variable.
6766
const packetID = 1;
6867

69-
let longBuffer = await makeLongBuffer(Date.now())
68+
let longBuffer = varint.craftInt64BE(BigInt(Date.now()));
7069

71-
let packetLengthBuffer = Buffer.from(varint.encode(varint.encodingLength(packetID) + longBuffer.length));
72-
let packetIDBuffer = Buffer.from(varint.encode(packetID));
70+
let packetLengthBuffer = varint.encode(varint.encodingLength(packetID) + longBuffer.length);
71+
let packetIDBuffer = varint.encode(packetID);
7372

74-
let craftedPacket = Buffer.concat([
73+
let craftedPacket = varint.concatUI8([
7574
packetLengthBuffer,
7675
packetIDBuffer,
7776
longBuffer
@@ -81,11 +80,5 @@ async function craftPingPacket() {
8180
}
8281

8382

84-
async function makeLongBuffer(longNumber: number) {
85-
let buf = Buffer.allocUnsafe(8);
86-
buf.writeBigInt64BE(BigInt(longNumber))
87-
88-
return buf;
89-
}
9083

9184
export default { craftHandshake, craftEmptyPacket, craftPingPacket }

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export class Packet {
1414
fullLength: null,
1515
metaLength: null,
1616
}
17-
dataBuffer = Buffer.alloc(0);
18-
fieldsBuffer = Buffer.alloc(0);
17+
dataBuffer: Uint8Array;
18+
fieldsBuffer: Uint8Array;
1919
crafted: PacketCrafted = {
2020
data: null,
2121
latency: null,

0 commit comments

Comments
 (0)