Compare commits
6 Commits
74b3b283f7
..
1.17.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 9fe4f78269 | |||
| bd3d6994c1 | |||
| 5fd78817a8 | |||
| 72bc125f84 | |||
| b18ea66def | |||
| 41f541a531 |
|
Before Width: | Height: | Size: 765 KiB After Width: | Height: | Size: 588 KiB |
|
Before Width: | Height: | Size: 742 KiB After Width: | Height: | Size: 569 KiB |
|
Before Width: | Height: | Size: 765 KiB After Width: | Height: | Size: 588 KiB |
|
Before Width: | Height: | Size: 2.9 MiB After Width: | Height: | Size: 2.4 MiB |
|
Before Width: | Height: | Size: 243 KiB After Width: | Height: | Size: 274 KiB |
@@ -29,9 +29,65 @@ import {
|
|||||||
} from "drizzle-orm";
|
} from "drizzle-orm";
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
|
import NodeCache from "node-cache";
|
||||||
|
import semver from "semver";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
|
|
||||||
|
const olmVersionCache = new NodeCache({ stdTTL: 3600 });
|
||||||
|
|
||||||
|
async function getLatestOlmVersion(): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const cachedVersion = olmVersionCache.get<string>("latestOlmVersion");
|
||||||
|
if (cachedVersion) {
|
||||||
|
return cachedVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), 1500);
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
"https://api.github.com/repos/fosrl/olm/tags",
|
||||||
|
{
|
||||||
|
signal: controller.signal
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
logger.warn(
|
||||||
|
`Failed to fetch latest Olm version from GitHub: ${response.status} ${response.statusText}`
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tags = await response.json();
|
||||||
|
if (!Array.isArray(tags) || tags.length === 0) {
|
||||||
|
logger.warn("No tags found for Olm repository");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
tags = tags.filter((version) => !version.name.includes("rc"));
|
||||||
|
const latestVersion = tags[0].name;
|
||||||
|
|
||||||
|
olmVersionCache.set("latestOlmVersion", latestVersion, 3600);
|
||||||
|
|
||||||
|
return latestVersion;
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.name === "AbortError") {
|
||||||
|
logger.warn("Request to fetch latest Olm version timed out (1.5s)");
|
||||||
|
} else if (error.cause?.code === "UND_ERR_CONNECT_TIMEOUT") {
|
||||||
|
logger.warn("Connection timeout while fetching latest Olm version");
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
"Error fetching latest Olm version:",
|
||||||
|
error.message || error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const listClientsParamsSchema = z.strictObject({
|
const listClientsParamsSchema = z.strictObject({
|
||||||
orgId: z.string()
|
orgId: z.string()
|
||||||
});
|
});
|
||||||
@@ -357,45 +413,44 @@ export async function listClients(
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// REMOVING THIS BECAUSE WE HAVE DIFFERENT TYPES OF CLIENTS NOW
|
const latestOlVersionPromise = getLatestOlmVersion();
|
||||||
// const latestOlmVersionPromise = getLatestOlmVersion();
|
|
||||||
|
|
||||||
// const olmsWithUpdates: OlmWithUpdateAvailable[] = clientsWithSites.map(
|
const olmsWithUpdates: OlmWithUpdateAvailable[] = clientsWithSites.map(
|
||||||
// (client) => {
|
(client) => {
|
||||||
// const OlmWithUpdate: OlmWithUpdateAvailable = { ...client };
|
const OlmWithUpdate: OlmWithUpdateAvailable = { ...client };
|
||||||
// // Initially set to false, will be updated if version check succeeds
|
// Initially set to false, will be updated if version check succeeds
|
||||||
// OlmWithUpdate.olmUpdateAvailable = false;
|
OlmWithUpdate.olmUpdateAvailable = false;
|
||||||
// return OlmWithUpdate;
|
return OlmWithUpdate;
|
||||||
// }
|
}
|
||||||
// );
|
);
|
||||||
|
|
||||||
// Try to get the latest version, but don't block if it fails
|
// Try to get the latest version, but don't block if it fails
|
||||||
// try {
|
try {
|
||||||
// const latestOlmVersion = await latestOlVersionPromise;
|
const latestOlVersion = await latestOlVersionPromise;
|
||||||
|
|
||||||
// if (latestOlVersion) {
|
if (latestOlVersion) {
|
||||||
// olmsWithUpdates.forEach((client) => {
|
olmsWithUpdates.forEach((client) => {
|
||||||
// try {
|
try {
|
||||||
// client.olmUpdateAvailable = semver.lt(
|
client.olmUpdateAvailable = semver.lt(
|
||||||
// client.olmVersion ? client.olmVersion : "",
|
client.olmVersion ? client.olmVersion : "",
|
||||||
// latestOlVersion
|
latestOlVersion
|
||||||
// );
|
);
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// client.olmUpdateAvailable = false;
|
client.olmUpdateAvailable = false;
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// // Log the error but don't let it block the response
|
// Log the error but don't let it block the response
|
||||||
// logger.warn(
|
logger.warn(
|
||||||
// "Failed to check for OLM updates, continuing without update info:",
|
"Failed to check for OLM updates, continuing without update info:",
|
||||||
// error
|
error
|
||||||
// );
|
);
|
||||||
// }
|
}
|
||||||
|
|
||||||
return response<ListClientsResponse>(res, {
|
return response<ListClientsResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
clients: clientsWithSites,
|
clients: olmsWithUpdates,
|
||||||
pagination: {
|
pagination: {
|
||||||
total: totalCount,
|
total: totalCount,
|
||||||
page,
|
page,
|
||||||
|
|||||||
@@ -30,10 +30,65 @@ import {
|
|||||||
} from "drizzle-orm";
|
} from "drizzle-orm";
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
|
import NodeCache from "node-cache";
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
|
|
||||||
|
const olmVersionCache = new NodeCache({ stdTTL: 3600 });
|
||||||
|
|
||||||
|
async function getLatestOlmVersion(): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const cachedVersion = olmVersionCache.get<string>("latestOlmVersion");
|
||||||
|
if (cachedVersion) {
|
||||||
|
return cachedVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), 1500);
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
"https://api.github.com/repos/fosrl/olm/tags",
|
||||||
|
{
|
||||||
|
signal: controller.signal
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
logger.warn(
|
||||||
|
`Failed to fetch latest Olm version from GitHub: ${response.status} ${response.statusText}`
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tags = await response.json();
|
||||||
|
if (!Array.isArray(tags) || tags.length === 0) {
|
||||||
|
logger.warn("No tags found for Olm repository");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
tags = tags.filter((version) => !version.name.includes("rc"));
|
||||||
|
const latestVersion = tags[0].name;
|
||||||
|
|
||||||
|
olmVersionCache.set("latestOlmVersion", latestVersion, 3600);
|
||||||
|
|
||||||
|
return latestVersion;
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.name === "AbortError") {
|
||||||
|
logger.warn("Request to fetch latest Olm version timed out (1.5s)");
|
||||||
|
} else if (error.cause?.code === "UND_ERR_CONNECT_TIMEOUT") {
|
||||||
|
logger.warn("Connection timeout while fetching latest Olm version");
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
"Error fetching latest Olm version:",
|
||||||
|
error.message || error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const listUserDevicesParamsSchema = z.strictObject({
|
const listUserDevicesParamsSchema = z.strictObject({
|
||||||
orgId: z.string()
|
orgId: z.string()
|
||||||
});
|
});
|
||||||
@@ -398,30 +453,29 @@ export async function listUserDevices(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// REMOVING THIS BECAUSE WE HAVE DIFFERENT TYPES OF CLIENTS NOW
|
// Try to get the latest version, but don't block if it fails
|
||||||
// // Try to get the latest version, but don't block if it fails
|
try {
|
||||||
// try {
|
const latestOlmVersion = await getLatestOlmVersion();
|
||||||
// const latestOlmVersion = await getLatestOlmVersion();
|
|
||||||
|
|
||||||
// if (latestOlmVersion) {
|
if (latestOlmVersion) {
|
||||||
// olmsWithUpdates.forEach((client) => {
|
olmsWithUpdates.forEach((client) => {
|
||||||
// try {
|
try {
|
||||||
// client.olmUpdateAvailable = semver.lt(
|
client.olmUpdateAvailable = semver.lt(
|
||||||
// client.olmVersion ? client.olmVersion : "",
|
client.olmVersion ? client.olmVersion : "",
|
||||||
// latestOlmVersion
|
latestOlmVersion
|
||||||
// );
|
);
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// client.olmUpdateAvailable = false;
|
client.olmUpdateAvailable = false;
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// // Log the error but don't let it block the response
|
// Log the error but don't let it block the response
|
||||||
// logger.warn(
|
logger.warn(
|
||||||
// "Failed to check for OLM updates, continuing without update info:",
|
"Failed to check for OLM updates, continuing without update info:",
|
||||||
// error
|
error
|
||||||
// );
|
);
|
||||||
// }
|
}
|
||||||
|
|
||||||
return response<ListUserDevicesResponse>(res, {
|
return response<ListUserDevicesResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@@ -21,11 +21,6 @@ import semver from "semver";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
|
|
||||||
// Stale-while-revalidate: keeps the last successfully fetched version so that
|
|
||||||
// a transient network failure / timeout does not flip every site back to
|
|
||||||
// newtUpdateAvailable: false.
|
|
||||||
let staleNewtVersion: string | null = null;
|
|
||||||
|
|
||||||
async function getLatestNewtVersion(): Promise<string | null> {
|
async function getLatestNewtVersion(): Promise<string | null> {
|
||||||
try {
|
try {
|
||||||
const cachedVersion = await cache.get<string>("latestNewtVersion");
|
const cachedVersion = await cache.get<string>("latestNewtVersion");
|
||||||
@@ -34,7 +29,7 @@ async function getLatestNewtVersion(): Promise<string | null> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timeoutId = setTimeout(() => controller.abort(), 1500);
|
const timeoutId = setTimeout(() => controller.abort(), 1500); // Reduced timeout to 1.5 seconds
|
||||||
|
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
"https://api.github.com/repos/fosrl/newt/tags",
|
"https://api.github.com/repos/fosrl/newt/tags",
|
||||||
@@ -49,46 +44,18 @@ async function getLatestNewtVersion(): Promise<string | null> {
|
|||||||
logger.warn(
|
logger.warn(
|
||||||
`Failed to fetch latest Newt version from GitHub: ${response.status} ${response.statusText}`
|
`Failed to fetch latest Newt version from GitHub: ${response.status} ${response.statusText}`
|
||||||
);
|
);
|
||||||
return staleNewtVersion;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tags = await response.json();
|
let tags = await response.json();
|
||||||
if (!Array.isArray(tags) || tags.length === 0) {
|
if (!Array.isArray(tags) || tags.length === 0) {
|
||||||
logger.warn("No tags found for Newt repository");
|
logger.warn("No tags found for Newt repository");
|
||||||
return staleNewtVersion;
|
return null;
|
||||||
}
|
}
|
||||||
|
tags = tags.filter((version) => !version.name.includes("rc"));
|
||||||
// Remove release-candidates, then sort descending by semver so that
|
|
||||||
// duplicate tags (e.g. "1.10.3" and "v1.10.3") and any ordering quirks
|
|
||||||
// from the GitHub API do not cause an older tag to be selected.
|
|
||||||
tags = tags.filter((tag: any) => !tag.name.includes("rc"));
|
|
||||||
tags.sort((a: any, b: any) => {
|
|
||||||
const va = semver.coerce(a.name);
|
|
||||||
const vb = semver.coerce(b.name);
|
|
||||||
if (!va && !vb) return 0;
|
|
||||||
if (!va) return 1;
|
|
||||||
if (!vb) return -1;
|
|
||||||
return semver.rcompare(va, vb);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Deduplicate: keep only the first (highest) entry per normalised version
|
|
||||||
const seen = new Set<string>();
|
|
||||||
tags = tags.filter((tag: any) => {
|
|
||||||
const normalised = semver.coerce(tag.name)?.version;
|
|
||||||
if (!normalised || seen.has(normalised)) return false;
|
|
||||||
seen.add(normalised);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (tags.length === 0) {
|
|
||||||
logger.warn("No valid semver tags found for Newt repository");
|
|
||||||
return staleNewtVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
const latestVersion = tags[0].name;
|
const latestVersion = tags[0].name;
|
||||||
|
|
||||||
staleNewtVersion = latestVersion;
|
await cache.set("latestNewtVersion", latestVersion, 3600);
|
||||||
await cache.set("cache:latestNewtVersion", latestVersion, 3600);
|
|
||||||
|
|
||||||
return latestVersion;
|
return latestVersion;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -106,7 +73,7 @@ async function getLatestNewtVersion(): Promise<string | null> {
|
|||||||
error.message || error
|
error.message || error
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return staleNewtVersion;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||