wrangler 2.0.22 → 2.0.25

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 (75) hide show
  1. package/README.md +20 -2
  2. package/bin/wrangler.js +1 -1
  3. package/miniflare-dist/index.mjs +643 -7
  4. package/package.json +17 -5
  5. package/src/__tests__/configuration.test.ts +89 -17
  6. package/src/__tests__/dev.test.tsx +121 -8
  7. package/src/__tests__/generate.test.ts +93 -0
  8. package/src/__tests__/helpers/mock-cfetch.ts +54 -2
  9. package/src/__tests__/index.test.ts +10 -27
  10. package/src/__tests__/jest.setup.ts +31 -1
  11. package/src/__tests__/kv.test.ts +82 -61
  12. package/src/__tests__/metrics.test.ts +5 -0
  13. package/src/__tests__/publish.test.ts +573 -254
  14. package/src/__tests__/r2.test.ts +173 -71
  15. package/src/__tests__/tail.test.ts +93 -39
  16. package/src/__tests__/user.test.ts +1 -0
  17. package/src/__tests__/validate-dev-props.test.ts +56 -0
  18. package/src/__tests__/version.test.ts +35 -0
  19. package/src/__tests__/whoami.test.tsx +60 -1
  20. package/src/api/dev.ts +49 -9
  21. package/src/bundle.ts +298 -37
  22. package/src/cfetch/internal.ts +34 -2
  23. package/src/config/config.ts +15 -3
  24. package/src/config/environment.ts +40 -8
  25. package/src/config/index.ts +13 -0
  26. package/src/config/validation.ts +111 -9
  27. package/src/create-worker-preview.ts +3 -1
  28. package/src/create-worker-upload-form.ts +25 -0
  29. package/src/dev/dev.tsx +145 -31
  30. package/src/dev/local.tsx +116 -24
  31. package/src/dev/remote.tsx +39 -12
  32. package/src/dev/use-esbuild.ts +28 -0
  33. package/src/dev/validate-dev-props.ts +31 -0
  34. package/src/dev-registry.tsx +160 -0
  35. package/src/dev.tsx +148 -67
  36. package/src/generate.ts +112 -14
  37. package/src/index.tsx +252 -7
  38. package/src/inspect.ts +90 -5
  39. package/src/metrics/index.ts +1 -0
  40. package/src/metrics/metrics-dispatcher.ts +1 -0
  41. package/src/metrics/metrics-usage-headers.ts +24 -0
  42. package/src/metrics/send-event.ts +2 -2
  43. package/src/miniflare-cli/assets.ts +546 -0
  44. package/src/miniflare-cli/index.ts +157 -6
  45. package/src/module-collection.ts +3 -3
  46. package/src/pages/build.tsx +36 -28
  47. package/src/pages/constants.ts +4 -0
  48. package/src/pages/deployments.tsx +10 -10
  49. package/src/pages/dev.tsx +155 -651
  50. package/src/pages/functions/buildPlugin.ts +4 -0
  51. package/src/pages/functions/buildWorker.ts +4 -0
  52. package/src/pages/functions/routes-consolidation.test.ts +66 -0
  53. package/src/pages/functions/routes-consolidation.ts +29 -0
  54. package/src/pages/functions/routes-transformation.test.ts +271 -0
  55. package/src/pages/functions/routes-transformation.ts +125 -0
  56. package/src/pages/projects.tsx +9 -3
  57. package/src/pages/publish.tsx +57 -15
  58. package/src/pages/types.ts +9 -0
  59. package/src/pages/upload.tsx +38 -21
  60. package/src/publish.ts +139 -112
  61. package/src/r2.ts +81 -0
  62. package/src/tail/index.ts +15 -2
  63. package/src/tail/printing.ts +41 -3
  64. package/src/user/choose-account.tsx +20 -11
  65. package/src/user/user.tsx +20 -2
  66. package/src/whoami.tsx +79 -1
  67. package/src/worker.ts +12 -0
  68. package/templates/first-party-worker-module-facade.ts +18 -0
  69. package/templates/format-dev-errors.ts +32 -0
  70. package/templates/pages-shim.ts +9 -0
  71. package/templates/{static-asset-facade.js → serve-static-assets.ts} +21 -7
  72. package/templates/service-bindings-module-facade.js +51 -0
  73. package/templates/service-bindings-sw-facade.js +39 -0
  74. package/wrangler-dist/cli.d.ts +38 -3
  75. package/wrangler-dist/cli.js +45244 -25199
@@ -29,6 +29,10 @@ export function buildPlugin({
29
29
  bundle: true,
30
30
  format: "esm",
31
31
  target: "esnext",
32
+ loader: {
33
+ ".html": "text",
34
+ ".txt": "text",
35
+ },
32
36
  outfile,
33
37
  minify,
34
38
  sourcemap,
@@ -34,6 +34,10 @@ export function buildWorker({
34
34
  bundle: true,
35
35
  format: "esm",
36
36
  target: "esnext",
37
+ loader: {
38
+ ".html": "text",
39
+ ".txt": "text",
40
+ },
37
41
  outfile,
38
42
  minify,
39
43
  sourcemap,
@@ -0,0 +1,66 @@
1
+ import { consolidateRoutes } from "./routes-consolidation";
2
+
3
+ describe("route-consolidation", () => {
4
+ describe("consolidateRoutes()", () => {
5
+ it("should consolidate redundant routes", () => {
6
+ expect(consolidateRoutes(["/api/foo", "/api/*"])).toEqual(["/api/*"]);
7
+ expect(
8
+ consolidateRoutes([
9
+ "/api/foo",
10
+ "/api/foo/*",
11
+ "/api/bar/*",
12
+ "/api/*",
13
+ "/foo",
14
+ "/foo/bar",
15
+ "/bar/*",
16
+ "/bar/baz/*",
17
+ "/bar/baz/hello",
18
+ ])
19
+ ).toEqual(["/api/*", "/foo", "/foo/bar", "/bar/*"]);
20
+ });
21
+ it("should consolidate thousands of redundant routes", () => {
22
+ // Test to make sure the consolidator isn't horribly slow
23
+ const routes: string[] = [];
24
+ const limit = 1000;
25
+ for (let i = 0; i < limit; i++) {
26
+ // Add 3 routes per id
27
+ const id = `some-id-${i}`;
28
+ routes.push(`/${id}/*`, `/${id}/foo`, `/${id}/bar/*`);
29
+ }
30
+ const consolidated = consolidateRoutes(routes);
31
+ expect(consolidated.length).toEqual(limit);
32
+ // Should be all unique
33
+ expect(Array.from(new Set(consolidated)).length).toEqual(limit);
34
+ // Should all have pattern `/$id/*`
35
+ expect(
36
+ consolidated.every((route) => route.match(/\/[a-z0-9-]+\/\*/) !== null)
37
+ ).toEqual(true);
38
+ });
39
+
40
+ it("should consolidate many redundant sub-routes", () => {
41
+ const routes: string[] = [];
42
+ const limit = 15;
43
+
44
+ // Create $limit of top-level catch-all routes, with a lot of sub-routes
45
+ for (let i = 0; i < limit; i++) {
46
+ routes.push(`/foo-${i}/*`);
47
+ for (let j = 0; j < limit; j++) {
48
+ routes.push(`/foo-${i}/bar-${j}/hello`);
49
+ for (let k = 0; k < limit; k++) {
50
+ routes.push(`/foo-${i}/bar-${j}/baz-${k}/*`);
51
+ routes.push(`/foo-${i}/bar-${j}/baz-${k}/profile`);
52
+ }
53
+ }
54
+ }
55
+
56
+ const consolidated = consolidateRoutes(routes);
57
+ expect(consolidated.length).toEqual(limit);
58
+ // Should be all unique
59
+ expect(Array.from(new Set(consolidated)).length).toEqual(limit);
60
+ // Should all have pattern `/$id/*`
61
+ expect(
62
+ consolidated.every((route) => route.match(/\/[a-z0-9-]+\/\*/) !== null)
63
+ ).toEqual(true);
64
+ });
65
+ });
66
+ });
@@ -0,0 +1,29 @@
1
+ /**
2
+ * consolidateRoutes consolidates redundant routes - eg. ["/api/*"", "/api/foo"] -> ["/api/*""]
3
+ * @param routes If this is the same order as Functions routes (with most-specific first),
4
+ * it will be more efficient to reverse it first. Should be in the format: /api/foo, /api/*
5
+ * @returns Non-redundant list of routes
6
+ */
7
+ export function consolidateRoutes(routes: string[]): string[] {
8
+ // create a map of the routes
9
+ const routesMap = new Map<string, boolean>();
10
+ for (const route of routes) {
11
+ routesMap.set(route, true);
12
+ }
13
+ // Find routes that might render other routes redundant
14
+ for (const route of routes.filter((r) => r.endsWith("/*"))) {
15
+ // Make sure the route still exists in the map
16
+ if (routesMap.has(route)) {
17
+ // Remove splat at the end, leaving the /
18
+ // eg. /api/* -> /api/
19
+ const routeTrimmed = route.substring(0, route.length - 1);
20
+ for (const nextRoute of routesMap.keys()) {
21
+ // Delete any route that has the wildcard route as a prefix
22
+ if (nextRoute !== route && nextRoute.startsWith(routeTrimmed)) {
23
+ routesMap.delete(nextRoute);
24
+ }
25
+ }
26
+ }
27
+ }
28
+ return Array.from(routesMap.keys());
29
+ }
@@ -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
+ });
@@ -0,0 +1,125 @@
1
+ import { join as pathJoin } from "node:path";
2
+ import { toUrlPath } from "../../paths";
3
+ import { MAX_FUNCTIONS_ROUTES_RULES, ROUTES_SPEC_VERSION } from "../constants";
4
+ import { consolidateRoutes } from "./routes-consolidation";
5
+ import type { RouteConfig } from "./routes";
6
+
7
+ /** Interface for _routes.json */
8
+ interface RoutesJSONSpec {
9
+ version: typeof ROUTES_SPEC_VERSION;
10
+ include: string[];
11
+ exclude: string[];
12
+ }
13
+
14
+ type RoutesJSONRouteInput = Pick<RouteConfig, "routePath" | "middleware">[];
15
+
16
+ /**
17
+ * TODO can we do better naming?
18
+ */
19
+ export function convertRoutesToGlobPatterns(
20
+ routes: RoutesJSONRouteInput
21
+ ): string[] {
22
+ const convertedRoutes = routes.map(({ routePath, middleware }) => {
23
+ const globbedRoutePath: string = routePath.replace(/:\w+\*?.*/, "*");
24
+
25
+ // Middleware mountings need to end in glob so that they can handle their
26
+ // own sub-path routes
27
+ if (
28
+ typeof middleware === "string" ||
29
+ (Array.isArray(middleware) && middleware.length > 0)
30
+ ) {
31
+ if (!globbedRoutePath.endsWith("*")) {
32
+ return toUrlPath(pathJoin(globbedRoutePath, "*"));
33
+ }
34
+ }
35
+
36
+ return toUrlPath(globbedRoutePath);
37
+ });
38
+
39
+ return Array.from(new Set(convertedRoutes));
40
+ }
41
+
42
+ /**
43
+ * Converts Functions routes like /foo/:bar to a Routing object that's used
44
+ * to determine if a request should run in the Functions user-worker.
45
+ * Also consolidates redundant routes such as [/foo/bar, /foo/:bar] -> /foo/*
46
+ *
47
+ * @returns RoutesJSONSpec to be written to _routes.json
48
+ */
49
+ export function convertRoutesToRoutesJSONSpec(
50
+ routes: RoutesJSONRouteInput
51
+ ): RoutesJSONSpec {
52
+ // The initial routes coming in are sorted most-specific to least-specific.
53
+ // The order doesn't have any affect on the output of this function, but
54
+ // it should speed up route consolidation with less-specific routes being first.
55
+ const reversedRoutes = [...routes].reverse();
56
+ const include = convertRoutesToGlobPatterns(reversedRoutes);
57
+ return optimizeRoutesJSONSpec({
58
+ version: ROUTES_SPEC_VERSION,
59
+ include,
60
+ exclude: [],
61
+ });
62
+ }
63
+
64
+ /**
65
+ * Optimizes and returns a new Routes JSON Spec instance performing
66
+ * de-duping, consolidation, truncation, and sorting
67
+ */
68
+ export function optimizeRoutesJSONSpec(spec: RoutesJSONSpec): RoutesJSONSpec {
69
+ const optimizedSpec = { ...spec };
70
+
71
+ let consolidatedRoutes = consolidateRoutes(optimizedSpec.include);
72
+ if (consolidatedRoutes.length > MAX_FUNCTIONS_ROUTES_RULES) {
73
+ consolidatedRoutes = ["/*"];
74
+ }
75
+ // Sort so that least-specific routes are first
76
+ consolidatedRoutes.sort((a, b) => compareRoutes(b, a));
77
+
78
+ optimizedSpec.include = consolidatedRoutes;
79
+
80
+ return optimizedSpec;
81
+ }
82
+
83
+ /**
84
+ * Simplified routes comparison (copied from the one in filepath-routing.)
85
+ * This version will sort most-specific to least-specific, but the input is simplified
86
+ * routes like /foo/*, /foo, etc
87
+ */
88
+ export function compareRoutes(routeA: string, routeB: string) {
89
+ function parseRoutePath(routePath: string): string[] {
90
+ return routePath.slice(1).split("/").filter(Boolean);
91
+ }
92
+
93
+ const segmentsA = parseRoutePath(routeA);
94
+ const segmentsB = parseRoutePath(routeB);
95
+
96
+ // sort routes with fewer segments after those with more segments
97
+ if (segmentsA.length !== segmentsB.length) {
98
+ return segmentsB.length - segmentsA.length;
99
+ }
100
+
101
+ for (let i = 0; i < segmentsA.length; i++) {
102
+ const isWildcardA = segmentsA[i].includes("*");
103
+ const isWildcardB = segmentsB[i].includes("*");
104
+
105
+ // sort wildcard segments after non-wildcard segments
106
+ if (isWildcardA && !isWildcardB) return 1;
107
+ if (!isWildcardA && isWildcardB) return -1;
108
+ }
109
+
110
+ // all else equal, just sort the paths lexicographically
111
+ return routeA.localeCompare(routeB);
112
+ }
113
+
114
+ export function isRoutesJSONSpec(data: unknown): data is RoutesJSONSpec {
115
+ return (
116
+ (typeof data === "object" &&
117
+ data &&
118
+ "version" in data &&
119
+ typeof (data as RoutesJSONSpec).version === "number" &&
120
+ (data as RoutesJSONSpec).version === ROUTES_SPEC_VERSION &&
121
+ Array.isArray((data as RoutesJSONSpec).include) &&
122
+ Array.isArray((data as RoutesJSONSpec).exclude)) ||
123
+ false
124
+ );
125
+ }
@@ -124,12 +124,18 @@ export async function CreateHandler({
124
124
  isGitDir = false;
125
125
  }
126
126
 
127
+ if (isGitDir) {
128
+ try {
129
+ productionBranch = execSync(`git rev-parse --abbrev-ref HEAD`)
130
+ .toString()
131
+ .trim();
132
+ } catch (err) {}
133
+ }
134
+
127
135
  productionBranch = await prompt(
128
136
  "Enter the production branch name:",
129
137
  "text",
130
- isGitDir
131
- ? execSync(`git rev-parse --abbrev-ref HEAD`).toString().trim()
132
- : "production"
138
+ productionBranch ?? "production"
133
139
  );
134
140
  }
135
141
 
@@ -16,22 +16,24 @@ import * as metrics from "../metrics";
16
16
  import { requireAuth } from "../user";
17
17
  import { buildFunctions } from "./build";
18
18
  import { PAGES_CONFIG_CACHE_FILENAME } from "./constants";
19
+ import {
20
+ isRoutesJSONSpec,
21
+ optimizeRoutesJSONSpec,
22
+ } from "./functions/routes-transformation";
19
23
  import { listProjects } from "./projects";
20
24
  import { upload } from "./upload";
21
25
  import { pagesBetaWarning } from "./utils";
22
- import type { Deployment, PagesConfigCache, Project } from "./types";
23
- import type { ArgumentsCamelCase, Argv } from "yargs";
24
-
25
- type PublishArgs = {
26
- directory: string;
27
- "project-name"?: string;
28
- branch?: string;
29
- "commit-hash"?: string;
30
- "commit-message"?: string;
31
- "commit-dirty"?: boolean;
32
- };
26
+ import type {
27
+ Deployment,
28
+ PagesConfigCache,
29
+ Project,
30
+ YargsOptionsToInterface,
31
+ } from "./types";
32
+ import type { Argv } from "yargs";
33
+
34
+ type PublishArgs = YargsOptionsToInterface<typeof Options>;
33
35
 
34
- export function Options(yargs: Argv): Argv<PublishArgs> {
36
+ export function Options(yargs: Argv) {
35
37
  return yargs
36
38
  .positional("directory", {
37
39
  type: "string",
@@ -77,7 +79,7 @@ export const Handler = async ({
77
79
  commitMessage,
78
80
  commitDirty,
79
81
  config: wranglerConfig,
80
- }: ArgumentsCamelCase<PublishArgs>) => {
82
+ }: PublishArgs) => {
81
83
  if (wranglerConfig) {
82
84
  throw new FatalError("Pages does not support wrangler.toml", 1);
83
85
  }
@@ -251,6 +253,7 @@ export const Handler = async ({
251
253
 
252
254
  let builtFunctions: string | undefined = undefined;
253
255
  const functionsDirectory = join(cwd(), "functions");
256
+ const routesOutputPath = join(tmpdir(), `_routes-${Math.random()}.json`);
254
257
  if (existsSync(functionsDirectory)) {
255
258
  const outfile = join(tmpdir(), `./functionsWorker-${Math.random()}.js`);
256
259
 
@@ -260,6 +263,7 @@ export const Handler = async ({
260
263
  functionsDirectory,
261
264
  onEnd: () => resolve(null),
262
265
  buildOutputDirectory: dirname(outfile),
266
+ routesOutputPath,
263
267
  })
264
268
  );
265
269
 
@@ -290,6 +294,7 @@ export const Handler = async ({
290
294
 
291
295
  let _headers: string | undefined,
292
296
  _redirects: string | undefined,
297
+ _routes: string | undefined,
293
298
  _workerJS: string | undefined;
294
299
 
295
300
  try {
@@ -300,6 +305,10 @@ export const Handler = async ({
300
305
  _redirects = readFileSync(join(directory, "_redirects"), "utf-8");
301
306
  } catch {}
302
307
 
308
+ try {
309
+ _routes = readFileSync(routesOutputPath, "utf-8");
310
+ } catch {}
311
+
303
312
  try {
304
313
  _workerJS = readFileSync(join(directory, "_worker.js"), "utf-8");
305
314
  } catch {}
@@ -312,12 +321,45 @@ export const Handler = async ({
312
321
  formData.append("_redirects", new File([_redirects], "_redirects"));
313
322
  }
314
323
 
324
+ if (_routes) {
325
+ formData.append("_routes.json", new File([_routes], "_routes.json"));
326
+ }
327
+
315
328
  if (builtFunctions) {
316
329
  formData.append("_worker.js", new File([builtFunctions], "_worker.js"));
317
330
  } else if (_workerJS) {
331
+ // Advanced Mode
332
+ // https://developers.cloudflare.com/pages/platform/functions/#advanced-mode
318
333
  formData.append("_worker.js", new File([_workerJS], "_worker.js"));
319
- }
320
334
 
335
+ try {
336
+ // In advanced mode, developers can specify a custom _routes.json
337
+ // file. In which case, we need to run it through optimization
338
+ // to potentially reduce the overall worker pipeline size
339
+ const routesPath = join(directory, "_routes.json");
340
+ const advancedModeRoutesString = readFileSync(routesPath, "utf-8");
341
+ const advancedModeRoutes = JSON.parse(advancedModeRoutesString);
342
+
343
+ if (!isRoutesJSONSpec(advancedModeRoutes)) {
344
+ throw new FatalError(
345
+ "Invalid _routes.json file found at:" + routesPath,
346
+ 1
347
+ );
348
+ }
349
+
350
+ _routes = JSON.stringify(optimizeRoutesJSONSpec(advancedModeRoutes));
351
+
352
+ logger.warn(
353
+ `🚨 _routes.json is an experimental feature and is subject to change. Don't use unless you really must!`
354
+ );
355
+ } catch (e) {
356
+ // Ignore file not existing errors for _routes.json but forward the potential
357
+ // FatalError from an invalid spec
358
+ if (e instanceof FatalError) {
359
+ throw e;
360
+ }
361
+ }
362
+ }
321
363
  const deploymentResponse = await fetchResult<Deployment>(
322
364
  `/accounts/${accountId}/pages/projects/${projectName}/deployments`,
323
365
  {
@@ -334,5 +376,5 @@ export const Handler = async ({
334
376
  logger.log(
335
377
  `✨ Deployment complete! Take a peek over at ${deploymentResponse.url}`
336
378
  );
337
- await metrics.sendMetricsEvent("deploy pages project");
379
+ await metrics.sendMetricsEvent("create pages deployment");
338
380
  };
@@ -1,3 +1,12 @@
1
+ import type { ArgumentsCamelCase, Argv } from "yargs";
2
+
3
+ /**
4
+ * Given some Yargs Options function factory, extract the interface
5
+ * that corresponds to the yargs arguments
6
+ */
7
+ export type YargsOptionsToInterface<T extends (yargs: Argv) => Argv> =
8
+ T extends (yargs: Argv) => Argv<infer P> ? ArgumentsCamelCase<P> : never;
9
+
1
10
  export type Project = {
2
11
  name: string;
3
12
  subdomain: string;