Skip to content

Commit

Permalink
feat: prefix "_minecraft._tcp." if needed for SRV lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan4m1 committed Sep 29, 2024
1 parent 334f4f4 commit f791dc3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fetchServerInfo({
To perform an SRV record lookup and query a Minecraft server using only a hostname, use:

```ts
// makes an SRV lookup for _minecraft._tcp.example.com
fetchServerInfo({
hostname: 'example.com',
timeout: 1000
Expand Down Expand Up @@ -71,6 +72,7 @@ import { fetchServerInfo } from 'minestat-es';
(async () => {
try {
// query by hostname (SRV lookup)
// _minecraft._tcp. will be prepended unless you supply it
const { online, error, players } = await fetchServerInfo({
hostname: 'mc.example.com'
});
Expand Down
19 changes: 17 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ describe('minestat-es', () => {
});
test('no SRV records', async () => {
const expectedError = new Error(
`No DNS records found for hostname ${hostname}`
`No DNS records found for hostname _minecraft._tcp.${hostname}`
);
const mockData: SrvRecord[] = [];

Expand All @@ -292,6 +292,21 @@ describe('minestat-es', () => {
expect(error).toEqual(expectedError);
}
});
test('does not prepend _minecraft._tcp. to hostname if already present', async () => {
const expectedError = new Error(
`No DNS records found for hostname _minecraft._tcp.${hostname}`
);
const mockData: SrvRecord[] = [];

resolveMock.mockImplementation(() => Promise.resolve(mockData));

expect.assertions(1);
try {
await fetchServerInfo({ hostname: `_minecraft._tcp.${hostname}` });
} catch (error) {
expect(error).toEqual(expectedError);
}
});
test('successful SRV lookup', async () => {
const socket = createMockSocket(
jest
Expand All @@ -307,7 +322,7 @@ describe('minestat-es', () => {
)
);
const mockData: SrvRecord[] = [
{ name: hostname, port, priority: 1, weight: 1 }
{ name: `_minecraft._tcp.${hostname}`, port, priority: 1, weight: 1 }
];

connectMock.mockImplementation(() => socket);
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ export async function fetchServerInfo(
// obtain address/port from DNS if required
if ('hostname' in options) {
const hostOptions = options as HostnameArgs;
const records = await dns.resolveSrv(hostOptions.hostname);
const mcHost = `${hostOptions.hostname.startsWith('_minecraft._tcp.') ? '' : '_minecraft._tcp.'}${hostOptions.hostname}`;
const records = await dns.resolveSrv(mcHost);

if (!records.length) {
throw new Error(
`No DNS records found for hostname ${hostOptions.hostname}`
);
throw new Error(`No DNS records found for hostname ${mcHost}`);
}

const record = records[Math.floor(Math.random() * records.length)];
Expand Down

0 comments on commit f791dc3

Please sign in to comment.