feat: Utils to do bindLan and have ipv4 and ipv6 if need be

This commit is contained in:
BluJ
2023-04-27 11:22:42 -06:00
parent 353692bf55
commit c7d38fc7ce
79 changed files with 1754 additions and 1744 deletions

View File

@@ -1,6 +1,6 @@
import { HealthStatus } from "../../types";
import { HealthStatus } from "../../types"
export type CheckResult = {
status: HealthStatus;
message?: string;
};
status: HealthStatus
message?: string
}

View File

@@ -1,5 +1,5 @@
import { Effects } from "../../types";
import { CheckResult } from "./CheckResult";
import { Effects } from "../../types"
import { CheckResult } from "./CheckResult"
export function containsAddress(x: string, port: number) {
const readPorts = x
.split("\n")
@@ -8,8 +8,8 @@ export function containsAddress(x: string, port: number) {
.map((x) => x.split(" ").filter(Boolean)[1]?.split(":")?.[1])
.filter(Boolean)
.map((x) => Number.parseInt(x, 16))
.filter(Number.isFinite);
return readPorts.indexOf(port) >= 0;
.filter(Number.isFinite)
return readPorts.indexOf(port) >= 0
}
/**
@@ -26,12 +26,12 @@ export async function checkPortListening(
): Promise<CheckResult> {
const hasAddress =
containsAddress(await effects.runCommand(`cat /proc/net/tcp`), port) ||
containsAddress(await effects.runCommand("cat /proc/net/udp"), port);
containsAddress(await effects.runCommand("cat /proc/net/udp"), port)
if (hasAddress) {
return { status: "passing", message };
return { status: "passing", message }
}
return {
status: "failing",
message: error,
};
}
}

View File

@@ -1,6 +1,6 @@
import { Effects } from "../../types";
import { CheckResult } from "./CheckResult";
import { timeoutPromise } from "./index";
import { Effects } from "../../types"
import { CheckResult } from "./CheckResult"
import { timeoutPromise } from "./index"
/**
* This is a helper function to check if a web url is reachable.
@@ -23,9 +23,9 @@ export const checkWebUrl = async (
message: successMessage,
}))
.catch((e) => {
effects.warn(`Error while fetching URL: ${url}`);
effects.error(JSON.stringify(e));
effects.error(e.toString());
return { status: "failing" as const, message: errorMessage };
});
};
effects.console.warn(`Error while fetching URL: ${url}`)
effects.console.error(JSON.stringify(e))
effects.console.error(e.toString())
return { status: "failing" as const, message: errorMessage }
})
}

View File

@@ -1,11 +1,11 @@
import { runHealthScript } from "./runHealthScript";
export { checkPortListening } from "./checkPortListening";
export { CheckResult } from "./CheckResult";
export { checkWebUrl } from "./checkWebUrl";
import { runHealthScript } from "./runHealthScript"
export { checkPortListening } from "./checkPortListening"
export { CheckResult } from "./CheckResult"
export { checkWebUrl } from "./checkWebUrl"
export function timeoutPromise(ms: number, { message = "Timed out" } = {}) {
return new Promise<never>((resolve, reject) =>
setTimeout(() => reject(new Error(message)), ms),
);
)
}
export { runHealthScript };
export { runHealthScript }

View File

@@ -1,6 +1,6 @@
import { CommandType, Effects } from "../../types";
import { CheckResult } from "./CheckResult";
import { timeoutPromise } from "./index";
import { CommandType, Effects } from "../../types"
import { CheckResult } from "./CheckResult"
import { timeoutPromise } from "./index"
/**
* Running a health script, is used when we want to have a simple
@@ -23,13 +23,13 @@ export const runHealthScript = async <A extends string>(
effects.runCommand(runCommand, { timeoutMillis: timeout }),
timeoutPromise(timeout),
]).catch((e) => {
effects.warn(errorMessage);
effects.warn(JSON.stringify(e));
effects.warn(e.toString());
throw { status: "failing", message: errorMessage } as CheckResult;
});
effects.console.warn(errorMessage)
effects.console.warn(JSON.stringify(e))
effects.console.warn(e.toString())
throw { status: "failing", message: errorMessage } as CheckResult
})
return {
status: "passing",
message: message(res),
} as CheckResult;
};
} as CheckResult
}