1
+ import { URL } from 'url'
1
2
import Joi from 'joi'
2
3
import { NotFound , pathParam , queryParam } from '../index.js'
3
4
import { ConditionalGithubAuthV3Service } from '../github/github-auth-service.js'
@@ -37,7 +38,12 @@ export default class ScoopVersion extends ConditionalGithubAuthV3Service {
37
38
'[Scoop](https://scoop.sh/) is a command-line installer for Windows' ,
38
39
parameters : [
39
40
pathParam ( { name : 'app' , example : 'ngrok' } ) ,
40
- queryParam ( { name : 'bucket' , example : 'extras' } ) ,
41
+ queryParam ( {
42
+ name : 'bucket' ,
43
+ description :
44
+ "App's containing bucket. Can either be a name (e.g `extras`) or a URL to a GitHub Repo (e.g `https://github.com/jewlexx/personal-scoop`)" ,
45
+ example : 'extras' ,
46
+ } ) ,
41
47
] ,
42
48
} ,
43
49
} ,
@@ -60,9 +66,30 @@ export default class ScoopVersion extends ConditionalGithubAuthV3Service {
60
66
} )
61
67
}
62
68
const bucket = queryParams . bucket || 'main'
63
- const bucketUrl = this . buckets [ bucket ]
69
+ let bucketUrl = this . buckets [ bucket ]
64
70
if ( ! bucketUrl ) {
65
- throw new NotFound ( { prettyMessage : `bucket "${ bucket } " not found` } )
71
+ // Parsing URL here will throw an error if the url is invalid
72
+ try {
73
+ const url = new URL ( decodeURIComponent ( bucket ) )
74
+
75
+ // Throw errors to go to jump to catch statement
76
+ // The error messages here are purely for code readability, and will never reach the user.
77
+ if ( url . hostname !== 'github.com' ) {
78
+ throw new Error ( 'Not a GitHub URL' )
79
+ }
80
+ const path = url . pathname . split ( '/' ) . filter ( value => value !== '' )
81
+
82
+ if ( path . length !== 2 ) {
83
+ throw new Error ( 'Not a valid GitHub Repo' )
84
+ }
85
+
86
+ const [ user , repo ] = path
87
+
88
+ // Reconstructing the url here ensures that the url will match the regex
89
+ bucketUrl = `https://github.com/${ user } /${ repo } `
90
+ } catch ( e ) {
91
+ throw new NotFound ( { prettyMessage : `bucket "${ bucket } " not found` } )
92
+ }
66
93
}
67
94
const {
68
95
groups : { user, repo } ,
0 commit comments