Skip to content

Commit

Permalink
refactor: follow-ups to 5998 (#6044)
Browse files Browse the repository at this point in the history
* refactor: reuse regex in isFunction

* refactor: always rely on getFunctionForURLPath
  • Loading branch information
Skn0tt authored Oct 10, 2023
1 parent e7d258b commit 25a10d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/lib/functions/registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { getPathInProject } from '../settings.mjs'
import NetlifyFunction from './netlify-function.mjs'
import runtimes from './runtimes/index.mjs'

const DEFAULT_URL_EXPRESSION = /^\/.netlify\/(functions|builders)\/([^/]+).*/
export const DEFAULT_FUNCTION_URL_EXPRESSION = /^\/.netlify\/(functions|builders)\/([^/]+).*/
const TYPES_PACKAGE = '@netlify/functions'
const ZIP_EXTENSION = '.zip'

Expand Down Expand Up @@ -238,7 +238,7 @@ export class FunctionsRegistry {
// the incoming URL. It doesn't really matter that we don't have the actual
// local URL with the correct port.
const urlPath = new URL(url, 'http://localhost').pathname
const defaultURLMatch = urlPath.match(DEFAULT_URL_EXPRESSION)
const defaultURLMatch = urlPath.match(DEFAULT_FUNCTION_URL_EXPRESSION)

if (defaultURLMatch) {
const func = this.get(defaultURLMatch[2])
Expand Down
17 changes: 9 additions & 8 deletions src/utils/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
isEdgeFunctionsRequest,
} from '../lib/edge-functions/proxy.mjs'
import { fileExistsAsync, isFileAsync } from '../lib/fs.mjs'
import { DEFAULT_FUNCTION_URL_EXPRESSION } from '../lib/functions/registry.mjs'
import renderErrorTemplate from '../lib/render-error-template.mjs'

import { NETLIFYDEVLOG, NETLIFYDEVWARN, log, chalk } from './command-helpers.mjs'
Expand Down Expand Up @@ -87,7 +88,7 @@ function isInternal(url) {
* @param {string} url
*/
function isFunction(functionsPort, url) {
return functionsPort && url.match(/^\/.netlify\/(functions|builders)\/.+/)
return functionsPort && url.match(DEFAULT_FUNCTION_URL_EXPRESSION)
}

/**
Expand Down Expand Up @@ -328,24 +329,24 @@ const serveRedirect = async function ({ env, functionsRegistry, match, options,
return proxy.web(req, res, { target: options.functionsServer })
}

const functionWithCustomRoute =
functionsRegistry && (await functionsRegistry.getFunctionForURLPath(destURL, req.method))
const matchingFunction = functionsRegistry && (await functionsRegistry.getFunctionForURLPath(destURL, req.method))
const destStaticFile = await getStatic(dest.pathname, options.publicFolder)
let statusValue
if (
match.force ||
(!staticFile && ((!options.framework && destStaticFile) || isInternal(destURL) || functionWithCustomRoute))
(!staticFile && ((!options.framework && destStaticFile) || isInternal(destURL) || matchingFunction))
) {
req.url = destStaticFile ? destStaticFile + dest.search : destURL
const { status } = match
statusValue = status
console.log(`${NETLIFYDEVLOG} Rewrote URL to`, req.url)
}

if (isFunction(options.functionsPort, req.url) || functionWithCustomRoute) {
const functionHeaders = functionWithCustomRoute
? { [NFFunctionName]: functionWithCustomRoute.func.name, [NFFunctionRoute]: functionWithCustomRoute.route }
: {}
if (matchingFunction) {
const functionHeaders = {
[NFFunctionName]: matchingFunction.func.name,
[NFFunctionRoute]: matchingFunction.route,
}
const url = reqToURL(req, originalURL)
req.headers['x-netlify-original-pathname'] = url.pathname
req.headers['x-netlify-original-search'] = url.search
Expand Down

2 comments on commit 25a10d2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,388
  • Package size: 398 MB

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,388
  • Package size: 398 MB

Please sign in to comment.