Skip to content

Commit 1de49e9

Browse files
add support for assets bindings to getPlatformProxy
1 parent 139b5ec commit 1de49e9

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-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
@@ -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: 21 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 { DEFAULT_MODULE_RULES } from "../../../deployment-bundle/rules";
55
import { getBindings } from "../../../dev";
@@ -15,6 +15,7 @@ import { getLegacyAssetPaths, getSiteAssetPaths } from "../../../sites";
1515
import { CacheStorage } from "./caches";
1616
import { ExecutionContext } from "./executionContext";
1717
import { getServiceBindings } from "./services";
18+
import type { AssetsOptions } from "../../../assets";
1819
import type { Config, RawConfig, RawEnvironment } from "../../../config";
1920
import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental";
2021
import type { MiniflareOptions, ModuleRule, WorkerOptions } from "miniflare";
@@ -153,6 +154,24 @@ async function getMiniflareOptionsFromConfig(
153154
imagesLocalMode: false,
154155
});
155156

157+
let processedAssetOptions: AssetsOptions | undefined;
158+
159+
try {
160+
processedAssetOptions = getAssetsOptions({ assets: undefined }, rawConfig);
161+
} catch (e) {
162+
const isNonExistentError = e instanceof NonExistentAssetsDirError;
163+
// we want to loosen up the assets directory existence restriction here,
164+
// since `getPlatformProxy` can be run when the assets directory doesn't actual
165+
// exist, but all other exceptions should still be thrown
166+
if (!isNonExistentError) {
167+
throw e;
168+
}
169+
}
170+
171+
const assetOptions = processedAssetOptions
172+
? buildAssetOptions({ assets: processedAssetOptions })
173+
: {};
174+
156175
const persistOptions = getMiniflarePersistOptions(options.persist);
157176

158177
const serviceBindings = await getServiceBindings(bindings.services);
@@ -167,6 +186,7 @@ async function getMiniflareOptionsFromConfig(
167186
...serviceBindings,
168187
...bindingOptions.serviceBindings,
169188
},
189+
...assetOptions,
170190
},
171191
...externalWorkers,
172192
],

packages/wrangler/src/assets.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ export type AssetsOptions = {
331331
assetConfig: AssetConfig;
332332
};
333333

334+
export class NonExistentAssetsDirError extends UserError {}
335+
334336
export function getAssetsOptions(
335337
args: { assets: string | undefined; script?: string },
336338
config: Config
@@ -364,7 +366,7 @@ export function getAssetsOptions(
364366
? '"--assets" command line argument'
365367
: '"assets.directory" field in your configuration file';
366368

367-
throw new UserError(
369+
throw new NonExistentAssetsDirError(
368370
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
369371
`${resolvedAssetsPath}`,
370372

0 commit comments

Comments
 (0)