@deitylamb/mcping 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.prettierrc ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": false,
4
+ "trailingComma": "all",
5
+ "tabWidth": 2,
6
+ "printWidth": 80
7
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 HarmoniaMC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # @deitylamb/mcping
2
+
3
+ Robust, and well-typed Minecraft server ping library for Node.js. Supports both **Java Edition** (1.7.x - 1.21+) and **Bedrock Edition**.
4
+
5
+ ## CLI Usage
6
+
7
+ You can use the CLI to ping any server directly from your terminal:
8
+
9
+ ```bash
10
+ # Using npx
11
+ npx @deitylamb/mcping mc.hypixel.net
12
+
13
+ # Options
14
+ npx @deitylamb/mcping mc.hypixel.net --timeout 1000
15
+ npx @deitylamb/mcping play.cubecraft.net --type bedrock
16
+
17
+ # Or install globally
18
+ npm install -g @deitylamb/mcping
19
+ mcping mc.hypixel.net
20
+ ```
21
+
22
+ ## Features
23
+
24
+ - **Unified API**: One simple function `ping(target, options)` for all protocols.
25
+ - **Auto-Detect**: Automatically detects if a server is Java or Bedrock.
26
+ - **High Performance Caching**: Built-in support for multiple caching strategies.
27
+ - **Modern & Typed**: Written in TypeScript with full ESM and CJS support.
28
+ - **SRV Support**: Native DNS SRV record resolution (`_minecraft._tcp` and `_minecraft._udp`).
29
+ - **Real IP Resolution**: Always resolves and returns the final physical IP address.
30
+ - **Rich Responses**: Includes normalized MOTD (handling JSON and colors), player counts, versions, and latency.
31
+ - **Legacy & Modded**: Verified compatibility with 1.7.10+, Forge, and modded network proxies.
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ npm install @deitylamb/mcping
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Simple Ping (Auto-detect)
42
+
43
+ ```typescript
44
+ import { ping } from "@deitylamb/mcping";
45
+
46
+ const res = await ping("mc.hypixel.net");
47
+ console.log(`${res.players.online}/${res.players.max} players online.`);
48
+ console.log(`Resolved IP: ${res.target.ip}:${res.target.port}`);
49
+ ```
50
+
51
+ ### Caching Strategies
52
+
53
+ The library supports two main caching strategies to optimize performance and reduce network overhead:
54
+
55
+ 1. **`lazy` (Cache-Aside)**: Checks for a fresh cached result before making a network request.
56
+ 2. **`swr` (Stale-While-Revalidate)**: Returns cached data immediately (even if stale) while refreshing the cache in the background.
57
+
58
+ ```typescript
59
+ const res = await ping("mc.hypixel.net", {
60
+ cache: {
61
+ ttl: 30000, // 30 seconds
62
+ strategy: "swr", // 'lazy' | 'swr'
63
+ },
64
+ });
65
+
66
+ console.log(res.cached ? "(From Cache)" : "(Live)");
67
+ ```
68
+
69
+ ### Specific Protocol
70
+
71
+ ```typescript
72
+ const res = await ping("geo.hivebedrock.network", { type: "bedrock" });
73
+ ```
74
+
75
+ ### Options
76
+
77
+ ```typescript
78
+ const res = await ping("example.com", {
79
+ timeout: 3000, // default: 5000ms
80
+ type: "java", // 'java' | 'bedrock' | null (auto-detect)
81
+ });
82
+ ```
83
+
84
+ ## Response Schema
85
+
86
+ ```typescript
87
+ {
88
+ type: "java" | "bedrock";
89
+ version: {
90
+ name: string;
91
+ protocol: number;
92
+ };
93
+ players: {
94
+ online: number;
95
+ max: number;
96
+ };
97
+ motd: string; // Cleaned string with formatting preserved as § codes
98
+ latency: number; // Round-trip time in ms (Java only, 0 for Bedrock)
99
+ target: {
100
+ host: string; // Resolved hostname (e.g. from SRV)
101
+ port: number; // Final port
102
+ ip: string; // Physical IP address
103
+ };
104
+ raw: any; // The original raw response from the server
105
+ cached?: boolean; // True if the response was served from cache
106
+ }
107
+ ```
108
+
109
+ ## License
110
+
111
+ MIT © HarmoniaMC
package/dist/cli.cjs ADDED
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env node
2
+ const require_src = require("./src-Baubd7mm.cjs");
3
+ //#region src/ansi.ts
4
+ const mcColorMap = {
5
+ "0": "\x1B[30m",
6
+ "1": "\x1B[34m",
7
+ "2": "\x1B[32m",
8
+ "3": "\x1B[36m",
9
+ "4": "\x1B[31m",
10
+ "5": "\x1B[35m",
11
+ "6": "\x1B[33m",
12
+ "7": "\x1B[37m",
13
+ "8": "\x1B[90m",
14
+ "9": "\x1B[94m",
15
+ a: "\x1B[92m",
16
+ b: "\x1B[96m",
17
+ c: "\x1B[91m",
18
+ d: "\x1B[95m",
19
+ e: "\x1B[93m",
20
+ f: "\x1B[97m",
21
+ l: "\x1B[1m",
22
+ m: "\x1B[9m",
23
+ n: "\x1B[4m",
24
+ o: "\x1B[3m",
25
+ r: "\x1B[0m"
26
+ };
27
+ /**
28
+ * Converts Minecraft color codes (§) to ANSI terminal colors.
29
+ */
30
+ function mcToAnsi(text) {
31
+ const reset = "\x1B[0m";
32
+ let result = "";
33
+ const parts = text.split("§");
34
+ result += parts[0];
35
+ for (let i = 1; i < parts.length; i++) {
36
+ const part = parts[i];
37
+ if (part.length === 0) continue;
38
+ const ansi = mcColorMap[part[0].toLowerCase()];
39
+ if (ansi) result += ansi + part.substring(1);
40
+ else result += "§" + part;
41
+ }
42
+ return result + reset;
43
+ }
44
+ //#endregion
45
+ //#region src/cli.ts
46
+ const colors = {
47
+ reset: "\x1B[0m",
48
+ bright: "\x1B[1m",
49
+ cyan: "\x1B[36m",
50
+ green: "\x1B[32m",
51
+ yellow: "\x1B[33m",
52
+ magenta: "\x1B[35m",
53
+ white: "\x1B[37m",
54
+ gray: "\x1B[90m"
55
+ };
56
+ /**
57
+ * Poor man's argument parser
58
+ */
59
+ function parseArgs(args) {
60
+ const result = {};
61
+ for (let i = 0; i < args.length; i++) {
62
+ const arg = args[i];
63
+ if (arg === "--type" || arg === "-t") {
64
+ const val = args[++i]?.toLowerCase();
65
+ if (val === "java" || val === "bedrock") result.type = val;
66
+ } else if (arg === "--timeout") {
67
+ const val = parseInt(args[++i]);
68
+ if (!isNaN(val)) result.timeout = val;
69
+ } else if (!result.target && !arg.startsWith("-")) result.target = arg;
70
+ }
71
+ return result;
72
+ }
73
+ async function main() {
74
+ const parsed = parseArgs(process.argv.slice(2));
75
+ if (!parsed.target) {
76
+ console.log(`\n${colors.bright}Usage:${colors.reset} mcping <host[:port]> [options]`);
77
+ console.log(`\n${colors.bright}Options:${colors.reset}`);
78
+ console.log(` --type, -t <java|bedrock> Force protocol type`);
79
+ console.log(` --timeout <ms> Connection timeout (default: 5000)`);
80
+ console.log(`\n${colors.bright}Examples:${colors.reset}`);
81
+ console.log(` mcping mc.hypixel.net`);
82
+ console.log(` mcping geo.hivebedrock.network --type bedrock`);
83
+ console.log(` mcping play.example.com --timeout 2000\n`);
84
+ process.exit(1);
85
+ }
86
+ try {
87
+ const res = await require_src.ping(parsed.target, {
88
+ type: parsed.type,
89
+ timeout: parsed.timeout
90
+ });
91
+ const resolvedStr = `${res.target.ip}:${res.target.port}`;
92
+ const targetDisplay = parsed.target === resolvedStr ? parsed.target : `${parsed.target} ${colors.gray}→${colors.reset} ${resolvedStr}`;
93
+ console.log(`\n ${colors.bright}${targetDisplay}${colors.reset}`);
94
+ const editionStr = `${colors.bright}${res.type.toUpperCase()}${colors.reset}`;
95
+ const versionStr = `${colors.gray}${res.version.name}${colors.reset}`;
96
+ const latencyStr = res.type === "java" ? ` ${colors.magenta}${res.latency}ms${colors.reset}` : "";
97
+ console.log(` ${editionStr} ${colors.gray}•${colors.reset} ${versionStr}${latencyStr}`);
98
+ console.log(` ${colors.bright}${res.players.online}${colors.reset} ${colors.gray}/ ${res.players.max} players${colors.reset}`);
99
+ const cleanMotd = mcToAnsi(res.motd).trim().split("\n").map((line) => line.trim()).filter((line) => line.length > 0).join(`\n `);
100
+ console.log(`\n ${cleanMotd}${colors.reset}\n`);
101
+ } catch (err) {
102
+ console.error(`\n ${colors.yellow}Error:${colors.reset} ${err.message}\n`);
103
+ process.exit(1);
104
+ }
105
+ }
106
+ main();
107
+ //#endregion
package/dist/cli.mjs ADDED
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env node
2
+ import { t as ping } from "./src-HHWxQmnZ.mjs";
3
+ //#region src/ansi.ts
4
+ const mcColorMap = {
5
+ "0": "\x1B[30m",
6
+ "1": "\x1B[34m",
7
+ "2": "\x1B[32m",
8
+ "3": "\x1B[36m",
9
+ "4": "\x1B[31m",
10
+ "5": "\x1B[35m",
11
+ "6": "\x1B[33m",
12
+ "7": "\x1B[37m",
13
+ "8": "\x1B[90m",
14
+ "9": "\x1B[94m",
15
+ a: "\x1B[92m",
16
+ b: "\x1B[96m",
17
+ c: "\x1B[91m",
18
+ d: "\x1B[95m",
19
+ e: "\x1B[93m",
20
+ f: "\x1B[97m",
21
+ l: "\x1B[1m",
22
+ m: "\x1B[9m",
23
+ n: "\x1B[4m",
24
+ o: "\x1B[3m",
25
+ r: "\x1B[0m"
26
+ };
27
+ /**
28
+ * Converts Minecraft color codes (§) to ANSI terminal colors.
29
+ */
30
+ function mcToAnsi(text) {
31
+ const reset = "\x1B[0m";
32
+ let result = "";
33
+ const parts = text.split("§");
34
+ result += parts[0];
35
+ for (let i = 1; i < parts.length; i++) {
36
+ const part = parts[i];
37
+ if (part.length === 0) continue;
38
+ const ansi = mcColorMap[part[0].toLowerCase()];
39
+ if (ansi) result += ansi + part.substring(1);
40
+ else result += "§" + part;
41
+ }
42
+ return result + reset;
43
+ }
44
+ //#endregion
45
+ //#region src/cli.ts
46
+ const colors = {
47
+ reset: "\x1B[0m",
48
+ bright: "\x1B[1m",
49
+ cyan: "\x1B[36m",
50
+ green: "\x1B[32m",
51
+ yellow: "\x1B[33m",
52
+ magenta: "\x1B[35m",
53
+ white: "\x1B[37m",
54
+ gray: "\x1B[90m"
55
+ };
56
+ /**
57
+ * Poor man's argument parser
58
+ */
59
+ function parseArgs(args) {
60
+ const result = {};
61
+ for (let i = 0; i < args.length; i++) {
62
+ const arg = args[i];
63
+ if (arg === "--type" || arg === "-t") {
64
+ const val = args[++i]?.toLowerCase();
65
+ if (val === "java" || val === "bedrock") result.type = val;
66
+ } else if (arg === "--timeout") {
67
+ const val = parseInt(args[++i]);
68
+ if (!isNaN(val)) result.timeout = val;
69
+ } else if (!result.target && !arg.startsWith("-")) result.target = arg;
70
+ }
71
+ return result;
72
+ }
73
+ async function main() {
74
+ const parsed = parseArgs(process.argv.slice(2));
75
+ if (!parsed.target) {
76
+ console.log(`\n${colors.bright}Usage:${colors.reset} mcping <host[:port]> [options]`);
77
+ console.log(`\n${colors.bright}Options:${colors.reset}`);
78
+ console.log(` --type, -t <java|bedrock> Force protocol type`);
79
+ console.log(` --timeout <ms> Connection timeout (default: 5000)`);
80
+ console.log(`\n${colors.bright}Examples:${colors.reset}`);
81
+ console.log(` mcping mc.hypixel.net`);
82
+ console.log(` mcping geo.hivebedrock.network --type bedrock`);
83
+ console.log(` mcping play.example.com --timeout 2000\n`);
84
+ process.exit(1);
85
+ }
86
+ try {
87
+ const res = await ping(parsed.target, {
88
+ type: parsed.type,
89
+ timeout: parsed.timeout
90
+ });
91
+ const resolvedStr = `${res.target.ip}:${res.target.port}`;
92
+ const targetDisplay = parsed.target === resolvedStr ? parsed.target : `${parsed.target} ${colors.gray}→${colors.reset} ${resolvedStr}`;
93
+ console.log(`\n ${colors.bright}${targetDisplay}${colors.reset}`);
94
+ const editionStr = `${colors.bright}${res.type.toUpperCase()}${colors.reset}`;
95
+ const versionStr = `${colors.gray}${res.version.name}${colors.reset}`;
96
+ const latencyStr = res.type === "java" ? ` ${colors.magenta}${res.latency}ms${colors.reset}` : "";
97
+ console.log(` ${editionStr} ${colors.gray}•${colors.reset} ${versionStr}${latencyStr}`);
98
+ console.log(` ${colors.bright}${res.players.online}${colors.reset} ${colors.gray}/ ${res.players.max} players${colors.reset}`);
99
+ const cleanMotd = mcToAnsi(res.motd).trim().split("\n").map((line) => line.trim()).filter((line) => line.length > 0).join(`\n `);
100
+ console.log(`\n ${cleanMotd}${colors.reset}\n`);
101
+ } catch (err) {
102
+ console.error(`\n ${colors.yellow}Error:${colors.reset} ${err.message}\n`);
103
+ process.exit(1);
104
+ }
105
+ }
106
+ main();
107
+ //#endregion
108
+ export {};
package/dist/index.cjs ADDED
@@ -0,0 +1,3 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_src = require("./src-Baubd7mm.cjs");
3
+ exports.ping = require_src.ping;
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { t as ping } from "./src-HHWxQmnZ.mjs";
2
+ export { ping };