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: don't set the request body on get/head requests #3459

Merged
merged 2 commits into from
Jan 20, 2022
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/cool-bananas-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': patch
---

Avoid setting the body of the request when the request method is GET or HEAD
34 changes: 22 additions & 12 deletions packages/adapter-netlify/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,7 @@ export function init(manifest) {
const app = new App(manifest);

return async (event) => {
const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;

const encoding = isBase64Encoded ? 'base64' : 'utf-8';
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;

const rendered = await app.render(
new Request(rawUrl, {
method: httpMethod,
headers: new Headers(headers),
body: rawBody
})
);
const rendered = await app.render(to_request(event));

const partial_response = {
statusCode: rendered.status,
Expand All @@ -46,6 +35,27 @@ export function init(manifest) {
};
}

/**
* @param {import('@netlify/functions').HandlerEvent} event
* @returns {Request}
*/
function to_request(event) {
const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;

/** @type {RequestInit} */
const init = {
method: httpMethod,
headers: new Headers(headers)
};

if (httpMethod !== 'GET' && httpMethod !== 'HEAD') {
const encoding = isBase64Encoded ? 'base64' : 'utf-8';
init.body = typeof body === 'string' ? Buffer.from(body, encoding) : body;
}

return new Request(rawUrl, init);
}

/**
* Splits headers into two categories: single value and multi value
* @param {Headers} headers
Expand Down