Improve typing on getHash

This commit is contained in:
Ajay 2022-02-06 20:01:19 -05:00
parent 816e9a78be
commit 0b6ade4a1d
2 changed files with 7 additions and 5 deletions

View file

@ -31,6 +31,8 @@ export interface FetchResponse {
ok: boolean
}
export type HashedValue = string & { __hashBrand: unknown };
export interface VideoDurationResponse {
duration: number;
}

View file

@ -1,5 +1,5 @@
import Config from "./config";
import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContainer, Registration } from "./types";
import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContainer, Registration, HashedValue } from "./types";
import * as CompileConfig from "../config.json";
import { findValidElementFromSelector } from "./utils/pageUtils";
@ -475,10 +475,10 @@ export default class Utils {
return typeof(browser) !== "undefined";
}
async getHash(value: string, times = 5000): Promise<string> {
if (times <= 0) return "";
async getHash<T extends string>(value: T, times = 5000): Promise<T & HashedValue> {
if (times <= 0) return "" as T & HashedValue;
let hashHex = value;
let hashHex: string = value;
for (let i = 0; i < times; i++) {
const hashBuffer = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(hashHex).buffer);
@ -486,6 +486,6 @@ export default class Utils {
hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
return hashHex;
return hashHex as T & HashedValue;
}
}