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

Commit

Permalink
feat: remove JSON import statement. Now, you cannot import JSON module.
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed May 5, 2020
1 parent 8ea4cf2 commit 23ff6f3
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 38 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"headers": {
"missing-url": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"url": "https://invalid_meta.com/missing_headers"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"headers": {},
"url": "https://unknown_module.com/unknown_module_without_extension"
}
14 changes: 12 additions & 2 deletions core/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { getExtensionFromFile } from "./extension";
import { getExtensionFromFile, isValidDenoModuleExtension } from "./extension";

test("core / extension: getExtensionFromFile", async () => {
expect(getExtensionFromFile("./foo.ts")).toBe(".ts");
expect(getExtensionFromFile("./foo.tsx")).toBe(".tsx");
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();
});
9 changes: 8 additions & 1 deletion core/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type Extension =
| ".d.ts"
| ".js"
| ".jsx"
| ".json"
| ".wasm"
| "";

Expand All @@ -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;
}
44 changes: 26 additions & 18 deletions core/hash_meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
9 changes: 4 additions & 5 deletions core/hash_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export enum Type {
JavaScriptReact = "javascriptreact",
TypeScript = "typescript",
TypeScriptReact = "typescriptreact",
JSON = "json",
WebAssembly = "WebAssembly",
PlainText = "plaintext",
}
Expand All @@ -35,7 +34,6 @@ const extNameMap: { [key: string]: Type } = {
".js": Type.JavaScript,
".jsx": Type.JavaScriptReact,
".mjs": Type.JavaScript,
".json": Type.JSON,
".wasm": Type.WebAssembly,
};

Expand All @@ -55,7 +53,6 @@ const contentTypeMap: [string[], Type][] = [
],
Type.JavaScript,
],
[["application/json"], Type.JSON],
[["application/wasm"], Type.WebAssembly],
];

Expand All @@ -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(
Expand Down Expand Up @@ -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";
Expand Down
8 changes: 3 additions & 5 deletions core/module_resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,15 @@ 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([
{
extension: ".ts",
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",
Expand Down Expand Up @@ -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[]);
});

Expand Down
15 changes: 13 additions & 2 deletions core/module_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,6 +125,10 @@ export class ModuleResolver implements ModuleResolverInterface {
}
}

if (!meta.extension) {
return;
}

return {
origin: origin,
filepath: moduleFilepath,
Expand Down Expand Up @@ -149,7 +157,10 @@ export class ModuleResolver implements ModuleResolverInterface {
normalizeFilepath(moduleName)
);

if (!pathExistsSync(moduleFilepath)) {
if (
!pathExistsSync(moduleFilepath) ||
!isValidDenoModuleExtension(moduleFilepath)
) {
return;
}

Expand Down

0 comments on commit 23ff6f3

Please sign in to comment.