Skip to content

Commit b613d89

Browse files
add support for assets bindings to getPlatformProxy
1 parent f0f38b3 commit b613d89

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

.changeset/thick-dots-sit.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
add support for assets bindings to `getPlatformProxy`
6+
7+
this change makes sure that that `getPlatformProxy`, when the input configuration
8+
file contains an assets field, correctly returns the appropriate asset binding proxy
9+
10+
example:
11+
12+
```json
13+
// wrangler.json
14+
{
15+
"name": "my-worker",
16+
"assets": {
17+
"directory": "./public/",
18+
"binding": "ASSETS"
19+
},
20+
"vars": {
21+
"MY_VAR": "my-var"
22+
}
23+
}
24+
```
25+
26+
```js
27+
import { getPlatformProxy } from "wrangler";
28+
29+
const { env } = await getPlatformProxy();
30+
31+
if (env.ASSETS) {
32+
const text = await (
33+
await p.env.ASSETS.fetch("http://0.0.0.0/file.txt")
34+
).text();
35+
console.log(text); // logs the content of file.txt
36+
}
37+
38+
p.dispose();
39+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a test text file!

fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import path from "path";
22
import { D1Database, R2Bucket } from "@cloudflare/workers-types";
33
import { beforeEach, describe, expect, it, vi } from "vitest";
44
import { getPlatformProxy } from "./shared";
5-
import type { Hyperdrive, KVNamespace } from "@cloudflare/workers-types";
5+
import type {
6+
Fetcher,
7+
Hyperdrive,
8+
KVNamespace,
9+
} from "@cloudflare/workers-types";
610
import type { Unstable_DevWorker } from "wrangler";
711

812
type Env = {
@@ -15,6 +19,7 @@ type Env = {
1519
MY_BUCKET: R2Bucket;
1620
MY_D1: D1Database;
1721
MY_HYPERDRIVE: Hyperdrive;
22+
ASSETS: Fetcher;
1823
};
1924

2025
const wranglerTomlFilePath = path.join(__dirname, "..", "wrangler.toml");
@@ -115,6 +120,17 @@ describe("getPlatformProxy - env", () => {
115120
}
116121
});
117122

123+
it("correctly obtains functioning ASSETS bindings", async () => {
124+
const { env, dispose } = await getPlatformProxy<Env>({
125+
configPath: wranglerTomlFilePath,
126+
});
127+
const { ASSETS } = env;
128+
const res = await ASSETS.fetch("https://0.0.0.0/test.txt");
129+
const text = await res.text();
130+
expect(text).toEqual("this is a test text file!\n");
131+
await dispose();
132+
});
133+
118134
it("correctly obtains functioning KV bindings", async () => {
119135
const { env, dispose } = await getPlatformProxy<Env>({
120136
configPath: wranglerTomlFilePath,

fixtures/get-platform-proxy/wrangler.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ MY_VAR = "my-var-value"
77
MY_VAR_A = "my-var-a"
88
MY_JSON_VAR = { test = true }
99

10+
[assets]
11+
directory = "./public"
12+
binding = "ASSETS"
13+
html_handling = "auto-trailing-slash"
14+
not_found_handling = "none"
15+
1016
[[kv_namespaces]]
1117
binding = "MY_KV"
1218
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

packages/wrangler/src/api/integrations/platform/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ async function getMiniflareOptionsFromConfig(
158158
migrations: rawConfig.migrations,
159159
});
160160

161+
const processedAssetOptions = getAssetsOptions(
162+
{ assets: undefined },
163+
rawConfig
164+
);
165+
const assetOptions = processedAssetOptions
166+
? buildAssetOptions({ assets: processedAssetOptions })
167+
: {};
168+
161169
const persistOptions = getMiniflarePersistOptions(options.persist);
162170

163171
const serviceBindings = await getServiceBindings(bindings.services);
@@ -172,6 +180,7 @@ async function getMiniflareOptionsFromConfig(
172180
...serviceBindings,
173181
...bindingOptions.serviceBindings,
174182
},
183+
...assetOptions,
175184
},
176185
...externalWorkers,
177186
],

0 commit comments

Comments
 (0)