[READ-ONLY] Mirror of https://github.com/FoxxMD/komodo-utilities. Small utilities to enhance Komodo
gotify komodo
0

Configure Feed

Select the types of activity you want to include in your feed.

fix: Parse url without TLD or protocol

Fixes #2

FoxxMD (Apr 30, 2025, 2:12 PM UTC) b008eb56 95309de1

+53
+8
notifiers/common/stringUtils.ts
··· 3 3 import { URLData } from "./atomic.ts"; 4 4 5 5 const QUOTES_UNWRAP_REGEX: RegExp = new RegExp(/^"(.*)"$/); 6 + // domain:port/path (:port is optional) 7 + const NON_PROTOCOL_DOMAIN_REGEX: RegExp = new RegExp(/^[a-zA-Z0-9-]+(:[0-9]+)?(\/[^\/].*)?$/); 6 8 7 9 export const normalizeWebAddress = (val: string): URLData => { 8 10 let cleanUserUrl = val.trim(); 9 11 const results = parseRegexSingle(QUOTES_UNWRAP_REGEX, val); 10 12 if (results !== undefined && results.groups && results.groups.length > 0) { 11 13 cleanUserUrl = results.groups[0]; 14 + } 15 + const nonProto = parseRegexSingle(NON_PROTOCOL_DOMAIN_REGEX, cleanUserUrl); 16 + if(nonProto !== undefined) { 17 + // url does not have protocol or TLD, need to add protocol so URL constructor doesn't try to use domain as protocol 18 + // correct protocol should be determined by code below 19 + cleanUserUrl = `http://${cleanUserUrl}`; 12 20 } 13 21 14 22 let normal = normalizeUrl(cleanUserUrl, {removeTrailingSlash: true});
+45
notifiers/tests/utils.test.ts
··· 1 + import { expect } from "jsr:@std/expect"; 2 + import { normalizeWebAddress } from "../common/stringUtils.ts"; 3 + 4 + Deno.test({ 5 + name: "Expects fully qualified address to be the same", 6 + fn() { 7 + expect(normalizeWebAddress('https://example.com').normal).toBe('https://example.com'); 8 + expect(normalizeWebAddress('http://example.com').normal).toBe('http://example.com'); 9 + }, 10 + }); 11 + 12 + Deno.test({ 13 + name: "Expects address without protocol to be http", 14 + fn() { 15 + const addr = normalizeWebAddress('example.com'); 16 + expect(addr.normal).toBe('http://example.com'); 17 + expect(addr.url.hostname).toBe('example.com'); 18 + expect(addr.port).toBe(80); 19 + }, 20 + }); 21 + 22 + Deno.test({ 23 + name: "Expects address without protocol but port 443 to be https", 24 + fn() { 25 + expect(normalizeWebAddress('example.com:443').url.protocol).toBe('https:'); 26 + }, 27 + }); 28 + 29 + Deno.test({ 30 + name: "Expects hostname without extension to be detected", 31 + fn() { 32 + expect(normalizeWebAddress('example:8080').url.protocol).toBe('http:'); 33 + expect(normalizeWebAddress('example:443').url.protocol).toBe('https:'); 34 + 35 + const web = normalizeWebAddress('example:8000/test'); 36 + expect(web.url.protocol).toBe('http:'); 37 + expect(web.url.port).toBe('8000'); 38 + expect(web.url.pathname).toBe('/test'); 39 + expect(web.url.hostname).toBe('example'); 40 + 41 + const hostonly = normalizeWebAddress('example'); 42 + expect(hostonly.url.protocol).toBe('http:'); 43 + expect(hostonly.port).toBe(80); 44 + }, 45 + });