Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
declare module 'minecraft-pinger'

export interface pingPromiseInterface {
/**
* Server Description , May not exist
*/
description?: {
/**
* Raw Text
*/
text: string;
extra?: {
/**
* Color of the text
*/
color?: string;
/**
* Raw Text
*/
text: string;
/**
* If Text is bold
*/
bold?: boolean;
/**
* If Text is strikethroughed
*/
strikethrough?: boolean;

extra?: {
/**
* Color
*/
color: string;
/**
* Raw Text
*/
text: string;
}
};
};
players: {
/**
* Current Number of players online
*/
online: number;
/**
* Maximum Number of players that could be online
*/
max: number;
};
version: {
/**
* Server Software & Version Supports
*/
name: string;
/**
* Protocol Version
*/
protocol: number};
/**
* RoundTrip Latency in milliseconds
*/
ping: number;
/**
* Moderator Info
*/
modinfo?: {
/**
* Mod list type
*/
type: string;
/**
* Moderator List May not exist
*/
modList: string[]};
/**
* Server Favicon , Can be more than 1000 Characters
*/
favicon?: string;
}


/**
* Returns basic info about the server, asynchronously
*
* @param {string} hostname The host name of the server
* @param {number} port Port of the server most servers default to 25565
* @returns {Promise<pingPromiseInterface>} The information of the server
*/
export declare function pingPromise(
/**
* The Host Name
*/
hostname:string,
/**
* The Port of the server , most servers default to 25565
*/
port:number): Promise<pingPromiseInterface>;
/**
* Returns basic info about the server
*
* @param {string} hostname The host name of the server
* @param {number} port Port of the server most servers default to 25565
* @returns The information of the server
*/
export declare function ping(
/**
* The Host Name
*/
hostname:string,
/**
* The Port of the server , most servers default to 25565
*/
port:number):pingPromiseInterface
20 changes: 16 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ function openConnection (address) {

return new Promise((resolve, reject) => {
let connection = net.createConnection(port, hostname, () => {
// Decode incoming packets
/**
* Decode incoming packets
*/
let packetDecoder = new PacketDecoder()
connection.pipe(packetDecoder)

// Write handshake packet
/**
* Write handshake packet
*/
connection.write(createHandshakePacket(hostname, port))

packetDecoder.once('error', error => {
Expand Down Expand Up @@ -89,14 +93,22 @@ function openConnection (address) {
reject(new Error('Timed out'))
})

// Packet timeout (10 seconds)
/**
* Packet timeout (10 seconds)
*/
let timeout = setTimeout(() => {
connection.end()
reject(new Error('Timed out (10 seconds passed)'))
}, 10000)
})
}

/**
*
* @param {string} hostname Hostname of the website you want to check
* @returns Hostname & port
*
* Checks The Service Record Of the Hostname
*/
function checkSrvRecord (hostname) {
return new Promise((resolve, reject) => {
if (net.isIP(hostname) !== 0) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"dependencies": {
"node-int64": "^0.4.0",
"varint": "^4.0.1"
}
},
"types": "lib/index.d.ts"
}