Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netlify Adapter does not return properly formed response for binary data #4174

Closed
bummzack opened this issue Mar 2, 2022 · 3 comments · Fixed by #5048
Closed

Netlify Adapter does not return properly formed response for binary data #4174

bummzack opened this issue Mar 2, 2022 · 3 comments · Fixed by #5048
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. pkg:adapter-netlify
Milestone

Comments

@bummzack
Copy link
Contributor

bummzack commented Mar 2, 2022

Describe the bug

In order to return binary data (eg. a generated Image) from a Netlify function, the data must be converted to a base64 encoded string with the proper content-type.

This is done in handler.js, but the condition that checks for Uint8Array will always fail, as the response type changed with #3384

From what I can tell, there is currently no way to determine the "type" from the response, other than looking at the content-type header and using something like is_text

Reproduction

Here's a minimal setup: https://github.com/bummzack/sveltekit-netlify-binary-response

There's twice the same code running in a SvelteKit Endpoint (image.png) and a separate Netlify function.

The SvelteKit endpoint does not encode the image properly and it won't be displayed:
https://elated-knuth-7439db.netlify.app/image.png

The direct implementation as a separate Netlify function works as expected:
https://elated-knuth-7439db.netlify.app/.netlify/functions/test

Logs

No response

System Info

Local system is irrelevant, since the issue doesn't occur locally.

  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.30 
    @sveltejs/kit: next => 1.0.0-next.288 
    svelte: ^3.44.0 => 3.46.4

Severity

annoyance

Additional Information

No response

@bummzack bummzack changed the title Netlify Adapter does not return binary response Netlify Adapter does not return properly formed response for binary data Mar 2, 2022
@Rich-Harris Rich-Harris added bug Something isn't working pkg:adapter-netlify help wanted PRs welcomed. The implementation details are unlikely to cause debate labels Mar 2, 2022
@bummzack
Copy link
Contributor Author

bummzack commented Mar 2, 2022

Sorry, I wanted to create a PR for this, but I banged my head against the keyboard while trying to understand how I could import is_text from kit/src/runtime/server/endpoint.js into the handler function…

I guess it somehow needs to be exported from kit, but the whole build/setup/pipeline eludes me.

@benmccann benmccann added this to the 1.0 milestone Mar 3, 2022
@goynang
Copy link

goynang commented Mar 5, 2022

I'm having the same problem on Netlify when trying to serve out a dynamically generated PDF. Works fine locally.

I tried to create a PR for the is_text fix that @bummzack mentions but hit the same issues. I tried good old fashion copy/paste but then got lost within the darker arts of npm trying to get it to run my forked version (from the monorepo package) and deploy.

This does look like it should be a relatively simple thing to fix for anyone that knows what they are doing (unlike myself!).

I will run a custom netlify function in the meantime as a workaround.

@benmccann benmccann added the p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. label Mar 17, 2022
Rich-Harris added a commit that referenced this issue May 24, 2022
* base64-encode binary responses - closes #4174

* tweak comments
@bummzack
Copy link
Contributor Author

Thanks @Rich-Harris ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. pkg:adapter-netlify
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants