From a19031161278ce0dd175dbe49ad6ab7e0702ae07 Mon Sep 17 00:00:00 2001 From: Refringe Date: Tue, 7 Nov 2023 23:27:52 -0500 Subject: [PATCH] Removes IUUidGenerator Class Removes the `IUUidGenerator` class in favour of the built-in `crypto.randomUUID()` method. --- project/src/ErrorHandler.ts | 3 +- project/src/di/Container.ts | 3 -- .../src/models/spt/utils/IUuidGenerator.ts | 4 --- project/src/utils/UUidGenerator.ts | 29 ------------------- project/src/utils/VFS.ts | 23 +++++++-------- .../utils/logging/AbstractWinstonLogger.ts | 27 +++++++++-------- .../src/utils/logging/WinstonMainLogger.ts | 6 ++-- .../src/utils/logging/WinstonRequestLogger.ts | 6 ++-- 8 files changed, 29 insertions(+), 72 deletions(-) delete mode 100644 project/src/models/spt/utils/IUuidGenerator.ts delete mode 100644 project/src/utils/UUidGenerator.ts diff --git a/project/src/ErrorHandler.ts b/project/src/ErrorHandler.ts index 9180cbec..917f7c37 100644 --- a/project/src/ErrorHandler.ts +++ b/project/src/ErrorHandler.ts @@ -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 diff --git a/project/src/di/Container.ts b/project/src/di/Container.ts index 8df38cc7..41e578d3 100644 --- a/project/src/di/Container.ts +++ b/project/src/di/Container.ts @@ -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, { lifecycle: Lifecycle.Singleton }); depContainer.register("Watermark", Watermark, { lifecycle: Lifecycle.Singleton }); depContainer.register("AsyncQueue", AsyncQueue, { lifecycle: Lifecycle.Singleton }); - depContainer.register("UUidGenerator", UUidGenerator, { lifecycle: Lifecycle.Singleton }); depContainer.register("HttpFileUtil", HttpFileUtil, { lifecycle: Lifecycle.Singleton }); depContainer.register("ModLoadOrder", ModLoadOrder, { lifecycle: Lifecycle.Singleton }); depContainer.register("ModTypeCheck", ModTypeCheck, { lifecycle: Lifecycle.Singleton }); diff --git a/project/src/models/spt/utils/IUuidGenerator.ts b/project/src/models/spt/utils/IUuidGenerator.ts deleted file mode 100644 index 05d8b025..00000000 --- a/project/src/models/spt/utils/IUuidGenerator.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface IUUidGenerator -{ - generate(): string -} \ No newline at end of file diff --git a/project/src/utils/UUidGenerator.ts b/project/src/utils/UUidGenerator.ts deleted file mode 100644 index 12099ee9..00000000 --- a/project/src/utils/UUidGenerator.ts +++ /dev/null @@ -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); - }); - } -} diff --git a/project/src/utils/VFS.ts b/project/src/utils/VFS.ts index 896fe5ca..37ea722e 100644 --- a/project/src/utils/VFS.ts +++ b/project/src/utils/VFS.ts @@ -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; 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 { 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 { 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); diff --git a/project/src/utils/logging/AbstractWinstonLogger.ts b/project/src/utils/logging/AbstractWinstonLogger.ts index 9d8dd79b..5a3c923c 100644 --- a/project/src/utils/logging/AbstractWinstonLogger.ts +++ b/project/src/utils/logging/AbstractWinstonLogger.ts @@ -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; 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 { 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): Promise { 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): Promise { 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): Promise { 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): Promise { 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, textColor: LogTextColor, backgroundColor = LogBackgroundColor.DEFAULT): Promise { 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) }; } diff --git a/project/src/utils/logging/WinstonMainLogger.ts b/project/src/utils/logging/WinstonMainLogger.ts index dee95420..8dd08fa9 100644 --- a/project/src/utils/logging/WinstonMainLogger.ts +++ b/project/src/utils/logging/WinstonMainLogger.ts @@ -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 diff --git a/project/src/utils/logging/WinstonRequestLogger.ts b/project/src/utils/logging/WinstonRequestLogger.ts index 23857ecc..85edc0a3 100644 --- a/project/src/utils/logging/WinstonRequestLogger.ts +++ b/project/src/utils/logging/WinstonRequestLogger.ts @@ -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