UNPKG

zephyr-agent

Version:
46 lines (45 loc) • 1.65 kB
import "node:module"; function normalizeBasePath(baseHref) { if (!baseHref) return ''; let normalized = baseHref.trim(); if (!normalized || [ '/', './', '.', 'auto' ].includes(normalized)) return ''; if (/^(?:https?:)?\/\//i.test(normalized)) try { normalized = new URL(normalized.startsWith('//') ? `https:${normalized}` : normalized).pathname; } catch { return ''; } normalized = normalized.split(/[?#]/, 1)[0]?.trim() ?? ''; const segments = normalized.replace(/^\.\//, '').split('/').map((segment)=>segment.trim()).filter((segment)=>segment && '.' !== segment); return segments.includes('..') ? '' : segments.join('/'); } function isAbsolutePath(path) { return path.startsWith('/') || path.includes('://'); } function isIndexHtml(path) { return path.endsWith('index.html'); } function applyBaseHrefToPath(path, normalizedBaseHref) { if (isAbsolutePath(path) || isIndexHtml(path)) return path; if (!normalizedBaseHref) return path; return `${normalizedBaseHref}/${path}`; } function applyBaseHrefToAssets(assetsMap, baseHref) { const normalizedBaseHref = normalizeBasePath(baseHref); if (!normalizedBaseHref) return assetsMap; const result = {}; Object.values(assetsMap).forEach((asset)=>{ const newAsset = { ...asset }; newAsset.path = applyBaseHrefToPath(asset.path, normalizedBaseHref); result[newAsset.path] = newAsset; }); return result; } export { applyBaseHrefToAssets, applyBaseHrefToPath, normalizeBasePath }; //# sourceMappingURL=ze-basehref-handler.mjs.map