nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Server assets using `getItemRaw` is inconsistent

Open okydk opened this issue 2 years ago • 6 comments

Environment

Node 20+ Nitro 2.7.2

Reproduction

StackBlitz npm run dev returns Buffer npm run build && npm run preview returns string

Describe the bug

Getting the raw file using getItemRaw return Buffer vs string in preview. Testet with node-server and firebase providers.

Additional context

I'm trying to get a static server assets PDF generation. Example included as /pdf in the StackBlitz.

I know getItemRaw is still marked experimental, but I don't see any other API to access simple PNG data?

Logs

No response

okydk avatar Nov 19 '23 12:11 okydk

Might be related for firebase - see e.g. https://github.com/unjs/nitro/issues/1903

TheAlexLichter avatar Nov 19 '23 12:11 TheAlexLichter

I assume this issue is storage related and also seem to be the case for node-server. The linked issue #1903 (im the author) was regarding receiving the raw body from requests.

okydk avatar Nov 19 '23 13:11 okydk

https://github.com/unjs/nitro/discussions/2256

kleinpetr avatar Mar 15 '24 11:03 kleinpetr

Hi,

in my case I was trying to use pdfmake in the vercel

I've been facing 2 issues

  1. when I use getItemRaw the output is inconsistent as it's described above (locally works fine for images)
  2. pdfmake doesn't accept neither buffer nor base64 for the fonts, it has to be path to regular file

in order to use file asset as a regular file I had to do a small hack to include assets into the final bundle.

in the package.json I had to update the build script like this

"build": "nitropack build && cp ./assets ./.vercel/output/functions/__nitro.func -r",

then I can use regular path, for example using path

  const fontDescriptors = {
    Roboto: {
      normal: path.join('assets', 'fonts/Roboto-Regular.ttf'),
      bold: path.join('assets', 'fonts/Roboto-Medium.ttf'),
      italics: path.join('assets', 'fonts/Roboto-Italic.ttf'),
      bolditalics: path.join('assets', 'fonts/Roboto-MediumItalic.ttf'),
    },
  }

@pi0 is there is any better option how to append a custom folder to final build ideally through the nitro config?

Thanks :)

kleinpetr avatar Mar 18 '24 19:03 kleinpetr

Hi, I have the same problem with getItem

This api... image ...returns a string in dev mode and raw in preview. This is not provider related.

I think it might be related with: #2481

Zebnastien avatar Aug 15 '24 15:08 Zebnastien

Since getItemRaw is now marked as experimental, I've reverted to using getItem and created a new buffer from there. This seems to be consistant both in dev, preview and node-server.

const rawBuffer = await storage.getItem('myCert.pem').then((item) => Buffer.from(item as any))

Feels a bit hacky and quite redundant, but I guess it will have to do for now. Maybe a getItemBuffer is needed?

okydk avatar Sep 19 '24 18:09 okydk

Proposed storage.get("key", { type: "bytes" ) (https://github.com/unjs/unstorage/issues/528) will ensure return-type is consistent.

Current raw implementation between drivers currently varies and has different return values.

pi0 avatar Jan 07 '25 19:01 pi0

Amazing! Thanks for the heads up Pooya ♥️

okydk avatar Jan 07 '25 21:01 okydk