Skip to content

Commit

Permalink
fix: trim id's passed to the url-utils (#1073)
Browse files Browse the repository at this point in the history
* fix invalid getURLVideoID test

#CopyPaste

* trim id's passed to url-utils
  • Loading branch information
TimeForANinja authored Mar 5, 2022
1 parent 21bc728 commit b35bdad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
10 changes: 5 additions & 5 deletions lib/url-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ const validQueryDomains = new Set([
]);
const validPathDomains = /^https?:\/\/(youtu\.be\/|(www\.)?youtube\.com\/(embed|v|shorts)\/)/;
exports.getURLVideoID = link => {
const parsed = new URL(link);
const parsed = new URL(link.trim());
let id = parsed.searchParams.get('v');
if (validPathDomains.test(link) && !id) {
if (validPathDomains.test(link.trim()) && !id) {
const paths = parsed.pathname.split('/');
id = parsed.host === 'youtu.be' ? paths[1] : paths[2];
} else if (parsed.hostname && !validQueryDomains.has(parsed.hostname)) {
throw Error('Not a YouTube domain');
}
if (!id) {
throw Error(`No video id found: ${link}`);
throw Error(`No video id found: "${link}"`);
}
id = id.substring(0, 11);
if (!exports.validateID(id)) {
Expand All @@ -57,7 +57,7 @@ const urlRegex = /^https?:\/\//;
exports.getVideoID = str => {
if (exports.validateID(str)) {
return str;
} else if (urlRegex.test(str)) {
} else if (urlRegex.test(str.trim())) {
return exports.getURLVideoID(str);
} else {
throw Error(`No video id found: ${str}`);
Expand All @@ -72,7 +72,7 @@ exports.getVideoID = str => {
* @return {boolean}
*/
const idRegex = /^[a-zA-Z0-9-_]{11}$/;
exports.validateID = id => idRegex.test(id);
exports.validateID = id => idRegex.test(id.trim());


/**
Expand Down
40 changes: 25 additions & 15 deletions test/url-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,43 @@ describe('getURLVideoID()', () => {

it('Retrives the video ID from the url', () => {
let id;
id = getVideoID('http://www.youtube.com/watch?v=RAW_VIDEOID');
id = getURLVideoID('http://www.youtube.com/watch?v=RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtu.be/RAW_VIDEOID');
id = getURLVideoID('http://youtu.be/RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/v/RAW_VIDEOID');
id = getURLVideoID('http://youtube.com/v/RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/embed/RAW_VIDEOID');
id = getURLVideoID('http://youtube.com/embed/RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/shorts/RAW_VIDEOID');
id = getURLVideoID('http://youtube.com/shorts/RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('http://youtube.com/v/RAW_VIDEOID/FakeVideoID');
id = getURLVideoID('http://youtube.com/v/RAW_VIDEOID/FakeVideoID');
assert.strictEqual(id, 'RAW_VIDEOID');
id = getVideoID('https://music.youtube.com/watch?v=RAW_VIDEOID&list=RDAMVMmtLgabce8KQ');
id = getURLVideoID('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');
id = getURLVideoID('https://gaming.youtube.com/watch?v=RAW_VIDEOID');
assert.strictEqual(id, 'RAW_VIDEOID');
assert.throws(() => {
getVideoID('https://any.youtube.com/watch?v=RAW_VIDEOID');
getURLVideoID('https://any.youtube.com/watch?v=RAW_VIDEOID');
}, /Not a YouTube domain/);
assert.throws(() => {
getVideoID('https://www.twitch.tv/user/v/1234');
getURLVideoID('https://www.twitch.tv/user/v/1234');
}, /Not a YouTube domain/);
assert.throws(() => {
getVideoID('www.youtube.com');
}, /No video id found: \S+/);
getURLVideoID('www.youtube.com');
}, /Invalid URL/);
assert.throws(() => {
getVideoID('http://www.youtube.com/playlist?list=1337');
}, /No video id found: \S+/);
getURLVideoID('http://www.youtube.com/playlist?list=1337');
}, /No video id found: "\S+"/);
assert.throws(() => {
getVideoID('http://www.youtube.com/watch?v=asdf$%^ddf-');
getURLVideoID('http://www.youtube.com/watch?v=asdf$%^ddf-');
}, /Video id \([^)]+\) does not match expected format/);
});

it('Ignores surrounding whitespaces', () => {
let id = getURLVideoID('\n\t http://www.youtube.com/watch?v=RAW_VIDEOID ');
assert.strictEqual(id, 'RAW_VIDEOID');
});
});


Expand Down Expand Up @@ -78,6 +83,11 @@ describe('getVideoID()', () => {
getVideoID('http://www.youtube.com/playlist?list=1337');
}, /No video id found: \S+/);
});

it('Ignores surrounding whitespaces', () => {
let id = getVideoID('\n\t http://www.youtube.com/watch?v=RAW_VIDEOID ');
assert.strictEqual(id, 'RAW_VIDEOID');
});
});


Expand Down

0 comments on commit b35bdad

Please sign in to comment.