feat: add HTTP and WebSocket development proxy
This commit is contained in:
+55
-2
@@ -13,6 +13,26 @@ export interface IcebergServerOptions {
|
||||
port?: number;
|
||||
strictPort?: boolean;
|
||||
open?: boolean;
|
||||
proxy?: ProxyConfig;
|
||||
}
|
||||
|
||||
export interface ProxyOptions {
|
||||
target: string;
|
||||
changeOrigin?: boolean;
|
||||
ws?: boolean;
|
||||
rewrite?: (path: string) => string;
|
||||
secure?: boolean;
|
||||
}
|
||||
|
||||
export type ProxyConfig = Record<string, string | ProxyOptions>;
|
||||
|
||||
export interface ResolvedProxyOptions {
|
||||
target: string;
|
||||
targetUrl: URL;
|
||||
changeOrigin: boolean;
|
||||
ws: boolean;
|
||||
rewrite?: (path: string) => string;
|
||||
secure: boolean;
|
||||
}
|
||||
|
||||
export interface IcebergConfig {
|
||||
@@ -26,7 +46,13 @@ export interface ResolvedIcebergConfig {
|
||||
root: string;
|
||||
base: string;
|
||||
build: Required<IcebergBuildOptions>;
|
||||
server: Required<IcebergServerOptions>;
|
||||
server: {
|
||||
host: string;
|
||||
port: number;
|
||||
strictPort: boolean;
|
||||
open: boolean;
|
||||
proxy: Record<string, ResolvedProxyOptions>;
|
||||
};
|
||||
}
|
||||
|
||||
export function defineConfig(config: IcebergConfig): IcebergConfig {
|
||||
@@ -55,7 +81,8 @@ export async function loadConfig(cwd: string, inline?: IcebergConfig): Promise<R
|
||||
host: merged.server?.host ?? 'localhost',
|
||||
port: merged.server?.port ?? 3000,
|
||||
strictPort: merged.server?.strictPort ?? false,
|
||||
open: merged.server?.open ?? false
|
||||
open: merged.server?.open ?? false,
|
||||
proxy: normalizeProxy(merged.server?.proxy)
|
||||
};
|
||||
if (merged.base !== undefined && typeof merged.base !== 'string') throw new Error('Iceberg config: base must be a string.');
|
||||
if (typeof build.outDir !== 'string' || !build.outDir) throw new Error('Iceberg config: build.outDir must be a non-empty string.');
|
||||
@@ -66,6 +93,32 @@ export async function loadConfig(cwd: string, inline?: IcebergConfig): Promise<R
|
||||
return {root, base: normalizeBase(merged.base ?? '/'), build, server};
|
||||
}
|
||||
|
||||
function normalizeProxy(proxy: ProxyConfig | undefined): Record<string, ResolvedProxyOptions> {
|
||||
if (proxy === undefined) return {};
|
||||
if (!proxy || typeof proxy !== 'object' || Array.isArray(proxy)) throw new Error('Iceberg config: server.proxy must be an object.');
|
||||
const normalized: Record<string, ResolvedProxyOptions> = {};
|
||||
for (const [route, value] of Object.entries(proxy)) {
|
||||
if (!route.startsWith('/') || route.includes('?') || route.includes('#')) throw new Error(`Iceberg config: proxy route '${route}' must be a path beginning with '/'.`);
|
||||
if (route === '/__iceberg_ws' || route.startsWith('/__iceberg_ws/')) throw new Error(`Iceberg config: proxy route '${route}' conflicts with Iceberg's reserved /__iceberg_ws endpoint.`);
|
||||
const options = typeof value === 'string' ? {target: value} : value;
|
||||
if (!options || typeof options !== 'object' || typeof options.target !== 'string') throw new Error(`Iceberg config: proxy route '${route}' must define a target URL.`);
|
||||
let targetUrl: URL;
|
||||
try { targetUrl = new URL(options.target); } catch { throw new Error(`Iceberg config: proxy target '${options.target}' is not a valid URL.`); }
|
||||
if (targetUrl.protocol !== 'http:' && targetUrl.protocol !== 'https:') throw new Error(`Iceberg config: proxy target '${options.target}' must use http:// or https://.`);
|
||||
if (options.rewrite !== undefined && typeof options.rewrite !== 'function') throw new Error(`Iceberg config: proxy rewrite for '${route}' must be a function.`);
|
||||
if (options.changeOrigin !== undefined && typeof options.changeOrigin !== 'boolean') throw new Error(`Iceberg config: proxy changeOrigin for '${route}' must be a boolean.`);
|
||||
if (options.ws !== undefined && typeof options.ws !== 'boolean') throw new Error(`Iceberg config: proxy ws for '${route}' must be a boolean.`);
|
||||
if (options.secure !== undefined && typeof options.secure !== 'boolean') throw new Error(`Iceberg config: proxy secure for '${route}' must be a boolean.`);
|
||||
normalized[normalizeProxyRoute(route)] = {target: options.target, targetUrl, changeOrigin: options.changeOrigin ?? false, ws: options.ws ?? false, rewrite: options.rewrite, secure: options.secure ?? true};
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function normalizeProxyRoute(route: string) {
|
||||
if (route.length > 1) return route.replace(/\/+$/, '');
|
||||
return route;
|
||||
}
|
||||
|
||||
function normalizeBase(base: string) {
|
||||
const normalized = base.replace(/\\/g, '/');
|
||||
return `/${normalized.replace(/^\/+|\/+$/g, '')}/`.replace(/^\/\/$/, '/');
|
||||
|
||||
Reference in New Issue
Block a user