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
wondering if is a good idea to try: https://deno.land/x/deno_cache@0.6.3/mod.ts
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:
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.Yes, I believe you can use that to get the source file from Deno's cache if available
how about when running in deno deploy?