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

[fix] adapter-netlify: encode UInt8Array response bodies as base64 #2630

Merged
merged 2 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-toes-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': patch
---

[fix] encode UInt8Array response bodies as base64
28 changes: 21 additions & 7 deletions packages/adapter-netlify/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,32 @@ export async function handler(event) {
rawBody
});

if (rendered) {
if (!rendered) {
return {
isBase64Encoded: false,
statusCode: rendered.status,
...splitHeaders(rendered.headers),
statusCode: 404,
body: 'Not found'
};
}

const partial_response = {
statusCode: rendered.status,
...split_headers(rendered.headers)
};

if (typeof rendered.body === 'string') {
return {
...partial_response,
body: rendered.body
};
}

// Function responses should always be strings, and responses with binary
// content should be base64 encoded and set isBase64Encoded to true.
// https://github.com/netlify/functions/blob/main/src/function/response.d.ts
return {
statusCode: 404,
body: 'Not found'
...partial_response,
isBase64Encoded: true,
body: Buffer.from(rendered.body).toString('base64')
Xenonym marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand All @@ -42,7 +56,7 @@ export async function handler(event) {
* multiValueHeaders: Record<string, string[]>
* }}
*/
function splitHeaders(headers) {
function split_headers(headers) {
const h = {};
const m = {};
for (const key in headers) {
Expand Down