feat: add virtualHost option (#6)

This commit is contained in:
envizar 2025-02-07 08:01:12 +03:00 committed by GitHub
parent 9469564736
commit 73a2fffe8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 4 deletions

View File

@ -11,12 +11,13 @@ import varint from "./varint.js";
/**
* Ping a Minecraft Java server.
* @param {string} host The host of the Java server.
* @param {string} virtualHost The host sent in handshake.
* @param {number} [port=25565] The port of the Java server.
* @param {function} cb The callback function to handle the ping response.
* @param {number} [timeout=5000] The timeout duration in milliseconds.
* @param {number} [protocolVersion=-1] The protocol version of the Java client.
*/
function ping(host, port = 25565, cb, timeout = 5000, protocolVersion = -1) {
function ping(host, virtualHost, port = 25565, cb, timeout = 5000, protocolVersion = -1) {
const socket = net.createConnection({ host, port });
// Set manual timeout interval.
@ -56,8 +57,8 @@ function ping(host, port = 25565, cb, timeout = 5000, protocolVersion = -1) {
const handshake = varint.concat([
varint.encodeInt(0),
varint.encodeInt(protocolVersion),
varint.encodeInt(host.length),
varint.encodeString(host),
varint.encodeInt(virtualHost.length),
varint.encodeString(virtualHost),
varint.encodeUShort(port),
varint.encodeInt(1),
]);
@ -131,11 +132,12 @@ function ping(host, port = 25565, cb, timeout = 5000, protocolVersion = -1) {
export function pingJava(host, options = {}) {
if (!host) throw new Error("Host argument is not provided");
const { port = 25565, timeout = 5000, protocolVersion = -1 } = options;
const { port = 25565, timeout = 5000, protocolVersion = -1, virtualHost = null } = options;
return new Promise((resolve, reject) => {
ping(
host,
virtualHost || host,
port,
(res, err) => {
err ? reject(err) : resolve(res);

1
types/lib/java.d.ts vendored
View File

@ -7,6 +7,7 @@ export type JavaPingOptions = {
port?: number | undefined;
timeout?: number | undefined;
protocolVersion?: number | undefined;
virtualHost?: string | undefined;
};
/**