import * as interfaces from './interfaces/index.js'; import * as plugins from './plugins.js'; import { WorkerEvent } from './classes.workerevent.js'; import * as domainInstructions from './domaininstructions/index.js'; export class DomainRouter { private smartmatches: plugins.smartmatch.SmartMatch[] = []; constructor() { for (const key of Object.keys(domainInstructions.instructionObject)) { this.smartmatches.push(new plugins.smartmatch.SmartMatch(key)); } } /** * * @param cworkerevent */ public routeToResponder(cworkerevent: WorkerEvent) { const match = this.smartmatches.find(smartmatchArg => { return smartmatchArg.match(cworkerevent.request.url); }); cworkerevent.responderInstruction = match ? domainInstructions.instructionObject[match.wildcard] : { type: 'cache' }; } /** * rendertronRouter */ public checkWetherReRouteToRendertron(cworkerevent: WorkerEvent) { let needsRendertron = false; for (const botAgentIdentifier of domainInstructions.botUserAgents) { if (needsRendertron) { continue; } if ( cworkerevent.request.headers.get('user-agent') && cworkerevent.request.headers.get('user-agent').toLowerCase().includes(botAgentIdentifier.toLowerCase()) && !cworkerevent.request.url.includes('lossless.one') ) { needsRendertron = true; } } if (needsRendertron) { cworkerevent.routedThroughRendertron = true; } } /** * check wether this is a preflight request that should be handled */ public checkWetherIsPreflight (cworkerevent: WorkerEvent) { if ( cworkerevent.request.method === 'OPTIONS' && cworkerevent.request.headers.get('Origin') !== null && cworkerevent.request.headers.get('Access-Control-Request-Method') !== null && cworkerevent.request.headers.get('Access-Control-Request-Headers') !== null ) { cworkerevent.isPreflight = true; } } }