LCOV - code coverage report
Current view: top level - pkg/lib/cockpit/_internal - location-utils.ts Coverage Total Hit
Test: cockpit Lines: 100.0 % 50 50
Test Date: 2026-06-25 11:17:56

            Line data    Source code
       1              : // SPDX-License-Identifier: LGPL-2.1-or-later
       2          758 : function get_url_root(): string | null {
       3          758 :     const meta_url_root = document.head.querySelector("meta[name='url-root']");
       4          146 :     if (meta_url_root instanceof HTMLMetaElement) {
       5          146 :         return meta_url_root.content.replace(/^\/+|\/+$/g, '');
       6          138 :     } else {
       7              :         // fallback for cockpit-ws < 272
       8          750 :         try {
       9              :             // Sometimes this throws a SecurityError such as during testing
      10          750 :             return window.localStorage.getItem('url-root');
      11          138 :         } catch {
      12          138 :             return null;
      13          138 :         }
      14          750 :     }
      15          758 : }
      16          758 : export const url_root = get_url_root();
      17              : 
      18          758 : export const transport_origin = window.location.origin;
      19              : 
      20          369 : export function calculate_application(): string {
      21           67 :     let path = window.location.pathname || "/";
      22          369 :     let _url_root = url_root;
      23           67 :     if (window.mock?.pathname)
      24           67 :         path = window.mock.pathname;
      25           67 :     if (window.mock?.url_root)
      26           67 :         _url_root = window.mock.url_root;
      27              : 
      28           71 :     if (_url_root && path.indexOf('/' + _url_root) === 0)
      29           67 :         path = path.replace('/' + _url_root, '') || '/';
      30              : 
      31          346 :     if (path.indexOf("/cockpit/") !== 0 && path.indexOf("/cockpit+") !== 0) {
      32          346 :         if (path.indexOf("/=") === 0)
      33           67 :             path = "/cockpit+" + path.split("/")[1];
      34              :         else
      35          346 :             path = "/cockpit";
      36          346 :     }
      37              : 
      38          369 :     return path.split("/")[1];
      39          369 : }
      40              : 
      41          345 : export function calculate_url(suffix?: string): string {
      42          345 :     if (!suffix)
      43          345 :         suffix = "socket";
      44          345 :     const window_loc = window.location.toString();
      45          345 :     let _url_root = url_root;
      46              : 
      47           63 :     if (window.mock?.url)
      48           63 :         return window.mock.url;
      49           63 :     if (window.mock?.url_root)
      50           63 :         _url_root = window.mock.url_root;
      51              : 
      52          345 :     let prefix = calculate_application();
      53          345 :     if (_url_root)
      54           67 :         prefix = _url_root + "/" + prefix;
      55              : 
      56          344 :     if (window_loc.indexOf('http:') === 0) {
      57          344 :         return "ws://" + window.location.host + "/" + prefix + "/" + suffix;
      58           63 :     } else if (window_loc.indexOf('https:') === 0) {
      59           64 :         return "wss://" + window.location.host + "/" + prefix + "/" + suffix;
      60           63 :     } else {
      61           63 :         throw new Error("Cockpit must be used over http or https");
      62           63 :     }
      63          345 : }
        

Generated by: LCOV version 2.0-1