Skip to content

Commit 6dbc45b

Browse files
jaxoncreedRubenVerborgh
authored andcommitted
Fix file to URL resolving for multi-host.
* Patch fix * Remove console log * Fix Ruben Comments * replace backslashes to handle windows routes * Fix tests * Reverted original test back because it is async * More specific test to check for error * Correct error comaparison
1 parent b7b5bff commit 6dbc45b

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

lib/resource-mapper.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,20 @@ class ResourceMapper {
6565
// Maps a given server file to a URL
6666
async mapFileToUrl ({ path, hostname }) {
6767
// Remove the root path if specified
68+
path = path.replace(/\\/g, '/')
6869
if (path.startsWith(this._rootPath)) {
6970
path = path.substring(this._rootPath.length)
7071
}
72+
if (this._includeHost) {
73+
if (!path.startsWith(`/${hostname}/`)) {
74+
throw new Error(`Path must start with hostname (/${hostname})`)
75+
}
76+
path = path.substring(hostname.length + 1)
77+
}
7178

7279
// Determine the URL by chopping off everything after the dollar sign
7380
const pathname = this._removeDollarExtension(path)
74-
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname.replace(/\\/g, '/'))}`
81+
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}`
7582
return { url, contentType: this._getContentTypeByExtension(path) }
7683
}
7784

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
@prefix : <#>.
22
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
3-
@prefix c: <https://localhost:7000/profile/card#>.
4-
@prefix c0: <https://localhost:8443/profile/card#>.
53

6-
c:me
7-
acl:trustedApp
8-
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].
9-
c0:me
4+
:me
105
acl:trustedApp
116
[
127
acl:mode acl:Append, acl:Control, acl:Read, acl:Write;
138
acl:origin <https://trusted.app>
14-
].
9+
],
10+
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
@prefix : <#>.
22
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
3-
@prefix c: <https://localhost:7010/profile/card#>.
4-
@prefix c0: <https://localhost:8443/profile/card#>.
53

6-
c:me
7-
acl:trustedApp
8-
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].
9-
c0:me
4+
:me
105
acl:trustedApp
116
[
127
acl:mode acl:Append, acl:Control, acl:Read, acl:Write;
138
acl:origin <https://trusted.app>
14-
].
9+
],
10+
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].

test/unit/resource-mapper-test.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ describe('ResourceMapper', () => {
535535

536536
itMapsFile(mapper, 'a file on a host',
537537
{
538-
path: `${rootPath}space/foo.html`,
538+
path: `${rootPath}example.org/space/foo.html`,
539539
hostname: 'example.org'
540540
},
541541
{
@@ -550,7 +550,7 @@ describe('ResourceMapper', () => {
550550

551551
itMapsFile(mapper, 'a file on a host',
552552
{
553-
path: `${rootPath}space/foo.html`,
553+
path: `${rootPath}example.org/space/foo.html`,
554554
hostname: 'example.org'
555555
},
556556
{
@@ -564,10 +564,11 @@ describe('ResourceMapper', () => {
564564

565565
itMapsFile(mapper, 'a file with the port',
566566
{
567-
path: `${rootPath}space/foo.html`
567+
path: `${rootPath}example.org/space/foo.html`,
568+
hostname: 'example.org'
568569
},
569570
{
570-
url: 'http://localhost:81/space/foo.html',
571+
url: 'http://localhost:81/example.org/space/foo.html',
571572
contentType: 'text/html'
572573
})
573574
})
@@ -577,7 +578,7 @@ describe('ResourceMapper', () => {
577578

578579
itMapsFile(mapper, 'a file with the port',
579580
{
580-
path: `${rootPath}space/foo.html`,
581+
path: `${rootPath}example.org/space/foo.html`,
581582
hostname: 'example.org'
582583
},
583584
{
@@ -591,10 +592,11 @@ describe('ResourceMapper', () => {
591592

592593
itMapsFile(mapper, 'a file with the port',
593594
{
594-
path: `${rootPath}space/foo.html`
595+
path: `${rootPath}example.org/space/foo.html`,
596+
hostname: 'example.org'
595597
},
596598
{
597-
url: 'https://localhost:81/space/foo.html',
599+
url: 'https://localhost:81/example.org/space/foo.html',
598600
contentType: 'text/html'
599601
})
600602
})
@@ -604,14 +606,25 @@ describe('ResourceMapper', () => {
604606

605607
itMapsFile(mapper, 'a file with the port',
606608
{
607-
path: `${rootPath}space/foo.html`,
609+
path: `${rootPath}example.org/space/foo.html`,
608610
hostname: 'example.org'
609611
},
610612
{
611613
url: 'https://example.org:81/space/foo.html',
612614
contentType: 'text/html'
613615
})
614616
})
617+
618+
describe('A ResourceMapper instance for an HTTPS host with non-default port in a multi-host setup', () => {
619+
const mapper = new ResourceMapper({ rootUrl: 'https://localhost:81/', rootPath, includeHost: true })
620+
621+
it('throws an error when there is an improper file path', () => {
622+
return expect(mapper.mapFileToUrl({
623+
path: `${rootPath}example.orgspace/foo.html`,
624+
hostname: 'example.org'
625+
})).to.be.rejectedWith(Error, 'Path must start with hostname (/example.org)')
626+
})
627+
})
615628
})
616629

617630
function asserter (assert) {

0 commit comments

Comments
 (0)