Skip to content

Commit

Permalink
fix: bug in URL decection (#984)
Browse files Browse the repository at this point in the history
* Update url-utils.js

* Fixed youtu.be not working

* Added new tests

* linting

* hotfix for tests
  • Loading branch information
Histmy committed Aug 4, 2021
1 parent 273cf1a commit d785dc9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/url-utils.js
Expand Up @@ -22,13 +22,13 @@ const validQueryDomains = new Set([
'music.youtube.com',
'gaming.youtube.com',
]);
const validPathDomains = /^https?:\/\/(youtu\.be\/|(www\.)?youtube.com\/(embed|v|shorts)\/)/;
const validPathDomains = /^https?:\/\/(youtu\.be\/|(www\.)?youtube\.com\/(embed|v|shorts)\/)/;
exports.getURLVideoID = link => {
const parsed = new URL(link);
let id = parsed.searchParams.get('v');
if (validPathDomains.test(link) && !id) {
const paths = parsed.pathname.split('/');
id = paths[paths.length - 1];
id = parsed.host === 'youtu.be' ? paths[1] : paths[2];
} else if (parsed.hostname && !validQueryDomains.has(parsed.hostname)) {
throw Error('Not a YouTube domain');
}
Expand Down
4 changes: 4 additions & 0 deletions test/url-utils-test.js
Expand Up @@ -19,6 +19,8 @@ describe('getURLVideoID()', () => {
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/shorts/RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/v/RAW_VIDEOID/FakeVideoID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('https://music.youtube.com/watch?v=RAW_VIDEOID&list=RDAMVMmtLgabce8KQ');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('https://gaming.youtube.com/watch?v=RAW_VIDEOID');
Expand Down Expand Up @@ -59,6 +61,8 @@ describe('getVideoID()', () => {
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/shorts/RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/v/RAW_VIDEOID/FakeVideoID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('_LENGTH_11_');
assert.strictEqual(id, '_LENGTH_11_');
assert.throws(() => {
Expand Down

0 comments on commit d785dc9

Please sign in to comment.