@@ -4,6 +4,9 @@ import { getFileName } from './utils.js'
44
55export const baseUrl = 'https://huggingface.co'
66
7+ function getFullName ( url : HFUrl ) : RepoFullName {
8+ return url . type === 'dataset' ? `datasets/${ url . repo } ` : url . type === 'space' ? `spaces/${ url . repo } ` : url . repo
9+ }
710function getSourceParts ( url : HFUrl ) : SourcePart [ ] {
811 const fullName = getFullName ( url )
912 const sourceParts : SourcePart [ ] = [ {
@@ -30,9 +33,6 @@ function getSourceParts(url: HFUrl): SourcePart[] {
3033function getPrefix ( url : DirectoryUrl ) : string {
3134 return `${ url . origin } /${ getFullName ( url ) } /tree/${ url . branch } ${ url . path } ` . replace ( / \/ $ / , '' )
3235}
33- function getFullName ( url : HFUrl ) : RepoFullName {
34- return url . type === 'dataset' ? `datasets/${ url . repo } ` : url . type === 'space' ? `spaces/${ url . repo } ` : url . repo
35- }
3636async function fetchFilesList ( url : DirectoryUrl , options ?: { requestInit ?: RequestInit , accessToken ?: string } ) : Promise < FileMetadata [ ] > {
3737 const filesIterator = listFiles ( {
3838 repo : {
@@ -141,15 +141,25 @@ export function parseHuggingFaceUrl(url: string): HFUrl {
141141 throw new Error ( 'Not a Hugging Face URL' )
142142 }
143143
144- const repoGroups = / ^ (?< type > \/ d a t a s e t s | \/ s p a c e s ) \/ (?< namespace > [ ^ / ] + ) \/ (?< repo > [ ^ / ] + ) \/ ? $ / . exec (
145- urlObject . pathname
144+ let { pathname } = urlObject
145+ let type : RepoType = 'model'
146+ if ( pathname . startsWith ( '/datasets' ) ) {
147+ type = 'dataset'
148+ pathname = pathname . slice ( '/datasets' . length )
149+ } else if ( pathname . startsWith ( '/spaces' ) ) {
150+ type = 'space'
151+ pathname = pathname . slice ( '/spaces' . length )
152+ }
153+
154+ const repoGroups = / ^ \/ (?< namespace > [ ^ / ] + ) \/ (?< repo > [ ^ / ] + ) \/ ? $ / . exec (
155+ pathname
146156 ) ?. groups
147- if ( repoGroups ?. type !== undefined && repoGroups . namespace !== undefined && repoGroups . repo !== undefined ) {
157+ if ( repoGroups ?. namespace !== undefined && repoGroups . repo !== undefined ) {
148158 return {
149159 kind : 'directory' ,
150160 source : url ,
151161 origin : urlObject . origin ,
152- type : parseRepoType ( repoGroups . type . slice ( 1 ) ) ,
162+ type,
153163 repo : repoGroups . namespace + '/' + repoGroups . repo ,
154164 action : 'tree' ,
155165 branch : 'main' , // hardcode the default branch
@@ -158,25 +168,25 @@ export function parseHuggingFaceUrl(url: string): HFUrl {
158168 }
159169
160170 const folderGroups =
161- / ^ (?< type > \/ d a t a s e t s | \/ s p a c e s ) \/ (?< namespace > [ ^ / ] + ) \/ (?< repo > [ ^ / ] + ) \/ (?< action > t r e e ) \/ (?< branch > ( r e f s \/ ( c o n v e r t | p r ) \/ ) ? [ ^ / ] + ) (?< path > ( \/ [ ^ / ] + ) * ) \/ ? $ / . exec (
162- urlObject . pathname
171+ / ^ \/ (?< namespace > [ ^ / ] + ) \/ (?< repo > [ ^ / ] + ) \/ (?< action > t r e e ) \/ (?< branch > ( r e f s \/ ( c o n v e r t | p r ) \/ ) ? [ ^ / ] + ) (?< path > ( \/ [ ^ / ] + ) * ) \/ ? $ / . exec (
172+ pathname
163173 ) ?. groups
164174 if (
165- folderGroups ?. type !== undefined &&
166- folderGroups . namespace !== undefined &&
175+ folderGroups ?. namespace !== undefined &&
167176 folderGroups . repo !== undefined &&
168177 folderGroups . action !== undefined &&
169178 folderGroups . branch !== undefined &&
170179 folderGroups . path !== undefined &&
171180 folderGroups . branch !== 'refs'
172181 ) {
182+ const typePath = type === 'dataset' ? '/datasets' : type === 'space' ? '/spaces' : ''
173183 const branch = folderGroups . branch . replace ( / \/ / g, '%2F' )
174- const source = `${ urlObject . origin } ${ folderGroups . type } /${ folderGroups . namespace } /${ folderGroups . repo } /${ folderGroups . action } /${ branch } ${ folderGroups . path } `
184+ const source = `${ urlObject . origin } ${ typePath } /${ folderGroups . namespace } /${ folderGroups . repo } /${ folderGroups . action } /${ branch } ${ folderGroups . path } `
175185 return {
176186 kind : 'directory' ,
177187 source,
178188 origin : urlObject . origin ,
179- type : parseRepoType ( folderGroups . type . slice ( 1 ) ) ,
189+ type,
180190 repo : folderGroups . namespace + '/' + folderGroups . repo ,
181191 action : 'tree' ,
182192 branch,
@@ -185,30 +195,30 @@ export function parseHuggingFaceUrl(url: string): HFUrl {
185195 }
186196
187197 const fileGroups =
188- / ^ (?< type > \/ d a t a s e t s | \/ s p a c e s ) \/ (?< namespace > [ ^ / ] + ) \/ (?< repo > [ ^ / ] + ) \/ (?< action > b l o b | r e s o l v e ) \/ (?< branch > ( r e f s \/ ( c o n v e r t | p r ) \/ ) ? [ ^ / ] + ) (?< path > ( \/ [ ^ / ] + ) + ) $ / . exec (
189- urlObject . pathname
198+ / ^ \/ (?< namespace > [ ^ / ] + ) \/ (?< repo > [ ^ / ] + ) \/ (?< action > b l o b | r e s o l v e ) \/ (?< branch > ( r e f s \/ ( c o n v e r t | p r ) \/ ) ? [ ^ / ] + ) (?< path > ( \/ [ ^ / ] + ) + ) $ / . exec (
199+ pathname
190200 ) ?. groups
191201 if (
192- fileGroups ?. type !== undefined &&
193- fileGroups . namespace !== undefined &&
202+ fileGroups ?. namespace !== undefined &&
194203 fileGroups . repo !== undefined &&
195204 fileGroups . action !== undefined &&
196205 fileGroups . branch !== undefined &&
197206 fileGroups . path !== undefined &&
198207 fileGroups . branch !== 'refs'
199208 ) {
209+ const typePath = type === 'dataset' ? '/datasets' : type === 'space' ? '/spaces' : ''
200210 const branch = fileGroups . branch . replace ( / \/ / g, '%2F' )
201- const source = `${ urlObject . origin } ${ fileGroups . type } /${ fileGroups . namespace } /${ fileGroups . repo } /${ fileGroups . action } /${ branch } ${ fileGroups . path } `
211+ const source = `${ urlObject . origin } ${ typePath } /${ fileGroups . namespace } /${ fileGroups . repo } /${ fileGroups . action } /${ branch } ${ fileGroups . path } `
202212 return {
203213 kind : 'file' ,
204214 source,
205215 origin : urlObject . origin ,
206- type : parseRepoType ( fileGroups . type . slice ( 1 ) ) ,
216+ type,
207217 repo : fileGroups . namespace + '/' + fileGroups . repo ,
208218 action : fileGroups . action === 'blob' ? 'blob' : 'resolve' ,
209219 branch,
210220 path : fileGroups . path ,
211- resolveUrl : `${ urlObject . origin } ${ fileGroups . type } /${ fileGroups . namespace } /${ fileGroups . repo } /resolve/${ branch } ${ fileGroups . path } ` ,
221+ resolveUrl : `${ urlObject . origin } ${ typePath } /${ fileGroups . namespace } /${ fileGroups . repo } /resolve/${ branch } ${ fileGroups . path } ` ,
212222 }
213223 }
214224
0 commit comments