Files
WT-Offline/packages/local-service/src/server/router.ts
T
N0VAN0M4LY aa272fc82b Refresh the storefront and autoplay pack demos
Cache allowlisted official media so the in-game browser can load it locally. Match the official catalog snapshot and start pack demos muted and looping on entry.
2026-11-27 00:43:50 +08:00

32 lines
1.2 KiB
TypeScript

/**
* Hand each request to the service that accepts it. A request no service accepts is answered
* with 404 and marked in the traffic log, so missing services show up there.
*/
import type { Service, ServiceRequest, ServiceResponse } from './exchange.ts';
export interface RoutedResponse {
readonly service: string;
readonly response: ServiceResponse;
}
export class Router {
readonly #services: readonly Service[];
constructor(services: readonly Service[]) {
this.#services = services;
}
async route(request: ServiceRequest): Promise<RoutedResponse> {
const service = this.#services.find((candidate) => candidate.accepts(request));
if (!service) {
return { service: 'none', response: { status: 404, body: 'no local service for this request', logNotes: { unhandled: true } } };
}
try {
return { service: service.name, response: await service.handle(request) };
} catch (error) {
const message = error instanceof Error ? error.stack ?? error.message : String(error);
return { service: service.name, response: { status: 500, body: 'local service error', logNotes: { summary: 'ERROR', responseText: message } } };
}
}
}