wrangler 0.0.0-e64812e → 0.0.0-e8e684c7
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/README.md +27 -3
- package/bin/wrangler.js +150 -62
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +663 -7
- package/package.json +173 -152
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +30 -24
- package/src/__tests__/configuration.test.ts +4015 -3363
- package/src/__tests__/dev.test.tsx +1290 -952
- package/src/__tests__/generate.test.ts +91 -0
- package/src/__tests__/guess-worker-format.test.ts +68 -68
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/mock-account-id.ts +24 -24
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-cfetch.ts +179 -94
- package/src/__tests__/helpers/mock-console.ts +49 -39
- package/src/__tests__/helpers/mock-dialogs.ts +94 -71
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
- package/src/__tests__/helpers/mock-http-server.ts +30 -30
- package/src/__tests__/helpers/mock-istty.ts +65 -18
- package/src/__tests__/helpers/mock-known-routes.ts +7 -0
- package/src/__tests__/helpers/mock-kv.ts +26 -26
- package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
- package/src/__tests__/helpers/mock-process.ts +39 -0
- package/src/__tests__/helpers/mock-stdin.ts +82 -77
- package/src/__tests__/helpers/mock-web-socket.ts +21 -21
- package/src/__tests__/helpers/run-in-tmp.ts +27 -28
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/write-worker-source.ts +16 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +142 -109
- package/src/__tests__/index.test.ts +222 -233
- package/src/__tests__/init.test.ts +2139 -1603
- package/src/__tests__/jest.setup.ts +114 -34
- package/src/__tests__/kv.test.ts +1222 -1032
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/metrics.test.ts +450 -0
- package/src/__tests__/package-manager.test.ts +303 -303
- package/src/__tests__/pages.test.ts +1173 -652
- package/src/__tests__/parse.test.ts +252 -252
- package/src/__tests__/publish.test.ts +6788 -5301
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +328 -225
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +470 -459
- package/src/__tests__/tail.test.ts +676 -564
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/user.test.ts +136 -112
- package/src/__tests__/validate-dev-props.test.ts +56 -0
- package/src/__tests__/version.test.ts +35 -0
- package/src/__tests__/whoami.test.tsx +267 -83
- package/src/__tests__/worker-namespace.test.ts +329 -0
- package/src/abort.d.ts +1 -1
- package/src/api/dev.ts +93 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +430 -157
- package/src/cfetch/index.ts +80 -80
- package/src/cfetch/internal.ts +244 -91
- package/src/cli.ts +21 -7
- package/src/config/config.ts +224 -195
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +422 -357
- package/src/config/index.ts +219 -193
- package/src/config/validation-helpers.ts +379 -357
- package/src/config/validation.ts +1690 -1367
- package/src/config-cache.ts +81 -41
- package/src/create-worker-preview.ts +208 -136
- package/src/create-worker-upload-form.ts +275 -238
- package/src/dev/dev-vars.ts +13 -13
- package/src/dev/dev.tsx +477 -351
- package/src/dev/local.tsx +426 -295
- package/src/dev/remote.tsx +406 -211
- package/src/dev/use-esbuild.ts +154 -94
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +160 -0
- package/src/dev.tsx +696 -0
- package/src/dialogs.tsx +97 -97
- package/src/dispatch-namespace.ts +206 -0
- package/src/durable.ts +87 -87
- package/src/entry.ts +234 -228
- package/src/environment-variables.ts +23 -23
- package/src/errors.ts +6 -6
- package/src/faye-websocket.d.ts +6 -0
- package/src/generate.ts +131 -0
- package/src/git-client.ts +42 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +84 -80
- package/src/index.tsx +2164 -2768
- package/src/init.ts +622 -0
- package/src/inspect.ts +678 -593
- package/src/intl-polyfill.d.ts +123 -123
- package/src/is-ci.ts +14 -0
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +299 -277
- package/src/logger.ts +46 -39
- package/src/metrics/index.ts +5 -0
- package/src/metrics/metrics-config.ts +239 -0
- package/src/metrics/metrics-dispatcher.ts +96 -0
- package/src/metrics/metrics-usage-headers.ts +24 -0
- package/src/metrics/send-event.ts +92 -0
- package/src/miniflare-cli/assets.ts +554 -0
- package/src/miniflare-cli/enum-keys.ts +8 -8
- package/src/miniflare-cli/index.ts +198 -32
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/module-collection.ts +212 -212
- package/src/open-in-browser.ts +4 -6
- package/src/package-manager.ts +173 -128
- package/src/pages/build.tsx +238 -0
- package/src/pages/constants.ts +12 -0
- package/src/pages/deployments.tsx +106 -0
- package/src/pages/dev.tsx +528 -0
- package/src/pages/errors.ts +22 -0
- package/src/pages/functions/buildPlugin.ts +109 -0
- package/src/pages/functions/buildWorker.ts +155 -0
- package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes-consolidation.test.ts +250 -0
- package/src/pages/functions/routes-consolidation.ts +73 -0
- package/src/pages/functions/routes-transformation.test.ts +271 -0
- package/src/pages/functions/routes-transformation.ts +122 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/functions.tsx +96 -0
- package/src/pages/index.tsx +94 -0
- package/src/pages/projects.tsx +169 -0
- package/src/pages/publish.tsx +391 -0
- package/src/pages/types.ts +49 -0
- package/src/pages/upload.tsx +413 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +209 -125
- package/src/paths.ts +6 -6
- package/src/preview.ts +31 -0
- package/src/proxy.ts +410 -389
- package/src/publish.ts +773 -534
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +623 -0
- package/src/r2.ts +100 -19
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +278 -201
- package/src/tail/filters.ts +111 -109
- package/src/tail/index.ts +230 -217
- package/src/tail/printing.ts +86 -46
- package/src/update-check.ts +11 -11
- package/src/user/choose-account.tsx +69 -0
- package/src/user/env-vars.ts +46 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +3 -0
- package/src/user/user.tsx +1191 -0
- package/src/whoami.tsx +141 -42
- package/src/worker.ts +123 -99
- package/src/zones.ts +167 -0
- package/templates/checked-fetch.js +17 -0
- package/templates/first-party-worker-module-facade.ts +18 -0
- package/templates/format-dev-errors.ts +32 -0
- package/templates/new-worker-scheduled.js +3 -3
- package/templates/new-worker-scheduled.ts +15 -15
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +15 -15
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +167 -0
- package/templates/pages-template-worker.ts +172 -0
- package/templates/serve-static-assets.ts +63 -0
- package/templates/service-bindings-module-facade.js +54 -0
- package/templates/service-bindings-sw-facade.js +42 -0
- package/templates/tsconfig.json +95 -95
- package/wrangler-dist/cli.d.ts +338 -0
- package/wrangler-dist/cli.js +95449 -67524
- package/pages/functions/buildPlugin.ts +0 -105
- package/pages/functions/buildWorker.ts +0 -151
- package/pages/functions/filepath-routing.ts +0 -189
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -156
- package/pages/functions/template-plugin.ts +0 -147
- package/pages/functions/template-worker.ts +0 -143
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/pages.tsx +0 -2038
- package/src/user.tsx +0 -1203
- package/templates/static-asset-facade.js +0 -47
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
- package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
- package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
- package/vendor/wrangler-mime/CHANGELOG.md +0 -289
- package/vendor/wrangler-mime/LICENSE +0 -21
- package/vendor/wrangler-mime/Mime.js +0 -97
- package/vendor/wrangler-mime/README.md +0 -187
- package/vendor/wrangler-mime/cli.js +0 -46
- package/vendor/wrangler-mime/index.js +0 -4
- package/vendor/wrangler-mime/lite.js +0 -4
- package/vendor/wrangler-mime/package.json +0 -52
- package/vendor/wrangler-mime/types/other.js +0 -1
- package/vendor/wrangler-mime/types/standard.js +0 -1
|
@@ -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,122 @@
|
|
|
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
|
+
export function convertRoutesToGlobPatterns(
|
|
17
|
+
routes: RoutesJSONRouteInput
|
|
18
|
+
): string[] {
|
|
19
|
+
const convertedRoutes = routes.map(({ routePath, middleware }) => {
|
|
20
|
+
const globbedRoutePath: string = routePath.replace(/:\w+\*?.*/, "*");
|
|
21
|
+
|
|
22
|
+
// Middleware mountings need to end in glob so that they can handle their
|
|
23
|
+
// own sub-path routes
|
|
24
|
+
if (
|
|
25
|
+
typeof middleware === "string" ||
|
|
26
|
+
(Array.isArray(middleware) && middleware.length > 0)
|
|
27
|
+
) {
|
|
28
|
+
if (!globbedRoutePath.endsWith("*")) {
|
|
29
|
+
return toUrlPath(pathJoin(globbedRoutePath, "*"));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return toUrlPath(globbedRoutePath);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return Array.from(new Set(convertedRoutes));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Converts Functions routes like /foo/:bar to a Routing object that's used
|
|
41
|
+
* to determine if a request should run in the Functions user-worker.
|
|
42
|
+
* Also consolidates redundant routes such as [/foo/bar, /foo/:bar] -> /foo/*
|
|
43
|
+
*
|
|
44
|
+
* @returns RoutesJSONSpec to be written to _routes.json
|
|
45
|
+
*/
|
|
46
|
+
export function convertRoutesToRoutesJSONSpec(
|
|
47
|
+
routes: RoutesJSONRouteInput
|
|
48
|
+
): RoutesJSONSpec {
|
|
49
|
+
// The initial routes coming in are sorted most-specific to least-specific.
|
|
50
|
+
// The order doesn't have any affect on the output of this function, but
|
|
51
|
+
// it should speed up route consolidation with less-specific routes being first.
|
|
52
|
+
const reversedRoutes = [...routes].reverse();
|
|
53
|
+
const include = convertRoutesToGlobPatterns(reversedRoutes);
|
|
54
|
+
return optimizeRoutesJSONSpec({
|
|
55
|
+
version: ROUTES_SPEC_VERSION,
|
|
56
|
+
include,
|
|
57
|
+
exclude: [],
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Optimizes and returns a new Routes JSON Spec instance performing
|
|
63
|
+
* de-duping, consolidation, truncation, and sorting
|
|
64
|
+
*/
|
|
65
|
+
export function optimizeRoutesJSONSpec(spec: RoutesJSONSpec): RoutesJSONSpec {
|
|
66
|
+
const optimizedSpec = { ...spec };
|
|
67
|
+
|
|
68
|
+
let consolidatedRoutes = consolidateRoutes(optimizedSpec.include);
|
|
69
|
+
if (consolidatedRoutes.length > MAX_FUNCTIONS_ROUTES_RULES) {
|
|
70
|
+
consolidatedRoutes = ["/*"];
|
|
71
|
+
}
|
|
72
|
+
// Sort so that least-specific routes are first
|
|
73
|
+
consolidatedRoutes.sort((a, b) => compareRoutes(b, a));
|
|
74
|
+
|
|
75
|
+
optimizedSpec.include = consolidatedRoutes;
|
|
76
|
+
|
|
77
|
+
return optimizedSpec;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Simplified routes comparison (copied from the one in filepath-routing.)
|
|
82
|
+
* This version will sort most-specific to least-specific, but the input is simplified
|
|
83
|
+
* routes like /foo/*, /foo, etc
|
|
84
|
+
*/
|
|
85
|
+
export function compareRoutes(routeA: string, routeB: string) {
|
|
86
|
+
function parseRoutePath(routePath: string): string[] {
|
|
87
|
+
return routePath.slice(1).split("/").filter(Boolean);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const segmentsA = parseRoutePath(routeA);
|
|
91
|
+
const segmentsB = parseRoutePath(routeB);
|
|
92
|
+
|
|
93
|
+
// sort routes with fewer segments after those with more segments
|
|
94
|
+
if (segmentsA.length !== segmentsB.length) {
|
|
95
|
+
return segmentsB.length - segmentsA.length;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (let i = 0; i < segmentsA.length; i++) {
|
|
99
|
+
const isWildcardA = segmentsA[i].includes("*");
|
|
100
|
+
const isWildcardB = segmentsB[i].includes("*");
|
|
101
|
+
|
|
102
|
+
// sort wildcard segments after non-wildcard segments
|
|
103
|
+
if (isWildcardA && !isWildcardB) return 1;
|
|
104
|
+
if (!isWildcardA && isWildcardB) return -1;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// all else equal, just sort the paths lexicographically
|
|
108
|
+
return routeA.localeCompare(routeB);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function isRoutesJSONSpec(data: unknown): data is RoutesJSONSpec {
|
|
112
|
+
return (
|
|
113
|
+
(typeof data === "object" &&
|
|
114
|
+
data &&
|
|
115
|
+
"version" in data &&
|
|
116
|
+
typeof (data as RoutesJSONSpec).version === "number" &&
|
|
117
|
+
(data as RoutesJSONSpec).version === ROUTES_SPEC_VERSION &&
|
|
118
|
+
Array.isArray((data as RoutesJSONSpec).include) &&
|
|
119
|
+
Array.isArray((data as RoutesJSONSpec).exclude)) ||
|
|
120
|
+
false
|
|
121
|
+
);
|
|
122
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isValidIdentifier, normalizeIdentifier } from "./identifiers";
|
|
4
|
+
import type { UrlPath } from "../../paths";
|
|
5
|
+
|
|
6
|
+
export const HTTP_METHODS = [
|
|
7
|
+
"HEAD",
|
|
8
|
+
"OPTIONS",
|
|
9
|
+
"GET",
|
|
10
|
+
"POST",
|
|
11
|
+
"PUT",
|
|
12
|
+
"PATCH",
|
|
13
|
+
"DELETE",
|
|
14
|
+
] as const;
|
|
15
|
+
export type HTTPMethod = typeof HTTP_METHODS[number];
|
|
16
|
+
|
|
17
|
+
export type RoutesCollection = Array<{
|
|
18
|
+
routePath: UrlPath;
|
|
19
|
+
mountPath: UrlPath;
|
|
20
|
+
method?: HTTPMethod;
|
|
21
|
+
modules: string[];
|
|
22
|
+
middlewares: string[];
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
export type Config = {
|
|
26
|
+
routes?: RouteConfig[];
|
|
27
|
+
schedules?: unknown;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type RouteConfig = {
|
|
31
|
+
routePath: UrlPath;
|
|
32
|
+
mountPath: UrlPath;
|
|
33
|
+
method?: HTTPMethod;
|
|
34
|
+
middleware?: string | string[];
|
|
35
|
+
module?: string | string[];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type ImportMap = Map<
|
|
39
|
+
string,
|
|
40
|
+
{
|
|
41
|
+
filepath: string;
|
|
42
|
+
name: string;
|
|
43
|
+
identifier: string;
|
|
44
|
+
}
|
|
45
|
+
>;
|
|
46
|
+
|
|
47
|
+
type Arguments = {
|
|
48
|
+
config: Config;
|
|
49
|
+
outfile: string;
|
|
50
|
+
srcDir: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export async function writeRoutesModule({
|
|
54
|
+
config,
|
|
55
|
+
srcDir,
|
|
56
|
+
outfile = "_routes.js",
|
|
57
|
+
}: Arguments) {
|
|
58
|
+
const { importMap, routes } = parseConfig(config, srcDir);
|
|
59
|
+
const routesModule = generateRoutesModule(importMap, routes);
|
|
60
|
+
|
|
61
|
+
await fs.writeFile(outfile, routesModule);
|
|
62
|
+
|
|
63
|
+
return outfile;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function parseConfig(config: Config, baseDir: string) {
|
|
67
|
+
const routes: RoutesCollection = [];
|
|
68
|
+
const importMap: ImportMap = new Map();
|
|
69
|
+
const identifierCount = new Map<string, number>(); // to keep track of identifier collisions
|
|
70
|
+
|
|
71
|
+
function parseModuleIdentifiers(paths: string | string[] | undefined) {
|
|
72
|
+
if (typeof paths === "undefined") {
|
|
73
|
+
paths = [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (typeof paths === "string") {
|
|
77
|
+
paths = [paths];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return paths.map((modulePath) => {
|
|
81
|
+
const [filepath, name = "default"] = modulePath.split(":");
|
|
82
|
+
let { identifier } = importMap.get(modulePath) ?? {};
|
|
83
|
+
|
|
84
|
+
const resolvedPath = path.resolve(baseDir, filepath);
|
|
85
|
+
|
|
86
|
+
// ensure the filepath isn't attempting to resolve to anything outside of the project
|
|
87
|
+
if (path.relative(baseDir, resolvedPath).startsWith("..")) {
|
|
88
|
+
throw new Error(`Invalid module path "${filepath}"`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ensure the module name (if provided) is a valid identifier to guard against injection attacks
|
|
92
|
+
if (name !== "default" && !isValidIdentifier(name)) {
|
|
93
|
+
throw new Error(`Invalid module identifier "${name}"`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!identifier) {
|
|
97
|
+
identifier = normalizeIdentifier(`__${filepath}_${name}`);
|
|
98
|
+
|
|
99
|
+
let count = identifierCount.get(identifier) ?? 0;
|
|
100
|
+
identifierCount.set(identifier, ++count);
|
|
101
|
+
|
|
102
|
+
if (count > 1) {
|
|
103
|
+
identifier += `_${count}`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
importMap.set(modulePath, { filepath: resolvedPath, name, identifier });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return identifier;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
for (const { routePath, mountPath, method, ...props } of config.routes ??
|
|
114
|
+
[]) {
|
|
115
|
+
routes.push({
|
|
116
|
+
routePath,
|
|
117
|
+
mountPath,
|
|
118
|
+
method,
|
|
119
|
+
middlewares: parseModuleIdentifiers(props.middleware),
|
|
120
|
+
modules: parseModuleIdentifiers(props.module),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { routes, importMap };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function generateRoutesModule(
|
|
128
|
+
importMap: ImportMap,
|
|
129
|
+
routes: RoutesCollection
|
|
130
|
+
) {
|
|
131
|
+
return `${[...importMap.values()]
|
|
132
|
+
.map(
|
|
133
|
+
({ filepath, name, identifier }) =>
|
|
134
|
+
`import { ${name} as ${identifier} } from ${JSON.stringify(filepath)}`
|
|
135
|
+
)
|
|
136
|
+
.join("\n")}
|
|
137
|
+
|
|
138
|
+
export const routes = [
|
|
139
|
+
${routes
|
|
140
|
+
.map(
|
|
141
|
+
(route) => ` {
|
|
142
|
+
routePath: "${route.routePath}",
|
|
143
|
+
mountPath: "${route.mountPath}",
|
|
144
|
+
method: "${route.method}",
|
|
145
|
+
middlewares: [${route.middlewares.join(", ")}],
|
|
146
|
+
modules: [${route.modules.join(", ")}],
|
|
147
|
+
},`
|
|
148
|
+
)
|
|
149
|
+
.join("\n")}
|
|
150
|
+
]`;
|
|
151
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { FatalError } from "../errors";
|
|
4
|
+
import { logger } from "../logger";
|
|
5
|
+
import { isInPagesCI, ROUTES_SPEC_VERSION } from "./constants";
|
|
6
|
+
import {
|
|
7
|
+
isRoutesJSONSpec,
|
|
8
|
+
optimizeRoutesJSONSpec,
|
|
9
|
+
} from "./functions/routes-transformation";
|
|
10
|
+
import { pagesBetaWarning } from "./utils";
|
|
11
|
+
import type { YargsOptionsToInterface } from "./types";
|
|
12
|
+
import type { Argv } from "yargs";
|
|
13
|
+
|
|
14
|
+
type OptimizeRoutesArgs = YargsOptionsToInterface<typeof OptimizeRoutesOptions>;
|
|
15
|
+
|
|
16
|
+
export function OptimizeRoutesOptions(yargs: Argv) {
|
|
17
|
+
return yargs
|
|
18
|
+
.options({
|
|
19
|
+
"routes-path": {
|
|
20
|
+
type: "string",
|
|
21
|
+
demandOption: true,
|
|
22
|
+
description: "The location of the _routes.json file",
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
.options({
|
|
26
|
+
"output-routes-path": {
|
|
27
|
+
type: "string",
|
|
28
|
+
demandOption: true,
|
|
29
|
+
description: "The location of the optimized output routes file",
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function OptimizeRoutesHandler({
|
|
35
|
+
routesPath,
|
|
36
|
+
outputRoutesPath,
|
|
37
|
+
}: OptimizeRoutesArgs) {
|
|
38
|
+
if (!isInPagesCI) {
|
|
39
|
+
// Beta message for `wrangler pages <commands>` usage
|
|
40
|
+
logger.log(pagesBetaWarning);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let routesFileContents: string;
|
|
44
|
+
const routesOutputDirectory = path.dirname(outputRoutesPath);
|
|
45
|
+
|
|
46
|
+
if (!existsSync(routesPath)) {
|
|
47
|
+
throw new FatalError(
|
|
48
|
+
`Oops! File ${routesPath} does not exist. Please make sure --routes-path is a valid file path (for example "/public/_routes.json").`,
|
|
49
|
+
1
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (
|
|
54
|
+
!existsSync(routesOutputDirectory) ||
|
|
55
|
+
!lstatSync(routesOutputDirectory).isDirectory()
|
|
56
|
+
) {
|
|
57
|
+
throw new FatalError(
|
|
58
|
+
`Oops! Folder ${routesOutputDirectory} does not exist. Please make sure --output-routes-path is a valid file path (for example "/public/_routes.json").`,
|
|
59
|
+
1
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
routesFileContents = readFileSync(routesPath, "utf-8");
|
|
65
|
+
} catch (err) {
|
|
66
|
+
throw new FatalError(`Error while reading ${routesPath} file: ${err}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const routes = JSON.parse(routesFileContents);
|
|
70
|
+
|
|
71
|
+
if (!isRoutesJSONSpec(routes)) {
|
|
72
|
+
throw new FatalError(
|
|
73
|
+
`
|
|
74
|
+
Invalid _routes.json file found at: ${routesPath}. Please make sure the JSON object has the following format:
|
|
75
|
+
{
|
|
76
|
+
version: ${ROUTES_SPEC_VERSION};
|
|
77
|
+
include: string[];
|
|
78
|
+
exclude: string[];
|
|
79
|
+
}
|
|
80
|
+
`,
|
|
81
|
+
1
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const optimizedRoutes = optimizeRoutesJSONSpec(routes);
|
|
86
|
+
const optimizedRoutesContents = JSON.stringify(optimizedRoutes);
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
writeFileSync(outputRoutesPath, optimizedRoutesContents);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
throw new FatalError(
|
|
92
|
+
`Error writing to ${outputRoutesPath} file: ${err}`,
|
|
93
|
+
1
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|