From 0b6ade4a1d9c547fdc475387e7a581fa8a3f5415 Mon Sep 17 00:00:00 2001 From: Ajay Date: Sun, 6 Feb 2022 20:01:19 -0500 Subject: [PATCH] Improve typing on getHash --- src/types.ts | 2 ++ src/utils.ts | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/types.ts b/src/types.ts index 297c3ae5..bcace06d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -31,6 +31,8 @@ export interface FetchResponse { ok: boolean } +export type HashedValue = string & { __hashBrand: unknown }; + export interface VideoDurationResponse { duration: number; } diff --git a/src/utils.ts b/src/utils.ts index 5b3c71dc..087f40ab 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { - if (times <= 0) return ""; + async getHash(value: T, times = 5000): Promise { + 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; } }