From 9c97434e5efca9db32ca511b7b5cb2fb7d5f9beb Mon Sep 17 00:00:00 2001 From: LuanRT Date: Wed, 6 Jul 2022 23:47:38 -0300 Subject: [PATCH] chore: tidy things up and fix typos --- lib/utils/wrappers/BrowserCache.js | 5 +---- lib/utils/wrappers/NodeCache.js | 17 +++++++---------- scripts/build-parser-json.js | 1 + scripts/globals.js | 5 +++-- test/browser/main.test.js | 17 +++++++++-------- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/utils/wrappers/BrowserCache.js b/lib/utils/wrappers/BrowserCache.js index 96259f64..007e11aa 100644 --- a/lib/utils/wrappers/BrowserCache.js +++ b/lib/utils/wrappers/BrowserCache.js @@ -1,4 +1,5 @@ 'use strict'; + const { openDB } = require('idb'); class BrowserCache { @@ -11,7 +12,6 @@ class BrowserCache { } /** - * * @param {string} key * @returns {Promise} */ @@ -21,7 +21,6 @@ class BrowserCache { } /** - * * @param {string} key * @param {ArrayBuffer} data * @returns {Promise} @@ -31,7 +30,6 @@ class BrowserCache { } /** - * * @param {string} key * @returns {Promise} */ @@ -41,7 +39,6 @@ class BrowserCache { } /** - * * @param {string} key * @returns {Promise} */ diff --git a/lib/utils/wrappers/NodeCache.js b/lib/utils/wrappers/NodeCache.js index 91a76b64..29df2303 100644 --- a/lib/utils/wrappers/NodeCache.js +++ b/lib/utils/wrappers/NodeCache.js @@ -1,12 +1,9 @@ 'use strict'; + const fs = require('fs'); class NodeCache { - constructor() { - } - /** - * * @param {string} key * @returns {Promise} */ @@ -15,7 +12,6 @@ class NodeCache { } /** - * * @param {string} key * @param {ArrayBuffer} data * @returns {Promise} @@ -23,19 +19,21 @@ class NodeCache { async write(key, data) { // Make sure the directory exists const parts = key.split('/').slice(0, -1); + let current = ''; - for (let i = 0; i < parts.length; i++) { - current += `${parts[i]}/`; + + for (const part of parts) { + current += `${part}/`; + if (!(await this.exists(current))) { await fs.promises.mkdir(current); } } - + return await fs.promises.writeFile(key, data); } /** - * * @param {string} key * @returns {Promise} */ @@ -44,7 +42,6 @@ class NodeCache { } /** - * * @param {string} key * @returns {Promise} */ diff --git a/scripts/build-parser-json.js b/scripts/build-parser-json.js index 72a0fdb2..ca4f511b 100644 --- a/scripts/build-parser-json.js +++ b/scripts/build-parser-json.js @@ -1,4 +1,5 @@ 'use strict'; + const glob = require('glob'); const fs = require('fs'); diff --git a/scripts/globals.js b/scripts/globals.js index 342e94dc..3e8f60b0 100644 --- a/scripts/globals.js +++ b/scripts/globals.js @@ -1,6 +1,7 @@ 'use strict'; + global.XMLHttpRequest = class extends require('xhr2') { - // This is for browser-like environments not nessacarily a browser - // So disabling this is okay, sort of + // This is for browser-like environments not necessarily a browser. + // So disabling this is okay, sort of. _restrictedHeaders = {}; }; \ No newline at end of file diff --git a/test/browser/main.test.js b/test/browser/main.test.js index 13df07c8..e518fe5b 100644 --- a/test/browser/main.test.js +++ b/test/browser/main.test.js @@ -1,17 +1,18 @@ 'use strict'; const Fs = require('fs'); -const Innertube = require('../../build/node'); +const Innertube = require('../../build/browser'); const { default: NToken } = require('../../lib/deciphers/NToken'); const { default: Signature} = require('../../lib/deciphers/Signature'); const Constants = require('../constants'); -// NOTE: -// these tests disable the restricted headers in XMLHttpRequest -// so techinically these tests are not 100% accurate, so they -// really represent a browser-like environment and not necessarily a browser -// i.e. electron, where you could disable this restriction - +/** + * Note: + * These tests disable the restricted headers in XMLHttpRequest. + * So technically, they are not 100% accurate and represent browser-like + * environments such as Electron. + */ + describe('YouTube.js Tests', () => { beforeAll(async () => { this.session = await new Innertube(); @@ -83,7 +84,7 @@ describe('YouTube.js Tests', () => { }); /* - // After a lot of testing, I don't think it is possible to download videos in browser + // After a lot of testing, I don't think it is possible to download videos in a browser. it('Should download video', async () => { const result = await download(Constants.VIDEOS[1].ID, this.session); expect(result).toBeTruthy();