Server/project/src/routers/serializers/BundleSerializer.ts

39 lines
1.3 KiB
TypeScript
Raw Normal View History

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";
2023-03-03 15:23:46 +00:00
@injectable()
export class BundleSerializer extends Serializer {
2023-03-03 15:23:46 +00:00
constructor(
@inject("PrimaryLogger") protected logger: ILogger,
2023-03-03 15:23:46 +00:00
@inject("BundleLoader") protected bundleLoader: BundleLoader,
2023-11-15 20:35:05 -05:00
@inject("HttpFileUtil") protected httpFileUtil: HttpFileUtil,
) {
2023-03-03 15:23:46 +00:00
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;
}
2023-03-03 15:23:46 +00:00
this.httpFileUtil.sendFile(resp, `${bundle.modpath}/bundles/${bundle.filename}`);
2023-03-03 15:23:46 +00:00
}
public override canHandle(route: string): boolean {
2023-03-03 15:23:46 +00:00
return route === "BUNDLE";
}
2023-11-15 20:35:05 -05:00
}