Server/project/src/routers/HttpRouter.ts

87 lines
2.5 KiB
TypeScript
Raw Normal View History

import { IncomingMessage } from "node:http";
2023-03-03 16:23:46 +01:00
import { injectable, injectAll } from "tsyringe";
import { DynamicRouter, Router, StaticRouter } from "@spt-aki/di/Router";
2023-03-03 16:23:46 +01:00
@injectable()
export class HttpRouter
{
constructor(
@injectAll("StaticRoutes") protected staticRouters: StaticRouter[],
@injectAll("DynamicRoutes") protected dynamicRoutes: DynamicRouter[]
)
{ }
protected groupBy<T>(list: T[], keyGetter: (t:T) => string): Map<string, T[]>
{
const map: Map<string, T[]> = new Map();
list.forEach((item) =>
{
const key = keyGetter(item);
const collection = map.get(key);
if (!collection)
{
map.set(key, [item]);
}
else
{
collection.push(item);
}
});
return map;
}
public getResponse(req: IncomingMessage, info: any, sessionID: string): string
{
const wrapper: ResponseWrapper = new ResponseWrapper("");
let url = req.url;
// remove retry from url
2023-03-18 10:19:20 +01:00
if (url?.includes("?retry="))
2023-03-03 16:23:46 +01:00
{
url = url.split("?retry=")[0];
}
const handled = this.handleRoute(url, info, sessionID, wrapper, this.staticRouters, false);
if (!handled)
{
this.handleRoute(url, info, sessionID, wrapper, this.dynamicRoutes, true);
}
// TODO: Temporary hack to change ItemEventRouter response sessionID binding to what client expects
2023-03-18 10:19:20 +01:00
if (wrapper.output?.includes("\"profileChanges\":{"))
2023-03-03 16:23:46 +01:00
{
wrapper.output = wrapper.output.replace(sessionID, `pmc${sessionID}`);
}
return wrapper.output;
}
protected handleRoute(url: string, info: any, sessionID: string, wrapper: ResponseWrapper, routers: Router[], dynamic: boolean): boolean
{
let matched = false;
for (const route of routers)
{
if (route.canHandle(url, dynamic))
{
if (dynamic)
{
wrapper.output = (route as DynamicRouter).handleDynamic(url, info, sessionID, wrapper.output);
}
else
{
wrapper.output = (route as StaticRouter).handleStatic(url, info, sessionID, wrapper.output);
}
matched = true;
}
}
return matched;
}
}
class ResponseWrapper
{
constructor(
public output: string
)
{}
}