Candeia
Candeia11mo ago

Read source code content

Is there a way to read the current source code of the a given a file URL (using import.meta.url) without re-downloading it? I know that inside $DENO_DIR or cache directories there are the content of the already downloaded files, but I suppose they are content-addressable somehow (sha1, maybe?). I have a code that generates a schema based on types definitions but currently I need to download them all again because I can't leverage the deno cache.
4 Replies
Candeia
CandeiaOP11mo ago
wondering if is a good idea to try: https://deno.land/x/deno_cache@0.6.3/mod.ts
Deno AI Helper
Deno AI Helper11mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on your message, I can provide some general advice. In Deno, import.meta.url gives you the URL of the current module. To read the current source code of the module without re-downloading it, you could theoretically use Deno's built-in functions to read from the local file system, assuming you have the necessary permissions. Here's a general approach you might consider:
const currentModuleUrl = new URL(import.meta.url);
if (currentModuleUrl.protocol === "file:") {
const currentModulePath = currentModuleUrl.pathname;
const sourceCode = await Deno.readTextFile(currentModulePath);
console.log(sourceCode);
}
const currentModuleUrl = new URL(import.meta.url);
if (currentModuleUrl.protocol === "file:") {
const currentModulePath = currentModuleUrl.pathname;
const sourceCode = await Deno.readTextFile(currentModulePath);
console.log(sourceCode);
}
This code snippet checks if the module URL's protocol is file:, which means it's a local file. It then reads the file content directly from the file system without re-downloading it. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this advice. If the file URL is not a local file but a remote resource, you would indeed need to access the cached version, which might not be straightforward as it could be content-addressed in Deno's cache. For that scenario, there isn't a documented API in Deno to directly access cached files by their original URLs. You might need to look into the structure of the $DENO_DIR to understand how files are cached and addressed.
Leokuma
Leokuma11mo ago
Yes, I believe you can use that to get the source file from Deno's cache if available
Candeia
CandeiaOP11mo ago
how about when running in deno deploy?

Did you find this page helpful?