Server/project/src/loaders/BundleLoader.ts
TheSparta c3e203922e bundle-crc-cache (!274)
This PR is required by SPT-AKI/Modules!104 in order for it to function correctly.

## Overview

- Adds the package `buffer-crc32`, it can generate CRC32 hashes from buffers or strings
- Splits `HashCacheService` into 2 classes `ModHashCacheService` does exactly the same `HashCacheService` used to do, and added a new `BundleHashCacheService`
- `BundleLoader` now generates a CRC32 hash of every bundle file from every loaded mod
- Reworked `BundleInfo` to better represent the data expected by the client when requesting `/singleplayer/bundles`
- Removes all checks on `BundleLoader` that verified if the request was made to a localhost address, this is now addressed by the client.

## Testing

The code has been tested by @Senko-san and me.

Co-authored-by: chomp <chomp@noreply.dev.sp-tarkov.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/274
Co-authored-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
Co-committed-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
2024-03-29 18:43:36 +00:00

95 lines
2.6 KiB
TypeScript

import path from "node:path";
import { inject, injectable } from "tsyringe";
import { HttpServerHelper } from "@spt-aki/helpers/HttpServerHelper";
import { BundleHashCacheService } from "@spt-aki/services/cache/BundleHashCacheService";
import { JsonUtil } from "@spt-aki/utils/JsonUtil";
import { VFS } from "@spt-aki/utils/VFS";
export class BundleInfo
{
modpath: string;
filename: string;
crc: number;
dependencies: string[];
constructor(modpath: string, bundle: BundleManifestEntry, bundleHash: number)
{
this.modpath = modpath;
this.filename = bundle.key;
this.crc = bundleHash;
this.dependencies = bundle.dependencyKeys || [];
}
}
@injectable()
export class BundleLoader
{
protected bundles: Record<string, BundleInfo> = {};
constructor(
@inject("HttpServerHelper") protected httpServerHelper: HttpServerHelper,
@inject("VFS") protected vfs: VFS,
@inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("BundleHashCacheService") protected bundleHashCacheService: BundleHashCacheService,
)
{}
/**
* Handle singleplayer/bundles
*/
public getBundles(): BundleInfo[]
{
const result: BundleInfo[] = [];
for (const bundle in this.bundles)
{
result.push(this.getBundle(bundle));
}
return result;
}
public getBundle(key: string): BundleInfo
{
return this.jsonUtil.clone(this.bundles[key]);
}
public addBundles(modpath: string): void
{
const bundleManifestArr =
this.jsonUtil.deserialize<BundleManifest>(this.vfs.readFile(`${modpath}bundles.json`)).manifest;
for (const bundleManifest of bundleManifestArr)
{
const absoluteModPath = path.join(process.cwd(), modpath).slice(0, -1).replace(/\\/g, "/");
const bundleLocalPath = `${modpath}bundles/${bundleManifest.key}`.replace(/\\/g, "/");
if (!this.bundleHashCacheService.calculateAndMatchHash(bundleLocalPath))
{
this.bundleHashCacheService.calculateAndStoreHash(bundleLocalPath);
}
const bundleHash = this.bundleHashCacheService.getStoredValue(bundleLocalPath);
this.addBundle(bundleManifest.key, new BundleInfo(absoluteModPath, bundleManifest, bundleHash));
}
}
public addBundle(key: string, b: BundleInfo): void
{
this.bundles[key] = b;
}
}
export interface BundleManifest
{
manifest: BundleManifestEntry[];
}
export interface BundleManifestEntry
{
key: string;
dependencyKeys: string[];
}