style: lint code

This commit is contained in:
LuanRT
2022-07-06 23:55:42 -03:00
parent fd52556603
commit 650b563301
3 changed files with 55 additions and 26 deletions

View File

@@ -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 };
const functions = {
findNode,
observe,
getTmpdir,
getRandomUserAgent,
generateSidAuth,
generateRandomString,
getStringBetweenStrings,
camelToSnake,
isValidClient,
throwIfMissing,
timeToSeconds,
refineNTokenData
};
module.exports = {
...functions,
...errors
};

View File

@@ -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);
}

View File

@@ -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",