fix: use http module instead of fetch (#17)

allows usage of RCZ with NodeJS v14 and v16
This commit is contained in:
2024-03-04 22:53:48 +02:00
parent dd4ca1f1cc
commit 8cc93d55da

View File

@@ -37,6 +37,7 @@ import { Command } from "commander";
import semver from "semver"; import semver from "semver";
import { execSync } from "child_process"; import { execSync } from "child_process";
import { tmpdir } from "os"; import { tmpdir } from "os";
import { request } from "http";
const GetConfig = async () => { const GetConfig = async () => {
if (fs.existsSync(path.join(process.cwd(), ".rczrc"))) { if (fs.existsSync(path.join(process.cwd(), ".rczrc"))) {
@@ -105,6 +106,21 @@ const GetPostReleaseScript = async () => {
return null; return null;
} }
}; };
const isOnline = () => {
return new Promise<boolean>((resolve) => {
request({ method: "GET", hostname: "icanhazip.com" }, (res) => {
res.on("data", () => {});
res.on("end", () => {
resolve(res.statusCode === 200);
});
})
.on("error", () => {
resolve(false);
})
.end();
});
};
const CheckForUpdates = async () => { const CheckForUpdates = async () => {
const updateText = const updateText =
@@ -119,8 +135,7 @@ const CheckForUpdates = async () => {
// even if cached once in a while it should get newest data // even if cached once in a while it should get newest data
if ((cachedVersion && Math.random() < 0.1) || cachedVersion === null) { if ((cachedVersion && Math.random() < 0.1) || cachedVersion === null) {
const isOnline = (await fetch("https://icanhazip.com")).status === 200; if (!(await isOnline())) return;
if (!isOnline) return;
const serverVersion = execSync("npm show @resultium/rcz version") const serverVersion = execSync("npm show @resultium/rcz version")
.toString() .toString()