index.ts 3.02 KB
import type { PageManifest } from '@single-pages/contracts';
import { PAGE_MANIFESTS } from '@single-pages/contracts';

export const APP_NAME = 'single-pages';

export const FRONT_PAGES: readonly PageManifest[] = PAGE_MANIFESTS;

export const DEFAULT_DEV_SERVER_PORT = 4173;
export const DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS = [
  'https://dj.glsx.com.cn',
  'https://frontend.test.glsx.com.cn',
  'http://192.168.3.191:8543',
  'http://192.168.1.146:8080',
] as const;

export const DEFAULT_PRELEARNING_ALLOWED_ORIGINS = [...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS];
export const DEFAULT_REMOTE_BACKEND_API_BASE_PATH = '/backend';

export interface BackendApiPathOptions {
  hostname?: string;
  envBasePath?: string;
}

export function parseSopPreviewAllowedOrigins(rawValue: string | undefined): string[] | '*' {
  const text = rawValue?.trim();

  if (!text) {
    return [...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS];
  }

  if (text === '*') {
    return '*';
  }

  return text
    .split(',')
    .map((item) => item.trim())
    .filter((item) => item.length > 0)
    .map((item) => {
      try {
        return new URL(item).origin;
      } catch {
        return item;
      }
    });
}

export function parsePrelearningAllowedOrigins(rawValue: string | undefined): string[] | '*' {
  const text = rawValue?.trim();

  if (!text) {
    return [...DEFAULT_PRELEARNING_ALLOWED_ORIGINS];
  }

  if (text === '*') {
    return '*';
  }

  return text
    .split(',')
    .map((item) => item.trim())
    .filter((item) => item.length > 0)
    .map((item) => {
      try {
        return new URL(item).origin;
      } catch {
        return item;
      }
    });
}

export function isLocalRuntimeHostname(hostname: string | undefined): boolean {
  const normalized = hostname?.trim().toLowerCase();

  if (!normalized) {
    return true;
  }

  if (
    normalized === 'localhost'
    || normalized === '0.0.0.0'
    || normalized === '::1'
    || normalized.startsWith('127.')
    || normalized.startsWith('192.168.')
    || normalized.startsWith('10.')
  ) {
    return true;
  }

  const private172Match = /^172\.(\d+)\./.exec(normalized);
  if (private172Match) {
    const secondOctet = Number(private172Match[1]);
    return secondOctet >= 16 && secondOctet <= 31;
  }

  return false;
}

export function normalizeBackendApiBasePath(basePath: string): string {
  const trimmed = basePath.trim();

  if (!trimmed || trimmed === '/') {
    return '';
  }

  return `/${trimmed.replace(/^\/+|\/+$/g, '')}`;
}

export function resolveBackendApiBasePath(options: BackendApiPathOptions = {}): string {
  if (options.envBasePath !== undefined) {
    return normalizeBackendApiBasePath(options.envBasePath);
  }

  return isLocalRuntimeHostname(options.hostname) ? '' : DEFAULT_REMOTE_BACKEND_API_BASE_PATH;
}

export function buildBackendApiPath(pathname: string, options: BackendApiPathOptions = {}): string {
  const basePath = resolveBackendApiBasePath(options);
  const normalizedPathname = pathname.startsWith('/') ? pathname : `/${pathname}`;

  return `${basePath}${normalizedPathname}`;
}