wrangler 4.30.0 → 4.31.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wrangler",
3
- "version": "4.30.0",
3
+ "version": "4.31.0",
4
4
  "description": "Command-line interface for all things Cloudflare Workers",
5
5
  "keywords": [
6
6
  "wrangler",
@@ -54,15 +54,16 @@
54
54
  "esbuild": "0.25.4",
55
55
  "path-to-regexp": "6.3.0",
56
56
  "unenv": "2.0.0-rc.19",
57
- "workerd": "1.20250813.0",
58
- "@cloudflare/kv-asset-handler": "0.4.0",
59
- "@cloudflare/unenv-preset": "2.6.1",
60
- "miniflare": "4.20250813.1"
57
+ "workerd": "1.20250816.0",
58
+ "miniflare": "4.20250816.0",
59
+ "@cloudflare/unenv-preset": "2.6.2",
60
+ "@cloudflare/kv-asset-handler": "0.4.0"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@aws-sdk/client-s3": "^3.721.0",
64
+ "@cloudflare/jsrpc": "link:../../vendor/jsrpc",
64
65
  "@cloudflare/types": "6.18.4",
65
- "@cloudflare/workers-types": "^4.20250813.0",
66
+ "@cloudflare/workers-types": "^4.20250816.0",
66
67
  "@cspotcode/source-map-support": "0.8.1",
67
68
  "@iarna/toml": "^3.0.0",
68
69
  "@sentry/node": "^7.86.0",
@@ -136,15 +137,15 @@
136
137
  "xdg-app-paths": "^8.3.0",
137
138
  "xxhash-wasm": "^1.0.1",
138
139
  "yargs": "^17.7.2",
139
- "@cloudflare/cli": "1.1.1",
140
140
  "@cloudflare/containers-shared": "0.2.10",
141
- "@cloudflare/pages-shared": "^0.13.63",
142
- "@cloudflare/workers-shared": "0.18.5",
141
+ "@cloudflare/eslint-config-worker": "1.1.0",
142
+ "@cloudflare/cli": "1.1.1",
143
+ "@cloudflare/pages-shared": "^0.13.64",
143
144
  "@cloudflare/workers-tsconfig": "0.0.0",
144
- "@cloudflare/eslint-config-worker": "1.1.0"
145
+ "@cloudflare/workers-shared": "0.18.5"
145
146
  },
146
147
  "peerDependencies": {
147
- "@cloudflare/workers-types": "^4.20250813.0"
148
+ "@cloudflare/workers-types": "^4.20250816.0"
148
149
  },
149
150
  "peerDependenciesMeta": {
150
151
  "@cloudflare/workers-types": {
@@ -0,0 +1,133 @@
1
+ import { newWorkersRpcResponse } from "@cloudflare/jsrpc";
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 @cloudflare/jsrpc, 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: ForwardableEmailMessage) {
46
+ // @ts-expect-error EmailMessage::raw is defined in packages/miniflare/src/workers/email/email.worker.ts
47
+ const message = new EmailMessage(e.from, e.to, e["EmailMessage::raw"]);
48
+ return (targetBinding as SendEmail).send(message);
49
+ },
50
+ };
51
+ }
52
+
53
+ if (url.searchParams.has("MF-Dispatch-Namespace-Options")) {
54
+ const { name, args, options } = JSON.parse(
55
+ url.searchParams.get("MF-Dispatch-Namespace-Options")!
56
+ );
57
+ return (targetBinding as DispatchNamespace).get(name, args, options);
58
+ }
59
+
60
+ return targetBinding;
61
+ }
62
+
63
+ function getExposedFetcher(request: Request, env: Env) {
64
+ const bindingName = request.headers.get("MF-Binding");
65
+ if (!bindingName) {
66
+ throw new BindingNotFoundError();
67
+ }
68
+
69
+ const targetBinding = env[bindingName];
70
+ if (!targetBinding) {
71
+ throw new BindingNotFoundError(bindingName);
72
+ }
73
+
74
+ // Special case the Dispatch Namespace binding because it has a top-level synchronous .get() call
75
+ const dispatchNamespaceOptions = request.headers.get(
76
+ "MF-Dispatch-Namespace-Options"
77
+ );
78
+ if (dispatchNamespaceOptions) {
79
+ const { name, args, options } = JSON.parse(dispatchNamespaceOptions);
80
+ return (targetBinding as DispatchNamespace).get(name, args, options);
81
+ }
82
+ return targetBinding as Fetcher;
83
+ }
84
+
85
+ /**
86
+ * This Worker can proxy two types of remote binding:
87
+ * 1. "raw" bindings, where this Worker has been configured to pass through the raw
88
+ * fetch from a local workerd instance to the relevant binding
89
+ * 2. JSRPC bindings, where this Worker uses @cloudflare/jsrpc to proxy RPC
90
+ * communication in userland. This is always over a WebSocket connection
91
+ */
92
+ function isJSRPCBinding(request: Request): boolean {
93
+ const url = new URL(request.url);
94
+ return request.headers.has("Upgrade") && url.searchParams.has("MF-Binding");
95
+ }
96
+
97
+ export default {
98
+ async fetch(request, env) {
99
+ try {
100
+ if (isJSRPCBinding(request)) {
101
+ return newWorkersRpcResponse(
102
+ request,
103
+ getExposedJSRPCBinding(request, env)
104
+ );
105
+ } else {
106
+ const fetcher = getExposedFetcher(request, env);
107
+ const originalHeaders = new Headers();
108
+ for (const [name, value] of request.headers) {
109
+ if (name.startsWith("mf-header-")) {
110
+ originalHeaders.set(name.slice("mf-header-".length), value);
111
+ } else if (name === "upgrade") {
112
+ // The `Upgrade` header needs to be special-cased to prevent:
113
+ // TypeError: Worker tried to return a WebSocket in a response to a request which did not contain the header "Upgrade: websocket"
114
+ originalHeaders.set(name, value);
115
+ }
116
+ }
117
+
118
+ return fetcher.fetch(
119
+ request.headers.get("MF-URL") ?? "http://example.com",
120
+ new Request(request, {
121
+ redirect: "manual",
122
+ headers: originalHeaders,
123
+ })
124
+ );
125
+ }
126
+ } catch (e) {
127
+ if (e instanceof BindingNotFoundError) {
128
+ return new Response(e.message, { status: 400 });
129
+ }
130
+ return new Response((e as Error).message, { status: 500 });
131
+ }
132
+ },
133
+ } satisfies ExportedHandler<Env>;
@@ -1,4 +1,4 @@
1
1
  {
2
- "main": "./index.ts",
2
+ "main": "./ProxyServerWorker.ts",
3
3
  "compatibility_date": "2025-04-28",
4
4
  }