/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/crawler/Router * * Simple route handler pattern matching Crawlee's Router design. */ import type { CrawlContext } from "./Crawler.ts"; type HandlerFn = (context: ContextType) => Promise; export class Router { private handlers = new Map>(); private defaultHandler: HandlerFn | null = null; addHandler(label: string, handler: HandlerFn): void { this.handlers.set(label, handler); } addDefaultHandler(handler: HandlerFn): void { this.defaultHandler = handler; } getHandler(label?: string): HandlerFn { if (label && this.handlers.has(label)) { return this.handlers.get(label)!; } if (this.defaultHandler) { return this.defaultHandler; } throw new Error(`No handler registered for label: ${label ?? "default"}`); } createRequestHandler(): HandlerFn { return async (context) => { let label: string | undefined; const req = context.request as any; if (req.userData && typeof req.userData === "object") { label = req.userData.label; } if (!label && req.payload) { try { const parsed = JSON.parse(req.payload); label = parsed.userData?.label; } catch { // Ignore } } const handler = this.getHandler(label); await handler(context); }; } } export function createRouter< ContextType extends CrawlContext, >(): Router { return new Router(); }