diff --git a/src/status.ts b/src/status.ts index 55a626d..698d0f4 100644 --- a/src/status.ts +++ b/src/status.ts @@ -2,78 +2,86 @@ import superagent from 'superagent'; import { BASE_URL } from './enum'; interface JavaStatusOptions { - query?: boolean, - baseURL?: string, + query?: boolean, + timeout?: number, + baseURL?: string, } interface BedrockStatusOptions { - baseURL?: string, + timeout?: number, + baseURL?: string, } interface StatusResponse { - online: boolean, - host: string, - port: number, - eula_blocked: boolean, - retrieved_at: number, - expires_at: number + online: boolean, + host: string, + port: number, + ip_address: string | null, + eula_blocked: boolean, + retrieved_at: number, + expires_at: number } interface JavaStatusResponse extends StatusResponse { - version?: { - name_raw: string, - name_clean: string, - name_html: string, - protocol: number - } | null, - players?: { - online: number, - max: number, - list: { - uuid: string, - name_raw: string, - name_clean: string, - name_html: string - }[] - }, - motd?: { - raw: string, - clean: string, - html: string - }, - icon?: string | null, - mods?: { - name: string, - version: string - }[], - plugins?: { - name: string, - version: string - }[], - software?: string + version?: { + name_raw: string, + name_clean: string, + name_html: string, + protocol: number + } | null, + players?: { + online: number, + max: number, + list: { + uuid: string, + name_raw: string, + name_clean: string, + name_html: string + }[] + }, + motd?: { + raw: string, + clean: string, + html: string + }, + icon?: string | null, + mods?: { + name: string, + version: string + }[], + plugins?: { + name: string, + version: string + }[], + software?: string } interface BedrockStatusResponse extends StatusResponse { - version?: { - name: string, - protocol: number - }, - players?: { - online: number, - max: number - }, - motd?: { - raw: string, - clean: string, - html: string - }, - gamemode?: string, - server_id?: string, - edition?: 'MCPE' | 'MCEE' + version?: { + name: string, + protocol: number + }, + players?: { + online: number, + max: number + }, + motd?: { + raw: string, + clean: string, + html: string + }, + gamemode?: string, + server_id?: string, + edition?: 'MCPE' | 'MCEE' } const statusJava = async (host: string, port = 25565, options?: JavaStatusOptions): Promise => { - const result = await superagent.get(`${options?.baseURL ?? BASE_URL}/status/java/${host}:${port}?query=${options?.query ?? true}`); + let url = `${options?.baseURL ?? BASE_URL}/status/java/${host}:${port}?query=${options?.query ?? true}`; + if (options?.timeout) { + url += `&timeout=${options.timeout}`; + } + + const result = await superagent.get(url); if (result.statusCode !== 200) { throw new Error(result.body); @@ -83,7 +91,12 @@ const statusJava = async (host: string, port = 25565, options?: JavaStatusOption }; const statusBedrock = async (host: string, port = 19132, options?: BedrockStatusOptions): Promise => { - const result = await superagent.get(`${options?.baseURL ?? BASE_URL}/status/bedrock/${host}:${port}`); + let url = `${options?.baseURL ?? BASE_URL}/status/bedrock/${host}:${port}`; + if (options?.timeout) { + url += `?timeout=${options.timeout}`; + } + + const result = await superagent.get(url); if (result.statusCode !== 200) { throw new Error(result.body); diff --git a/test/status-bedrock.test.js b/test/status-bedrock.test.js index c46f681..993dd89 100644 --- a/test/status-bedrock.test.js +++ b/test/status-bedrock.test.js @@ -23,6 +23,12 @@ test('statusBedrock()', () => { expect(result.port).toBe(port); expect(Number.isInteger(result.port)).toBe(true); + // result.ip_address + expect(typeof result.ip_address === 'string' || result.ip_address === null).toBe(true); + if (result.ip_address !== null) { + expect(result.ip_address.length).toBeGreaterThan(0); + } + // result.eula_blocked expect(typeof result.eula_blocked).toBe('boolean'); diff --git a/test/status-java.test.js b/test/status-java.test.js index 8cd92d6..5334c8d 100644 --- a/test/status-java.test.js +++ b/test/status-java.test.js @@ -23,6 +23,12 @@ test('statusJava()', () => { expect(result.port).toBe(port); expect(Number.isInteger(result.port)).toBe(true); + // result.ip_address + expect(typeof result.ip_address === 'string' || result.ip_address === null).toBe(true); + if (result.ip_address !== null) { + expect(result.ip_address.length).toBeGreaterThan(0); + } + // result.eula_blocked expect(typeof result.eula_blocked).toBe('boolean');