From 2145088e2cff6619cb22934155eba95862ba4a15 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 12 Dec 2023 23:15:10 -0500 Subject: [PATCH] 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');