@@ -21,13 +21,13 @@ export async function downloadTorrent(url: string): Promise<Torrent> {
2121
2222 const torrentBlob : Blob = await response . blob ( ) ;
2323 const torrentData : string = await convertBlobToString ( torrentBlob ) ;
24+ let decodedTorrentData : any ;
2425 try {
25- validateTorrentData ( response , torrentData ) ;
26+ decodedTorrentData = decodeTorrentDataAndValidate ( response , torrentData ) ;
2627 } catch ( error ) {
2728 reject ( error ) ;
2829 return ;
29- }
30- const decodedTorrentData = bencode . decode ( Buffer . from ( torrentData , 'ascii' ) ) ;
30+ } ;
3131
3232 resolve ( {
3333 data : torrentBlob ,
@@ -41,8 +41,10 @@ export async function downloadTorrent(url: string): Promise<Torrent> {
4141 } ) ;
4242}
4343
44- function validateTorrentData ( response : Response , data : string ) : void {
45- if ( ! data || data . length < 10 || ! data . startsWith ( "d8:announce" ) ) {
44+ function decodeTorrentDataAndValidate ( response : Response , torrentData : string ) : any {
45+ try {
46+ return bencode . decode ( Buffer . from ( torrentData , 'ascii' ) ) ;
47+ } catch ( error ) {
4648 let contentType = response . headers . get ( "Content-Type" ) ;
4749 if ( contentType ) {
4850 const semicolonPos = contentType . indexOf ( ";" ) ;
@@ -51,8 +53,8 @@ function validateTorrentData(response: Response, data: string): void {
5153 contentType = "unknown"
5254 }
5355
54- console . error ( "Invalid torrent data received: " , data ) ;
55-
56- throw new Error ( "Received " + contentType + " content instead of a . torrent file" ) ;
56+ console . error ( "Invalid torrent data received" , torrentData ) ;
57+
58+ throw new Error ( "Received " + contentType + " instead of a torrent file. Please check the devtools view for details. " ) ;
5759 }
5860}
0 commit comments