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

Commit

Permalink
fix: In Deno's cache module, x-typescript-types and redirects are n…
Browse files Browse the repository at this point in the history
…ot parsed correctly. close #147
  • Loading branch information
axetroy committed Apr 17, 2020
1 parent 072573c commit a3a957f
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "./result"
},
"url": "https://redirect.com/redirect_to_relative"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "bb:aa@123"
},
"url": "https://redirect.com/invalid_location"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "/circle"
},
"url": "https://redirect.com/circle"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"headers": {},
"url": "https://redirect.com/entry"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "/result"
},
"url": "https://redirect.com/redirect"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"headers": {},
"url": "https://redirect.com/result"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"location": "https://redirect.com/result"
},
"url": "https://redirect.com/full_url_redirect"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"headers": {
"x-typescript-types": "/result"
},
"url": "https://redirect.com/redirect-by-x-typescript-types"
}
152 changes: 147 additions & 5 deletions core/deno_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("core / deno_cache", () => {

expect(cacheModule).not.toBe(undefined);

expect(cacheModule.url.href).toEqual("https://example.com/demo/mod.ts");
expect(cacheModule.meta.url.href).toEqual("https://example.com/demo/mod.ts");
expect(cacheModule.filepath).toEqual(cacheFilepath);

// resolve sub module
Expand All @@ -35,7 +35,7 @@ test("core / deno_cache", () => {
) as DenoCacheModule;

expect(subCacheModule).not.toBe(undefined);
expect(subCacheModule.url.href).toEqual(
expect(subCacheModule.meta.url.href).toEqual(
"https://example.com/demo/sub/mod.ts"
);
expect(subCacheModule.filepath).toEqual(
Expand All @@ -51,7 +51,9 @@ test("core / deno_cache", () => {
) as DenoCacheModule;

expect(esmCacheModule).not.toBe(undefined);
expect(esmCacheModule.url.href).toEqual("https://example.com/esm/mod.ts");
expect(esmCacheModule.meta.url.href).toEqual(
"https://example.com/esm/mod.ts"
);
expect(esmCacheModule.filepath).toEqual(
path.join(
path.dirname(cacheFilepath),
Expand All @@ -65,7 +67,7 @@ test("core / deno_cache", () => {
) as DenoCacheModule;

expect(remoteCacheModule).not.toBe(undefined);
expect(remoteCacheModule.url.href).toEqual(
expect(remoteCacheModule.meta.url.href).toEqual(
"https://another.example.com/path/mod.ts"
);
expect(remoteCacheModule.filepath).toEqual(
Expand All @@ -78,7 +80,9 @@ test("core / deno_cache", () => {
});

test("core / deno_cache if filepath not exist in $DENO_DIR", () => {
const cacheModule = CacheModule.create(__filename);
const cacheModule = CacheModule.create(
path.join(__dirname, "not_exist_file")
);

expect(cacheModule).toBe(undefined);
});
Expand Down Expand Up @@ -119,3 +123,141 @@ test("core / deno_cache resolve a invalid module", () => {
expect(cacheModule).not.toBe(undefined);
expect(cacheModule.resolveModule("invalid:/@url(dk#!")).toBe(undefined);
});

test("core / deno_cache resolve a redirect module", () => {
const cacheFilepath = path.join(
denoDir,
"deps",
"https",
"redirect.com",
"39bce9e2269e45937f28bc4ff60fb3add9df7e0048078c06dec04c490a90bf9f"
);

const cacheModule = CacheModule.create(cacheFilepath) as DenoCacheModule;

expect(cacheModule).not.toBe(undefined);

{
const result = cacheModule.resolveModule("/redirect") as DenoCacheModule;
expect(result).not.toBe(undefined);

expect(result.filepath).toBe(
path.join(
denoDir,
"deps",
"https",
"redirect.com",
"6a3cfff3f701f7e3e2611e61d98e9b04ec6a12fe2a72418eb96b929d3836dfd9"
)
);
}

{
const result = cacheModule.resolveModule(
"https://redirect.com/redirect"
) as DenoCacheModule;
expect(result).not.toBe(undefined);

expect(result.filepath).toBe(
path.join(
denoDir,
"deps",
"https",
"redirect.com",
"6a3cfff3f701f7e3e2611e61d98e9b04ec6a12fe2a72418eb96b929d3836dfd9"
)
);
}

{
const result = cacheModule.resolveModule("./redirect") as DenoCacheModule;
expect(result).not.toBe(undefined);

expect(result.filepath).toBe(
path.join(
denoDir,
"deps",
"https",
"redirect.com",
"6a3cfff3f701f7e3e2611e61d98e9b04ec6a12fe2a72418eb96b929d3836dfd9"
)
);
}

{
const result = cacheModule.resolveModule(
"/invalid_location"
) as DenoCacheModule;
expect(result).not.toBe(undefined);
}

{
const result = cacheModule.resolveModule("/circle") as DenoCacheModule;
expect(result).toBe(undefined);
}

{
const result = cacheModule.resolveModule(
"/full_url_redirect"
) as DenoCacheModule;
expect(result.filepath).toBe(
path.join(
denoDir,
"deps",
"https",
"redirect.com",
"6a3cfff3f701f7e3e2611e61d98e9b04ec6a12fe2a72418eb96b929d3836dfd9"
)
);
expect(result.meta.url.href).toBe("https://redirect.com/result");
}

{
const result = cacheModule.resolveModule(
"/redirect_to_relative"
) as DenoCacheModule;
expect(result.filepath).toBe(
path.join(
denoDir,
"deps",
"https",
"redirect.com",
"0278046a318a368d7a8da89fdf59c60892dbf3498b996b36f11faf7d74d141c8"
)
);
expect(result.meta.url.href).toBe(
"https://redirect.com/redirect_to_relative"
);
}
});

test("core / deno_cache resolve a module which have set a x-typescript-types header", () => {
const cacheFilepath = path.join(
denoDir,
"deps",
"https",
"redirect.com",
"39bce9e2269e45937f28bc4ff60fb3add9df7e0048078c06dec04c490a90bf9f"
);

const cacheModule = CacheModule.create(cacheFilepath) as DenoCacheModule;

expect(cacheModule).not.toBe(undefined);

{
const result = cacheModule.resolveModule(
"/redirect-by-x-typescript-types"
) as DenoCacheModule;
expect(result).not.toBe(undefined);

expect(result.filepath).toBe(
path.join(
denoDir,
"deps",
"https",
"redirect.com",
"6a3cfff3f701f7e3e2611e61d98e9b04ec6a12fe2a72418eb96b929d3836dfd9"
)
);
}
});
Loading

0 comments on commit a3a957f

Please sign in to comment.