@@ -37,8 +37,29 @@ class ResourceMapper {
3737 }
3838 }
3939
40- get rootPath ( ) {
41- return this . _rootPath
40+ // Gets the base file path for the given hostname
41+ getBaseFilePath ( hostname ) {
42+ return ! this . _includeHost ? this . _rootPath : `${ this . _rootPath } /${ hostname } `
43+ }
44+
45+ // Resolve a URL for the given hostname
46+ // Optionally, a pathname may be passed that will be appended to the baseUrl.
47+ resolveUrl ( hostname , pathname = '' ) {
48+ return ! this . _includeHost ? `${ this . _rootUrl } ${ pathname } `
49+ : `${ this . _protocol } //${ hostname } ${ this . _port } ${ this . _rootUrl } ${ pathname } `
50+ }
51+
52+ // Maps a given server file to a URL
53+ async mapFileToUrl ( { path, hostname } ) {
54+ // Remove the root path if specified
55+ if ( path . startsWith ( this . _rootPath ) ) {
56+ path = path . substring ( this . _rootPath . length )
57+ }
58+
59+ // Determine the URL by chopping off everything after the dollar sign
60+ const pathname = this . _removeDollarExtension ( path )
61+ const url = `${ this . resolveUrl ( hostname ) } ${ encodeURI ( pathname . replace ( / \\ / g, '/' ) ) } `
62+ return { url, contentType : this . _getContentTypeByExtension ( path ) }
4263 }
4364
4465 // Maps the request for a given resource and representation format to a server file
@@ -100,27 +121,6 @@ class ResourceMapper {
100121 }
101122 }
102123
103- // Maps a given server file to a URL
104- async mapFileToUrl ( { path, hostname } ) {
105- // Determine the URL by chopping off everything after the dollar sign
106- let pathname = this . _removeDollarExtension ( path . substring ( this . _rootPath . length ) )
107- pathname = this . _replaceBackslashes ( pathname )
108- const url = `${ this . resolveUrl ( hostname ) } ${ encodeURI ( pathname ) } `
109- return { url, contentType : this . _getContentTypeByExtension ( path ) }
110- }
111-
112- // Gets the base file path for the given hostname
113- getBaseFilePath ( hostname ) {
114- return ! this . _includeHost ? this . _rootPath : `${ this . _rootPath } /${ hostname } `
115- }
116-
117- // Resolve a URL for the given hostname
118- // Optionally, a pathname may be passed that will be appended to the baseUrl.
119- resolveUrl ( hostname , pathname = '' ) {
120- return ! this . _includeHost ? `${ this . _rootUrl } ${ pathname } `
121- : `${ this . _protocol } //${ hostname } ${ this . _port } ${ this . _rootUrl } ${ pathname } `
122- }
123-
124124 // Determine the full file path corresponding to a URL
125125 _getFilePath ( url ) {
126126 const { pathname, hostname } = this . _parseUrl ( url )
@@ -163,10 +163,6 @@ class ResourceMapper {
163163 const dollarPos = path . lastIndexOf ( '$' )
164164 return dollarPos < 0 ? path : path . substr ( 0 , dollarPos )
165165 }
166-
167- _replaceBackslashes ( path ) {
168- return path . replace ( / \\ / g, '/' )
169- }
170166}
171167
172168module . exports = ResourceMapper
0 commit comments