Skip to content

Commit 6befada

Browse files
add support for assets bindings to getPlatformProxy
1 parent 9a50486 commit 6befada

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { kCurrentWorker, Miniflare } from "miniflare";
2-
import { getAssetsOptions } from "../../../assets";
2+
import type { AssetsOptions} from "../../../assets";
3+
import { getAssetsOptions, NonExistentAssetsDirError } from "../../../assets";
34
import { readConfig } from "../../../config";
45
import { DEFAULT_MODULE_RULES } from "../../../deployment-bundle/rules";
56
import { getBindings } from "../../../dev";
@@ -159,6 +160,28 @@ async function getMiniflareOptionsFromConfig(
159160
imagesLocalMode: false,
160161
});
161162

163+
164+
let processedAssetOptions: AssetsOptions|undefined;
165+
166+
try {
167+
processedAssetOptions = getAssetsOptions(
168+
{ assets: undefined },
169+
rawConfig,
170+
);
171+
} catch (e) {
172+
const isNonExistentError = e instanceof NonExistentAssetsDirError;
173+
// we want to loosen up the assets directory existence restriction here,
174+
// since `getPlatformProxy` can be run when the assets directory doesn't actual
175+
// exist (yes), but all other exceptions should still be thrown
176+
if(!isNonExistentError) {
177+
throw e;
178+
}
179+
}
180+
181+
const assetOptions = processedAssetOptions
182+
? buildAssetOptions({ assets: processedAssetOptions })
183+
: {};
184+
162185
const persistOptions = getMiniflarePersistOptions(options.persist);
163186

164187
const serviceBindings = await getServiceBindings(bindings.services);
@@ -173,6 +196,7 @@ async function getMiniflareOptionsFromConfig(
173196
...serviceBindings,
174197
...bindingOptions.serviceBindings,
175198
},
199+
...assetOptions,
176200
},
177201
...externalWorkers,
178202
],

packages/wrangler/src/assets.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ export type AssetsOptions = {
321321
assetConfig: AssetConfig;
322322
};
323323

324+
325+
export class NonExistentAssetsDirError extends UserError {}
326+
324327
export function getAssetsOptions(
325328
args: { assets: string | undefined; script?: string },
326329
config: Config
@@ -351,9 +354,9 @@ export function getAssetsOptions(
351354
? '"--assets" command line argument'
352355
: '"assets.directory" field in your configuration file';
353356

354-
throw new UserError(
357+
throw new nonExistentAssetsDirError(
355358
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
356-
`${resolvedAssetsPath}`
359+
`${resolvedAssetsPath}`,
357360
);
358361
}
359362

0 commit comments

Comments
 (0)