11import JavaProtocol , { NewPingResult } from 'minecraft-protocol' ;
22import { ServerStatus } from '../classes/Server.js' ;
33import BedrockProtocol from 'bedrock-protocol' ;
4+ import { Worker } from 'worker_threads' ;
5+ import { fileURLToPath } from 'url' ;
6+ import path from 'path' ;
7+
8+ const __filename = fileURLToPath ( import . meta. url ) ;
9+ const __dirname = path . dirname ( __filename ) ;
410
511export interface PingData {
612 status : Exclude < ServerStatus , 'Starting' | 'Stopping' > ;
@@ -21,8 +27,22 @@ export interface PingOptions {
2127export type JavaPingOptions = Omit < PingOptions , 'protocol' > & { protocol : 'java' } ;
2228export type BedrockPingOptions = Omit < PingOptions , 'protocol' | 'timeout' > & { protocol : 'bedrock' } ;
2329
24- export async function pingServer ( options : JavaPingOptions | BedrockPingOptions ) : Promise < PingData > {
25- return options . protocol === 'java' ? pingJavaServer ( options ) : pingBedrockServer ( options ) ;
30+ export async function pingServer ( options : ( JavaPingOptions | BedrockPingOptions ) & { useWorkerThread ?: boolean ; } ) : Promise < PingData > {
31+ if ( options . useWorkerThread === false ) {
32+ return options . protocol === 'java' ? await pingJavaServer ( options ) : await pingBedrockServer ( options ) ;
33+ }
34+
35+ return new Promise ( ( res , rej ) => {
36+ const worker = new Worker ( path . join ( __dirname , './ping.worker.js' ) , {
37+ workerData : options
38+ } ) ;
39+
40+ worker . on ( 'message' , res ) ;
41+ worker . on ( 'error' , rej ) ;
42+ worker . on ( 'exit' , code => {
43+ if ( code !== 0 ) rej ( new Error ( `Ping worker exited with an error code: ${ code } ` ) ) ;
44+ } ) ;
45+ } ) ;
2646}
2747
2848export async function pingJavaServer ( options : JavaPingOptions ) : Promise < PingData > {
0 commit comments