From 5f1fce4199a4fff5f8fcde03a5b76fa5201b7ead Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Sun, 12 Nov 2023 13:57:29 +0800 Subject: [PATCH 1/3] fix api methods analysis during build --- .changeset/swift-deers-draw.md | 5 +++++ packages/kit/src/core/postbuild/analyse.js | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 .changeset/swift-deers-draw.md diff --git a/.changeset/swift-deers-draw.md b/.changeset/swift-deers-draw.md new file mode 100644 index 000000000000..9ce5c9ab1264 --- /dev/null +++ b/.changeset/swift-deers-draw.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correctly analyse exported server API methods during build diff --git a/packages/kit/src/core/postbuild/analyse.js b/packages/kit/src/core/postbuild/analyse.js index c311dfe43337..db1e0cb3721d 100644 --- a/packages/kit/src/core/postbuild/analyse.js +++ b/packages/kit/src/core/postbuild/analyse.js @@ -93,10 +93,11 @@ async function analyse({ manifest_path, env }) { prerender = mod.prerender; } - Object.values(mod).forEach((/** @type {import('types').HttpMethod} */ method) => { - if (mod[method] && ENDPOINT_METHODS.has(method)) { - api_methods.push(method); - } else if (mod.fallback) { + Object.keys(mod).forEach((key) => { + // @ts-ignore + if (mod[key] && ENDPOINT_METHODS.has(key)) { + api_methods.push(/** @type {import('types').HttpMethod} */ (key)); + } else if (mod.fallback && key === 'fallback') { api_methods.push('*'); } }); From fc8a03dcf28b3cde898fdf46da679ba6234676a4 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Mon, 13 Nov 2023 19:38:39 +0800 Subject: [PATCH 2/3] fix eslint --- packages/kit/src/core/postbuild/analyse.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/core/postbuild/analyse.js b/packages/kit/src/core/postbuild/analyse.js index db1e0cb3721d..8b11dbfe00cb 100644 --- a/packages/kit/src/core/postbuild/analyse.js +++ b/packages/kit/src/core/postbuild/analyse.js @@ -94,9 +94,9 @@ async function analyse({ manifest_path, env }) { } Object.keys(mod).forEach((key) => { - // @ts-ignore - if (mod[key] && ENDPOINT_METHODS.has(key)) { - api_methods.push(/** @type {import('types').HttpMethod} */ (key)); + const method = /** @type {import('types').HttpMethod} */ (key); + if (mod[method] && ENDPOINT_METHODS.has(method)) { + api_methods.push(method); } else if (mod.fallback && key === 'fallback') { api_methods.push('*'); } From 2145088e2cff6619cb22934155eba95862ba4a15 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 12 Dec 2023 23:15:10 -0500 Subject: [PATCH 3/3] simplify ENDPOINT_METHODS and PAGE_METHODS stuff --- packages/kit/src/constants.js | 12 ++---------- packages/kit/src/core/postbuild/analyse.js | 15 +++++++-------- packages/kit/src/runtime/server/endpoint.js | 2 +- packages/kit/src/runtime/server/utils.js | 2 +- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/packages/kit/src/constants.js b/packages/kit/src/constants.js index 4360335d4820..7790ab1c66a6 100644 --- a/packages/kit/src/constants.js +++ b/packages/kit/src/constants.js @@ -6,14 +6,6 @@ export const SVELTE_KIT_ASSETS = '/_svelte_kit_assets'; export const GENERATED_COMMENT = '// this file is generated — do not edit it\n'; -export const ENDPOINT_METHODS = new Set([ - 'GET', - 'POST', - 'PUT', - 'PATCH', - 'DELETE', - 'OPTIONS', - 'HEAD' -]); +export const ENDPOINT_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']; -export const PAGE_METHODS = new Set(['GET', 'POST', 'HEAD']); +export const PAGE_METHODS = ['GET', 'POST', 'HEAD']; diff --git a/packages/kit/src/core/postbuild/analyse.js b/packages/kit/src/core/postbuild/analyse.js index 8b11dbfe00cb..6cfd975969c7 100644 --- a/packages/kit/src/core/postbuild/analyse.js +++ b/packages/kit/src/core/postbuild/analyse.js @@ -93,14 +93,13 @@ async function analyse({ manifest_path, env }) { prerender = mod.prerender; } - Object.keys(mod).forEach((key) => { - const method = /** @type {import('types').HttpMethod} */ (key); - if (mod[method] && ENDPOINT_METHODS.has(method)) { - api_methods.push(method); - } else if (mod.fallback && key === 'fallback') { - api_methods.push('*'); - } - }); + for (const method of /** @type {import('types').HttpMethod[]} */ (ENDPOINT_METHODS)) { + if (mod[method]) api_methods.push(method); + } + + if (mod.fallback) { + api_methods.push('*'); + } config = mod.config; entries = mod.entries; diff --git a/packages/kit/src/runtime/server/endpoint.js b/packages/kit/src/runtime/server/endpoint.js index d939676f7aad..55bcd87807b9 100644 --- a/packages/kit/src/runtime/server/endpoint.js +++ b/packages/kit/src/runtime/server/endpoint.js @@ -81,7 +81,7 @@ export function is_endpoint_request(event) { const { method, headers } = event.request; // These methods exist exclusively for endpoints - if (ENDPOINT_METHODS.has(method) && !PAGE_METHODS.has(method)) { + if (ENDPOINT_METHODS.includes(method) && !PAGE_METHODS.includes(method)) { return true; } diff --git a/packages/kit/src/runtime/server/utils.js b/packages/kit/src/runtime/server/utils.js index 6e5a4ee1b6ad..bfd6f144d1d4 100644 --- a/packages/kit/src/runtime/server/utils.js +++ b/packages/kit/src/runtime/server/utils.js @@ -35,7 +35,7 @@ export function method_not_allowed(mod, method) { /** @param {Partial>} mod */ export function allowed_methods(mod) { - const allowed = Array.from(ENDPOINT_METHODS).filter((method) => method in mod); + const allowed = ENDPOINT_METHODS.filter((method) => method in mod); if ('GET' in mod || 'HEAD' in mod) allowed.push('HEAD');