diff --git a/__test__/deno_dir_manifest/deps/https/example.com/8611ea80bb4f73e9a0c1207b611d77892ac9a19f840b2d389275e6e4d22c0259 b/__test__/deno_dir_manifest/deps/https/example.com/8611ea80bb4f73e9a0c1207b611d77892ac9a19f840b2d389275e6e4d22c0259 deleted file mode 100644 index 6b8a859..0000000 --- a/__test__/deno_dir_manifest/deps/https/example.com/8611ea80bb4f73e9a0c1207b611d77892ac9a19f840b2d389275e6e4d22c0259 +++ /dev/null @@ -1 +0,0 @@ -// /demo.json \ No newline at end of file diff --git a/__test__/deno_dir_manifest/deps/https/example.com/8611ea80bb4f73e9a0c1207b611d77892ac9a19f840b2d389275e6e4d22c0259.metadata.json b/__test__/deno_dir_manifest/deps/https/example.com/8611ea80bb4f73e9a0c1207b611d77892ac9a19f840b2d389275e6e4d22c0259.metadata.json deleted file mode 100644 index d6e79cc..0000000 --- a/__test__/deno_dir_manifest/deps/https/example.com/8611ea80bb4f73e9a0c1207b611d77892ac9a19f840b2d389275e6e4d22c0259.metadata.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "headers": {}, - "url": "https://example.com/demo.json" -} diff --git a/__test__/deno_dir_manifest/deps/https/invalid_meta.com/1d6ab9051e9ae4e3a9d501b1a4316583ae8ecd51d41d327b514abf0194c14dc4 b/__test__/deno_dir_manifest/deps/https/invalid_meta.com/1d6ab9051e9ae4e3a9d501b1a4316583ae8ecd51d41d327b514abf0194c14dc4 new file mode 100644 index 0000000..e69de29 diff --git a/__test__/deno_dir_manifest/deps/https/invalid_meta.com/1d6ab9051e9ae4e3a9d501b1a4316583ae8ecd51d41d327b514abf0194c14dc4.metadata.json b/__test__/deno_dir_manifest/deps/https/invalid_meta.com/1d6ab9051e9ae4e3a9d501b1a4316583ae8ecd51d41d327b514abf0194c14dc4.metadata.json new file mode 100644 index 0000000..6d4f3ad --- /dev/null +++ b/__test__/deno_dir_manifest/deps/https/invalid_meta.com/1d6ab9051e9ae4e3a9d501b1a4316583ae8ecd51d41d327b514abf0194c14dc4.metadata.json @@ -0,0 +1,5 @@ +{ + "headers": { + "missing-url": true + } +} diff --git a/__test__/deno_dir_manifest/deps/https/invalid_meta.com/2f5c120425d2222c81506ea48f25c608a89272ed179233ab4fd47debb0f5d05f b/__test__/deno_dir_manifest/deps/https/invalid_meta.com/2f5c120425d2222c81506ea48f25c608a89272ed179233ab4fd47debb0f5d05f new file mode 100644 index 0000000..e69de29 diff --git a/__test__/deno_dir_manifest/deps/https/invalid_meta.com/2f5c120425d2222c81506ea48f25c608a89272ed179233ab4fd47debb0f5d05f.metadata.json b/__test__/deno_dir_manifest/deps/https/invalid_meta.com/2f5c120425d2222c81506ea48f25c608a89272ed179233ab4fd47debb0f5d05f.metadata.json new file mode 100644 index 0000000..ec4c24c --- /dev/null +++ b/__test__/deno_dir_manifest/deps/https/invalid_meta.com/2f5c120425d2222c81506ea48f25c608a89272ed179233ab4fd47debb0f5d05f.metadata.json @@ -0,0 +1,3 @@ +{ + "url": "https://invalid_meta.com/missing_headers" +} diff --git a/__test__/deno_dir_manifest/deps/https/unknown_module.com/27e936010957fa71c39ae46b56a40b4b4c5b388f50d950637e4522708a340663 b/__test__/deno_dir_manifest/deps/https/unknown_module.com/27e936010957fa71c39ae46b56a40b4b4c5b388f50d950637e4522708a340663 new file mode 100644 index 0000000..e69de29 diff --git a/__test__/deno_dir_manifest/deps/https/unknown_module.com/27e936010957fa71c39ae46b56a40b4b4c5b388f50d950637e4522708a340663.metadata.json b/__test__/deno_dir_manifest/deps/https/unknown_module.com/27e936010957fa71c39ae46b56a40b4b4c5b388f50d950637e4522708a340663.metadata.json new file mode 100644 index 0000000..ed8023d --- /dev/null +++ b/__test__/deno_dir_manifest/deps/https/unknown_module.com/27e936010957fa71c39ae46b56a40b4b4c5b388f50d950637e4522708a340663.metadata.json @@ -0,0 +1,4 @@ +{ + "headers": {}, + "url": "https://unknown_module.com/unknown_module_without_extension" +} diff --git a/core/extension.test.ts b/core/extension.test.ts index f7c16be..00e7020 100644 --- a/core/extension.test.ts +++ b/core/extension.test.ts @@ -1,4 +1,4 @@ -import { getExtensionFromFile } from "./extension"; +import { getExtensionFromFile, isValidDenoModuleExtension } from "./extension"; test("core / extension: getExtensionFromFile", async () => { expect(getExtensionFromFile("./foo.ts")).toBe(".ts"); @@ -6,7 +6,17 @@ test("core / extension: getExtensionFromFile", async () => { expect(getExtensionFromFile("./foo.js")).toBe(".js"); expect(getExtensionFromFile("./foo.jsx")).toBe(".jsx"); expect(getExtensionFromFile("./foo.d.ts")).toBe(".d.ts"); - expect(getExtensionFromFile("./foo.json")).toBe(".json"); expect(getExtensionFromFile("./foo.wasm")).toBe(".wasm"); expect(getExtensionFromFile("./foo")).toBe(""); }); + +test("core / extension: isValidDenoModuleExtension", async () => { + expect(isValidDenoModuleExtension("./foo.ts")).toBeTruthy(); + expect(isValidDenoModuleExtension("./foo.tsx")).toBeTruthy(); + expect(isValidDenoModuleExtension("./foo.js")).toBeTruthy(); + expect(isValidDenoModuleExtension("./foo.jsx")).toBeTruthy(); + expect(isValidDenoModuleExtension("./foo.d.ts")).toBeTruthy(); + expect(isValidDenoModuleExtension("./foo.wasm")).toBeTruthy(); + expect(isValidDenoModuleExtension("./foo")).toBeFalsy(); + expect(isValidDenoModuleExtension("./foo.json")).toBeFalsy(); +}); diff --git a/core/extension.ts b/core/extension.ts index 6bf4949..30bdb12 100644 --- a/core/extension.ts +++ b/core/extension.ts @@ -6,7 +6,6 @@ export type Extension = | ".d.ts" | ".js" | ".jsx" - | ".json" | ".wasm" | ""; @@ -21,3 +20,11 @@ export function getExtensionFromFile(filename: string): Extension { return extName as Extension; } + +export function isValidDenoModuleExtension(filename: string): boolean { + if (/\.(tsx?|jsx?|d\.ts|wasm)$/.test(filename)) { + return true; + } + + return false; +} diff --git a/core/hash_meta.test.ts b/core/hash_meta.test.ts index 66fc632..9fb5757 100644 --- a/core/hash_meta.test.ts +++ b/core/hash_meta.test.ts @@ -48,24 +48,6 @@ test("core / hash_meta with javascript file", () => { expect(meta.destinationFilepath).toEqual(cacheFilepath); }); -test("core / hash_meta with json file", () => { - const cacheFilepath = path.join( - denoDir, - "deps", - "https", - "example.com", - "8611ea80bb4f73e9a0c1207b611d77892ac9a19f840b2d389275e6e4d22c0259" - ); - - const meta = HashMeta.create(cacheFilepath + ".metadata.json") as HashMeta; - expect(meta).not.toBe(undefined); - expect(meta.filepath).toEqual(cacheFilepath + ".metadata.json"); - expect(meta.type).toEqual(Type.JSON); - expect(meta.extension).toEqual(".json"); - expect(meta.url.href).toEqual("https://example.com/demo.json"); - expect(meta.destinationFilepath).toEqual(cacheFilepath); -}); - test("core / hash_meta without extension name", () => { const cacheFilepath = path.join( denoDir, @@ -132,3 +114,29 @@ test("core / hash_meta: if not exist", () => { const meta = HashMeta.create(notExistCache + ".metadata.json"); expect(meta).toBe(undefined); }); + +test("core / hash_meta: if meta file missing url", () => { + const notExistCache = path.join( + denoDir, + "deps", + "https", + "invalid_mta.com", + "1d6ab9051e9ae4e3a9d501b1a4316583ae8ecd51d41d327b514abf0194c14dc4" + ); + + const meta = HashMeta.create(notExistCache + ".metadata.json"); + expect(meta).toBe(undefined); +}); + +test("core / hash_meta: if meta file missing headers", () => { + const notExistCache = path.join( + denoDir, + "deps", + "https", + "invalid_mta.com", + "2f5c120425d2222c81506ea48f25c608a89272ed179233ab4fd47debb0f5d05f" + ); + + const meta = HashMeta.create(notExistCache + ".metadata.json"); + expect(meta).toBe(undefined); +}); diff --git a/core/hash_meta.ts b/core/hash_meta.ts index 2cbf553..555bee1 100644 --- a/core/hash_meta.ts +++ b/core/hash_meta.ts @@ -16,7 +16,6 @@ export enum Type { JavaScriptReact = "javascriptreact", TypeScript = "typescript", TypeScriptReact = "typescriptreact", - JSON = "json", WebAssembly = "WebAssembly", PlainText = "plaintext", } @@ -35,7 +34,6 @@ const extNameMap: { [key: string]: Type } = { ".js": Type.JavaScript, ".jsx": Type.JavaScriptReact, ".mjs": Type.JavaScript, - ".json": Type.JSON, ".wasm": Type.WebAssembly, }; @@ -55,7 +53,6 @@ const contentTypeMap: [string[], Type][] = [ ], Type.JavaScript, ], - [["application/json"], Type.JSON], [["application/wasm"], Type.WebAssembly], ]; @@ -69,6 +66,10 @@ export class HashMeta implements HashMetaInterface { fs.readFileSync(metaFilepath, { encoding: "utf8" }) ); + if (!metaMap.url || !metaMap.headers) { + return; + } + return new HashMeta(metaFilepath, new URL(metaMap.url), metaMap.headers); } private constructor( @@ -117,8 +118,6 @@ export class HashMeta implements HashMetaInterface { /* istanbul ignore next */ case Type.TypeScriptReact: return ".tsx"; - case Type.JSON: - return ".json"; /* istanbul ignore next */ case Type.WebAssembly: return ".wasm"; diff --git a/core/module_resolver.test.ts b/core/module_resolver.test.ts index e385b9d..76f6ba5 100644 --- a/core/module_resolver.test.ts +++ b/core/module_resolver.test.ts @@ -79,6 +79,7 @@ test("core / module_resolver: resolve module from local", () => { `file://${path.join(__dirname, "cache.ts")}`, `file://./cache.ts`, `file://../client/src/extension.ts`, + `https://unknown_module.com/unknown_module_without_extension`, ]) ).toEqual([ { @@ -86,11 +87,7 @@ test("core / module_resolver: resolve module from local", () => { origin: "./deno.ts", filepath: path.join(__dirname, "deno.ts"), }, - { - extension: ".json", - origin: "../package.json", - filepath: path.join(__dirname, "..", "package.json"), - }, + undefined, { extension: ".ts", origin: "https://example.com/esm/mod.ts", @@ -146,6 +143,7 @@ test("core / module_resolver: resolve module from local", () => { origin: "file://../client/src/extension.ts", filepath: path.join(__dirname, "..", "client", "src", "extension.ts"), }, + undefined, ] as ResolvedModule[]); }); diff --git a/core/module_resolver.ts b/core/module_resolver.ts index 7129184..ce40ca9 100644 --- a/core/module_resolver.ts +++ b/core/module_resolver.ts @@ -8,7 +8,11 @@ import { ImportMap } from "./import_map"; import { HashMeta } from "./hash_meta"; import { pathExistsSync, isHttpURL, hashURL, normalizeFilepath } from "./util"; import { Logger } from "./logger"; -import { Extension, getExtensionFromFile } from "./extension"; +import { + Extension, + getExtensionFromFile, + isValidDenoModuleExtension, +} from "./extension"; export type ResolvedModule = { origin: string; @@ -121,6 +125,10 @@ export class ModuleResolver implements ModuleResolverInterface { } } + if (!meta.extension) { + return; + } + return { origin: origin, filepath: moduleFilepath, @@ -149,7 +157,10 @@ export class ModuleResolver implements ModuleResolverInterface { normalizeFilepath(moduleName) ); - if (!pathExistsSync(moduleFilepath)) { + if ( + !pathExistsSync(moduleFilepath) || + !isValidDenoModuleExtension(moduleFilepath) + ) { return; }