@@ -3,13 +3,11 @@ import clientAutoVersion from 'minecraft-protocol/src/client/autoVersion'
3
3
4
4
export const pingServerVersion = async ( ip : string , port ?: number , mergeOptions : Record < string , any > = { } ) => {
5
5
const fakeClient = new EventEmitter ( ) as any
6
- fakeClient . on ( 'error' , ( err ) => {
7
- throw new Error ( err . message ?? err )
8
- } )
9
6
const options = {
10
7
host : ip ,
11
8
port,
12
- noPongTimeout : Infinity , // disable timeout
9
+ noPongTimeout : 10_000 ,
10
+ closeTimeout : 20_000 ,
13
11
...mergeOptions ,
14
12
}
15
13
let latency = 0
@@ -19,12 +17,26 @@ export const pingServerVersion = async (ip: string, port?: number, mergeOptions:
19
17
fullInfo = res
20
18
} ]
21
19
22
- // TODO! use client.socket.destroy() instead of client.end() for faster cleanup
23
- await clientAutoVersion ( fakeClient , options )
20
+ // TODO use client.socket.destroy() instead of client.end() for faster cleanup
21
+ clientAutoVersion ( fakeClient , options )
22
+ await Promise . race ( [
23
+ new Promise < void > ( ( resolve , reject ) => {
24
+ fakeClient . once ( 'connect_allowed' , ( ) => {
25
+ resolve ( )
26
+ } )
27
+ } ) ,
28
+ new Promise < void > ( ( resolve , reject ) => {
29
+ fakeClient . on ( 'error' , ( err ) => {
30
+ reject ( new Error ( err . message ?? err ) )
31
+ } )
32
+ if ( mergeOptions . stream ) {
33
+ mergeOptions . stream . on ( 'end' , ( err ) => {
34
+ reject ( new Error ( 'Connection closed' ) )
35
+ } )
36
+ }
37
+ } )
38
+ ] )
24
39
25
- await new Promise < void > ( ( resolve , reject ) => {
26
- fakeClient . once ( 'connect_allowed' , resolve )
27
- } )
28
40
return {
29
41
version : fakeClient . version ,
30
42
latency,
0 commit comments