Skip to content

Commit

Permalink
[fix] Adapter netlify: don't set the request body on get/head requests (
Browse files Browse the repository at this point in the history
#3459)

* don't set the request body on get/head requests

* add changeset
  • Loading branch information
JeanJPNM committed Jan 20, 2022
1 parent 799f1bc commit 4d58e10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
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

0 comments on commit 4d58e10

Please sign in to comment.