Skip to content

Commit c9fb4c6

Browse files
committed
fix: normalize URL
1 parent 3073ed3 commit c9fb4c6

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

apps/uptime/src/actions.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ export async function lookupWebsite(
5959
}
6060
}
6161

62-
async function pingWebsite(url: string): Promise<FetchSuccess | FetchFailure> {
62+
function normalizeUrl(url: string): string {
63+
if (url.startsWith("http://") || url.startsWith("https://")) {
64+
return url;
65+
}
66+
return `https://${url}`;
67+
}
68+
69+
async function pingWebsite(originalUrl: string): Promise<FetchSuccess | FetchFailure> {
70+
const url = normalizeUrl(originalUrl);
6371
const abort = new AbortController();
6472
const timeout = setTimeout(() => abort.abort(), CONFIG.timeout);
6573
const start = performance.now();
@@ -130,7 +138,7 @@ async function pingWebsite(url: string): Promise<FetchSuccess | FetchFailure> {
130138
: error.message;
131139
}
132140

133-
console.error("Ping failed:", JSON.stringify({ url, error: message }));
141+
console.error("Ping failed:", JSON.stringify({ url: originalUrl, error: message }));
134142

135143
return {
136144
ok: false,
@@ -292,11 +300,12 @@ export async function checkUptime(
292300
maxRetries: number = CONFIG.maxRetries
293301
): Promise<ActionResult<UptimeData>> {
294302
try {
303+
const normalizedUrl = normalizeUrl(url);
295304
const timestamp = Date.now();
296305

297306
// gather all the data we need in parallel
298307
const [pingResult, lastBeat, probe] = await Promise.all([
299-
pingWebsite(url),
308+
pingWebsite(normalizedUrl),
300309
getLastHeartbeat(siteId),
301310
getProbeMetadata(),
302311
]);
@@ -309,13 +318,13 @@ export async function checkUptime(
309318

310319
// site is down - minimal data
311320
if (!pingResult.ok) {
312-
const cert = await checkCertificate(url);
321+
const cert = await checkCertificate(normalizedUrl);
313322

314323
return {
315324
success: true,
316325
data: {
317326
site_id: siteId,
318-
url,
327+
url: normalizedUrl,
319328
timestamp,
320329
status,
321330
http_code: pingResult.statusCode,
@@ -341,7 +350,7 @@ export async function checkUptime(
341350

342351
// site is up - full enrichment
343352
const [cert, contentHash] = await Promise.all([
344-
checkCertificate(url),
353+
checkCertificate(normalizedUrl),
345354
Promise.resolve(
346355
createHash("sha256").update(pingResult.content).digest("hex")
347356
),
@@ -352,7 +361,7 @@ export async function checkUptime(
352361
success: true,
353362
data: {
354363
site_id: siteId,
355-
url,
364+
url: normalizedUrl,
356365
timestamp,
357366
status,
358367
http_code: pingResult.statusCode,

0 commit comments

Comments
 (0)