Skip to content

Instantly share code, notes, and snippets.

@LukeChannings
Last active February 19, 2021 13:00
Show Gist options
  • Save LukeChannings/151edca27f4e19f50b10af35edb3bf97 to your computer and use it in GitHub Desktop.
Save LukeChannings/151edca27f4e19f50b10af35edb3bf97 to your computer and use it in GitHub Desktop.
serve_deno_cache.ts
import { serve } from "https://deno.land/std@0.83.0/http/server.ts";
import { walk } from "https://deno.land/std@0.83.0/fs/walk.ts";
const PORT = 8001;
interface DenoInfo {
denoDir: string;
modulesCache: string;
typescriptCache: string;
}
const getDenoInfo = async (): Promise<DenoInfo> => {
const infoProcess = Deno.run({
cmd: ["deno", "info", "--unstable", "--json"],
stdout: "piped",
});
const textDecoder = new TextDecoder();
return JSON.parse(textDecoder.decode(await infoProcess.output()));
};
const { modulesCache } = await getDenoInfo();
interface DenoCacheMetadata {
headers: Record<string, string>;
url: string;
}
const cacheMetadata = new Map<string, DenoCacheMetadata>();
for await (const entry of walk(modulesCache, { exts: [".metadata.json"] })) {
try {
const metadataText = await Deno.readTextFile(entry.path);
const metadata: DenoCacheMetadata = JSON.parse(metadataText);
cacheMetadata.set(entry.path.replace(".metadata.json", ""), metadata);
} catch (err) {
console.log(`${entry.path}: ${err.message}`);
}
}
const server = serve({ port: PORT });
console.log(`Serving Deno cache on port ${PORT}`);
for await (const req of server) {
try {
let foundMetadata: [string, DenoCacheMetadata] | undefined;
for (const [cachePath, metadata] of cacheMetadata) {
if (metadata.url.endsWith(req.url)) {
foundMetadata = [cachePath, metadata];
break;
}
}
if (foundMetadata) {
const [cachePath, cacheMetadata] = foundMetadata;
const cachedFile = await Deno.readTextFile(cachePath);
if (cachedFile) {
console.log(cacheMetadata.url);
req.respond({
status: 200,
body: cachedFile,
headers: new Headers(cacheMetadata.headers),
});
} else {
throw new Error("No cache entry founda");
}
}
} catch (_) {
req.respond({ status: 404 });
}
}
@LukeChannings
Copy link
Author

LukeChannings commented Jan 9, 2021

serve_deno_cache

A simple utility that starts an HTTP server to serve Deno's cache directory.

Running

Install with deno install --allow-run --allow-net --allow-read https://gist.githubusercontent.com/LukeChannings/151edca27f4e19f50b10af35edb3bf97/raw/db0b6aed7ee19bac3cb81a1fcdf75e0b35730c99/serve_deno_cache.ts

Run with serve_deno_cache

Or run once with deno run --allow-run --allow-net --allow-read https://gist.githubusercontent.com/LukeChannings/151edca27f4e19f50b10af35edb3bf97/raw/db0b6aed7ee19bac3cb81a1fcdf75e0b35730c99/serve_deno_cache.ts

Using the server

Let's say you've installed a dependency like https://deno.land/std@0.83.0/http/server.ts in one of your projects, you can load the module in a browser by navigating to http://localhost:8001/deno.land/std@0.83.0/http/server.ts when serve_deno_cache is running.

You can manually cache dependencies (including TypeScript types) using deno cache https://cdn.skypack.dev/react@17.0.1?dts (for example).

You can then use them in your browser with <script type="module" src="http://localhost:8001/cdn.skypack.dev/react@17.0.1?dts"></script>, all offline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment