fix: only perform cleanup and fire the callback on the first error

This commit is contained in:
Timofey Gelazoniya 2025-06-15 00:18:55 +03:00
parent 27011d4091
commit ef2bebe755
Signed by: zeldon
GPG Key ID: D99707D1FF69FAB0
2 changed files with 16 additions and 6 deletions

View File

@ -113,10 +113,9 @@ const ping = (host, port = 19132, cb, timeout = 5000) => {
* @param {Error} err The error that occurred. * @param {Error} err The error that occurred.
*/ */
const handleError = (err) => { const handleError = (err) => {
closeSocket();
if (!didFireError) { if (!didFireError) {
didFireError = true; didFireError = true;
closeSocket();
cb(null, err); cb(null, err);
} }
}; };

View File

@ -17,7 +17,14 @@ import varint from "./varint.js";
* @param {number} [timeout=5000] The timeout duration in milliseconds. * @param {number} [timeout=5000] The timeout duration in milliseconds.
* @param {number} [protocolVersion=-1] The protocol version of the Java client. * @param {number} [protocolVersion=-1] The protocol version of the Java client.
*/ */
function ping(host, virtualHost, port = 25565, cb, timeout = 5000, protocolVersion = -1) { function ping(
host,
virtualHost,
port = 25565,
cb,
timeout = 5000,
protocolVersion = -1
) {
const socket = net.createConnection({ host, port }); const socket = net.createConnection({ host, port });
// Set manual timeout interval. // Set manual timeout interval.
@ -41,10 +48,9 @@ function ping(host, virtualHost, port = 25565, cb, timeout = 5000, protocolVersi
* @param {Error} err The error that occurred. * @param {Error} err The error that occurred.
*/ */
const handleError = (err) => { const handleError = (err) => {
closeSocket();
if (!didFireError) { if (!didFireError) {
didFireError = true; didFireError = true;
closeSocket();
cb(null, err); cb(null, err);
} }
}; };
@ -132,7 +138,12 @@ function ping(host, virtualHost, port = 25565, cb, timeout = 5000, protocolVersi
export function pingJava(host, options = {}) { export function pingJava(host, options = {}) {
if (!host) throw new Error("Host argument is not provided"); if (!host) throw new Error("Host argument is not provided");
const { port = 25565, timeout = 5000, protocolVersion = -1, virtualHost = null } = options; const {
port = 25565,
timeout = 5000,
protocolVersion = -1,
virtualHost = null,
} = options;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ping( ping(