Server/project/src/routers/serializers/BundleSerializer.ts
Refringe 5740774a46
Apply Biome Formatting
This is the result of running `npm run format` which applies the Biome formatting rules. Rejoice!
2024-07-23 11:12:53 -04:00

39 lines
1.3 KiB
TypeScript

import { IncomingMessage, ServerResponse } from "node:http";
import { Serializer } from "@spt/di/Serializer";
import { BundleLoader } from "@spt/loaders/BundleLoader";
import { ILogger } from "@spt/models/spt/utils/ILogger";
import { HttpFileUtil } from "@spt/utils/HttpFileUtil";
import { inject, injectable } from "tsyringe";
@injectable()
export class BundleSerializer extends Serializer {
constructor(
@inject("PrimaryLogger") protected logger: ILogger,
@inject("BundleLoader") protected bundleLoader: BundleLoader,
@inject("HttpFileUtil") protected httpFileUtil: HttpFileUtil,
) {
super();
}
public override serialize(sessionID: string, req: IncomingMessage, resp: ServerResponse, body: any): void {
const key = decodeURI(req.url.split("/bundle/")[1]);
const bundle = this.bundleLoader.getBundle(key);
if (!bundle) {
return;
}
this.logger.info(`[BUNDLE]: ${req.url}`);
if (!bundle.modpath) {
this.logger.error(`Mod: ${key} lacks a modPath property, skipped loading`);
return;
}
this.httpFileUtil.sendFile(resp, `${bundle.modpath}/bundles/${bundle.filename}`);
}
public override canHandle(route: string): boolean {
return route === "BUNDLE";
}
}