From c54ff933c9b452a1c156e4da737d224fce3c05b9 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 8 Sep 2022 20:49:17 -0700 Subject: [PATCH] [breaking] request creation cleanup --- .changeset/red-cherries-fly.md | 7 ++++++ packages/adapter-node/src/handler.js | 2 +- packages/adapter-vercel/files/serverless.js | 2 +- packages/kit/src/exports/node/index.js | 22 +++++++++++-------- packages/kit/src/exports/vite/dev/index.js | 7 ++++-- .../kit/src/exports/vite/preview/index.js | 7 ++++-- packages/kit/types/ambient.d.ts | 8 +++---- 7 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 .changeset/red-cherries-fly.md diff --git a/.changeset/red-cherries-fly.md b/.changeset/red-cherries-fly.md new file mode 100644 index 000000000000..09d9775075f7 --- /dev/null +++ b/.changeset/red-cherries-fly.md @@ -0,0 +1,7 @@ +--- +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-vercel': patch +'@sveltejs/kit': patch +--- + +[breaking] request creation cleanup diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index b0951fccdc30..4a29502e71f0 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -49,7 +49,7 @@ const ssr = async (req, res) => { let request; try { - request = await getRequest(origin || get_origin(req.headers), req); + request = await getRequest({ base: origin || get_origin(req.headers), request: req }); } catch (err) { res.statusCode = err.status || 400; res.end(err.reason || 'Invalid request body'); diff --git a/packages/adapter-vercel/files/serverless.js b/packages/adapter-vercel/files/serverless.js index c647fd8b1f9b..36fbae4c7799 100644 --- a/packages/adapter-vercel/files/serverless.js +++ b/packages/adapter-vercel/files/serverless.js @@ -20,7 +20,7 @@ export default async (req, res) => { let request; try { - request = await getRequest(`https://${req.headers.host}`, req); + request = await getRequest({ base: `https://${req.headers.host}`, request: req }); } catch (err) { res.statusCode = err.status || 400; return res.end(err.reason || 'Invalid request body'); diff --git a/packages/kit/src/exports/node/index.js b/packages/kit/src/exports/node/index.js index 46026ce077bf..d2fc32c2ce07 100644 --- a/packages/kit/src/exports/node/index.js +++ b/packages/kit/src/exports/node/index.js @@ -1,6 +1,8 @@ import * as set_cookie_parser from 'set-cookie-parser'; -/** @param {import('http').IncomingMessage} req */ +/** + * @param {import('http').IncomingMessage} req + */ function get_raw_body(req) { const h = req.headers; @@ -11,8 +13,10 @@ function get_raw_body(req) { const length = Number(h['content-length']); // check if no request body - // https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95 - if (isNaN(length) && h['transfer-encoding'] == null) { + if ( + (req.httpVersionMajor === 1 && isNaN(length) && h['transfer-encoding'] == null) || + length === 0 + ) { return null; } @@ -65,9 +69,9 @@ function get_raw_body(req) { } /** @type {import('@sveltejs/kit/node').getRequest} */ -export async function getRequest(base, req) { - let headers = /** @type {Record} */ (req.headers); - if (req.httpVersionMajor === 2) { +export async function getRequest({ request, base }) { + let headers = /** @type {Record} */ (request.headers); + if (request.httpVersionMajor === 2) { // we need to strip out the HTTP/2 pseudo-headers because node-fetch's // Request implementation doesn't like them // TODO is this still true with Node 18 @@ -78,10 +82,10 @@ export async function getRequest(base, req) { delete headers[':scheme']; } - return new Request(base + req.url, { - method: req.method, + return new Request(base + request.url, { + method: request.method, headers, - body: get_raw_body(req) + body: get_raw_body(request) }); } diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index b8ecb67e0202..8bfd4eaf3e4f 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -391,10 +391,13 @@ export async function dev(vite, vite_config, svelte_config) { let request; try { - request = await getRequest(base, req); + request = await getRequest({ + base, + request: req + }); } catch (/** @type {any} */ err) { res.statusCode = err.status || 400; - return res.end(err.reason || 'Invalid request body'); + return res.end(err.message || 'Invalid request body'); } const template = load_template(cwd, svelte_config); diff --git a/packages/kit/src/exports/vite/preview/index.js b/packages/kit/src/exports/vite/preview/index.js index 9f1a80298e77..55a59105b65f 100644 --- a/packages/kit/src/exports/vite/preview/index.js +++ b/packages/kit/src/exports/vite/preview/index.js @@ -131,10 +131,13 @@ export async function preview(vite, vite_config, svelte_config) { let request; try { - request = await getRequest(`${protocol}://${host}`, req); + request = await getRequest({ + base: `${protocol}://${host}`, + request: req + }); } catch (/** @type {any} */ err) { res.statusCode = err.status || 400; - return res.end(err.reason || 'Invalid request body'); + return res.end(err.message || 'Invalid request body'); } setResponse( diff --git a/packages/kit/types/ambient.d.ts b/packages/kit/types/ambient.d.ts index dd1c45222429..ea601187a62d 100644 --- a/packages/kit/types/ambient.d.ts +++ b/packages/kit/types/ambient.d.ts @@ -408,10 +408,10 @@ declare module '@sveltejs/kit/node/polyfills' { * Utilities used by adapters for Node-like environments. */ declare module '@sveltejs/kit/node' { - export function getRequest( - base: string, - request: import('http').IncomingMessage - ): Promise; + export function getRequest(opts: { + base: string; + request: import('http').IncomingMessage; + }): Promise; export function setResponse(res: import('http').ServerResponse, response: Response): void; }