Skip to content

Commit dacfc35

Browse files
dario-piotrowiczvicbCarmenPopoviciu
authored
V3 backport [#7871]: add support for assets bindings to getPlatformProxy (#9694)
* add support for assets bindings to `getPlatformProxy` * Apply suggestions from code review Co-authored-by: Victor Berchet <[email protected]> * fix formatting * fix CI * Update packages/wrangler/src/api/integrations/platform/index.ts --------- Co-authored-by: Victor Berchet <[email protected]> Co-authored-by: Carmen Popoviciu <[email protected]>
1 parent b4fb0e2 commit dacfc35

File tree

6 files changed

+80
-4
lines changed

6 files changed

+80
-4
lines changed

.changeset/thick-dots-sit.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
```jsonc
13+
// wrangler.jsonc
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+
const text = await (await env.ASSETS.fetch("http://0.0.0.0/file.txt")).text();
29+
console.log(text); // logs the content of file.txt
30+
31+
await dispose();
32+
```
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: 16 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,16 @@ 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 res = await env.ASSETS.fetch("https://0.0.0.0/test.txt");
136+
const text = await res.text();
137+
expect(text).toEqual("this is a test text file!\n");
138+
await dispose();
139+
});
140+
126141
it("correctly obtains functioning KV bindings", async () => {
127142
const { env, dispose } = await getPlatformProxy<Env>({
128143
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: 22 additions & 2 deletions
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";
@@ -18,6 +18,7 @@ import { dedent } from "../../../utils/dedent";
1818
import { CacheStorage } from "./caches";
1919
import { ExecutionContext } from "./executionContext";
2020
import { getServiceBindings } from "./services";
21+
import type { AssetsOptions } from "../../../assets";
2122
import type { Config, RawConfig, RawEnvironment } from "../../../config";
2223
import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental";
2324
import type { MiniflareOptions, ModuleRule, WorkerOptions } from "miniflare";
@@ -171,10 +172,28 @@ async function getMiniflareOptionsFromConfig(
171172
imagesLocalMode: false,
172173
});
173174

174-
const persistOptions = getMiniflarePersistOptions(options.persist);
175+
let processedAssetOptions: AssetsOptions | undefined;
176+
177+
try {
178+
processedAssetOptions = getAssetsOptions({ assets: undefined }, rawConfig);
179+
} catch (e) {
180+
const isNonExistentError = e instanceof NonExistentAssetsDirError;
181+
// we want to loosen up the assets directory existence restriction here,
182+
// since `getPlatformProxy` can be run when the assets directory doesn't actual
183+
// exist, but all other exceptions should still be thrown
184+
if (!isNonExistentError) {
185+
throw e;
186+
}
187+
}
188+
189+
const assetOptions = processedAssetOptions
190+
? buildAssetOptions({ assets: processedAssetOptions })
191+
: {};
175192

176193
const serviceBindings = await getServiceBindings(bindings.services);
177194

195+
const persistOptions = getMiniflarePersistOptions(options.persist);
196+
178197
const miniflareOptions: MiniflareOptions = {
179198
workers: [
180199
{
@@ -186,6 +205,7 @@ async function getMiniflareOptionsFromConfig(
186205
...serviceBindings,
187206
...bindingOptions.serviceBindings,
188207
},
208+
...assetOptions,
189209
},
190210
...externalWorkers,
191211
],

packages/wrangler/src/assets.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ export type AssetsOptions = {
354354
_headers?: string;
355355
};
356356

357+
export class NonExistentAssetsDirError extends UserError {}
358+
357359
export function getAssetsOptions(
358360
args: { assets: string | undefined; script?: string },
359361
config: Config
@@ -387,7 +389,7 @@ export function getAssetsOptions(
387389
? '"--assets" command line argument'
388390
: '"assets.directory" field in your configuration file';
389391

390-
throw new UserError(
392+
throw new NonExistentAssetsDirError(
391393
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
392394
`${resolvedAssetsPath}`,
393395

0 commit comments

Comments
 (0)