83 lines
2.9 KiB
TypeScript
83 lines
2.9 KiB
TypeScript
/**
|
|
* HTTP router service that exposes mod-registered routes under `/mods/<modId>/...`
|
|
* so mods can provide custom APIs or embedded-browser pages without touching core routes.
|
|
*/
|
|
import type { ModHttpHandler, ModHttpRequest } from '@wto/mods';
|
|
import type { Service, ServiceRequest, ServiceResponse } from '../transport/exchange.ts';
|
|
|
|
export interface RegisteredModHttpRoute {
|
|
readonly modId: string;
|
|
/** Full normalized path prefix or exact path under `/mods/<modId>/...`. */
|
|
readonly pattern: string;
|
|
readonly isWildcard: boolean;
|
|
readonly prefix: string;
|
|
readonly handler: ModHttpHandler;
|
|
}
|
|
|
|
export class ModHttpService implements Service {
|
|
readonly name = 'mods-http';
|
|
readonly #routes: readonly RegisteredModHttpRoute[];
|
|
|
|
constructor(routes: readonly RegisteredModHttpRoute[]) {
|
|
this.#routes = routes;
|
|
}
|
|
|
|
accepts(request: ServiceRequest): boolean {
|
|
if (request.originalHost !== undefined) return false;
|
|
return this.#match(request.path) !== undefined;
|
|
}
|
|
|
|
async handle(request: ServiceRequest): Promise<ServiceResponse> {
|
|
const matched = this.#match(request.path);
|
|
if (!matched) {
|
|
return { status: 404, body: 'mod route not found' };
|
|
}
|
|
const headers: Record<string, string | undefined> = {};
|
|
for (const [key, value] of Object.entries(request.headers)) {
|
|
headers[key] = Array.isArray(value) ? value[0] : value;
|
|
}
|
|
const query: Record<string, string> = {};
|
|
for (const [key, value] of request.query.entries()) query[key] = value;
|
|
|
|
const modReq: ModHttpRequest = {
|
|
method: request.method,
|
|
path: request.path,
|
|
query,
|
|
headers,
|
|
body: request.body,
|
|
};
|
|
const answer = await matched.handler(modReq);
|
|
const responseHeaders: Record<string, string> = { ...(answer.headers ?? {}) };
|
|
if (answer.contentType && !responseHeaders['content-type'] && !responseHeaders['Content-Type']) {
|
|
responseHeaders['content-type'] = answer.contentType;
|
|
}
|
|
return {
|
|
status: answer.status ?? 200,
|
|
headers: responseHeaders,
|
|
body: answer.body,
|
|
logNotes: { summary: `mod:${matched.modId} ${request.method} ${request.path}` },
|
|
};
|
|
}
|
|
|
|
#match(path: string): RegisteredModHttpRoute | undefined {
|
|
for (const route of this.#routes) {
|
|
if (route.isWildcard) {
|
|
if (path.startsWith(route.prefix)) return route;
|
|
} else if (path === route.pattern) {
|
|
return route;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function buildModHttpRoute(modId: string, subPath: string, handler: ModHttpHandler): RegisteredModHttpRoute {
|
|
const cleaned = subPath.startsWith('/') ? subPath : `/${subPath}`;
|
|
const full = `/mods/${modId}${cleaned === '/' ? '' : cleaned}`;
|
|
if (full.endsWith('/*')) {
|
|
const prefix = full.slice(0, -1); // keeps trailing '/'
|
|
return { modId, pattern: full, isWildcard: true, prefix, handler };
|
|
}
|
|
return { modId, pattern: full, isWildcard: false, prefix: full, handler };
|
|
}
|