Skip to content

Commit

Permalink
feat(isMagnetURI): support Bittorrent v2 (#1992)
Browse files Browse the repository at this point in the history
* feat(isMagnetURI): support Bittorrent v2

* fix(isMagnetURI): make sure `xt=` is uri component

* Apply suggestions from code review

Co-authored-by: Sarhan Aissi <tux-tn@users.noreply.github.com>

Co-authored-by: Sarhan Aissi <tux-tn@users.noreply.github.com>
  • Loading branch information
Rhilip and tux-tn committed Jun 30, 2022
1 parent 9a0bb35 commit 0175670
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/lib/isMagnetURI.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import assertString from './util/assertString';

const magnetURI = /^magnet:\?xt(?:\.1)?=urn:(?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?($|&)/i;
const magnetURIComponent = /(?:^magnet:\?|[^?&]&)xt(?:\.1)?=urn:(?:(?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?|btmh:1220[a-z0-9]{64})(?:$|&)/i;

export default function isMagnetURI(url) {
assertString(url);
return magnetURI.test(url.trim());

if (url.indexOf('magnet:?') !== 0) {
return false;
}

return magnetURIComponent.test(url);
}
4 changes: 4 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10256,6 +10256,8 @@ describe('Validators', () => {
'magnet:?xt=urn:md5:ABCDEFGHIJKLMNOPQRSTUVWXYZ123456',
'magnet:?xt=urn:tree:tiger:ABCDEFGHIJKLMNOPQRSTUVWXYZ123456',
'magnet:?xt=urn:ed2k:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234',
'magnet:?tr=udp://helloworld:1337/announce&xt=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234',
'magnet:?xt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e',
],
invalid: [
':?xt=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234',
Expand All @@ -10268,6 +10270,8 @@ describe('Validators', () => {
'magnet:?xt:urn:nonexisting:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234',
'magnet:?xt.2=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234',
'magnet:?xt=urn:ed2k:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234567890123456789ABCD',
'magnet:?xt=urn:btmh:1120caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e',
'magnet:?ttxt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e',
],
});
/* eslint-enable max-len */
Expand Down

0 comments on commit 0175670

Please sign in to comment.