Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

feat: support multiple paths in V2 functions #1586

Merged
merged 5 commits into from
Oct 5, 2023
Merged

Conversation

eduardoboucas
Copy link
Member

Summary

Allow an array of strings in the path in-source configuration property of V2 functions.

The types already allow this, as well as the rest of the infrastructure.

@eduardoboucas eduardoboucas requested a review from a team as a code owner October 4, 2023 20:42
@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2023

⏱ Benchmark results

Comparing with d98dd20

largeDepsEsbuild: 2.8s

⬆️ 7.85% increase vs. d98dd20

^   3.1s                                             3s                                                   
│   ┌──┐                                            ┌──┐                                                  
│   |  |                            2.7s            |  |            2.8s    2.8s    2.8s            2.8s  
│ ──┼──┼────2.6s────────────────────┌──┐────2.6s────┼──┼────2.6s────┌──┐────┌──┐────┌──┐────2.6s────┌──┐──
│   |  |    ┌──┐    2.5s    2.5s    |  |    ┌──┐    |  |    ┌──┐    |  |    |  |    |  |    ┌──┐    |▒▒|  
│   |  |    |  |    ┌──┐    ┌──┐    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

largeDepsNft: 9.3s

⬆️ 18.07% increase vs. d98dd20

^   9.7s                                                                                                  
│   ┌──┐                                            9.4s                                            9.3s  
│   |  |                                            ┌──┐    8.7s                    8.7s            ┌──┐  
│   |  |                            8.2s    8.1s    |  |    ┌──┐    8.2s    8.3s    ┌──┐            |▒▒|  
│ ──┼──┼────7.9s────────────7.6s────┌──┐────┌──┐────┼──┼────┼──┼────┌──┐────┌──┐────┼──┼────7.6s────|▒▒|──
│   |  |    ┌──┐    7.5s    ┌──┐    |  |    |  |    |  |    |  |    |  |    |  |    |  |    ┌──┐    |▒▒|  
│   |  |    |  |    ┌──┐    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

largeDepsZisi: 16s

⬆️ 7.41% increase vs. d98dd20

^  18.3s                                            18s                                                   
│   ┌──┐                                            ┌──┐                                                  
│   |  |                            16s             |  |                   16.1s   16.4s                  
│   |  |   15.2s                    ┌──┐   15.3s    |  |   15.8s   15.8s    ┌──┐    ┌──┐            16s   
│ ──┼──┼────┌──┐───14.4s───14.9s────┼──┼────┌──┐────┼──┼────┌──┐────┌──┐────┼──┼────┼──┼───14.8s────┌──┐──
│   |  |    |  |    ┌──┐    ┌──┐    |  |    |  |    |  |    |  |    |  |    |  |    |  |    ┌──┐    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

lukasholzer
lukasholzer previously approved these changes Oct 5, 2023
Copy link
Contributor

@lukasholzer lukasholzer left a comment

Choose a reason for hiding this comment

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

Some small comments and request for tests but nothing blocking ;)

@@ -262,7 +262,7 @@ describe('V2 API', () => {
})

test('With an invalid pattern', () => {
expect.assertions(4)
expect.assertions(8)
Copy link
Contributor

Choose a reason for hiding this comment

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

oof 🫣

Copy link
Member Author

Choose a reason for hiding this comment

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

I've split this into two tests now.

}

export const config = {
path: ["/store/:category/products/:product-id", "/super-awesome-campaign"]
Copy link
Contributor

Choose a reason for hiding this comment

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

can we make the second one a named group as well?

  • Can we add a test for what if both routes are the same?
  • What if I add falsy values like null or undefined to the array? Do we throw type errors if numbers or whatever is provided? (You have a non nullable function but no test cases for it)

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added more tests to capture this.

const paths = Array.isArray(input) ? input : [input]
const routes = paths.map((path) => getRoute(path, functionName, methods ?? [])).filter(nonNullable)

return routes
Copy link
Contributor

Choose a reason for hiding this comment

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

I would do return [...new Set(routes)] to remove duplicates

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 9e2cac9.

Copy link
Member Author

@eduardoboucas eduardoboucas Oct 5, 2023

Choose a reason for hiding this comment

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

We can't do it on routes because it's an array of objects. I've done it on paths.

} catch {
throw new FunctionBundlingUserError(`'${path}' is not a valid path according to the URLPattern specification`, {
functionName,
runtime: RUNTIME.JAVASCRIPT,
})
}
}

export const getRoutes = (input: unknown, functionName: string, methods: string[]): Route[] => {
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: would love a small jsdoc that explains the parameters and the method. So that a tipsy on call engineer at 2am in the morning can understand it as well :D

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

return []
}

const getRoute = (path: unknown, functionName: string, methods: string[]): Route | undefined => {
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: small jsdoc would be nice

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@eduardoboucas
Copy link
Member Author

@lukasholzer I think I addressed all your feedback. I ended up having to make some changes in the AST parsing, because when processing arrays we were only looking for strings, so any other primitive values were ignored (so we couldn't throw a type error if people tried to use them).

This has now been addressed.

Copy link
Contributor

@lukasholzer lukasholzer left a comment

Choose a reason for hiding this comment

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

Thanks for the added test cases! 🚀

@eduardoboucas eduardoboucas merged commit 6be379e into main Oct 5, 2023
@eduardoboucas eduardoboucas deleted the feat/multiple-path branch October 5, 2023 14:53
Skn0tt pushed a commit to netlify/build that referenced this pull request May 21, 2024
…-it#1586)

* feat: support multiple paths in V2 functions

* refactor: remove unnecessary check

* refactor: support more primitive types

* fix: de-duplicate paths

* feat: look for `null`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants