Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

fix: mark invalid url patterns as user error #450

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions node/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { test, expect, vi } from 'vitest'
import { getRouteMatcher } from '../test/util.js'

import { BundleFormat } from './bundle.js'
import { BundleError } from './bundle_error.js'
import { Cache, FunctionConfig } from './config.js'
import { Declaration } from './declaration.js'
import { generateManifest } from './manifest.js'
Expand Down Expand Up @@ -266,6 +267,24 @@ test('URLPattern named groups are supported', () => {
expect(matcher('/products/jigsaw-doweling-jig')).toBeDefined()
})

test('Invalid Path patterns throw bundling errors', () => {
const functions = [{ name: 'customisation', path: '/path/to/customisation.ts' }]
const declarations: Declaration[] = [{ function: 'customisation', path: '/https://foo.netlify.app/' }]
const userFunctionConfig: Record<string, FunctionConfig> = {}
const internalFunctionConfig: Record<string, FunctionConfig> = {}

expect(() =>
generateManifest({
bundles: [],
declarations,
functions,
userFunctionConfig,
internalFunctionConfig,
featureFlags: { edge_functions_path_urlpattern: true },
}),
).toThrowError(BundleError)
})

test('Includes failure modes in manifest', () => {
const functions = [
{ name: 'func-1', path: '/path/to/func-1.ts' },
Expand Down
23 changes: 14 additions & 9 deletions node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { join } from 'path'
import globToRegExp from 'glob-to-regexp'

import type { Bundle } from './bundle.js'
import { wrapBundleError } from './bundle_error.js'
import { Cache, FunctionConfig, Path } from './config.js'
import { Declaration, parsePattern } from './declaration.js'
import { EdgeFunction } from './edge_function.js'
Expand Down Expand Up @@ -165,18 +166,22 @@ const generateManifest = ({

const pathToRegularExpression = (path: string, featureFlags?: FeatureFlags) => {
if (featureFlags?.edge_functions_path_urlpattern) {
const pattern = new ExtendedURLPattern({ pathname: path })
try {
const pattern = new ExtendedURLPattern({ pathname: path })

// Removing the `^` and `$` delimiters because we'll need to modify what's
// between them.
const source = pattern.regexp.pathname.source.slice(1, -1)
// Removing the `^` and `$` delimiters because we'll need to modify what's
// between them.
const source = pattern.regexp.pathname.source.slice(1, -1)

// Wrapping the expression source with `^` and `$`. Also, adding an optional
// trailing slash, so that a declaration of `path: "/foo"` matches requests
// for both `/foo` and `/foo/`.
const normalizedSource = `^${source}\\/?$`
// Wrapping the expression source with `^` and `$`. Also, adding an optional
// trailing slash, so that a declaration of `path: "/foo"` matches requests
// for both `/foo` and `/foo/`.
const normalizedSource = `^${source}\\/?$`

return normalizedSource
return normalizedSource
} catch (error) {
throw wrapBundleError(error)
}
}

// We use the global flag so that `globToRegExp` will not wrap the expression
Expand Down