@@ -37,8 +37,34 @@ class ResourceMapper {
3737 }
3838 }
3939
40- get rootPath ( ) {
41- return this . _rootPath
40+ // Returns the URL of the given HTTP request
41+ getRequestUrl ( req ) {
42+ const { hostname, pathname } = this . _parseUrl ( req )
43+ return this . resolveUrl ( hostname , pathname )
44+ }
45+
46+ // Returns the URL for the relative path on the pod
47+ resolveUrl ( hostname , pathname = '' ) {
48+ return ! this . _includeHost ? `${ this . _rootUrl } ${ pathname } `
49+ : `${ this . _protocol } //${ hostname } ${ this . _port } ${ this . _rootUrl } ${ pathname } `
50+ }
51+
52+ // Gets the base file path for the given hostname
53+ getBaseFilePath ( hostname ) {
54+ return ! this . _includeHost ? this . _rootPath : `${ this . _rootPath } /${ hostname } `
55+ }
56+
57+ // Maps a given server file to a URL
58+ async mapFileToUrl ( { path, hostname } ) {
59+ // Remove the root path if specified
60+ if ( path . startsWith ( this . _rootPath ) ) {
61+ path = path . substring ( this . _rootPath . length )
62+ }
63+
64+ // Determine the URL by chopping off everything after the dollar sign
65+ const pathname = this . _removeDollarExtension ( path )
66+ const url = `${ this . resolveUrl ( hostname ) } ${ encodeURI ( pathname . replace ( / \\ / g, '/' ) ) } `
67+ return { url, contentType : this . _getContentTypeByExtension ( path ) }
4268 }
4369
4470 // Maps the request for a given resource and representation format to a server file
@@ -100,32 +126,6 @@ class ResourceMapper {
100126 }
101127 }
102128
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- // Returns the URL for the given HTTP request
118- getRequestUrl ( req ) {
119- const { hostname, pathname } = this . _parseUrl ( req )
120- return this . resolveUrl ( hostname , pathname )
121- }
122-
123- // Returns the URL for the relative path on the pod
124- resolveUrl ( hostname , pathname = '' ) {
125- return ! this . _includeHost ? `${ this . _rootUrl } ${ pathname } `
126- : `${ this . _protocol } //${ hostname } ${ this . _port } ${ this . _rootUrl } ${ pathname } `
127- }
128-
129129 // Determine the full file path corresponding to a URL
130130 _getFilePath ( url ) {
131131 const { pathname, hostname } = this . _parseUrl ( url )
@@ -168,10 +168,6 @@ class ResourceMapper {
168168 const dollarPos = path . lastIndexOf ( '$' )
169169 return dollarPos < 0 ? path : path . substr ( 0 , dollarPos )
170170 }
171-
172- _replaceBackslashes ( path ) {
173- return path . replace ( / \\ / g, '/' )
174- }
175171}
176172
177173module . exports = ResourceMapper
0 commit comments