wrangler 2.0.24 → 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.
- package/miniflare-dist/index.mjs +130 -16
- package/package.json +1 -1
- package/src/__tests__/configuration.test.ts +1 -1
- package/src/__tests__/dev.test.tsx +26 -4
- package/src/__tests__/helpers/mock-cfetch.ts +2 -2
- package/src/__tests__/r2.test.ts +18 -0
- package/src/__tests__/tail.test.ts +93 -39
- package/src/api/dev.ts +6 -0
- package/src/bundle.ts +3 -2
- package/src/config/config.ts +1 -1
- package/src/config/validation.ts +1 -1
- package/src/dev/dev.tsx +12 -2
- package/src/dev/local.tsx +69 -5
- package/src/dev/use-esbuild.ts +3 -0
- package/src/dev-registry.tsx +3 -0
- package/src/dev.tsx +26 -17
- package/src/index.tsx +51 -21
- package/src/inspect.ts +1 -4
- package/src/miniflare-cli/assets.ts +19 -16
- package/src/miniflare-cli/index.ts +121 -2
- package/src/pages/build.tsx +36 -28
- package/src/pages/constants.ts +3 -0
- package/src/pages/deployments.tsx +9 -9
- package/src/pages/dev.tsx +85 -27
- package/src/pages/functions/buildPlugin.ts +4 -0
- package/src/pages/functions/buildWorker.ts +4 -0
- package/src/pages/functions/routes-consolidation.test.ts +66 -0
- package/src/pages/functions/routes-consolidation.ts +29 -0
- package/src/pages/functions/routes-transformation.test.ts +271 -0
- package/src/pages/functions/routes-transformation.ts +125 -0
- package/src/pages/projects.tsx +9 -3
- package/src/pages/publish.tsx +56 -14
- package/src/pages/types.ts +9 -0
- package/src/pages/upload.tsx +6 -8
- package/src/r2.ts +13 -0
- package/src/tail/index.ts +15 -2
- package/src/tail/printing.ts +41 -3
- package/wrangler-dist/cli.d.ts +6 -0
- package/wrangler-dist/cli.js +385 -89
|
@@ -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
|
+
}
|
package/src/pages/projects.tsx
CHANGED
|
@@ -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
|
-
|
|
131
|
-
? execSync(`git rev-parse --abbrev-ref HEAD`).toString().trim()
|
|
132
|
-
: "production"
|
|
138
|
+
productionBranch ?? "production"
|
|
133
139
|
);
|
|
134
140
|
}
|
|
135
141
|
|
package/src/pages/publish.tsx
CHANGED
|
@@ -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 {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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)
|
|
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
|
-
}:
|
|
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
|
{
|
package/src/pages/types.ts
CHANGED
|
@@ -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;
|
package/src/pages/upload.tsx
CHANGED
|
@@ -26,15 +26,12 @@ import {
|
|
|
26
26
|
MAX_UPLOAD_ATTEMPTS,
|
|
27
27
|
} from "./constants";
|
|
28
28
|
import { pagesBetaWarning } from "./utils";
|
|
29
|
-
import type { UploadPayloadFile } from "./types";
|
|
30
|
-
import type {
|
|
29
|
+
import type { UploadPayloadFile, YargsOptionsToInterface } from "./types";
|
|
30
|
+
import type { Argv } from "yargs";
|
|
31
31
|
|
|
32
|
-
type UploadArgs =
|
|
33
|
-
directory: string;
|
|
34
|
-
"output-manifest-path"?: string;
|
|
35
|
-
};
|
|
32
|
+
type UploadArgs = YargsOptionsToInterface<typeof Options>;
|
|
36
33
|
|
|
37
|
-
export function Options(yargs: Argv)
|
|
34
|
+
export function Options(yargs: Argv) {
|
|
38
35
|
return yargs
|
|
39
36
|
.positional("directory", {
|
|
40
37
|
type: "string",
|
|
@@ -53,7 +50,7 @@ export function Options(yargs: Argv): Argv<UploadArgs> {
|
|
|
53
50
|
export const Handler = async ({
|
|
54
51
|
directory,
|
|
55
52
|
outputManifestPath,
|
|
56
|
-
}:
|
|
53
|
+
}: UploadArgs) => {
|
|
57
54
|
if (!directory) {
|
|
58
55
|
throw new FatalError("Must specify a directory.", 1);
|
|
59
56
|
}
|
|
@@ -106,6 +103,7 @@ export const upload = async (
|
|
|
106
103
|
"_worker.js",
|
|
107
104
|
"_redirects",
|
|
108
105
|
"_headers",
|
|
106
|
+
"_routes.json",
|
|
109
107
|
".DS_Store",
|
|
110
108
|
"node_modules",
|
|
111
109
|
".git",
|
package/src/r2.ts
CHANGED
|
@@ -116,3 +116,16 @@ export async function putR2Object(
|
|
|
116
116
|
}
|
|
117
117
|
);
|
|
118
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Delete an Object
|
|
121
|
+
*/
|
|
122
|
+
export async function deleteR2Object(
|
|
123
|
+
accountId: string,
|
|
124
|
+
bucketName: string,
|
|
125
|
+
objectName: string
|
|
126
|
+
): Promise<void> {
|
|
127
|
+
await fetchR2Objects(
|
|
128
|
+
`/accounts/${accountId}/r2/buckets/${bucketName}/objects/${objectName}`,
|
|
129
|
+
{ method: "DELETE" }
|
|
130
|
+
);
|
|
131
|
+
}
|
package/src/tail/index.ts
CHANGED
|
@@ -174,12 +174,12 @@ export type TailEventMessage = {
|
|
|
174
174
|
/**
|
|
175
175
|
* The event that triggered the worker. In the case of an HTTP request,
|
|
176
176
|
* this will be a RequestEvent. If it's a cron trigger, it'll be a
|
|
177
|
-
* ScheduledEvent.
|
|
177
|
+
* ScheduledEvent. If it's a durable object alarm, it's an AlarmEvent.
|
|
178
178
|
*
|
|
179
179
|
* Until workers-types exposes individual types for export, we'll have
|
|
180
180
|
* to just re-define these types ourselves.
|
|
181
181
|
*/
|
|
182
|
-
event: RequestEvent | ScheduledEvent | undefined | null;
|
|
182
|
+
event: RequestEvent | ScheduledEvent | AlarmEvent | undefined | null;
|
|
183
183
|
};
|
|
184
184
|
|
|
185
185
|
/**
|
|
@@ -297,3 +297,16 @@ export type ScheduledEvent = {
|
|
|
297
297
|
*/
|
|
298
298
|
scheduledTime: number;
|
|
299
299
|
};
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A event that was triggered from a durable object alarm
|
|
303
|
+
*/
|
|
304
|
+
export type AlarmEvent = {
|
|
305
|
+
/**
|
|
306
|
+
* The datetime the alarm was scheduled for.
|
|
307
|
+
*
|
|
308
|
+
* This is sent as an ISO timestamp string (different than ScheduledEvent.scheduledTime),
|
|
309
|
+
* you should parse it later on on your own.
|
|
310
|
+
*/
|
|
311
|
+
scheduledTime: string;
|
|
312
|
+
};
|