From 4f0d95bf42e2786e2d5ad09715c900db8673db69 Mon Sep 17 00:00:00 2001 From: Daniel <790119+DanTheMan827@users.noreply.github.com> Date: Fri, 20 Mar 2026 17:13:23 -0500 Subject: [PATCH] refactor(hifi): update localStorage and token handling Modified token encoding to use globalThis and added a setToken method. --- js/HiFi.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/js/HiFi.ts b/js/HiFi.ts index 265ed6c..5d85532 100644 --- a/js/HiFi.ts +++ b/js/HiFi.ts @@ -18,8 +18,6 @@ export class TidalResponse extends Response { } } -/** A container for the mock localStorage */ - export class HiFiClient { private static tokenPromise: Promise | null = null; private static albumTracksMax = 20; @@ -31,8 +29,7 @@ export class HiFiClient { private static _localStorage: Record = {}; private static get localStorage() { return ( - globalThis?.localStorage ?? - window?.localStorage ?? { + globalThis?.localStorage ?? { getItem: (key) => HiFiClient._localStorage[key], setItem: (key, value) => { HiFiClient._localStorage[key] = String(value); @@ -94,13 +91,18 @@ export class HiFiClient { } private static encodeBasic(id: string, secret: string) { - if (typeof window !== 'undefined' && typeof window.btoa === 'function') { - return window.btoa(`${id}:${secret}`); + if (typeof globalThis.btoa === 'function') { + return btoa(`${id}:${secret}`); } // Node fallback return Buffer.from(`${id}:${secret}`).toString('base64'); } + static setToken(token: string, expiry: number = Date.now() + 60000) { + HiFiClient.token = token; + HiFiClient.appTokenExpiry = expiry + } + private static async fetchAppToken( signal: AbortSignal = new AbortController().signal, clientId: string, @@ -747,10 +749,6 @@ export class HiFiClient { offset: qp.offset ? Number(qp.offset) : undefined, }); default: - // unknown local route => treat as raw upstream path (forward) - if (pathOrUrl.startsWith('http')) { - return await this.fetchJson(pathOrUrl); - } throw new Error(`Unknown route: ${pathname}`); } } catch (err) {