From f40fede0dde3ab6944f9349d085822fe5d998479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20J=C3=A4ger?= Date: Thu, 12 Sep 2024 23:13:31 +0200 Subject: [PATCH] Remove dependency on path-to-regexp --- .changeset/quiet-lemons-collect.md | 5 ++ packages/astro/package.json | 1 - .../src/core/routing/manifest/generator.ts | 68 ++++++++----------- pnpm-lock.yaml | 8 --- 4 files changed, 35 insertions(+), 47 deletions(-) create mode 100644 .changeset/quiet-lemons-collect.md diff --git a/.changeset/quiet-lemons-collect.md b/.changeset/quiet-lemons-collect.md new file mode 100644 index 000000000000..90e869eab24d --- /dev/null +++ b/.changeset/quiet-lemons-collect.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Remove dependency on path-to-regexp diff --git a/packages/astro/package.json b/packages/astro/package.json index 11ad27c58c34..496ede9e4fcb 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -168,7 +168,6 @@ "ora": "^8.1.0", "p-limit": "^6.1.0", "p-queue": "^8.0.1", - "path-to-regexp": "^6.2.2", "preferred-pm": "^4.0.0", "prompts": "^2.4.2", "rehype": "^13.0.1", diff --git a/packages/astro/src/core/routing/manifest/generator.ts b/packages/astro/src/core/routing/manifest/generator.ts index 4ab635ec6608..4271bb8b34b9 100644 --- a/packages/astro/src/core/routing/manifest/generator.ts +++ b/packages/astro/src/core/routing/manifest/generator.ts @@ -1,7 +1,5 @@ import type { AstroConfig, RoutePart } from '../../../@types/astro.js'; -import { compile } from 'path-to-regexp'; - /** * Sanitizes the parameters object by normalizing string values and replacing certain characters with their URL-encoded equivalents. * @param {Record} params - The parameters object to be sanitized. @@ -24,45 +22,39 @@ export function getRouteGenerator( segments: RoutePart[][], addTrailingSlash: AstroConfig['trailingSlash'], ) { - const template = segments - .map((segment) => { - return ( - '/' + - segment - .map((part) => { - if (part.spread) { - return `:${part.content.slice(3)}(.*)?`; - } else if (part.dynamic) { - return `:${part.content}`; - } else { - return part.content - .normalize() - .replace(/\?/g, '%3F') - .replace(/#/g, '%23') - .replace(/%5B/g, '[') - .replace(/%5D/g, ']') - .replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - } - }) - .join('') - ); - }) - .join(''); - - // Unless trailingSlash config is set to 'always', don't automatically append it. - let trailing: '/' | '' = ''; - if (addTrailingSlash === 'always' && segments.length) { - trailing = '/'; - } - const toPath = compile(template + trailing); return (params: Record): string => { const sanitizedParams = sanitizeParams(params); - const path = toPath(sanitizedParams); - // When generating an index from a rest parameter route, `path-to-regexp` will return an - // empty string instead "/". This causes an inconsistency with static indexes that may result - // in the incorrect routes being rendered. - // To fix this, we return "/" when the path is empty. + // Unless trailingSlash config is set to 'always', don't automatically append it. + let trailing: '/' | '' = ''; + if (addTrailingSlash === 'always' && segments.length) { + trailing = '/'; + } + + const path = segments + .map((segment) => { + return ( + '/' + + segment + .map((part) => { + if (part.spread) { + return `${sanitizedParams[part.content.slice(3)] || ''}`; + } else if (part.dynamic) { + return `${sanitizedParams[part.content] || ''}`; + } else { + return part.content + .normalize() + .replace(/\?/g, '%3F') + .replace(/#/g, '%23') + .replace(/%5B/g, '[') + .replace(/%5D/g, ']'); + } + }) + .join('') + ); + }) + .join('') + trailing; + return path || '/'; }; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c658edeca2f2..151fce0a88e5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -693,9 +693,6 @@ importers: p-queue: specifier: ^8.0.1 version: 8.0.1 - path-to-regexp: - specifier: ^6.2.2 - version: 6.2.2 preferred-pm: specifier: ^4.0.0 version: 4.0.0 @@ -9546,9 +9543,6 @@ packages: resolution: {integrity: sha512-7xTavNy5RQXnsjANvVvMkEjvloOinkAjv/Z6Ildz9v2RinZ4SBKTWFOVRbaF8p0vpHnyjV/UwNDdKuUv6M5qcA==} engines: {node: '>=16 || 14 >=14.17'} - path-to-regexp@6.2.2: - resolution: {integrity: sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==} - path-type@4.0.0: resolution: {integrity: sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==} engines: {node: '>=8'} @@ -15582,8 +15576,6 @@ snapshots: lru-cache: 10.2.0 minipass: 7.1.2 - path-to-regexp@6.2.2: {} - path-type@4.0.0: {} path-type@5.0.0: {}