From 61028a2ab93b5bc60cb6711db335c010e602d287 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Thu, 3 Mar 2022 02:21:32 -0300 Subject: [PATCH] style: format and refactor code --- lib/Livechat.js | 6 +++++- lib/NToken.js | 5 +++++ lib/OAuth.js | 6 ++++-- lib/Sig.js | 9 ++------- lib/Utils.js | 26 +++++++++++++++++--------- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/Livechat.js b/lib/Livechat.js index c7cd9df2..b8e7cc80 100644 --- a/lib/Livechat.js +++ b/lib/Livechat.js @@ -117,8 +117,12 @@ class Livechat extends EventEmitter { }; } + /** + * Blocks a user. + * @todo Implement this method. + * @param {object} msg_params + */ async blockUser(msg_params) { - /* TODO: Implement this */ throw new Error('Not implemented'); } diff --git a/lib/NToken.js b/lib/NToken.js index c338c74f..cacc76e3 100644 --- a/lib/NToken.js +++ b/lib/NToken.js @@ -76,6 +76,7 @@ class NToken { /** * Gets a base64 alphabet and uses it as a lookup table to modify n. + * @returns */ #translate1(arr, token, is_reverse_base64) { const characters = is_reverse_base64 && Constants.BASE64_DIALECT.REVERSE || Constants.BASE64_DIALECT.NORMAL; @@ -93,6 +94,7 @@ class NToken { /** * Returns the requested base64 dialect, currently this is only used by 'translate2'. + * @returns {string[]} */ #getBase64Dia(is_reverse_base64) { const characters = is_reverse_base64 && Constants.BASE64_DIALECT.REVERSE || Constants.BASE64_DIALECT.NORMAL; @@ -101,6 +103,7 @@ class NToken { /** * Swaps the first element with the one at the given index. + * @returns */ #swap0(arr, index) { const old_elem = arr[0]; @@ -111,6 +114,7 @@ class NToken { /** * Rotates elements of the array. + * @returns */ #rotate(arr, index) { index = (index % arr.length + arr.length) % arr.length; @@ -119,6 +123,7 @@ class NToken { /** * Deletes one element at the given index. + * @returns */ #splice(arr, index) { index = (index % arr.length + arr.length) % arr.length; diff --git a/lib/OAuth.js b/lib/OAuth.js index 454dae3b..ee907973 100644 --- a/lib/OAuth.js +++ b/lib/OAuth.js @@ -28,6 +28,7 @@ class OAuth extends EventEmitter { /** * Asks the OAuth server for an auth code. + * @returns {Promise.} */ async #requestAuthCode() { const identity = await this.#getClientIdentity(); @@ -66,6 +67,7 @@ class OAuth extends EventEmitter { * Waits for sign-in authorization. * * @param {string} device_code Client's device code. + * @returns */ #waitForAuth(device_code) { const data = { @@ -122,7 +124,7 @@ class OAuth extends EventEmitter { /** * Gets a new access token using a refresh token. - * @returns {object} { credentials: { access_token: string, refresh_token: string, expires: string }, status: 'FAILED' | 'SUCCESS' } + * @returns {object.<{ credentials: { access_token: string; refresh_token: string; expires: Date }; status: string }>} */ async refreshAccessToken() { const identity = await this.#getClientIdentity(); @@ -166,7 +168,7 @@ class OAuth extends EventEmitter { /** * Gets client identity data. - * @returns {object} { id: string, secret: string } + * @returns {Promise.<{ id: string; secret: string }>} */ async #getClientIdentity() { // This request is made to get the auth script url, hard-coding it isn't viable as it changes overtime. diff --git a/lib/Sig.js b/lib/Sig.js index 97cb1e77..3f836da0 100644 --- a/lib/Sig.js +++ b/lib/Sig.js @@ -1,12 +1,10 @@ 'use strict'; -const NToken = require('./NToken'); const QueryString = require('querystring'); class SigDecipher { - constructor(url, cver, player) { + constructor(url, player) { this.url = url; - this.cver = cver; this.player = player; this.func_regex = /(.{2}):function\(.*?\){(.*?)}/g; this.actions_regex = /;.{2}\.(.{2})\(.*?,(.*?)\)/g; @@ -52,10 +50,7 @@ class SigDecipher { } const url_components = new URL(args.url); - args.sp !== undefined ? url_components.searchParams.set(args.sp, signature.join('')) : url_components.searchParams.set('signature', signature.join('')); - url_components.searchParams.set('cver', this.cver); - url_components.searchParams.set('ratebypass', 'yes'); - url_components.searchParams.set('n', new NToken(this.player.ntoken_sc).transform(url_components.searchParams.get('n'))); + args.sp ? url_components.searchParams.set(args.sp, signature.join('')) : url_components.searchParams.set('signature', signature.join('')); return url_components.toString(); } diff --git a/lib/Utils.js b/lib/Utils.js index 9a3447d8..5522dc8e 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -8,7 +8,8 @@ const UserAgent = require('user-agents'); /** * Returns a random user agent. * - * @param {string} type mobile | desktop + * @param {string} type - mobile | desktop + * @returns {object} */ function getRandomUserAgent(type) { switch (type) { @@ -23,7 +24,8 @@ function getRandomUserAgent(type) { /** * Generates an authentication token from a cookies' sid. * - * @param {string} sid Sid extracted from cookies + * @param {string} sid - Sid extracted from cookies + * @returns {string} */ function generateSidAuth(sid) { const youtube = 'https://www.youtube.com'; @@ -40,9 +42,9 @@ function generateSidAuth(sid) { /** * Gets a string between two delimiters. * - * @param {string} data The data. - * @param {string} start_string Start string. - * @param {string} end_string End string. + * @param {string} data - The data. + * @param {string} start_string - Start string. + * @param {string} end_string - End string. */ function getStringBetweenStrings(data, start_string, end_string) { const regex = new RegExp(`${escapeStringRegexp(start_string)}(.*?)${escapeStringRegexp(end_string)}`, "s"); @@ -58,7 +60,7 @@ function escapeStringRegexp(string) { * Converts time (h:m:s) to seconds. * * @param {string} time - * @returns {string} seconds + * @returns {number} seconds */ function timeToSeconds(time) { let params = time.split(':'); @@ -73,6 +75,7 @@ function timeToSeconds(time) { * Converts strings in camelCase to snake_case. * * @param {string} string The string in camelCase. + * @returns {string} */ function camelToSnake(string) { return string[0].toLowerCase() + string.slice(1, string.length).replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); @@ -83,6 +86,7 @@ function camelToSnake(string) { * * @param {string} channel_id * @param {string} index + * @returns {string} */ function encodeNotificationPref(channel_id, index) { const youtube_proto = Proto(Fs.readFileSync(`${__dirname}/proto/youtube.proto`)); @@ -104,6 +108,7 @@ function encodeNotificationPref(channel_id, index) { * * @param {string} channel_id * @param {string} video_id + * @returns {string} */ function encodeMessageParams(channel_id, video_id) { const youtube_proto = Proto(Fs.readFileSync(`${__dirname}/proto/youtube.proto`)); @@ -126,6 +131,7 @@ function encodeMessageParams(channel_id, video_id) { * Encodes comment params protobuf. * * @param {string} video_id + * @returns {string} */ function encodeCommentParams(video_id) { const youtube_proto = Proto(Fs.readFileSync(`${__dirname}/proto/youtube.proto`)); @@ -144,9 +150,10 @@ function encodeCommentParams(video_id) { /** * Encodes search filter protobuf * - * @param {string} period Period in which a video is uploaded: any | hour | day | week | month | year - * @param {string} duration The duration of a video: any | short | long - * @param {string} order The order of the search results: relevance | rating | age | views + * @param {string} period - Period in which a video is uploaded: any | hour | day | week | month | year + * @param {string} duration - The duration of a video: any | short | long + * @param {string} order - The order of the search results: relevance | rating | age | views + * @returns {string} */ function encodeFilter(period, duration, order) { const youtube_proto = Proto(Fs.readFileSync(`${__dirname}/proto/youtube.proto`)); @@ -171,6 +178,7 @@ function encodeFilter(period, duration, order) { * Turns the ntoken transform data into a valid json array * * @param {string} data + * @returns {string} */ function refineNTokenData(data) { return data