Skip to content

Commit

Permalink
fix: mark invalid url patterns as user error
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Aug 7, 2023
1 parent c09a517 commit 3201a7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
19 changes: 19 additions & 0 deletions node/manifest.test.ts
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.ts'
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
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.ts'
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

0 comments on commit 3201a7f

Please sign in to comment.