2 car garage
This commit is contained in:
43
src/utils/dnsResolver.ts
Normal file
43
src/utils/dnsResolver.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import dns from "dns";
|
||||
|
||||
export type ResolvedServer = {
|
||||
/**
|
||||
* The IP address of the server.
|
||||
*/
|
||||
ip: string;
|
||||
|
||||
/**
|
||||
* The port of the server.
|
||||
*/
|
||||
port: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves a minecraft server domain to an
|
||||
* IP address and port using the SRV record.
|
||||
*
|
||||
* @param domain the domain to resolve
|
||||
* @returns the resolved minecraft server
|
||||
*/
|
||||
export async function resolveDns(domain: string): Promise<ResolvedServer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
dns.resolveSrv(`_minecraft._tcp.${domain}`, (err, records) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
const record = records[0];
|
||||
if (record == undefined) {
|
||||
return reject(undefined);
|
||||
}
|
||||
resolve({
|
||||
ip: record.name,
|
||||
port: record.port,
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
reject(undefined);
|
||||
}
|
||||
});
|
||||
}
|
41
src/utils/fsUtils.ts
Normal file
41
src/utils/fsUtils.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import fs from "fs";
|
||||
|
||||
/**
|
||||
* Creates a directory at the given path.
|
||||
*
|
||||
* @param path the path to the file
|
||||
* @param recursive whether to create the directory tree if it doesn't exist (defaults to true)
|
||||
* @returns a promise that resolves when the file is created
|
||||
*/
|
||||
export async function createDirectory(
|
||||
path: string,
|
||||
recursive?: boolean
|
||||
): Promise<void> {
|
||||
if (recursive == undefined) {
|
||||
recursive = true; // Set to true by default
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.mkdir(path, { recursive: recursive }, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file exists at the given path.
|
||||
*
|
||||
* @param path the path to the file
|
||||
* @returns a promise that returns true if the file exists, false otherwise
|
||||
*/
|
||||
export async function exists(path: string): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
fs.exists(path, (exists) => {
|
||||
resolve(exists);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user