From 021a7fd97ac9850be43313d0b33b60b8f4d7bf06 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Wed, 6 Jul 2022 23:45:57 -0300 Subject: [PATCH] refactor(utils): add tmpdir function --- lib/utils/Utils.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/utils/Utils.js b/lib/utils/Utils.js index 06f094f5..339ea85d 100644 --- a/lib/utils/Utils.js +++ b/lib/utils/Utils.js @@ -131,6 +131,33 @@ function deepCompare(obj1, obj2) { }); } +/** + * Returns the os tmpdir. + * @returns {string} + */ +function getTmpdir() { + const env = BROWSER ? {} : process.env; + + 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 (trailing_slash_re.test(path)) { + path = path.slice(0, -1); + } + + return path; +} + /** * Finds a string between two delimiters. * @@ -284,7 +311,7 @@ function refineNTokenData(data) { const errors = { InnertubeError, UnavailableContentError, ParsingError, DownloadError, MissingParamError, NoStreamingDataError, OAuthError }; const functions = { - findNode, observe, getRandomUserAgent, generateSidAuth, generateRandomString, getStringBetweenStrings, + findNode, observe, getTmpdir, getRandomUserAgent, generateSidAuth, generateRandomString, getStringBetweenStrings, camelToSnake, isValidClient, throwIfMissing, timeToSeconds, refineNTokenData };