@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,190 @@
1
+ import { match } from "path-to-regexp";
2
+
3
+ //note: this explicitly does not include the * character, as pages requires this
4
+ const escapeRegex = /[.+?^${}()|[\]\\]/g;
5
+
6
+ type HTTPMethod =
7
+ | "HEAD"
8
+ | "OPTIONS"
9
+ | "GET"
10
+ | "POST"
11
+ | "PUT"
12
+ | "PATCH"
13
+ | "DELETE";
14
+
15
+ /* TODO: Grab these from @cloudflare/workers-types instead */
16
+ type Params<P extends string = string> = Record<P, string | string[]>;
17
+
18
+ type EventContext<Env, P extends string, Data> = {
19
+ request: Request;
20
+ functionPath: string;
21
+ waitUntil: (promise: Promise<unknown>) => void;
22
+ passThroughOnException: () => void;
23
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
24
+ env: Env & { ASSETS: { fetch: typeof fetch } };
25
+ params: Params<P>;
26
+ data: Data;
27
+ };
28
+
29
+ type EventPluginContext<Env, P extends string, Data, PluginArgs> = {
30
+ request: Request;
31
+ functionPath: string;
32
+ waitUntil: (promise: Promise<unknown>) => void;
33
+ passThroughOnException: () => void;
34
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
35
+ env: Env & { ASSETS: { fetch: typeof fetch } };
36
+ params: Params<P>;
37
+ data: Data;
38
+ pluginArgs: PluginArgs;
39
+ };
40
+
41
+ declare type PagesFunction<
42
+ Env = unknown,
43
+ P extends string = string,
44
+ Data extends Record<string, unknown> = Record<string, unknown>,
45
+ > = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
46
+
47
+ declare type PagesPluginFunction<
48
+ Env = unknown,
49
+ P extends string = string,
50
+ Data extends Record<string, unknown> = Record<string, unknown>,
51
+ PluginArgs = unknown,
52
+ > = (
53
+ context: EventPluginContext<Env, P, Data, PluginArgs>
54
+ ) => Response | Promise<Response>;
55
+ /* end @cloudflare/workers-types */
56
+
57
+ type RouteHandler = {
58
+ routePath: string;
59
+ mountPath: string;
60
+ method?: HTTPMethod;
61
+ modules: PagesFunction[];
62
+ middlewares: PagesFunction[];
63
+ };
64
+
65
+ // inject `routes` via ESBuild
66
+ declare const routes: RouteHandler[];
67
+
68
+ function* executeRequest(request: Request, relativePathname: string) {
69
+ // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
70
+ for (const route of [...routes].reverse()) {
71
+ if (route.method && route.method !== request.method) {
72
+ continue;
73
+ }
74
+
75
+ // replaces with "\\$&", this prepends a backslash to the matched string, e.g. "[" becomes "\["
76
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
77
+ end: false,
78
+ });
79
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
80
+ end: false,
81
+ });
82
+ const matchResult = routeMatcher(relativePathname);
83
+ const mountMatchResult = mountMatcher(relativePathname);
84
+ if (matchResult && mountMatchResult) {
85
+ for (const handler of route.middlewares.flat()) {
86
+ yield {
87
+ handler,
88
+ params: matchResult.params as Params,
89
+ path: mountMatchResult.path,
90
+ };
91
+ }
92
+ }
93
+ }
94
+
95
+ // Then look for the first exact route match and execute its "modules"
96
+ for (const route of routes) {
97
+ if (route.method && route.method !== request.method) {
98
+ continue;
99
+ }
100
+
101
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
102
+ end: true,
103
+ });
104
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
105
+ end: false,
106
+ });
107
+ const matchResult = routeMatcher(relativePathname);
108
+ const mountMatchResult = mountMatcher(relativePathname);
109
+ if (matchResult && mountMatchResult && route.modules.length) {
110
+ for (const handler of route.modules.flat()) {
111
+ yield {
112
+ handler,
113
+ params: matchResult.params as Params,
114
+ path: matchResult.path,
115
+ };
116
+ }
117
+ break;
118
+ }
119
+ }
120
+ }
121
+
122
+ export default function (pluginArgs: unknown) {
123
+ const onRequest: PagesPluginFunction = async (workerContext) => {
124
+ let { request } = workerContext;
125
+ const { env, next } = workerContext;
126
+ let { data } = workerContext;
127
+
128
+ const url = new URL(request.url);
129
+ // TODO: Replace this with something actually legible.
130
+ const relativePathname = `/${
131
+ url.pathname.replace(workerContext.functionPath, "") || ""
132
+ }`.replace(/^\/\//, "/");
133
+
134
+ const handlerIterator = executeRequest(request, relativePathname);
135
+ const pluginNext = async (input?: RequestInfo, init?: RequestInit) => {
136
+ if (input !== undefined) {
137
+ let url = input;
138
+ if (typeof input === "string") {
139
+ url = new URL(input, request.url).toString();
140
+ }
141
+ request = new Request(url, init);
142
+ }
143
+
144
+ const result = handlerIterator.next();
145
+ // Note we can't use `!result.done` because this doesn't narrow to the correct type
146
+ if (result.done === false) {
147
+ const { handler, params, path } = result.value;
148
+ const context = {
149
+ request: new Request(request.clone()),
150
+ functionPath: workerContext.functionPath + path,
151
+ next: pluginNext,
152
+ params,
153
+ get data() {
154
+ return data;
155
+ },
156
+ set data(value) {
157
+ if (typeof value !== "object" || value === null) {
158
+ throw new Error("context.data must be an object");
159
+ }
160
+ // user has overriden context.data, so we need to merge it with the existing data
161
+ data = value;
162
+ },
163
+ pluginArgs,
164
+ env,
165
+ waitUntil: workerContext.waitUntil.bind(workerContext),
166
+ passThroughOnException:
167
+ workerContext.passThroughOnException.bind(workerContext),
168
+ };
169
+
170
+ const response = await handler(context);
171
+
172
+ return cloneResponse(response);
173
+ } else {
174
+ return next(request);
175
+ }
176
+ };
177
+
178
+ return pluginNext();
179
+ };
180
+
181
+ return onRequest;
182
+ }
183
+
184
+ // This makes a Response mutable
185
+ const cloneResponse = (response: Response) =>
186
+ // https://fetch.spec.whatwg.org/#null-body-status
187
+ new Response(
188
+ [101, 204, 205, 304].includes(response.status) ? null : response.body,
189
+ response
190
+ );
@@ -0,0 +1,198 @@
1
+ import { match } from "path-to-regexp";
2
+
3
+ //note: this explicitly does not include the * character, as pages requires this
4
+ const escapeRegex = /[.+?^${}()|[\]\\]/g;
5
+
6
+ type HTTPMethod =
7
+ | "HEAD"
8
+ | "OPTIONS"
9
+ | "GET"
10
+ | "POST"
11
+ | "PUT"
12
+ | "PATCH"
13
+ | "DELETE";
14
+
15
+ /* TODO: Grab these from @cloudflare/workers-types instead */
16
+ type Params<P extends string = string> = Record<P, string | string[]>;
17
+
18
+ type EventContext<Env, P extends string, Data> = {
19
+ request: Request;
20
+ functionPath: string;
21
+ waitUntil: (promise: Promise<unknown>) => void;
22
+ passThroughOnException: () => void;
23
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
24
+ env: Env & { ASSETS: { fetch: typeof fetch } };
25
+ params: Params<P>;
26
+ data: Data;
27
+ };
28
+
29
+ declare type PagesFunction<
30
+ Env = unknown,
31
+ P extends string = string,
32
+ Data extends Record<string, unknown> = Record<string, unknown>,
33
+ > = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
34
+ /* end @cloudflare/workers-types */
35
+
36
+ type RouteHandler = {
37
+ routePath: string;
38
+ mountPath: string;
39
+ method?: HTTPMethod;
40
+ modules: PagesFunction[];
41
+ middlewares: PagesFunction[];
42
+ };
43
+
44
+ // inject `routes` via ESBuild
45
+ declare const routes: RouteHandler[];
46
+ // define `__FALLBACK_SERVICE__` via ESBuild
47
+ declare const __FALLBACK_SERVICE__: string;
48
+
49
+ // expect an ASSETS fetcher binding pointing to the asset-server stage
50
+ type FetchEnv = {
51
+ [name: string]: { fetch: typeof fetch };
52
+ ASSETS: { fetch: typeof fetch };
53
+ };
54
+
55
+ type WorkerContext = {
56
+ waitUntil: (promise: Promise<unknown>) => void;
57
+ passThroughOnException: () => void;
58
+ };
59
+
60
+ function* executeRequest(request: Request) {
61
+ const requestPath = new URL(request.url).pathname;
62
+
63
+ // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
64
+ for (const route of [...routes].reverse()) {
65
+ if (route.method && route.method !== request.method) {
66
+ continue;
67
+ }
68
+
69
+ // replaces with "\\$&", this prepends a backslash to the matched string, e.g. "[" becomes "\["
70
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
71
+ end: false,
72
+ });
73
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
74
+ end: false,
75
+ });
76
+ const matchResult = routeMatcher(requestPath);
77
+ const mountMatchResult = mountMatcher(requestPath);
78
+ if (matchResult && mountMatchResult) {
79
+ for (const handler of route.middlewares.flat()) {
80
+ yield {
81
+ handler,
82
+ params: matchResult.params as Params,
83
+ path: mountMatchResult.path,
84
+ };
85
+ }
86
+ }
87
+ }
88
+
89
+ // Then look for the first exact route match and execute its "modules"
90
+ for (const route of routes) {
91
+ if (route.method && route.method !== request.method) {
92
+ continue;
93
+ }
94
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
95
+ end: true,
96
+ });
97
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
98
+ end: false,
99
+ });
100
+ const matchResult = routeMatcher(requestPath);
101
+ const mountMatchResult = mountMatcher(requestPath);
102
+ if (matchResult && mountMatchResult && route.modules.length) {
103
+ for (const handler of route.modules.flat()) {
104
+ yield {
105
+ handler,
106
+ params: matchResult.params as Params,
107
+ path: matchResult.path,
108
+ };
109
+ }
110
+ break;
111
+ }
112
+ }
113
+ }
114
+
115
+ export default {
116
+ async fetch(
117
+ originalRequest: Request,
118
+ env: FetchEnv,
119
+ workerContext: WorkerContext
120
+ ) {
121
+ let request = originalRequest;
122
+ const handlerIterator = executeRequest(request);
123
+ let data = {}; // arbitrary data the user can set between functions
124
+ let isFailOpen = false;
125
+
126
+ const next = async (input?: RequestInfo, init?: RequestInit) => {
127
+ if (input !== undefined) {
128
+ let url = input;
129
+ if (typeof input === "string") {
130
+ url = new URL(input, request.url).toString();
131
+ }
132
+ request = new Request(url, init);
133
+ }
134
+
135
+ const result = handlerIterator.next();
136
+ // Note we can't use `!result.done` because this doesn't narrow to the correct type
137
+ if (result.done === false) {
138
+ const { handler, params, path } = result.value;
139
+ const context = {
140
+ request: new Request(request.clone()),
141
+ functionPath: path,
142
+ next,
143
+ params,
144
+ get data() {
145
+ return data;
146
+ },
147
+ set data(value) {
148
+ if (typeof value !== "object" || value === null) {
149
+ throw new Error("context.data must be an object");
150
+ }
151
+ // user has overriden context.data, so we need to merge it with the existing data
152
+ data = value;
153
+ },
154
+ env,
155
+ waitUntil: workerContext.waitUntil.bind(workerContext),
156
+ passThroughOnException: () => {
157
+ isFailOpen = true;
158
+ },
159
+ };
160
+
161
+ const response = await handler(context);
162
+
163
+ if (!(response instanceof Response)) {
164
+ throw new Error("Your Pages function should return a Response");
165
+ }
166
+
167
+ return cloneResponse(response);
168
+ } else if (__FALLBACK_SERVICE__) {
169
+ // There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
170
+ const response = await env[__FALLBACK_SERVICE__].fetch(request);
171
+ return cloneResponse(response);
172
+ } else {
173
+ // There was not fallback service so actually make the request to the origin.
174
+ const response = await fetch(request);
175
+ return cloneResponse(response);
176
+ }
177
+ };
178
+
179
+ try {
180
+ return await next();
181
+ } catch (error) {
182
+ if (isFailOpen) {
183
+ const response = await env[__FALLBACK_SERVICE__].fetch(request);
184
+ return cloneResponse(response);
185
+ }
186
+
187
+ throw error;
188
+ }
189
+ },
190
+ };
191
+
192
+ // This makes a Response mutable
193
+ const cloneResponse = (response: Response) =>
194
+ // https://fetch.spec.whatwg.org/#null-body-status
195
+ new Response(
196
+ [101, 204, 205, 304].includes(response.status) ? null : response.body,
197
+ response
198
+ );
@@ -0,0 +1,143 @@
1
+ import { newWorkersRpcResponse } from "capnweb";
2
+ import { EmailMessage } from "cloudflare:email";
3
+
4
+ interface Env extends Record<string, unknown> {}
5
+
6
+ class BindingNotFoundError extends Error {
7
+ constructor(name?: string) {
8
+ super(`Binding ${name ? `"${name}"` : ""} not found`);
9
+ }
10
+ }
11
+
12
+ /**
13
+ * For most bindings, we expose them as
14
+ * - RPC stubs directly to capnweb, or
15
+ * - HTTP based fetchers
16
+ * However, there are some special cases:
17
+ * - SendEmail bindings need to take EmailMessage as their first parameter,
18
+ * which is not serialisable. As such, we reconstruct it before sending it
19
+ * on to the binding. See packages/miniflare/src/workers/email/email.worker.ts
20
+ * - Dispatch Namespace bindings have a synchronous .get() method. Since we
21
+ * can't emulate that over an async boundary, we mock it locally and _actually_
22
+ * perform the .get() remotely at the first appropriate async point. See
23
+ * packages/miniflare/src/workers/dispatch-namespace/dispatch-namespace.worker.ts
24
+ *
25
+ * getExposedJSRPCBinding() and getExposedFetcher() perform the logic for figuring out
26
+ * which binding is being accessed, dependending on the request. Note: Both have logic
27
+ * for dispatch namespaces, because dispatch namespaces can use both fetch or RPC depending
28
+ * on context.
29
+ */
30
+
31
+ function getExposedJSRPCBinding(request: Request, env: Env) {
32
+ const url = new URL(request.url);
33
+ const bindingName = url.searchParams.get("MF-Binding");
34
+ if (!bindingName) {
35
+ throw new BindingNotFoundError();
36
+ }
37
+
38
+ const targetBinding = env[bindingName];
39
+ if (!targetBinding) {
40
+ throw new BindingNotFoundError(bindingName);
41
+ }
42
+
43
+ if (targetBinding.constructor.name === "SendEmail") {
44
+ return {
45
+ async send(e: any) {
46
+ // Check if this is an EmailMessage (has EmailMessage::raw property) or MessageBuilder
47
+ if ("EmailMessage::raw" in e) {
48
+ // EmailMessage API - reconstruct the EmailMessage object
49
+ const message = new EmailMessage(
50
+ e.from,
51
+ e.to,
52
+ e["EmailMessage::raw"]
53
+ );
54
+ return (targetBinding as SendEmail).send(message);
55
+ } else {
56
+ // MessageBuilder API - pass through directly as a plain object
57
+ return (targetBinding as SendEmail).send(e);
58
+ }
59
+ },
60
+ };
61
+ }
62
+
63
+ if (url.searchParams.has("MF-Dispatch-Namespace-Options")) {
64
+ const { name, args, options } = JSON.parse(
65
+ url.searchParams.get("MF-Dispatch-Namespace-Options")!
66
+ );
67
+ return (targetBinding as DispatchNamespace).get(name, args, options);
68
+ }
69
+
70
+ return targetBinding;
71
+ }
72
+
73
+ function getExposedFetcher(request: Request, env: Env) {
74
+ const bindingName = request.headers.get("MF-Binding");
75
+ if (!bindingName) {
76
+ throw new BindingNotFoundError();
77
+ }
78
+
79
+ const targetBinding = env[bindingName];
80
+ if (!targetBinding) {
81
+ throw new BindingNotFoundError(bindingName);
82
+ }
83
+
84
+ // Special case the Dispatch Namespace binding because it has a top-level synchronous .get() call
85
+ const dispatchNamespaceOptions = request.headers.get(
86
+ "MF-Dispatch-Namespace-Options"
87
+ );
88
+ if (dispatchNamespaceOptions) {
89
+ const { name, args, options } = JSON.parse(dispatchNamespaceOptions);
90
+ return (targetBinding as DispatchNamespace).get(name, args, options);
91
+ }
92
+ return targetBinding as Fetcher;
93
+ }
94
+
95
+ /**
96
+ * This Worker can proxy two types of remote binding:
97
+ * 1. "raw" bindings, where this Worker has been configured to pass through the raw
98
+ * fetch from a local workerd instance to the relevant binding
99
+ * 2. JSRPC bindings, where this Worker uses capnweb to proxy RPC
100
+ * communication in userland. This is always over a WebSocket connection
101
+ */
102
+ function isJSRPCBinding(request: Request): boolean {
103
+ const url = new URL(request.url);
104
+ return request.headers.has("Upgrade") && url.searchParams.has("MF-Binding");
105
+ }
106
+
107
+ export default {
108
+ async fetch(request, env) {
109
+ try {
110
+ if (isJSRPCBinding(request)) {
111
+ return newWorkersRpcResponse(
112
+ request,
113
+ getExposedJSRPCBinding(request, env)
114
+ );
115
+ } else {
116
+ const fetcher = getExposedFetcher(request, env);
117
+ const originalHeaders = new Headers();
118
+ for (const [name, value] of request.headers) {
119
+ if (name.startsWith("mf-header-")) {
120
+ originalHeaders.set(name.slice("mf-header-".length), value);
121
+ } else if (name === "upgrade") {
122
+ // The `Upgrade` header needs to be special-cased to prevent:
123
+ // TypeError: Worker tried to return a WebSocket in a response to a request which did not contain the header "Upgrade: websocket"
124
+ originalHeaders.set(name, value);
125
+ }
126
+ }
127
+
128
+ return fetcher.fetch(
129
+ request.headers.get("MF-URL") ?? "http://example.com",
130
+ new Request(request, {
131
+ redirect: "manual",
132
+ headers: originalHeaders,
133
+ })
134
+ );
135
+ }
136
+ } catch (e) {
137
+ if (e instanceof BindingNotFoundError) {
138
+ return new Response(e.message, { status: 400 });
139
+ }
140
+ return new Response((e as Error).message, { status: 500 });
141
+ }
142
+ },
143
+ } satisfies ExportedHandler<Env>;
@@ -0,0 +1,4 @@
1
+ {
2
+ "main": "./ProxyServerWorker.ts",
3
+ "compatibility_date": "2025-04-28",
4
+ }