import fs from 'node:fs'; import path from 'node:path'; const LOCAL_HOST_PATTERN = /^(localhost|127\.0\.0\.1|\[::1\]|192\.168\.|10\.|100\.)/i; export function resolvePublicRequestOrigin({ hostHeader = '', forwardedProto = '', protocol = 'http' } = {}) { const host = String(hostHeader ?? '').split(',')[0].trim(); if (!host) return ''; const fwdProto = String(forwardedProto ?? '').split(',')[0].trim(); const isLocalHost = LOCAL_HOST_PATTERN.test(host); const resolvedProtocol = isLocalHost ? fwdProto || protocol || 'http' : 'https'; return `${resolvedProtocol}://${host}`; } export function buildPublishedHtmlViewContext({ origin = '', requestPath = '', filePath = '', thumbnailPngPathForSvg = (value) => value, fileExists = fs.existsSync, } = {}) { const cleanPath = String(requestPath ?? '').split('?')[0].split('#')[0]; const servedName = path.basename(String(filePath ?? '')); const urlLast = decodeURIComponent(cleanPath.split('/').filter(Boolean).pop() ?? ''); const isImplicitIndex = urlLast.toLowerCase() !== servedName.toLowerCase(); const pageUrl = !origin ? '' : `${origin}${isImplicitIndex && !cleanPath.endsWith('/') ? `${cleanPath}/` : cleanPath}`; const pageDirUrl = !origin ? '' : isImplicitIndex ? `${origin}${cleanPath.endsWith('/') ? cleanPath : `${cleanPath}/`}` : `${origin}${cleanPath.slice(0, cleanPath.lastIndexOf('/') + 1)}`; let fallbackImageUrl = ''; const svgSibling = String(filePath ?? '').replace(/\.[^./]+$/, '.thumbnail.svg'); if (pageDirUrl && fileExists(svgSibling)) { const pngName = path.basename(thumbnailPngPathForSvg(svgSibling)); fallbackImageUrl = `${pageDirUrl}${pngName}`; } return { origin, cleanPath, pageUrl, pageDirUrl, fallbackImageUrl, }; } export const mindspacePublicPageContextInternals = { LOCAL_HOST_PATTERN, };