Skip to content

Commit

Permalink
fix: add cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed Jul 21, 2020
1 parent 09dec63 commit 4e9d477
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 112 deletions.
58 changes: 0 additions & 58 deletions cache.ts

This file was deleted.

4 changes: 3 additions & 1 deletion deps.ts
@@ -1,7 +1,7 @@
export {
resolve,
join,
extname
extname,
} from "https://deno.land/std@0.61.0/path/mod.ts";

export {
Expand All @@ -10,3 +10,5 @@ export {
} from "https://deno.land/std@0.61.0/fs/mod.ts";

export { createHash } from "https://deno.land/std@0.61.0/hash/mod.ts";

export { Cache } from "https://x.nest.land/cache@0.0.2/mod.ts";
61 changes: 9 additions & 52 deletions plug.ts
@@ -1,10 +1,9 @@
import { cache } from "./cache.ts";
import {
Cache,
join,
exists,
createHash,
extname,
ensureDir
ensureDir,
} from "./deps.ts";

export interface CrossOptions {
Expand Down Expand Up @@ -57,47 +56,11 @@ export class PlugImportError extends Error {
}
}

function hash(value: string) {
return createHash("sha256").update(value).toString();
}

async function ensure(...paths: string[]): Promise<void> {
await ensureDir(join(...paths));
}

async function fetchFile(url: URL, path: string) {
const protocol = url.protocol.slice(0, -1);
switch (protocol) {
case "file":
const ospath = join(url.host, url.pathname);
console.log(ospath);
if (!await exists(ospath)) {
throw new PlugImportError(`Plugin located at "${ospath}" does not exist.`);
}
await Deno.copyFile(ospath, path);
break;
case "http":
case "https": {
const download = await fetch(url);

if (!download.ok) {
throw new PlugImportError(`Plugin download from "${url}" failed.`);
}

const source = await download.arrayBuffer();
await Deno.writeFile(path, new Uint8Array(source));
break;
}
default:
throw new PlugImportError(`"${protocol}" protocol is not supported.`);
}
}

export async function prepare(options: Options): Promise<number> {
const name = options.name;
const policy = options.policy ?? CachePolicy.STORE;
const dir = options.cache ?? cache();
const ext = extensions[os];
const directory = options.cache ?? Cache.options.directory;
Cache.configure({ directory });

console.log(directory);

let url;
if ("urls" in options) {
Expand All @@ -111,16 +74,10 @@ export async function prepare(options: Options): Promise<number> {

url = new URL(url);

const digest = hash(url.href);
const path = join(dir, "plug", `${name}_${digest}.${ext}`);

ensure(dir, "plug");

if (policy === CachePolicy.NONE || !await exists(path)) {
await fetchFile(url, path);
}
const plug = Cache.namespace("plug");
const file = await plug.fetch(url);

return Deno.openPlugin(path);
return Deno.openPlugin(file.path);
}

export function getOpId(op: string): number {
Expand Down
5 changes: 4 additions & 1 deletion test/helpers.ts
@@ -1,5 +1,6 @@
import { Plug } from "../mod.ts";
import {
Cache,
assertEquals,
serve,
serveFile,
Expand Down Expand Up @@ -77,6 +78,8 @@ export async function assertScript(
script,
...args,
],
stderr: "inherit",
stdout: "inherit",
});
const status = await process.status();
process.close();
Expand All @@ -96,5 +99,5 @@ export async function assertCache(): Promise<void> {
}

export async function cleanCache() {
await Deno.remove(resolveTest("cache"), { recursive: true });
await Cache.purge("plug");
}
2 changes: 2 additions & 0 deletions test_deps.ts
Expand Up @@ -14,3 +14,5 @@ export {
} from "https://deno.land/std@0.61.0/path/mod.ts";

export { exists } from "https://deno.land/std@0.61.0/fs/mod.ts";

export { Cache } from "https://x.nest.land/cache@0.0.2/mod.ts";

0 comments on commit 4e9d477

Please sign in to comment.