Files
bare-operating-system/packages/bare-os-booter/lib/http-fetch-url.js
T
2026-04-03 17:45:55 -04:00

51 lines
1.8 KiB
JavaScript

/**
* URL normalization for fetch-based curl/wget subsets (scheme guessing like CLI tools).
* Flag support matrix: ../CLI_PARITY.md
*/
/** curl-style default (see https://everything.curl.dev/http/modify/user-agent.html); not a real libcurl build version. */
export const DEFAULT_CURL_USER_AGENT = 'curl/8.14.1'
/** GNU wget-style default (many builds use Wget/VERSION (linux-gnu)). */
export const DEFAULT_WGET_USER_AGENT = 'Wget/1.25.0 (linux-gnu)'
/** True if the string is usable by our fetch stack without guessing a scheme. */
export function isSupportedFetchUrl(s) {
return (
/^https?:\/\//i.test(s) || s.startsWith('data:') || s.startsWith('file://')
)
}
/**
* If there is no http(s)/data/file scheme, prepend http:// for host-like URLs (wget default).
* Protocol-relative //host → https://host. Leaves other schemes (ftp:, mailto:, …) unchanged.
* Does not rewrite `-`, ./ ../, or absolute /paths (not treated as remote URLs).
*/
export function normalizeFetchUrl(s) {
const t = String(s)
if (isSupportedFetchUrl(t)) return t
if (t.startsWith('//')) return 'https:' + t
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(t)) return t
if (t === '-') return t
if (t.startsWith('/') || t.startsWith('./') || t.startsWith('../')) return t
return 'http://' + t
}
/**
* Default local filename from URL path (last segment, or index.html for directory URLs).
* Matches wget/curl -O-style naming for simple cases.
* @param {string} urlString
*/
export function defaultFetchSaveName(urlString) {
try {
const u = new URL(urlString)
let p = u.pathname || '/'
if (p.endsWith('/') || p === '/' || p === '') return 'index.html'
const parts = p.split('/').filter(Boolean)
const base = parts[parts.length - 1]
return base && base.length > 0 ? base : 'index.html'
} catch {
return 'index.html'
}
}