Skip to content

Commit

Permalink
[breaking] request creation cleanup (#6681)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Sep 9, 2022
1 parent fc5b7e6 commit f1e762b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/red-cherries-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
'@sveltejs/kit': patch
---

[breaking] request creation cleanup
2 changes: 1 addition & 1 deletion packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-vercel/files/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 13 additions & 9 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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<string, string>} */ (req.headers);
if (req.httpVersionMajor === 2) {
export async function getRequest({ request, base }) {
let headers = /** @type {Record<string, string>} */ (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
Expand All @@ -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)
});
}

Expand Down
7 changes: 5 additions & 2 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Request>;
export function getRequest(opts: {
base: string;
request: import('http').IncomingMessage;
}): Promise<Request>;
export function setResponse(res: import('http').ServerResponse, response: Response): void;
}

Expand Down

0 comments on commit f1e762b

Please sign in to comment.