Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
fix: cannot resolve module if location headers is relative or absolut…
Browse files Browse the repository at this point in the history
…e path. close #97
  • Loading branch information
axetroy committed Mar 16, 2020
1 parent 42e9fb5 commit 75d6027
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "go_to_invalid"
},
"url": "https://example.com/redirect_to_invalid"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "/esm/mod.ts"
},
"url": "https://example.com/redirect_to_absolute"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "https://example.com/redirect_to_loop"
},
"url": "https://example.com/redirect_to_loop"
}
30 changes: 28 additions & 2 deletions core/module_resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,14 @@ test("core / module_resolver: resolve module if redirect", () => {

const resolver = ModuleResolver.create(__filename, importMapFile);

expect(resolver.resolveModules(["https://example.com/redirect"])).toEqual([
expect(
resolver.resolveModules([
"https://example.com/redirect",
"https://example.com/redirect_to_absolute",
"https://example.com/redirect_to_invalid",
"https://example.com/redirect_to_loop"
])
).toEqual([
{
origin: "https://another.example.com/path/mod.ts",
filepath: path.join(
Expand All @@ -180,6 +187,25 @@ test("core / module_resolver: resolve module if redirect", () => {
"another.example.com",
"32cd9336a09393d88fc22cf6f95ae006e3f2742a6c461967b2ba7954c5283fbf"
)
}
},
{
origin: "https://example.com/esm/mod.ts",
filepath: path.join(
denoDir,
"deps",
"https",
"example.com",
"8afd52da760dab7f2deda4b7453197f50421f310372c5da3f3847ffd062fa1cf"
),
module: path.join(
denoDir,
"deps",
"https",
"example.com",
"8afd52da760dab7f2deda4b7453197f50421f310372c5da3f3847ffd062fa1cf"
)
},
undefined,
undefined
] as ResolvedModule[]);
});
18 changes: 16 additions & 2 deletions core/module_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,23 @@ export class ModuleResolver implements ModuleResolverInterface {
return;
}

const redirect = meta.headers["location"];
let redirect = meta.headers["location"];

if (redirect) {
redirect = isHttpURL(redirect) // eg: https://redirect.com/path/to/redirect
? redirect
: path.posix.isAbsolute(redirect) // eg: /path/to/redirect
? `${url.protocol}//${url.host}${redirect}`
: // eg: ./path/to/redirect
`${url.protocol}//${url.host}${path.posix.resolve(
url.pathname,
redirect
)}`;

if (!isHttpURL(redirect) || redirect === httpModuleURL) {
return;
}

if (redirect && redirect !== httpModuleURL) {
return this.resolveFromRemote(redirect);
}

Expand Down

0 comments on commit 75d6027

Please sign in to comment.