wrangler 2.0.24 → 2.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/miniflare-dist/index.mjs +142 -16
  2. package/package.json +3 -3
  3. package/src/__tests__/configuration.test.ts +7 -3
  4. package/src/__tests__/dev.test.tsx +26 -4
  5. package/src/__tests__/generate.test.ts +2 -4
  6. package/src/__tests__/helpers/mock-cfetch.ts +35 -2
  7. package/src/__tests__/init.test.ts +537 -359
  8. package/src/__tests__/jest.setup.ts +7 -0
  9. package/src/__tests__/metrics.test.ts +1 -1
  10. package/src/__tests__/pages.test.ts +14 -0
  11. package/src/__tests__/r2.test.ts +22 -3
  12. package/src/__tests__/tail.test.ts +112 -42
  13. package/src/__tests__/user.test.ts +11 -0
  14. package/src/api/dev.ts +7 -0
  15. package/src/bundle.ts +3 -2
  16. package/src/cfetch/internal.ts +56 -0
  17. package/src/config/config.ts +1 -1
  18. package/src/config/validation-helpers.ts +19 -6
  19. package/src/config/validation.ts +9 -3
  20. package/src/config-cache.ts +2 -1
  21. package/src/dev/dev.tsx +16 -2
  22. package/src/dev/local.tsx +69 -5
  23. package/src/dev/use-esbuild.ts +3 -0
  24. package/src/dev-registry.tsx +3 -0
  25. package/src/dev.tsx +28 -19
  26. package/src/generate.ts +1 -1
  27. package/src/index.tsx +51 -21
  28. package/src/init.ts +111 -38
  29. package/src/inspect.ts +1 -4
  30. package/src/{metrics/is-ci.ts → is-ci.ts} +0 -0
  31. package/src/metrics/metrics-config.ts +1 -1
  32. package/src/miniflare-cli/assets.ts +27 -16
  33. package/src/miniflare-cli/index.ts +124 -2
  34. package/src/pages/build.tsx +75 -41
  35. package/src/pages/constants.ts +4 -0
  36. package/src/pages/deployments.tsx +9 -9
  37. package/src/pages/dev.tsx +178 -64
  38. package/src/pages/errors.ts +22 -0
  39. package/src/pages/functions/buildPlugin.ts +4 -0
  40. package/src/pages/functions/buildWorker.ts +4 -0
  41. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  42. package/src/pages/functions/routes-consolidation.ts +73 -0
  43. package/src/pages/functions/routes-transformation.test.ts +271 -0
  44. package/src/pages/functions/routes-transformation.ts +122 -0
  45. package/src/pages/functions.tsx +96 -0
  46. package/src/pages/index.tsx +65 -55
  47. package/src/pages/projects.tsx +9 -3
  48. package/src/pages/publish.tsx +75 -22
  49. package/src/pages/types.ts +9 -0
  50. package/src/pages/upload.tsx +6 -8
  51. package/src/proxy.ts +10 -0
  52. package/src/r2.ts +17 -4
  53. package/src/tail/filters.ts +3 -1
  54. package/src/tail/index.ts +15 -2
  55. package/src/tail/printing.ts +43 -3
  56. package/src/user/user.tsx +6 -4
  57. package/src/whoami.tsx +5 -5
  58. package/templates/pages-template-plugin.ts +16 -4
  59. package/templates/pages-template-worker.ts +16 -5
  60. package/templates/service-bindings-module-facade.js +10 -7
  61. package/templates/service-bindings-sw-facade.js +10 -7
  62. package/wrangler-dist/cli.d.ts +7 -0
  63. package/wrangler-dist/cli.js +1681 -1091
@@ -0,0 +1,250 @@
1
+ import { consolidateRoutes, shortenRoute } from "./routes-consolidation";
2
+
3
+ describe("route-consolidation", () => {
4
+ const maxRuleLength = 100; // from constants.MAX_FUNCTIONS_ROUTES_RULE_LENGTH
5
+ describe("consolidateRoutes()", () => {
6
+ it("should consolidate redundant routes", () => {
7
+ expect(consolidateRoutes(["/api/foo", "/api/*"])).toEqual(["/api/*"]);
8
+ expect(
9
+ consolidateRoutes([
10
+ "/api/foo",
11
+ "/api/foo/*",
12
+ "/api/bar/*",
13
+ "/api/*",
14
+ "/foo",
15
+ "/foo/bar",
16
+ "/bar/*",
17
+ "/bar/baz/*",
18
+ "/bar/baz/hello",
19
+ ])
20
+ ).toEqual(["/api/*", "/foo", "/foo/bar", "/bar/*"]);
21
+ });
22
+ it("should consolidate thousands of redundant routes", () => {
23
+ // Test to make sure the consolidator isn't horribly slow
24
+ const routes: string[] = [];
25
+ const limit = 1000;
26
+ for (let i = 0; i < limit; i++) {
27
+ // Add 3 routes per id
28
+ const id = `some-id-${i}`;
29
+ routes.push(`/${id}/*`, `/${id}/foo`, `/${id}/bar/*`);
30
+ }
31
+ const consolidated = consolidateRoutes(routes);
32
+ expect(consolidated.length).toEqual(limit);
33
+ // Should be all unique
34
+ expect(Array.from(new Set(consolidated)).length).toEqual(limit);
35
+ // Should all have pattern `/$id/*`
36
+ expect(
37
+ consolidated.every((route) => route.match(/\/[a-z0-9-]+\/\*/) !== null)
38
+ ).toEqual(true);
39
+ });
40
+
41
+ it("should consolidate many redundant sub-routes", () => {
42
+ const routes: string[] = [];
43
+ const limit = 15;
44
+
45
+ // Create $limit of top-level catch-all routes, with a lot of sub-routes
46
+ for (let i = 0; i < limit; i++) {
47
+ routes.push(`/foo-${i}/*`);
48
+ for (let j = 0; j < limit; j++) {
49
+ routes.push(`/foo-${i}/bar-${j}/hello`);
50
+ for (let k = 0; k < limit; k++) {
51
+ routes.push(`/foo-${i}/bar-${j}/baz-${k}/*`);
52
+ routes.push(`/foo-${i}/bar-${j}/baz-${k}/profile`);
53
+ }
54
+ }
55
+ }
56
+
57
+ const consolidated = consolidateRoutes(routes);
58
+ expect(consolidated.length).toEqual(limit);
59
+ // Should be all unique
60
+ expect(Array.from(new Set(consolidated)).length).toEqual(limit);
61
+ // Should all have pattern `/$id/*`
62
+ expect(
63
+ consolidated.every((route) => route.match(/\/[a-z0-9-]+\/\*/) !== null)
64
+ ).toEqual(true);
65
+ });
66
+
67
+ it("should truncate long single-level path into catch-all path, removing other paths", () => {
68
+ expect(
69
+ consolidateRoutes([
70
+ // [/aaaaaaa, /foo] -> [/*]
71
+ "/" + "a".repeat(maxRuleLength * 2),
72
+ "/foo",
73
+ "/bar/*",
74
+ "/baz/bagel/coffee",
75
+ ])
76
+ ).toEqual(["/*"]);
77
+ });
78
+
79
+ it("should truncate long nested path, removing other paths", () => {
80
+ expect(
81
+ consolidateRoutes([
82
+ // [/aaaaaaa, /foo] -> [/*]
83
+ "/foo/" + "a".repeat(maxRuleLength * 2),
84
+ "/foo/bar",
85
+ ])
86
+ ).toEqual(["/foo/*"]);
87
+ });
88
+ });
89
+
90
+ describe(`shortenRoute()`, () => {
91
+ it("should allow max length path", () => {
92
+ const route = "/" + "a".repeat(maxRuleLength - 1);
93
+ // Make sure we don't have an off-by-one error, that'd be embarrassing...
94
+ expect(route.length).toEqual(maxRuleLength);
95
+ expect(
96
+ // Should stay the same
97
+ shortenRoute(route)
98
+ ).toEqual(route);
99
+ });
100
+
101
+ it("should allow max length path (with slash)", () => {
102
+ const route = "/" + "a".repeat(maxRuleLength - 2) + "/";
103
+ expect(route.length).toEqual(maxRuleLength);
104
+ expect(
105
+ // Should stay the same
106
+ shortenRoute(route)
107
+ ).toEqual(route);
108
+ });
109
+
110
+ it("should allow max length wildcard path", () => {
111
+ const route = "/" + "a".repeat(maxRuleLength - 3) + "/*";
112
+ expect(route.length).toEqual(maxRuleLength);
113
+ expect(
114
+ // Should stay the same
115
+ shortenRoute(route)
116
+ ).toEqual(route);
117
+ });
118
+
119
+ it("should truncate long specific path to shorter wildcard path", () => {
120
+ const short = shortenRoute(
121
+ // /aaa/bbb -> /aaa/*
122
+ "/" +
123
+ "a".repeat(maxRuleLength * 0.6) +
124
+ "/" +
125
+ "b".repeat(maxRuleLength * 0.6)
126
+ );
127
+ expect(short).toEqual("/" + "a".repeat(maxRuleLength * 0.6) + "/*");
128
+ expect(short.length).toBeLessThanOrEqual(maxRuleLength);
129
+ });
130
+
131
+ it("should truncate long specific path (with slash) to shorter wildcard path", () => {
132
+ const short = shortenRoute(
133
+ // /aaa/bbb/ -> /aaa/*
134
+ "/" +
135
+ "a".repeat(maxRuleLength * 0.6) +
136
+ "/" +
137
+ "b".repeat(maxRuleLength * 0.6) +
138
+ "/"
139
+ );
140
+ expect(short).toEqual("/" + "a".repeat(maxRuleLength * 0.6) + "/*");
141
+ expect(short.length).toBeLessThanOrEqual(maxRuleLength);
142
+ });
143
+
144
+ it("should truncate long wildcard path to shorter wildcard path", () => {
145
+ const short = shortenRoute(
146
+ // /aaa/bbb/* -> /aaa/*
147
+ "/" +
148
+ "a".repeat(maxRuleLength * 0.6) +
149
+ "/" +
150
+ "b".repeat(maxRuleLength * 0.6) +
151
+ "/*"
152
+ );
153
+ expect(short).toEqual("/" + "a".repeat(maxRuleLength * 0.6) + "/*");
154
+ expect(short.length).toBeLessThanOrEqual(maxRuleLength);
155
+ });
156
+
157
+ it("should truncate long single-level specific path to catch-all path", () => {
158
+ expect(
159
+ shortenRoute(
160
+ // /aaa -> /*
161
+ "/" + "a".repeat(maxRuleLength * 2)
162
+ )
163
+ ).toEqual("/*");
164
+ });
165
+
166
+ it("should truncate long single-level specific path (with slash) to catch-all path", () => {
167
+ expect(
168
+ shortenRoute(
169
+ // /aaa/ -> /*
170
+ "/" + "a".repeat(maxRuleLength * 2) + "/"
171
+ )
172
+ ).toEqual("/*");
173
+ });
174
+
175
+ it("should truncate long single-level wildcard path to catch-all path", () => {
176
+ expect(
177
+ shortenRoute(
178
+ // /aaa/* -> /*
179
+ "/" + "a".repeat(maxRuleLength * 2) + "/*"
180
+ )
181
+ ).toEqual("/*");
182
+ });
183
+
184
+ it("should truncate many single-character segements", () => {
185
+ const short = shortenRoute(
186
+ // /a/a/a -> /a/a/*
187
+ "/a".repeat(maxRuleLength) // 2x limit
188
+ );
189
+ expect(short).toEqual("/a".repeat(maxRuleLength / 2 - 1) + "/*");
190
+ // Should be the exact max length
191
+ expect(short.length).toEqual(maxRuleLength);
192
+ });
193
+
194
+ it("should truncate many double-character segements", () => {
195
+ // === odd ===
196
+ const short = shortenRoute(
197
+ // /aa/aa/aa -> /aa/aa/*
198
+ "/aa".repeat(maxRuleLength) // 3x limit
199
+ );
200
+ expect(short).toEqual("/aa".repeat(maxRuleLength / 3 - 1) + "/*");
201
+ // Should be the exact max length
202
+ expect(short.length).toEqual(maxRuleLength - 2); // -2 because of the odd number
203
+ });
204
+
205
+ it("should truncate many single-character segements with wildcard", () => {
206
+ const short = shortenRoute(
207
+ // /a/a/a -> /a/a/*
208
+ "/a".repeat(maxRuleLength) + "/*" // 2x limit
209
+ );
210
+ expect(short).toEqual("/a".repeat(maxRuleLength / 2 - 1) + "/*");
211
+ // Should be the exact max length
212
+ expect(short.length).toEqual(maxRuleLength);
213
+ });
214
+
215
+ it("should truncate many double-character segements with wildcard", () => {
216
+ const short = shortenRoute(
217
+ // /aa/aa/aa -> /aa/*
218
+ "/aa".repeat(maxRuleLength) + "/*" // 2x limit
219
+ );
220
+ expect(short).toEqual("/aa".repeat(maxRuleLength / 3 - 1) + "/*");
221
+ // Should be the exact max length
222
+ expect(short.length).toEqual(maxRuleLength - 2); // -2 because of the odd number
223
+ });
224
+
225
+ // This is probably the best test here - tests variable-length segments, up until the max.
226
+ // This ensures that it's always able to shorten rules, without failing and returning "/*"
227
+ // The other tests are great for ensuring exact sequences instead of only asserting length, though.
228
+ for (const suffix of ["", "/", "/*"]) {
229
+ // Test each type of path: /a, /a/a, /a/*
230
+ it(`should truncate many variable-character segements (suffix="${suffix}") without truncating to /*`, () => {
231
+ // "/" + 97 chars + "/*" === 100
232
+ for (let i = 1; i < maxRuleLength - 2; i++) {
233
+ const segment = "/" + "a".repeat(i);
234
+ // make sure the segment isn't too long since we are testing not resulting to /*
235
+ expect(segment.length).toBeLessThanOrEqual(maxRuleLength);
236
+ const route =
237
+ segment.repeat((maxRuleLength / segment.length) * 2) + suffix;
238
+ // Make sure we made the rule too long
239
+ expect(route.length).toBeGreaterThan(maxRuleLength);
240
+ const short = shortenRoute(route);
241
+
242
+ // Make sure it's not over the limit
243
+ expect(short.length).toBeLessThanOrEqual(maxRuleLength);
244
+ // It should never have to fall back to /*
245
+ expect(short).not.toEqual("/*");
246
+ }
247
+ });
248
+ }
249
+ });
250
+ });
@@ -0,0 +1,73 @@
1
+ import { MAX_FUNCTIONS_ROUTES_RULE_LENGTH } from "../constants";
2
+
3
+ /**
4
+ * consolidateRoutes consolidates redundant routes - eg. ["/api/*"", "/api/foo"] -> ["/api/*""]
5
+ * @param routes If this is the same order as Functions routes (with most-specific first),
6
+ * it will be more efficient to reverse it first. Should be in the format: /api/foo, /api/*
7
+ * @returns Non-redundant list of routes
8
+ */
9
+ export function consolidateRoutes(routes: string[]): string[] {
10
+ // First we need to trim any rules that are too long and deduplicate the result
11
+ const routesShortened = Array.from(
12
+ new Set(routes.map((route) => shortenRoute(route)))
13
+ );
14
+
15
+ // create a map of the routes
16
+ const routesMap = new Map<string, boolean>();
17
+ for (const route of routesShortened) {
18
+ routesMap.set(route, true);
19
+ }
20
+ // Find routes that might render other routes redundant
21
+ for (const route of routesShortened.filter((r) => r.endsWith("/*"))) {
22
+ // Make sure the route still exists in the map
23
+ if (routesMap.has(route)) {
24
+ // Remove splat at the end, leaving the /
25
+ // eg. /api/* -> /api/
26
+ const routeTrimmed = route.substring(0, route.length - 1);
27
+ for (const nextRoute of routesMap.keys()) {
28
+ // Delete any route that has the wildcard route as a prefix
29
+ if (nextRoute !== route && nextRoute.startsWith(routeTrimmed)) {
30
+ routesMap.delete(nextRoute);
31
+ }
32
+ }
33
+ }
34
+ }
35
+ return Array.from(routesMap.keys());
36
+ }
37
+
38
+ /**
39
+ * Shortens a route until it's within the rule length limit defined in
40
+ * constants.MAX_FUNCTIONS_ROUTES_RULE_LENGTH
41
+ * Eg. /aaa/bbb -> /aaa/*
42
+ * @param routeToShorten Route to shorten if needed
43
+ * @param maxLength Max length of route to try to shorten to
44
+ */
45
+ export function shortenRoute(
46
+ routeToShorten: string,
47
+ maxLength: number = MAX_FUNCTIONS_ROUTES_RULE_LENGTH
48
+ ): string {
49
+ if (routeToShorten.length <= maxLength) {
50
+ return routeToShorten;
51
+ }
52
+
53
+ let route = routeToShorten;
54
+ // May have to try multiple times for longer segments
55
+ for (let i = 0; i < routeToShorten.length; i++) {
56
+ // Shorten to the first slash within the limit
57
+ for (let j = maxLength - 1 - i; j > 0; j--) {
58
+ if (route[j] === "/") {
59
+ route = route.slice(0, j) + "/*";
60
+ break;
61
+ }
62
+ }
63
+ if (route.length <= maxLength) {
64
+ break;
65
+ }
66
+ }
67
+
68
+ // If we failed to shorten it, fall back to include-all rather than breaking
69
+ if (route.length > maxLength) {
70
+ route = "/*";
71
+ }
72
+ return route;
73
+ }
@@ -0,0 +1,271 @@
1
+ import { toUrlPath } from "../../paths";
2
+ import { MAX_FUNCTIONS_ROUTES_RULES, ROUTES_SPEC_VERSION } from "../constants";
3
+ import {
4
+ compareRoutes,
5
+ convertRoutesToGlobPatterns,
6
+ convertRoutesToRoutesJSONSpec,
7
+ optimizeRoutesJSONSpec,
8
+ } from "./routes-transformation";
9
+
10
+ // TODO: make a convenience function for creating a list
11
+ // of `convertRoutesToGlobPatterns` inputs from a string array
12
+ describe("route-paths-to-glob-patterns", () => {
13
+ describe("convertRoutePathsToGlobPatterns()", () => {
14
+ it("should pass through routes with no wildcards", () => {
15
+ expect(
16
+ convertRoutesToGlobPatterns([{ routePath: toUrlPath("/api/foo") }])
17
+ ).toEqual(["/api/foo"]);
18
+ expect(
19
+ convertRoutesToGlobPatterns([
20
+ { routePath: toUrlPath("/api/foo") },
21
+ { routePath: toUrlPath("/api/bar") },
22
+ ])
23
+ ).toEqual(["/api/foo", "/api/bar"]);
24
+ expect(
25
+ convertRoutesToGlobPatterns([
26
+ { routePath: toUrlPath("/api/foo") },
27
+ { routePath: toUrlPath("/api/bar/foo") },
28
+ { routePath: toUrlPath("/foo/bar") },
29
+ ])
30
+ ).toEqual(["/api/foo", "/api/bar/foo", "/foo/bar"]);
31
+ });
32
+
33
+ it("should escalate a single param route to a wildcard", () => {
34
+ expect(
35
+ convertRoutesToGlobPatterns([{ routePath: toUrlPath("/api/:foo") }])
36
+ ).toEqual(["/api/*"]);
37
+ expect(
38
+ convertRoutesToGlobPatterns([{ routePath: toUrlPath("/api/foo/:bar") }])
39
+ ).toEqual(["/api/foo/*"]);
40
+ expect(
41
+ convertRoutesToGlobPatterns([
42
+ { routePath: toUrlPath("/bar/:barId/foo") },
43
+ ])
44
+ ).toEqual(["/bar/*"]);
45
+ expect(
46
+ convertRoutesToGlobPatterns([
47
+ { routePath: toUrlPath("/bar/:barId/foo/:fooId") },
48
+ ])
49
+ ).toEqual(["/bar/*"]);
50
+ expect(
51
+ convertRoutesToGlobPatterns([
52
+ { routePath: toUrlPath("/api/:foo") },
53
+ { routePath: toUrlPath("/bar/:barName/profile") },
54
+ { routePath: toUrlPath("/foo/bar/:barId/:fooId") },
55
+ ])
56
+ ).toEqual(["/api/*", "/bar/*", "/foo/bar/*"]);
57
+ });
58
+
59
+ it("should pass through a single wildcard route", () => {
60
+ expect(
61
+ convertRoutesToGlobPatterns([{ routePath: toUrlPath("/api/:baz*") }])
62
+ ).toEqual(["/api/*"]);
63
+ expect(
64
+ convertRoutesToGlobPatterns([
65
+ { routePath: toUrlPath("/api/foo/bar/:baz*") },
66
+ ])
67
+ ).toEqual(["/api/foo/bar/*"]);
68
+ expect(
69
+ convertRoutesToGlobPatterns([
70
+ { routePath: toUrlPath("/api/:foo/:bar*") },
71
+ ])
72
+ ).toEqual(["/api/*"]);
73
+ expect(
74
+ convertRoutesToGlobPatterns([
75
+ { routePath: toUrlPath("/foo/:foo*/bar/:bar*") },
76
+ ])
77
+ ).toEqual(["/foo/*"]);
78
+ expect(
79
+ convertRoutesToGlobPatterns([
80
+ { routePath: toUrlPath("/foo/:foo/bar/:bar*") },
81
+ ])
82
+ ).toEqual(["/foo/*"]);
83
+ expect(
84
+ convertRoutesToGlobPatterns([
85
+ { routePath: toUrlPath("/api/:baz*") },
86
+ { routePath: toUrlPath("/api/foo/bar/:baz*") },
87
+ { routePath: toUrlPath("/api/:foo/:bar*") },
88
+ ])
89
+ ).toEqual(["/api/*", "/api/foo/bar/*"]);
90
+ });
91
+
92
+ it("should deduplicate identical rules", () => {
93
+ expect(
94
+ convertRoutesToGlobPatterns([
95
+ { routePath: toUrlPath("/api/foo") },
96
+ { routePath: toUrlPath("/api/foo") },
97
+ ])
98
+ ).toEqual(["/api/foo"]);
99
+ expect(
100
+ convertRoutesToGlobPatterns([
101
+ { routePath: toUrlPath("/api/foo/bar") },
102
+ { routePath: toUrlPath("/foo/bar") },
103
+ { routePath: toUrlPath("/api/foo/bar") },
104
+ ])
105
+ ).toEqual(["/api/foo/bar", "/foo/bar"]);
106
+ expect(
107
+ convertRoutesToGlobPatterns([
108
+ { routePath: toUrlPath("/api/foo/:bar") },
109
+ { routePath: toUrlPath("/api/foo") },
110
+ { routePath: toUrlPath("/api/foo/:fooId/bar") },
111
+ { routePath: toUrlPath("/api/foo/*") },
112
+ ])
113
+ ).toEqual(["/api/foo/*", "/api/foo"]);
114
+ expect(
115
+ convertRoutesToGlobPatterns([
116
+ { routePath: toUrlPath("/api/:baz*") },
117
+ { routePath: toUrlPath("/api/:foo") },
118
+ ])
119
+ ).toEqual(["/api/*"]);
120
+ });
121
+
122
+ it("should handle middleware mounting", () => {
123
+ expect(
124
+ convertRoutesToGlobPatterns([
125
+ {
126
+ routePath: toUrlPath("/middleware"),
127
+ middleware: ["./some-middleware.ts"],
128
+ },
129
+ ])
130
+ ).toEqual(["/middleware/*"]);
131
+
132
+ expect(
133
+ convertRoutesToGlobPatterns([
134
+ {
135
+ routePath: toUrlPath("/middleware"),
136
+ middleware: "./some-middleware.ts",
137
+ },
138
+ ])
139
+ ).toEqual(["/middleware/*"]);
140
+
141
+ expect(
142
+ convertRoutesToGlobPatterns([
143
+ {
144
+ routePath: toUrlPath("/middleware"),
145
+ middleware: [],
146
+ },
147
+ ])
148
+ ).toEqual(["/middleware"]);
149
+ });
150
+ });
151
+
152
+ describe("convertRoutesToRoutesJSONSpec()", () => {
153
+ it("should convert and consolidate routes into JSONSpec", () => {
154
+ expect(
155
+ convertRoutesToRoutesJSONSpec([
156
+ { routePath: toUrlPath("/api/foo/bar") },
157
+ { routePath: toUrlPath("/foo/bar") },
158
+ { routePath: toUrlPath("/foo/:bar") },
159
+ { routePath: toUrlPath("/api/foo/bar") },
160
+ {
161
+ routePath: toUrlPath("/middleware"),
162
+ middleware: "./some-middleware.ts",
163
+ },
164
+ ])
165
+ ).toEqual({
166
+ version: ROUTES_SPEC_VERSION,
167
+ include: ["/middleware/*", "/foo/*", "/api/foo/bar"],
168
+ exclude: [],
169
+ });
170
+ });
171
+
172
+ it("should truncate all routes if over limit", () => {
173
+ const routes = [];
174
+ for (let i = 0; i < MAX_FUNCTIONS_ROUTES_RULES + 1; i++) {
175
+ routes.push({ routePath: toUrlPath(`/api/foo-${i}`) });
176
+ }
177
+ expect(convertRoutesToRoutesJSONSpec(routes)).toEqual({
178
+ version: ROUTES_SPEC_VERSION,
179
+ include: ["/*"],
180
+ exclude: [],
181
+ });
182
+ });
183
+
184
+ it("should allow max routes", () => {
185
+ const routes = [];
186
+ for (let i = 0; i < MAX_FUNCTIONS_ROUTES_RULES; i++) {
187
+ routes.push({ routePath: toUrlPath(`/api/foo-${i}`) });
188
+ }
189
+ expect(convertRoutesToRoutesJSONSpec(routes).include.length).toEqual(
190
+ MAX_FUNCTIONS_ROUTES_RULES
191
+ );
192
+ });
193
+ });
194
+
195
+ describe("optimizeRoutesJSONSpec()", () => {
196
+ it("should convert and consolidate routes into JSONSpec", () => {
197
+ expect(
198
+ optimizeRoutesJSONSpec({
199
+ version: ROUTES_SPEC_VERSION,
200
+ exclude: [],
201
+ include: [
202
+ "/api/foo/bar",
203
+ "/foo/bar",
204
+ "/foo/*",
205
+ "/api/foo/bar",
206
+ "/middleware/*",
207
+ ],
208
+ })
209
+ ).toEqual({
210
+ version: ROUTES_SPEC_VERSION,
211
+ include: ["/middleware/*", "/foo/*", "/api/foo/bar"],
212
+ exclude: [],
213
+ });
214
+ });
215
+
216
+ it("should truncate all routes if over limit", () => {
217
+ const include: string[] = [];
218
+ for (let i = 0; i < MAX_FUNCTIONS_ROUTES_RULES + 1; i++) {
219
+ include.push(`/api/foo-${i}`);
220
+ }
221
+ expect(
222
+ optimizeRoutesJSONSpec({
223
+ version: ROUTES_SPEC_VERSION,
224
+ include,
225
+ exclude: [],
226
+ })
227
+ ).toEqual({
228
+ version: ROUTES_SPEC_VERSION,
229
+ include: ["/*"],
230
+ exclude: [],
231
+ });
232
+ });
233
+
234
+ it("should allow max routes", () => {
235
+ const include: string[] = [];
236
+ for (let i = 0; i < MAX_FUNCTIONS_ROUTES_RULES; i++) {
237
+ include.push(`/api/foo-${i}`);
238
+ }
239
+ expect(
240
+ optimizeRoutesJSONSpec({
241
+ version: ROUTES_SPEC_VERSION,
242
+ include,
243
+ exclude: [],
244
+ }).include.length
245
+ ).toEqual(MAX_FUNCTIONS_ROUTES_RULES);
246
+ });
247
+ });
248
+
249
+ describe("compareRoutes()", () => {
250
+ describe("compareRoutes()", () => {
251
+ test("routes / last", () => {
252
+ expect(compareRoutes("/", "/foo")).toBeGreaterThanOrEqual(1);
253
+ expect(compareRoutes("/", "/*")).toBeGreaterThanOrEqual(1);
254
+ });
255
+
256
+ test("routes with fewer segments come after those with more segments", () => {
257
+ expect(compareRoutes("/foo", "/foo/bar")).toBeGreaterThanOrEqual(1);
258
+ expect(compareRoutes("/foo", "/foo/bar/cat")).toBeGreaterThanOrEqual(1);
259
+ });
260
+
261
+ test("routes with wildcard segments come after those without", () => {
262
+ expect(compareRoutes("/*", "/foo")).toBe(1);
263
+ expect(compareRoutes("/foo/*", "/foo/bar")).toBe(1);
264
+ });
265
+
266
+ test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
267
+ expect(compareRoutes("/foo/*/bar", "/foo/bar/*")).toBe(1);
268
+ });
269
+ });
270
+ });
271
+ });