From 10c15bfb9f131a2acea2f26ff3328993d8d8f4aa Mon Sep 17 00:00:00 2001 From: Luan Date: Mon, 28 Aug 2023 07:18:27 -0300 Subject: [PATCH] feat(Session): Add fallback for session data retrieval (#490) * feat(Session): Add fallback for session data retrieval Should have added this when we first implemented session data retrieval to be honest. It makes a request to YouTube's service worker and the data there can change or the request can just fail. * chore: lint --- src/core/Session.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/Session.ts b/src/core/Session.ts index 555fe7abd..39f3f3332 100644 --- a/src/core/Session.ts +++ b/src/core/Session.ts @@ -217,10 +217,17 @@ export default class Session extends EventEmitterLike { ) { let session_data: SessionData; + const session_args = { lang, location, time_zone: tz, device_category, client_name, enable_safety_mode, visitor_data }; + if (generate_session_locally) { - session_data = this.#generateSessionData({ lang, location, time_zone: tz, device_category, client_name, enable_safety_mode, visitor_data }); + session_data = this.#generateSessionData(session_args); } else { - session_data = await this.#retrieveSessionData({ lang, location, time_zone: tz, device_category, client_name, enable_safety_mode, visitor_data }, fetch); + try { + // This can fail if the data changes or the request is blocked for some reason. + session_data = await this.#retrieveSessionData(session_args, fetch); + } catch (err) { + session_data = this.#generateSessionData(session_args); + } } return { ...session_data, account_index };