Skip to content

Commit 0deb955

Browse files
add support for assets bindings to getPlatformProxy
1 parent 17d23d8 commit 0deb955

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed

.changeset/thick-dots-sit.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
21+
```
22+
23+
```js
24+
import { getPlatformProxy } from "wrangler";
25+
26+
const { env, dispose } = await getPlatformProxy();
27+
28+
if (env.ASSETS) {
29+
const text = await (await env.ASSETS.fetch("http://0.0.0.0/file.txt")).text();
30+
console.log(text); // logs the content of file.txt
31+
}
32+
33+
await dispose();
34+
```
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
@@ -10,7 +10,11 @@ import {
1010
vi,
1111
} from "vitest";
1212
import { getPlatformProxy } from "./shared";
13-
import type { Hyperdrive, KVNamespace } from "@cloudflare/workers-types";
13+
import type {
14+
Fetcher,
15+
Hyperdrive,
16+
KVNamespace,
17+
} from "@cloudflare/workers-types";
1418
import type { Unstable_DevWorker } from "wrangler";
1519

1620
type Env = {
@@ -23,6 +27,7 @@ type Env = {
2327
MY_BUCKET: R2Bucket;
2428
MY_D1: D1Database;
2529
MY_HYPERDRIVE: Hyperdrive;
30+
ASSETS: Fetcher;
2631
};
2732

2833
const wranglerConfigFilePath = path.join(__dirname, "..", "wrangler.jsonc");
@@ -123,6 +128,17 @@ describe("getPlatformProxy - env", () => {
123128
}
124129
});
125130

131+
it("correctly obtains functioning ASSETS bindings", async () => {
132+
const { env, dispose } = await getPlatformProxy<Env>({
133+
configPath: wranglerConfigFilePath,
134+
});
135+
const { ASSETS } = env;
136+
const res = await ASSETS.fetch("https://0.0.0.0/test.txt");
137+
const text = await res.text();
138+
expect(text).toEqual("this is a test text file!\n");
139+
await dispose();
140+
});
141+
126142
it("correctly obtains functioning KV bindings", async () => {
127143
const { env, dispose } = await getPlatformProxy<Env>({
128144
configPath: wranglerConfigFilePath,

fixtures/get-platform-proxy/wrangler.jsonc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
"name": "get-bindings-proxy-fixture",
33
"main": "src/index.ts",
44
"compatibility_date": "2023-11-21",
5+
"assets": {
6+
"directory": "./public",
7+
"binding": "ASSETS",
8+
"html_handling": "auto-trailing-slash",
9+
"not_found_handling": "none",
10+
},
511
"vars": {
612
"MY_VAR": "my-var-value",
713
"MY_VAR_A": "my-var-a",

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { kCurrentWorker, Miniflare } from "miniflare";
2-
import { getAssetsOptions } from "../../../assets";
2+
import { getAssetsOptions, NonExistentAssetsDirError } from "../../../assets";
33
import { readConfig } from "../../../config";
44
import { partitionDurableObjectBindings } from "../../../deployment-bundle/entry";
55
import { DEFAULT_MODULE_RULES } from "../../../deployment-bundle/rules";
@@ -187,6 +187,24 @@ async function getMiniflareOptionsFromConfig(
187187
false
188188
);
189189

190+
let processedAssetOptions: AssetsOptions | undefined;
191+
192+
try {
193+
processedAssetOptions = getAssetsOptions({ assets: undefined }, rawConfig);
194+
} catch (e) {
195+
const isNonExistentError = e instanceof NonExistentAssetsDirError;
196+
// we want to loosen up the assets directory existence restriction here,
197+
// since `getPlatformProxy` can be run when the assets directory doesn't actual
198+
// exist, but all other exceptions should still be thrown
199+
if (!isNonExistentError) {
200+
throw e;
201+
}
202+
}
203+
204+
const assetOptions = processedAssetOptions
205+
? buildAssetOptions({ assets: processedAssetOptions })
206+
: {};
207+
190208
const defaultPersistRoot = getMiniflarePersistRoot(options.persist);
191209

192210
const serviceBindings = await getServiceBindings(bindings.services);
@@ -202,6 +220,7 @@ async function getMiniflareOptionsFromConfig(
202220
...serviceBindings,
203221
...bindingOptions.serviceBindings,
204222
},
223+
...assetOptions,
205224
},
206225
...externalWorkers,
207226
],

packages/wrangler/src/assets.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ export type AssetsOptions = {
393393
run_worker_first?: boolean | string[];
394394
};
395395

396+
export class NonExistentAssetsDirError extends UserError {}
397+
396398
export function getAssetsOptions(
397399
args: { assets: string | undefined; script?: string },
398400
config: Config,
@@ -429,7 +431,7 @@ export function getAssetsOptions(
429431
? '"--assets" command line argument'
430432
: '"assets.directory" field in your configuration file';
431433

432-
throw new UserError(
434+
throw new NonExistentAssetsDirError(
433435
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
434436
`${directory}`,
435437

0 commit comments

Comments
 (0)