From 650b563301b67f409d8ba222ebe408fb61e69fd5 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Wed, 6 Jul 2022 23:55:42 -0300 Subject: [PATCH] style: lint code --- lib/utils/Utils.js | 71 +++++++++++++++++++++++---------- lib/utils/wrappers/NodeCache.js | 8 ++-- package.json | 2 +- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/lib/utils/Utils.js b/lib/utils/Utils.js index 339ea85d..8642ef38 100644 --- a/lib/utils/Utils.js +++ b/lib/utils/Utils.js @@ -36,7 +36,11 @@ class OAuthError extends InnertubeError {} function findNode(obj, key, target, depth, safe = true) { const flat_obj = Flatten(obj, { safe, maxDepth: depth || 2 }); const result = Object.keys(flat_obj).find((entry) => entry.includes(key) && JSON.stringify(flat_obj[entry] || '{}').includes(target)); - if (!result) throw new ParsingError(`Expected to find "${key}" with content "${target}" but got ${result}`, { key, target, data_snippet: `${JSON.stringify(flat_obj, null, 4).slice(0, 300)}..` }); + + if (!result) throw new ParsingError(`Expected to find "${key}" with content "${target}" but got ${result}`, { + key, target, data_snippet: `${JSON.stringify(flat_obj, null, 4).slice(0, 300)}..` + }); + return flat_obj[result]; } @@ -140,22 +144,22 @@ function getTmpdir() { const is_windows = process.platform === 'win32'; const trailing_slash_re = is_windows ? /[^:]\\$/ : /.\/$/; - + let path; - if (is_windows) { - path = env.TEMP || env.TMP || - (env.SystemRoot || env.windir) + '\\temp'; - } else { - path = env.TMPDIR || env.TMP || - env.TEMP || '/tmp'; - } + if (is_windows) { + path = env.TEMP || env.TMP || + `${env.SystemRoot || env.windir}\\temp`; + } else { + path = env.TMPDIR || env.TMP || + env.TEMP || '/tmp'; + } - if (trailing_slash_re.test(path)) { - path = path.slice(0, -1); - } + if (trailing_slash_re.test(path)) { + path = path.slice(0, -1); + } - return path; + return path; } /** @@ -191,7 +195,9 @@ function getRandomUserAgent(type) { case 'mobile': return new UserAgent(/Android/).data; case 'desktop': - return new UserAgent({ deviceCategory: 'desktop' }).data; + return new UserAgent({ + deviceCategory: 'desktop' + }).data; default: } } @@ -208,6 +214,7 @@ function generateSidAuth(sid) { const input = [ timestamp, sid, youtube ].join(' '); let gen_hash; + if (BROWSER) { const hash = Crypto.md.sha1.create(); hash.update(input); @@ -276,7 +283,8 @@ function camelToSnake(string) { * @returns {boolean} */ function isValidClient(client) { - return [ 'YOUTUBE', 'YTMUSIC' ].includes(client); + return [ 'YOUTUBE', + 'YTMUSIC' ].includes(client); } /** @@ -308,11 +316,32 @@ function refineNTokenData(data) { .replace(/""/g, '').replace(/length]\)}"/g, 'length])}'); } -const errors = { InnertubeError, UnavailableContentError, ParsingError, DownloadError, MissingParamError, NoStreamingDataError, OAuthError }; - -const functions = { - findNode, observe, getTmpdir, getRandomUserAgent, generateSidAuth, generateRandomString, getStringBetweenStrings, - camelToSnake, isValidClient, throwIfMissing, timeToSeconds, refineNTokenData +const errors = { + InnertubeError, + UnavailableContentError, + ParsingError, + DownloadError, + MissingParamError, + NoStreamingDataError, + OAuthError }; -module.exports = { ...functions, ...errors }; \ No newline at end of file +const functions = { + findNode, + observe, + getTmpdir, + getRandomUserAgent, + generateSidAuth, + generateRandomString, + getStringBetweenStrings, + camelToSnake, + isValidClient, + throwIfMissing, + timeToSeconds, + refineNTokenData +}; + +module.exports = { + ...functions, + ...errors +}; \ No newline at end of file diff --git a/lib/utils/wrappers/NodeCache.js b/lib/utils/wrappers/NodeCache.js index 29df2303..e47da053 100644 --- a/lib/utils/wrappers/NodeCache.js +++ b/lib/utils/wrappers/NodeCache.js @@ -19,17 +19,17 @@ class NodeCache { async write(key, data) { // Make sure the directory exists const parts = key.split('/').slice(0, -1); - + let current = ''; - + for (const part of parts) { current += `${part}/`; - + if (!(await this.exists(current))) { await fs.promises.mkdir(current); } } - + return await fs.promises.writeFile(key, data); } diff --git a/package.json b/package.json index 8ebd51eb..b2ef6f1f 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "funding": "https://ko-fi.com/luanrt", "license": "MIT", "scripts": { - "test": "npx jest", + "test": "npx jest --verbose", "test:node": "npm run build:node && npx jest node", "test:browser": "npm run build:browser && npx jest browser", "lint": "npx eslint ./lib",