Removes IUUidGenerator Class

Removes the `IUUidGenerator` class in favour of the built-in `crypto.randomUUID()` method.
This commit is contained in:
Refringe 2023-11-07 23:27:52 -05:00
parent 0ad85b45e1
commit a190311612
No known key found for this signature in database
GPG Key ID: 64E03E5F892C6F9E
8 changed files with 29 additions and 72 deletions

View File

@ -2,7 +2,6 @@ import readline from "node:readline";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { AsyncQueue } from "@spt-aki/utils/AsyncQueue";
import { UUidGenerator } from "@spt-aki/utils/UUidGenerator";
import { WinstonMainLogger } from "@spt-aki/utils/logging/WinstonMainLogger";
export class ErrorHandler
@ -12,7 +11,7 @@ export class ErrorHandler
constructor()
{
this.logger = new WinstonMainLogger(new AsyncQueue(), new UUidGenerator());
this.logger = new WinstonMainLogger(new AsyncQueue());
this.readLine = readline.createInterface({
input: process.stdin,
output: process.stdout

View File

@ -124,7 +124,6 @@ import { PostAkiModLoader } from "@spt-aki/loaders/PostAkiModLoader";
import { PostDBModLoader } from "@spt-aki/loaders/PostDBModLoader";
import { PreAkiModLoader } from "@spt-aki/loaders/PreAkiModLoader";
import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue";
import { IUUidGenerator } from "@spt-aki/models/spt/utils/IUuidGenerator";
import { EventOutputHolder } from "@spt-aki/routers/EventOutputHolder";
import { HttpRouter } from "@spt-aki/routers/HttpRouter";
import { ImageRouter } from "@spt-aki/routers/ImageRouter";
@ -240,7 +239,6 @@ import { MathUtil } from "@spt-aki/utils/MathUtil";
import { ObjectId } from "@spt-aki/utils/ObjectId";
import { RandomUtil } from "@spt-aki/utils/RandomUtil";
import { TimeUtil } from "@spt-aki/utils/TimeUtil";
import { UUidGenerator } from "@spt-aki/utils/UUidGenerator";
import { VFS } from "@spt-aki/utils/VFS";
import { Watermark, WatermarkLocale } from "@spt-aki/utils/Watermark";
import { WinstonMainLogger } from "@spt-aki/utils/logging/WinstonMainLogger";
@ -379,7 +377,6 @@ export class Container
depContainer.register<WatermarkLocale>("WatermarkLocale", WatermarkLocale, { lifecycle: Lifecycle.Singleton });
depContainer.register<Watermark>("Watermark", Watermark, { lifecycle: Lifecycle.Singleton });
depContainer.register<IAsyncQueue>("AsyncQueue", AsyncQueue, { lifecycle: Lifecycle.Singleton });
depContainer.register<IUUidGenerator>("UUidGenerator", UUidGenerator, { lifecycle: Lifecycle.Singleton });
depContainer.register<HttpFileUtil>("HttpFileUtil", HttpFileUtil, { lifecycle: Lifecycle.Singleton });
depContainer.register<ModLoadOrder>("ModLoadOrder", ModLoadOrder, { lifecycle: Lifecycle.Singleton });
depContainer.register<ModTypeCheck>("ModTypeCheck", ModTypeCheck, { lifecycle: Lifecycle.Singleton });

View File

@ -1,4 +0,0 @@
export interface IUUidGenerator
{
generate(): string
}

View File

@ -1,29 +0,0 @@
import { injectable } from "tsyringe";
import { IUUidGenerator } from "@spt-aki/models/spt/utils/IUuidGenerator";
@injectable()
export class UUidGenerator implements IUUidGenerator
{
// https://stackoverflow.com/a/8809472
public generate(): string
{ // Public Domain/MIT
let date = new Date().getTime();//Timestamp
let time = ((typeof performance !== "undefined") && performance.now && (performance.now() * 1000)) || 0;//Time in microseconds since page-load or 0 if unsupported
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c)
{
let rand = Math.random() * 16;//random number between 0 and 16
if (date > 0)
{//Use timestamp until depleted
rand = (date + rand) % 16 | 0;
date = Math.floor(date / 16);
}
else
{//Use microseconds since page-load if supported
rand = (time + rand) % 16 | 0;
time = Math.floor(time / 16);
}
return (c === "x" ? rand : (rand & 0x3 | 0x8)).toString(16);
});
}
}

View File

@ -1,13 +1,13 @@
import { writeFileSync } from "atomically";
import fs from "node:fs";
import path, { resolve } from "node:path";
import { promisify } from "node:util";
import lockfile from "proper-lockfile";
import "reflect-metadata";
import { inject, injectable } from "tsyringe";
import fs from "node:fs";
import crypto from "node:crypto";
import { promisify } from "node:util";
import path, { resolve } from "node:path";
import { writeFileSync } from "atomically";
import lockfile from "proper-lockfile";
import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue";
import { IUUidGenerator } from "@spt-aki/models/spt/utils/IUuidGenerator";
@injectable()
export class VFS
@ -24,9 +24,8 @@ export class VFS
renamePromisify: (oldPath: fs.PathLike, newPath: fs.PathLike) => Promise<void>;
constructor(
@inject("AsyncQueue") protected asyncQueue: IAsyncQueue,
@inject("UUidGenerator") protected uuidGenerator: IUUidGenerator
)
@inject("AsyncQueue") protected asyncQueue: IAsyncQueue
)
{
this.accessFilePromisify = promisify(fs.access);
this.copyFilePromisify = promisify(fs.copyFile);
@ -51,7 +50,7 @@ export class VFS
{
// Create the command to add to the queue
const command = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.accessFilePromisify(filepath)
};
// Wait for the command completion
@ -75,7 +74,7 @@ export class VFS
public async copyAsync(filepath: fs.PathLike, target: fs.PathLike): Promise<void>
{
const command = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.copyFilePromisify(filepath, target)
};
await this.asyncQueue.waitFor(command);
@ -89,7 +88,7 @@ export class VFS
public async createDirAsync(filepath: string): Promise<void>
{
const command = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.mkdirPromisify(filepath.substr(0, filepath.lastIndexOf("/")), { "recursive": true })
};
await this.asyncQueue.waitFor(command);

View File

@ -1,4 +1,5 @@
import fs from "node:fs";
import crypto from "node:crypto";
import { promisify } from "node:util";
import winston, { createLogger, format, transports } from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
@ -10,7 +11,6 @@ import { SptLogger } from "@spt-aki/models/spt/logging/SptLogger";
import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue";
import { ICommand } from "@spt-aki/models/spt/utils/ICommand";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { IUUidGenerator } from "@spt-aki/models/spt/utils/IUuidGenerator";
export abstract class AbstractWinstonLogger implements ILogger
{
@ -49,9 +49,8 @@ export abstract class AbstractWinstonLogger implements ILogger
protected writeFilePromisify: (path: fs.PathLike, data: string, options?: any) => Promise<void>;
constructor(
protected asyncQueue: IAsyncQueue,
protected uuidGenerator: IUUidGenerator
)
protected asyncQueue: IAsyncQueue
)
{
this.filePath = `${this.getFilePath()}${this.getFileName()}`;
this.writeFilePromisify = promisify(fs.writeFile);
@ -140,7 +139,7 @@ export abstract class AbstractWinstonLogger implements ILogger
public async writeToLogFile(data: string | Daum): Promise<void>
{
const command: ICommand = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.writeFilePromisify(this.filePath, `${data}\n`, true)
};
await this.asyncQueue.waitFor(command);
@ -165,14 +164,14 @@ export abstract class AbstractWinstonLogger implements ILogger
if (typeof (data) === "string")
{
command = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await tmpLogger.log("custom", data)
};
}
else
{
command = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await tmpLogger.log("custom", JSON.stringify(data, null, 4))
};
}
@ -183,7 +182,7 @@ export abstract class AbstractWinstonLogger implements ILogger
public async error(data: string | Record<string, unknown>): Promise<void>
{
const command: ICommand = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.logger.error(data)
};
await this.asyncQueue.waitFor(command);
@ -192,7 +191,7 @@ export abstract class AbstractWinstonLogger implements ILogger
public async warning(data: string | Record<string, unknown>): Promise<void>
{
const command: ICommand = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.logger.warn(data)
};
await this.asyncQueue.waitFor(command);
@ -201,7 +200,7 @@ export abstract class AbstractWinstonLogger implements ILogger
public async success(data: string | Record<string, unknown>): Promise<void>
{
const command: ICommand = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.logger.succ(data)
};
await this.asyncQueue.waitFor(command);
@ -210,7 +209,7 @@ export abstract class AbstractWinstonLogger implements ILogger
public async info(data: string | Record<string, unknown>): Promise<void>
{
const command: ICommand = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.logger.info(data)
};
await this.asyncQueue.waitFor(command);
@ -225,7 +224,7 @@ export abstract class AbstractWinstonLogger implements ILogger
public async logWithColor(data: string | Record<string, unknown>, textColor: LogTextColor, backgroundColor = LogBackgroundColor.DEFAULT): Promise<void>
{
const command: ICommand = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.log(data, textColor.toString(), backgroundColor.toString())
};
@ -239,14 +238,14 @@ export abstract class AbstractWinstonLogger implements ILogger
if (onlyShowInConsole)
{
command = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.log(data, this.logLevels.colors.debug)
};
}
else
{
command = {
uuid: this.uuidGenerator.generate(),
uuid: crypto.randomUUID(),
cmd: async () => await this.logger.debug(data)
};
}

View File

@ -1,7 +1,6 @@
import { inject, injectable } from "tsyringe";
import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue";
import { IUUidGenerator } from "@spt-aki/models/spt/utils/IUuidGenerator";
import { AbstractWinstonLogger } from "@spt-aki/utils/logging/AbstractWinstonLogger";
@injectable()
@ -9,11 +8,10 @@ export class WinstonMainLogger extends AbstractWinstonLogger
{
constructor(
@inject("AsyncQueue") protected asyncQueue: IAsyncQueue,
@inject("UUidGenerator") protected uuidGenerator: IUUidGenerator
@inject("AsyncQueue") protected asyncQueue: IAsyncQueue
)
{
super(asyncQueue, uuidGenerator);
super(asyncQueue);
}
protected isLogExceptions(): boolean

View File

@ -1,18 +1,16 @@
import { inject, injectable } from "tsyringe";
import { IAsyncQueue } from "@spt-aki/models/spt/utils/IAsyncQueue";
import { IUUidGenerator } from "@spt-aki/models/spt/utils/IUuidGenerator";
import { AbstractWinstonLogger } from "@spt-aki/utils/logging/AbstractWinstonLogger";
@injectable()
export class WinstonRequestLogger extends AbstractWinstonLogger
{
constructor(
@inject("AsyncQueue") protected asyncQueue: IAsyncQueue,
@inject("UUidGenerator") protected uuidGenerator: IUUidGenerator
@inject("AsyncQueue") protected asyncQueue: IAsyncQueue
)
{
super(asyncQueue, uuidGenerator);
super(asyncQueue);
}
protected isLogExceptions(): boolean