Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trim id's passed to the url-utils #1073

Merged
merged 2 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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