Skip to content

Conversation

SeaLoong
Copy link

No description provided.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the ping implementation by improving the ping-pong mechanism validation and timeout handling. The changes ensure that ping responses are properly validated by comparing payload data between ping and pong packets.

  • Renamed timeout variable for clarity and added ping payload validation
  • Implemented BigInt timestamp comparison to verify pong responses match ping requests
  • Added formatting improvement for state condition check

Comment on lines +48 to +51
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) {
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

clearTimeout(closeTimer)
client.end()
resolve(data)
if (BigInt(packet.time) === time) {
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants