refactor(HTTPClient): Use getCookie fn to get SAPISID token

This commit is contained in:
Luan
2024-06-03 17:41:09 -03:00
parent 000f3f0915
commit 184df79b3a
2 changed files with 12 additions and 5 deletions

View File

@@ -4,8 +4,8 @@ import {
Platform,
generateSidAuth,
getRandomUserAgent,
getStringBetweenStrings,
InnertubeError
InnertubeError,
getCookie
} from './Utils.js';
import type { Context, Session } from '../core/index.js';
@@ -14,6 +14,7 @@ import type { FetchFunction } from '../types/index.js';
export interface HTTPClientInit {
baseURL?: string;
}
export default class HTTPClient {
#session: Session;
#cookie?: string;
@@ -137,10 +138,10 @@ export default class HTTPClient {
}
if (this.#cookie) {
const papisid = getStringBetweenStrings(this.#cookie, 'PAPISID=', ';');
const sapisid = getCookie(this.#cookie, 'SAPISID');
if (papisid) {
request_headers.set('Authorization', await generateSidAuth(papisid));
if (sapisid) {
request_headers.set('Authorization', await generateSidAuth(sapisid));
request_headers.set('X-Goog-Authuser', this.#session.account_index.toString());
}

View File

@@ -239,4 +239,10 @@ export function base64ToU8(base64: string): Uint8Array {
export function isTextRun(run: TextRun | EmojiRun): run is TextRun {
return !('emoji' in run);
}
export function getCookie(cookies: string, name: string, matchWholeName = false): string | undefined {
const regex = matchWholeName ? `(^|\\s?)\\b${name}\\b=([^;]+)` : `(^|s?)${name}=([^;]+)`;
const match = cookies.match(new RegExp(regex));
return match ? match[2] : undefined;
}