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
129 changes: 71 additions & 58 deletions src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaStatusResponse> => {
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);
Expand All @@ -83,7 +91,12 @@ const statusJava = async (host: string, port = 25565, options?: JavaStatusOption
};

const statusBedrock = async (host: string, port = 19132, options?: BedrockStatusOptions): Promise<BedrockStatusResponse> => {
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);
Expand Down
6 changes: 6 additions & 0 deletions test/status-bedrock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
6 changes: 6 additions & 0 deletions test/status-java.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down