datasquirel/package-shared/utils/grab-host-names.ts

34 lines
1010 B
TypeScript
Raw Normal View History

2024-11-13 13:13:10 +00:00
// @ts-check
2025-01-10 19:10:28 +00:00
import https from "https";
import http from "http";
2024-11-13 13:13:10 +00:00
2025-01-10 19:10:28 +00:00
type GrabHostNamesReturn = {
host: string;
port: number | string;
scheme: typeof http | typeof https;
user_id: string | number;
};
2024-11-13 13:13:10 +00:00
/**
* # Grab Names For Query
*/
2025-01-10 19:10:28 +00:00
export default function grabHostNames(): GrabHostNamesReturn {
2024-11-13 13:13:10 +00:00
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
return {
host: remoteHost || localHost || "datasquirel.com",
port: remoteHostPort || localHostPort || 443,
scheme: scheme?.match(/^http$/i) ? http : https,
user_id: String(process.env.DSQL_API_USER_ID || 0),
2024-11-13 13:13:10 +00:00
};
}