#Is there a platform-independent way to read files?

35 messages · Page 1 of 1 (latest)

scenic compass
#

I'm trying to write a component which reads a file, pulls out some metadata, and then gives a download link for an unmodified version of that file. https://docs.astro.build/en/guides/imports/#node-builtins in the documentation kinda implies that there isn't an Astro alternative to node.js fs -- is that the case? I'd rather not depend on node if I don't need to.

Docs

Learn how to import different content types with Astro.

full plume
#

that would be object storage

#

serverless platforms are pedantic about code vs data

scenic compass
#

How so? It's fundamentally the same as serving an image, right?

full plume
#

yes and images had to be implemented differently for each platform, sometimes by using the platform provided image services

scenic compass
#

ah

#

i think i kinda assumed all the big static hosts were spicy nginx serving static files 😵‍💫

full plume
#

if you're self-hosting using the node adapter, you could do something along the lines of fs.read("../dist/client/xyz.json")

#

for context, your files in /public get put inside dist/client

scenic compass
full plume
#

it would need slightly different code for the dev mode, you could check whether import.meta.env.DEV is true or false for that

full plume
#

cloudflare provides env.assets.fetch() function

scenic compass
#

ah

scenic compass
full plume
#

you know how sometimes platforms would create a container to run your code

#

this container would have access to only your code and run in a specific datacenter

#

meanwhile the client js,css, images and prerendered html gets submitted to object storage or a cdn

scenic compass
#

ohhhh i see

#

to be clear when I'm talking about reading files platform-independently this is still all build-time static site generation

#

for context I do technical writeups and I'm trying to write a custom import which pulls some metadata out of files at build time and then outputs a component with that metadata and a link to the static file

#

I considered writing a rollup plugin and importing normally

#

but that would potentially lead to colliding with other normal-er import implementations

#

now that I think through it a little bit more what I probably want is just "import but like always as a byte array dont do any processing"

full plume
#

it's possible to make something like that

#

it becomes problematic because you would hit the code size limits

#

unless you introduce separate hosting for the files, they would be inlined into code, as base64 encoded for example

#

if you're creating an integration, you could read the metadata at buildtime, then manually copy over the original files into client.outDir (dist/client), and calculate the final path into the metadata for your code

scenic compass
full plume
#

or an astro integration that includes a rollup plugin as one part

scenic compass
#

hmm

#

i think thats workable

scenic compass
# full plume it's possible to make something like that

How would I go about doing an include as byte array? I might write up a "do meta stuff at build time and just link to github cdn where the website source is" as an mvp until i get around to writing an optimization for files small enough to serve out of the host directly

#

ahh i think i see -- astro imports are delegated to vite which allows you to just append ?raw ?