merge/youtube: bump youtubei.js to v10.1.0, update token format (#626)

to be in line with the structure youtubei.js expects when initializing
a session, the `expires` value of the stored tokens has been renamed
to `expiry_date`. if you have sessions that are stored in the cookies
file, cobalt ad-hoc transforms them into this new format.
This commit is contained in:
jj 2024-07-10 18:53:14 +02:00 committed by GitHub
commit c1f364929c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 27 deletions

22
package-lock.json generated
View file

@ -24,7 +24,7 @@
"set-cookie-parser": "2.6.0", "set-cookie-parser": "2.6.0",
"undici": "^5.19.1", "undici": "^5.19.1",
"url-pattern": "1.0.3", "url-pattern": "1.0.3",
"youtubei.js": "^9.3.0" "youtubei.js": "^10.1.0"
}, },
"engines": { "engines": {
"node": ">=18" "node": ">=18"
@ -73,9 +73,9 @@
} }
}, },
"node_modules/acorn": { "node_modules/acorn": {
"version": "8.11.3", "version": "8.12.1",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz",
"integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", "integrity": "sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==",
"bin": { "bin": {
"acorn": "bin/acorn" "acorn": "bin/acorn"
}, },
@ -683,9 +683,9 @@
} }
}, },
"node_modules/jintr": { "node_modules/jintr": {
"version": "1.1.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/jintr/-/jintr-1.1.0.tgz", "resolved": "https://registry.npmjs.org/jintr/-/jintr-2.0.0.tgz",
"integrity": "sha512-Tu9wk3BpN2v+kb8yT6YBtue+/nbjeLFv4vvVC4PJ7oCidHKbifWhvORrAbQfxVIQZG+67am/mDagpiGSVtvrZg==", "integrity": "sha512-RiVlevxttZ4eHEYB2dXKXDXluzHfRuw0DJQGsYuKCc5IvZj5/GbOakeqVX+Bar/G9kTty9xDJREcxukurkmYLA==",
"funding": [ "funding": [
"https://github.com/sponsors/LuanRT" "https://github.com/sponsors/LuanRT"
], ],
@ -1123,14 +1123,14 @@
} }
}, },
"node_modules/youtubei.js": { "node_modules/youtubei.js": {
"version": "9.4.0", "version": "10.1.0",
"resolved": "https://registry.npmjs.org/youtubei.js/-/youtubei.js-9.4.0.tgz", "resolved": "https://registry.npmjs.org/youtubei.js/-/youtubei.js-10.1.0.tgz",
"integrity": "sha512-8plCOZD2WabqWSEgZU3RjzigIIeR7sF028EERJENYrC9xO/6awpLMZfeoE1gNrNEbKcA+bzbMvonqlvBdxGdKg==", "integrity": "sha512-MokZMAnpWH11VYvWuW6qjPiiPmgRl5rfDgPQOpif9qXcVHoVw1hi8ePuRSD0AZSZ+uvWGe8rvas2dzp+Jv5JKQ==",
"funding": [ "funding": [
"https://github.com/sponsors/LuanRT" "https://github.com/sponsors/LuanRT"
], ],
"dependencies": { "dependencies": {
"jintr": "^1.1.0", "jintr": "^2.0.0",
"tslib": "^2.5.0", "tslib": "^2.5.0",
"undici": "^5.19.1" "undici": "^5.19.1"
} }

View file

@ -40,7 +40,7 @@
"set-cookie-parser": "2.6.0", "set-cookie-parser": "2.6.0",
"undici": "^5.19.1", "undici": "^5.19.1",
"url-pattern": "1.0.3", "url-pattern": "1.0.3",
"youtubei.js": "^9.3.0" "youtubei.js": "^10.1.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"freebind": "^0.2.2" "freebind": "^0.2.2"

View file

@ -28,20 +28,24 @@ const transformSessionData = (cookie) => {
if (!cookie) if (!cookie)
return; return;
const values = cookie.values(); const values = { ...cookie.values() };
const REQUIRED_VALUES = [ const REQUIRED_VALUES = [
'access_token', 'refresh_token', 'access_token', 'refresh_token',
'client_id', 'client_secret', 'client_id', 'client_secret'
'expires'
]; ];
if (REQUIRED_VALUES.some(x => typeof values[x] !== 'string')) { if (REQUIRED_VALUES.some(x => typeof values[x] !== 'string')) {
return; return;
} }
return {
...values, if (values.expires) {
expires: new Date(values.expires), values.expiry_date = values.expires;
}; delete values.expires;
} else if (!values.expiry_date) {
return;
}
return values;
} }
const cloneInnertube = async (customFetch) => { const cloneInnertube = async (customFetch) => {
@ -70,14 +74,19 @@ const cloneInnertube = async (customFetch) => {
} }
if (session.logged_in) { if (session.logged_in) {
await session.oauth.refreshIfRequired(); if (session.oauth.shouldRefreshToken()) {
const oldExpiry = new Date(cookie.values().expires); await session.oauth.refreshAccessToken();
const newExpiry = session.oauth.credentials.expires; }
const cookieValues = cookie.values();
const oldExpiry = new Date(cookieValues.expiry_date);
const newExpiry = new Date(session.oauth.oauth2_tokens.expiry_date);
if (oldExpiry.getTime() !== newExpiry.getTime()) { if (oldExpiry.getTime() !== newExpiry.getTime()) {
updateCookieValues(cookie, { updateCookieValues(cookie, {
...session.oauth.credentials, ...session.oauth.client_id,
expires: session.oauth.credentials.expires.toISOString() ...session.oauth.oauth2_tokens,
expiry_date: newExpiry.toISOString()
}); });
} }
} }

View file

@ -20,9 +20,9 @@ tube.session.once(
); );
tube.session.once('auth-error', (err) => bail('An error occurred:', err)); tube.session.once('auth-error', (err) => bail('An error occurred:', err));
tube.session.once('auth', ({ status, credentials, ...rest }) => { tube.session.once('auth', ({ credentials }) => {
if (status !== 'SUCCESS') { if (!credentials.access_token) {
bail('something went wrong', rest); bail('something went wrong');
} }
console.log( console.log(