Updated dependencies + A few other things (!150)
Initially this was going to be an update to dependencies but it seems i got a little carried away!
Anyways this PR removes 2 unused dependencies (`jshint` and `utf-8-validate`), and 2 other, `del` and `fs-extra` that were replaced by the built-in `fs/promises`.
It also renames all `tsconfig` and `Dockerfile` files, in a way that when viewed in a file tree sorted alphabetically they will be next to each other.
It also updates the typescript target to `ES2022`, and changes moduleResolution from `Node` to `Node10` (this isn't an update, they are the same thing but `Node` is now deprecated).
It also adds the `node:` discriminator to every import from built-in modules.
It also has major changes to the build script, `del` and `fs-extra` were only being used in the build script, it's now using `fs/promises` instead, cleaned up the code from some functions, adds better documentation to a few functions, and renames some gulp tasks and npm scripts to better represent what they actually do.
And finally it updates dependencies, except for `atomically` which can't be updated unless the project switches to ESM.
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/150
Co-authored-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
Co-committed-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
2023-10-14 09:01:41 +00:00
|
|
|
import crypto from "node:crypto";
|
2024-03-29 18:43:36 +00:00
|
|
|
import fs from "node:fs";
|
2024-07-23 11:12:53 -04:00
|
|
|
import { TimeUtil } from "@spt/utils/TimeUtil";
|
2024-03-29 18:43:36 +00:00
|
|
|
import crc32 from "buffer-crc32";
|
2024-07-08 21:11:58 +01:00
|
|
|
import { mongoid } from "mongoid-js";
|
2023-03-03 15:23:46 +00:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
|
|
|
@injectable()
|
2024-07-23 11:12:53 -04:00
|
|
|
export class HashUtil {
|
|
|
|
constructor(@inject("TimeUtil") protected timeUtil: TimeUtil) {}
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a 24 character id using the sha256 algorithm + current timestamp
|
|
|
|
* @returns 24 character hash
|
|
|
|
*/
|
2024-07-23 11:12:53 -04:00
|
|
|
public generate(): string {
|
2024-07-08 21:11:58 +01:00
|
|
|
return mongoid();
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 14:33:47 +01:00
|
|
|
/**
|
|
|
|
* is the passed in string a valid mongo id
|
|
|
|
* @param stringToCheck String to check
|
|
|
|
* @returns True when string is a valid mongo id
|
|
|
|
*/
|
|
|
|
public isValidMongoId(stringToCheck: string) {
|
|
|
|
return /^[a-fA-F0-9]{24}$/.test(stringToCheck);
|
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public generateMd5ForData(data: string): string {
|
2023-03-03 15:23:46 +00:00
|
|
|
return this.generateHashForData("md5", data);
|
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public generateSha1ForData(data: string): string {
|
2023-03-03 15:23:46 +00:00
|
|
|
return this.generateHashForData("sha1", data);
|
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public generateCRC32ForFile(filePath: fs.PathLike): number {
|
2024-03-29 18:43:36 +00:00
|
|
|
return crc32.unsigned(fs.readFileSync(filePath));
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:23:46 +00:00
|
|
|
/**
|
|
|
|
* Create a hash for the data parameter
|
|
|
|
* @param algorithm algorithm to use to hash
|
|
|
|
* @param data data to be hashed
|
|
|
|
* @returns hash value
|
|
|
|
*/
|
2024-07-23 11:12:53 -04:00
|
|
|
public generateHashForData(algorithm: string, data: crypto.BinaryLike): string {
|
2023-03-03 15:23:46 +00:00
|
|
|
const hashSum = crypto.createHash(algorithm);
|
|
|
|
hashSum.update(data);
|
|
|
|
return hashSum.digest("hex");
|
|
|
|
}
|
2023-10-10 11:03:20 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public generateAccountId(): number {
|
2023-12-29 22:53:16 +00:00
|
|
|
const min = 1000000;
|
|
|
|
const max = 1999999;
|
2024-05-07 23:57:08 -04:00
|
|
|
return max > min ? Math.floor(Math.random() * (max - min + 1) + min) : min;
|
2023-10-10 11:03:20 +00:00
|
|
|
}
|
2024-02-02 13:54:07 -05:00
|
|
|
}
|