@depup/wrangler 4.75.0-depup.0

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 (44) hide show
  1. package/README.md +33 -0
  2. package/bin/wrangler.js +93 -0
  3. package/changes.json +18 -0
  4. package/config-schema.json +3222 -0
  5. package/kv-asset-handler.js +1 -0
  6. package/package.json +221 -0
  7. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  8. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  9. package/templates/__tests__/tsconfig.json +8 -0
  10. package/templates/checked-fetch.js +28 -0
  11. package/templates/facade.d.ts +19 -0
  12. package/templates/middleware/common.ts +67 -0
  13. package/templates/middleware/loader-modules.ts +134 -0
  14. package/templates/middleware/loader-sw.ts +229 -0
  15. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  16. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  17. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  18. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  19. package/templates/middleware/middleware-pretty-error.ts +40 -0
  20. package/templates/middleware/middleware-scheduled.ts +29 -0
  21. package/templates/modules-watch-stub.js +4 -0
  22. package/templates/new-worker-scheduled.js +17 -0
  23. package/templates/new-worker-scheduled.ts +32 -0
  24. package/templates/new-worker.js +15 -0
  25. package/templates/new-worker.ts +33 -0
  26. package/templates/no-op-worker.js +10 -0
  27. package/templates/pages-dev-pipeline.ts +33 -0
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +9 -0
  30. package/templates/pages-template-plugin.ts +190 -0
  31. package/templates/pages-template-worker.ts +198 -0
  32. package/templates/remoteBindings/ProxyServerWorker.ts +143 -0
  33. package/templates/remoteBindings/wrangler.jsonc +4 -0
  34. package/templates/startDevWorker/InspectorProxyWorker.ts +699 -0
  35. package/templates/startDevWorker/ProxyWorker.ts +340 -0
  36. package/templates/tsconfig-sanity.ts +11 -0
  37. package/templates/tsconfig.init.json +22 -0
  38. package/templates/tsconfig.json +14 -0
  39. package/wrangler-dist/InspectorProxyWorker.js +486 -0
  40. package/wrangler-dist/ProxyServerWorker.js +3314 -0
  41. package/wrangler-dist/ProxyWorker.js +238 -0
  42. package/wrangler-dist/cli.d.ts +3154 -0
  43. package/wrangler-dist/cli.js +303399 -0
  44. package/wrangler-dist/metafile-cjs.json +1 -0
@@ -0,0 +1,229 @@
1
+ import {
2
+ __facade_invoke__,
3
+ __facade_register__,
4
+ __facade_registerInternal__,
5
+ Awaitable,
6
+ Dispatcher,
7
+ IncomingRequest,
8
+ Middleware,
9
+ } from "./common";
10
+
11
+ export { __facade_register__, __facade_registerInternal__ };
12
+
13
+ // Miniflare 2's `EventTarget` follows the spec and doesn't allow exceptions to
14
+ // be caught by `dispatchEvent`. Instead it has a custom `ThrowingEventTarget`
15
+ // class that rethrows errors from event listeners in `dispatchEvent`.
16
+ // We'd like errors to be propagated to the top-level `addEventListener`, so
17
+ // we'd like to use `ThrowingEventTarget`. Unfortunately, `ThrowingEventTarget`
18
+ // isn't exposed on the global scope, but `WorkerGlobalScope` (which extends
19
+ // `ThrowingEventTarget`) is. Therefore, we get at it in this nasty way.
20
+ let __FACADE_EVENT_TARGET__: EventTarget;
21
+ if ((globalThis as any).MINIFLARE) {
22
+ __FACADE_EVENT_TARGET__ = new (Object.getPrototypeOf(WorkerGlobalScope))();
23
+ } else {
24
+ __FACADE_EVENT_TARGET__ = new EventTarget();
25
+ }
26
+
27
+ function __facade_isSpecialEvent__(
28
+ type: string
29
+ ): type is "fetch" | "scheduled" {
30
+ return type === "fetch" || type === "scheduled";
31
+ }
32
+ const __facade__originalAddEventListener__ = globalThis.addEventListener;
33
+ const __facade__originalRemoveEventListener__ = globalThis.removeEventListener;
34
+ const __facade__originalDispatchEvent__ = globalThis.dispatchEvent;
35
+
36
+ globalThis.addEventListener = function (type, listener, options) {
37
+ if (__facade_isSpecialEvent__(type)) {
38
+ __FACADE_EVENT_TARGET__.addEventListener(
39
+ type,
40
+ listener as EventListenerOrEventListenerObject,
41
+ options
42
+ );
43
+ } else {
44
+ __facade__originalAddEventListener__(type, listener, options);
45
+ }
46
+ };
47
+ globalThis.removeEventListener = function (type, listener, options) {
48
+ if (__facade_isSpecialEvent__(type)) {
49
+ __FACADE_EVENT_TARGET__.removeEventListener(
50
+ type,
51
+ listener as EventListenerOrEventListenerObject,
52
+ options
53
+ );
54
+ } else {
55
+ __facade__originalRemoveEventListener__(type, listener, options);
56
+ }
57
+ };
58
+ globalThis.dispatchEvent = function (event) {
59
+ if (__facade_isSpecialEvent__(event.type)) {
60
+ return __FACADE_EVENT_TARGET__.dispatchEvent(event);
61
+ } else {
62
+ return __facade__originalDispatchEvent__(event);
63
+ }
64
+ };
65
+
66
+ declare global {
67
+ var addMiddleware: typeof __facade_register__;
68
+ var addMiddlewareInternal: typeof __facade_registerInternal__;
69
+ }
70
+ globalThis.addMiddleware = __facade_register__;
71
+ globalThis.addMiddlewareInternal = __facade_registerInternal__;
72
+
73
+ const __facade_waitUntil__ = Symbol("__facade_waitUntil__");
74
+ const __facade_response__ = Symbol("__facade_response__");
75
+ const __facade_dispatched__ = Symbol("__facade_dispatched__");
76
+
77
+ class __Facade_ExtendableEvent__ extends Event {
78
+ [__facade_waitUntil__]: Awaitable<unknown>[] = [];
79
+
80
+ waitUntil(promise: Awaitable<any>) {
81
+ if (!(this instanceof __Facade_ExtendableEvent__)) {
82
+ throw new TypeError("Illegal invocation");
83
+ }
84
+ this[__facade_waitUntil__].push(promise);
85
+ }
86
+ }
87
+
88
+ interface FetchEventInit extends EventInit {
89
+ request: Request;
90
+ passThroughOnException: FetchEvent["passThroughOnException"];
91
+ }
92
+
93
+ class __Facade_FetchEvent__ extends __Facade_ExtendableEvent__ {
94
+ #request: Request;
95
+ #passThroughOnException: FetchEvent["passThroughOnException"];
96
+ [__facade_response__]?: Awaitable<Response>;
97
+ [__facade_dispatched__] = false;
98
+
99
+ constructor(type: "fetch", init: FetchEventInit) {
100
+ super(type);
101
+ this.#request = init.request;
102
+ this.#passThroughOnException = init.passThroughOnException;
103
+ }
104
+
105
+ get request() {
106
+ return this.#request;
107
+ }
108
+
109
+ respondWith(response: Awaitable<Response>) {
110
+ if (!(this instanceof __Facade_FetchEvent__)) {
111
+ throw new TypeError("Illegal invocation");
112
+ }
113
+ if (this[__facade_response__] !== undefined) {
114
+ throw new DOMException(
115
+ "FetchEvent.respondWith() has already been called; it can only be called once.",
116
+ "InvalidStateError"
117
+ );
118
+ }
119
+ if (this[__facade_dispatched__]) {
120
+ throw new DOMException(
121
+ "Too late to call FetchEvent.respondWith(). It must be called synchronously in the event handler.",
122
+ "InvalidStateError"
123
+ );
124
+ }
125
+ this.stopImmediatePropagation();
126
+ this[__facade_response__] = response;
127
+ }
128
+
129
+ passThroughOnException() {
130
+ if (!(this instanceof __Facade_FetchEvent__)) {
131
+ throw new TypeError("Illegal invocation");
132
+ }
133
+ // Need to call native method immediately in case uncaught error thrown
134
+ this.#passThroughOnException();
135
+ }
136
+ }
137
+
138
+ interface ScheduledEventInit extends EventInit {
139
+ scheduledTime: number;
140
+ cron: string;
141
+ noRetry: ScheduledEvent["noRetry"];
142
+ }
143
+
144
+ class __Facade_ScheduledEvent__ extends __Facade_ExtendableEvent__ {
145
+ #scheduledTime: number;
146
+ #cron: string;
147
+ #noRetry: ScheduledEvent["noRetry"];
148
+
149
+ constructor(type: "scheduled", init: ScheduledEventInit) {
150
+ super(type);
151
+ this.#scheduledTime = init.scheduledTime;
152
+ this.#cron = init.cron;
153
+ this.#noRetry = init.noRetry;
154
+ }
155
+
156
+ get scheduledTime() {
157
+ return this.#scheduledTime;
158
+ }
159
+
160
+ get cron() {
161
+ return this.#cron;
162
+ }
163
+
164
+ noRetry() {
165
+ if (!(this instanceof __Facade_ScheduledEvent__)) {
166
+ throw new TypeError("Illegal invocation");
167
+ }
168
+ // Need to call native method immediately in case uncaught error thrown
169
+ this.#noRetry();
170
+ }
171
+ }
172
+
173
+ __facade__originalAddEventListener__("fetch", (event) => {
174
+ const ctx: ExecutionContext = {
175
+ waitUntil: event.waitUntil.bind(event),
176
+ passThroughOnException: event.passThroughOnException.bind(event),
177
+ };
178
+
179
+ const __facade_sw_dispatch__: Dispatcher = function (type, init) {
180
+ if (type === "scheduled") {
181
+ const facadeEvent = new __Facade_ScheduledEvent__("scheduled", {
182
+ scheduledTime: Date.now(),
183
+ cron: init.cron ?? "",
184
+ noRetry() {},
185
+ });
186
+
187
+ __FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
188
+ event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
189
+ }
190
+ };
191
+
192
+ const __facade_sw_fetch__: Middleware = function (request, _env, ctx) {
193
+ const facadeEvent = new __Facade_FetchEvent__("fetch", {
194
+ request,
195
+ passThroughOnException: ctx.passThroughOnException,
196
+ });
197
+
198
+ __FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
199
+ facadeEvent[__facade_dispatched__] = true;
200
+ event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
201
+
202
+ const response = facadeEvent[__facade_response__];
203
+ if (response === undefined) {
204
+ throw new Error("No response!"); // TODO: proper error message
205
+ }
206
+ return response;
207
+ };
208
+
209
+ event.respondWith(
210
+ __facade_invoke__(
211
+ event.request as IncomingRequest,
212
+ globalThis,
213
+ ctx,
214
+ __facade_sw_dispatch__,
215
+ __facade_sw_fetch__
216
+ )
217
+ );
218
+ });
219
+
220
+ __facade__originalAddEventListener__("scheduled", (event) => {
221
+ const facadeEvent = new __Facade_ScheduledEvent__("scheduled", {
222
+ scheduledTime: event.scheduledTime,
223
+ cron: event.cron,
224
+ noRetry: event.noRetry.bind(event),
225
+ });
226
+
227
+ __FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
228
+ event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
229
+ });
@@ -0,0 +1,18 @@
1
+ import type { Middleware } from "./common";
2
+
3
+ const drainBody: Middleware = async (request, env, _ctx, middlewareCtx) => {
4
+ try {
5
+ return await middlewareCtx.next(request, env);
6
+ } finally {
7
+ try {
8
+ if (request.body !== null && !request.bodyUsed) {
9
+ const reader = request.body.getReader();
10
+ while (!(await reader.read()).done) {}
11
+ }
12
+ } catch (e) {
13
+ console.error("Failed to drain the unused request body.", e);
14
+ }
15
+ }
16
+ };
17
+
18
+ export default drainBody;
@@ -0,0 +1,32 @@
1
+ import type { Middleware } from "./common";
2
+
3
+ interface JsonError {
4
+ message?: string;
5
+ name?: string;
6
+ stack?: string;
7
+ cause?: JsonError;
8
+ }
9
+
10
+ function reduceError(e: any): JsonError {
11
+ return {
12
+ name: e?.name,
13
+ message: e?.message ?? String(e),
14
+ stack: e?.stack,
15
+ cause: e?.cause === undefined ? undefined : reduceError(e.cause),
16
+ };
17
+ }
18
+
19
+ // See comment in `bundle.ts` for details on why this is needed
20
+ const jsonError: Middleware = async (request, env, _ctx, middlewareCtx) => {
21
+ try {
22
+ return await middlewareCtx.next(request, env);
23
+ } catch (e: any) {
24
+ const error = reduceError(e);
25
+ return Response.json(error, {
26
+ status: 500,
27
+ headers: { "MF-Experimental-Error-Stack": "true" },
28
+ });
29
+ }
30
+ };
31
+
32
+ export default jsonError;
@@ -0,0 +1,3 @@
1
+ declare module "config:middleware/patch-console-prefix" {
2
+ export const prefix: string;
3
+ }
@@ -0,0 +1,21 @@
1
+ /// <reference path="middleware-patch-console-prefix.d.ts"/>
2
+
3
+ import { prefix } from "config:middleware/patch-console-prefix";
4
+ import type { Middleware } from "./common";
5
+
6
+ // @ts-expect-error globalThis.console _does_ exist
7
+ globalThis.console = new Proxy(globalThis.console, {
8
+ get(target, p, receiver) {
9
+ if (p === "log" || p === "debug" || p === "info") {
10
+ return (...args: unknown[]) =>
11
+ Reflect.get(target, p, receiver)(prefix, ...args);
12
+ }
13
+ return Reflect.get(target, p, receiver);
14
+ },
15
+ });
16
+
17
+ const passthrough: Middleware = (request, env, _ctx, middlewareCtx) => {
18
+ return middlewareCtx.next(request, env);
19
+ };
20
+
21
+ export default passthrough;
@@ -0,0 +1,40 @@
1
+ import type { Middleware } from "./common";
2
+
3
+ // A middleware has to be a function of type Middleware
4
+ const prettyError: Middleware = async (request, env, _ctx, middlewareCtx) => {
5
+ try {
6
+ const response = await middlewareCtx.next(request, env);
7
+ return response;
8
+ } catch (e: any) {
9
+ const html = `
10
+ <!DOCTYPE html>
11
+ <html lang="en">
12
+ <head>
13
+ <meta charset="UTF-8">
14
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
15
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
16
+ <title>Error 🚨</title>
17
+ <style>
18
+ pre {
19
+ margin: 16px auto;
20
+ max-width: 600px;
21
+ background-color: #eeeeee;
22
+ border-radius: 4px;
23
+ padding: 16px;
24
+ }
25
+ </style>
26
+ </head>
27
+ <body>
28
+ <pre>${e.stack}</pre>
29
+ </body>
30
+ </html>
31
+ `;
32
+
33
+ return new Response(html, {
34
+ status: 500,
35
+ headers: { "Content-Type": "text/html;charset=utf-8" },
36
+ });
37
+ }
38
+ };
39
+
40
+ export default prettyError;
@@ -0,0 +1,29 @@
1
+ import type { Middleware } from "./common";
2
+
3
+ // A middleware has to be a function of type Middleware
4
+ const scheduled: Middleware = async (request, env, _ctx, middlewareCtx) => {
5
+ const url = new URL(request.url);
6
+ if (url.pathname === "/__scheduled") {
7
+ const cron = url.searchParams.get("cron") ?? "";
8
+ await middlewareCtx.dispatch("scheduled", { cron });
9
+
10
+ return new Response("Ran scheduled event");
11
+ }
12
+
13
+ const resp = await middlewareCtx.next(request, env);
14
+
15
+ // If you open the `/__scheduled` page in a browser, the browser will automatically make a request to `/favicon.ico`.
16
+ // For scheduled Workers _without_ a fetch handler, this will result in a 500 response that clutters the log with unhelpful error messages.
17
+ // To avoid this, inject a 404 response to favicon.ico loads on the `/__scheduled` page
18
+ if (
19
+ request.headers.get("referer")?.endsWith("/__scheduled") &&
20
+ url.pathname === "/favicon.ico" &&
21
+ resp.status === 500
22
+ ) {
23
+ return new Response(null, { status: 404 });
24
+ }
25
+
26
+ return resp;
27
+ };
28
+
29
+ export default scheduled;
@@ -0,0 +1,4 @@
1
+ // `esbuild` doesn't support returning `watch*` options from `onStart()`
2
+ // plugin callbacks. Instead, we define an empty virtual module that is
3
+ // imported by this injected file. Importing the module registers watchers.
4
+ import "wrangler:modules-watch";
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first scheduled worker.
3
+ *
4
+ * - Run `wrangler dev` in your terminal to start a development server
5
+ * - Run `curl "http://localhost:8787/cdn-cgi/handler/scheduled"` to trigger the scheduled event
6
+ * - Go back to the console to see what your worker has logged
7
+ * - Update the Cron trigger in wrangler.toml (see https://developers.cloudflare.com/workers/configuration/cron-triggers/)
8
+ * - Run `wrangler publish --name my-worker` to publish your worker
9
+ *
10
+ * Learn more at https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/
11
+ */
12
+
13
+ export default {
14
+ async scheduled(controller, env, ctx) {
15
+ console.log(`Hello World!`);
16
+ },
17
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first scheduled worker.
3
+ *
4
+ * - Run `wrangler dev` in your terminal to start a development server
5
+ * - Run `curl "http://localhost:8787/cdn-cgi/handler/scheduled"` to trigger the scheduled event
6
+ * - Go back to the console to see what your worker has logged
7
+ * - Update the Cron trigger in wrangler.toml (see https://developers.cloudflare.com/workers/configuration/cron-triggers/)
8
+ * - Run `wrangler deploy --name my-worker` to deploy your worker
9
+ *
10
+ * Learn more at https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/
11
+ */
12
+
13
+ export interface Env {
14
+ // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
15
+ // MY_KV_NAMESPACE: KVNamespace;
16
+ //
17
+ // Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
18
+ // MY_DURABLE_OBJECT: DurableObjectNamespace;
19
+ //
20
+ // Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
21
+ // MY_BUCKET: R2Bucket;
22
+ }
23
+
24
+ export default {
25
+ async scheduled(
26
+ controller: ScheduledController,
27
+ env: Env,
28
+ ctx: ExecutionContext
29
+ ): Promise<void> {
30
+ console.log(`Hello World!`);
31
+ },
32
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first worker.
3
+ *
4
+ * - Run `npx wrangler dev src/index.js` in your terminal to start a development server
5
+ * - Open a browser tab at http://localhost:8787/ to see your worker in action
6
+ * - Run `npx wrangler publish src/index.js --name my-worker` to publish your worker
7
+ *
8
+ * Learn more at https://developers.cloudflare.com/workers/
9
+ */
10
+
11
+ export default {
12
+ async fetch(request, env, ctx) {
13
+ return new Response("Hello World!");
14
+ },
15
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first worker.
3
+ *
4
+ * - Run `wrangler dev src/index.ts` in your terminal to start a development server
5
+ * - Open a browser tab at http://localhost:8787/ to see your worker in action
6
+ * - Run `wrangler deploy src/index.ts --name my-worker` to deploy your worker
7
+ *
8
+ * Learn more at https://developers.cloudflare.com/workers/
9
+ */
10
+
11
+ export interface Env {
12
+ // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
13
+ // MY_KV_NAMESPACE: KVNamespace;
14
+ //
15
+ // Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
16
+ // MY_DURABLE_OBJECT: DurableObjectNamespace;
17
+ //
18
+ // Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
19
+ // MY_BUCKET: R2Bucket;
20
+ //
21
+ // Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
22
+ // MY_SERVICE: Fetcher;
23
+ }
24
+
25
+ export default {
26
+ async fetch(
27
+ request: Request,
28
+ env: Env,
29
+ ctx: ExecutionContext
30
+ ): Promise<Response> {
31
+ return new Response("Hello World!");
32
+ },
33
+ };
@@ -0,0 +1,10 @@
1
+ export default {
2
+ fetch() {
3
+ return new Response("Not found", {
4
+ status: 404,
5
+ headers: {
6
+ "Content-Type": "text/html",
7
+ },
8
+ });
9
+ },
10
+ };
@@ -0,0 +1,33 @@
1
+ // @ts-ignore entry point will get replaced
2
+ import worker from "__ENTRY_POINT__";
3
+ import { isRoutingRuleMatch } from "./pages-dev-util";
4
+
5
+ // @ts-ignore entry point will get replaced
6
+ export * from "__ENTRY_POINT__";
7
+
8
+ // @ts-ignore routes are injected
9
+ const routes = __ROUTES__;
10
+
11
+ export default <ExportedHandler<{ ASSETS: Fetcher }>>{
12
+ fetch(request, env, context) {
13
+ const { pathname } = new URL(request.url);
14
+
15
+ for (const exclude of routes.exclude) {
16
+ if (isRoutingRuleMatch(pathname, exclude)) {
17
+ return env.ASSETS.fetch(request);
18
+ }
19
+ }
20
+
21
+ for (const include of routes.include) {
22
+ if (isRoutingRuleMatch(pathname, include)) {
23
+ const workerAsHandler = worker as ExportedHandler;
24
+ if (workerAsHandler.fetch === undefined) {
25
+ throw new TypeError("Entry point missing `fetch` handler");
26
+ }
27
+ return workerAsHandler.fetch(request, env, context);
28
+ }
29
+ }
30
+
31
+ return env.ASSETS.fetch(request);
32
+ },
33
+ };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @param pathname A pathname string, such as `/foo` or `/foo/bar`
3
+ * @param routingRule The routing rule, such as `/foo/*`
4
+ * @returns True if pathname matches the routing rule
5
+ *
6
+ * / -> /
7
+ * /* -> /*
8
+ * /foo -> /foo
9
+ * /foo* -> /foo, /foo-bar, /foo/*
10
+ * /foo/* -> /foo, /foo/bar
11
+ */
12
+ export function isRoutingRuleMatch(
13
+ pathname: string,
14
+ routingRule: string
15
+ ): boolean {
16
+ // sanity checks
17
+ if (!pathname) {
18
+ throw new Error("Pathname is undefined.");
19
+ }
20
+ if (!routingRule) {
21
+ throw new Error("Routing rule is undefined.");
22
+ }
23
+
24
+ const ruleRegExp = transformRoutingRuleToRegExp(routingRule);
25
+ return pathname.match(ruleRegExp) !== null;
26
+ }
27
+
28
+ function transformRoutingRuleToRegExp(rule: string): RegExp {
29
+ let transformedRule;
30
+
31
+ if (rule === "/" || rule === "/*") {
32
+ transformedRule = rule;
33
+ } else if (rule.endsWith("/*")) {
34
+ // make `/*` an optional group so we can match both /foo/* and /foo
35
+ // /foo/* => /foo(/*)?
36
+ transformedRule = `${rule.substring(0, rule.length - 2)}(/*)?`;
37
+ } else if (rule.endsWith("/")) {
38
+ // make `/` an optional group so we can match both /foo/ and /foo
39
+ // /foo/ => /foo(/)?
40
+ transformedRule = `${rule.substring(0, rule.length - 1)}(/)?`;
41
+ } else if (rule.endsWith("*")) {
42
+ transformedRule = rule;
43
+ } else {
44
+ transformedRule = `${rule}(/)?`;
45
+ }
46
+
47
+ // /foo* => /foo.* => ^/foo.*$
48
+ // /*.* => /*\.* => /.*\..* => ^/.*\..*$
49
+ transformedRule = `^${transformedRule
50
+ .replaceAll(/\./g, "\\.")
51
+ .replaceAll(/\*/g, ".*")}$`;
52
+
53
+ // ^/foo.*$ => /^\/foo.*$/
54
+ return new RegExp(transformedRule);
55
+ }
@@ -0,0 +1,9 @@
1
+ // This Worker is used as a default when no Pages Functions are present.
2
+ // It proxies the request directly on to the asset server binding.
3
+
4
+ export default <ExportedHandler<{ ASSETS: Fetcher }>>{
5
+ async fetch(request, env, context) {
6
+ const response = await env.ASSETS.fetch(request.url, request);
7
+ return new Response(response.body, response);
8
+ },
9
+ };