Skip to content
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
8 changes: 5 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ declare module 'minecraft-protocol' {
}

export interface OldPingResult {
maxPlayers: number,
maxPlayers: number
motd: string
playerCount: number
prefix: string
Expand All @@ -282,9 +282,11 @@ declare module 'minecraft-protocol' {
protocol: number
}
favicon?: string
latency: number
latency?: number
}

export type PingResult = OldPingResult | NewPingResult

export interface RealmsOptions {
realmId?: string
pickRealm?: (realms: Realm[]) => Realm
Expand All @@ -301,5 +303,5 @@ declare module 'minecraft-protocol' {
export function createSerializer({ state, isServer, version, customPackets }: SerializerOptions): any
export function createDeserializer({ state, isServer, version, customPackets }: SerializerOptions): any

export function ping(options: PingOptions, callback?: (error: Error, result: OldPingResult | NewPingResult) => void): Promise<OldPingResult | NewPingResult>
export function ping(options: PingOptions, callback?: (error: Error, result: PingResult) => void): Promise<PingResult>
}
20 changes: 13 additions & 7 deletions src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,28 @@ function ping (options) {
client.once('server_info', function (packet) {
const data = JSON.parse(packet.response)
const start = Date.now()
const maxTime = setTimeout(() => {
const maxTimer = setTimeout(() => {
clearTimeout(closeTimer)
client.end()
resolve(data)
}, options.noPongTimeout)
const time = BigInt(Date.now())
client.once('ping', function (packet) {
data.latency = Date.now() - start
clearTimeout(maxTime)
clearTimeout(closeTimer)
client.end()
resolve(data)
if (BigInt(packet.time) === time) {
Comment on lines +48 to +51
Copy link
Preview

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using BigInt for Date.now() may cause issues since Date.now() returns a regular number. The comparison on line 51 expects BigInt(packet.time) to equal time, but if packet.time is a regular number, this comparison will always fail. Consider using a regular number or ensure packet.time is also a BigInt.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in protodef, i64 are read as an array but with a valueOf() that allows the array to become a BigInt when used in operations. So casting with the BigInt constructor should indeed implicitly call .valueOf() and make this test pass

Copy link
Preview

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comparison assumes packet.time can be converted to BigInt and will equal the BigInt time variable. If packet.time is undefined, null, or not a valid number, BigInt(packet.time) will throw an error. Consider adding proper validation before the BigInt conversion.

Suggested change
if (BigInt(packet.time) === time) {
let packetTimeIsValid = false;
try {
if (
(typeof packet.time === 'string' && packet.time.trim() !== '') ||
(typeof packet.time === 'number' && !isNaN(packet.time))
) {
packetTimeIsValid = (BigInt(packet.time) === time);
}
} catch (e) {
packetTimeIsValid = false;
}
if (packetTimeIsValid) {

Copilot uses AI. Check for mistakes.

// pong payload should be the same as ping payload
clearTimeout(maxTimer)
clearTimeout(closeTimer)
client.end()
resolve(data)
}
})
client.write('ping', { time: [0, 0] })
client.write('ping', { time })
})
client.on('state', function (newState) {
if (newState === states.STATUS) { client.write('ping_start', {}) }
if (newState === states.STATUS) {
client.write('ping_start', {})
}
})
// TODO: refactor with src/client/setProtocol.js
client.on('connect', function () {
Expand Down
Loading