Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .changeset/thick-dots-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
"wrangler": patch
---

add support for assets bindings to `getPlatformProxy`

this change makes sure that that `getPlatformProxy`, when the input configuration
file contains an assets field, correctly returns the appropriate asset binding proxy

example:

```json
// wrangler.json
{
"name": "my-worker",
"assets": {
"directory": "./public/",
"binding": "ASSETS"
},
"vars": {
"MY_VAR": "my-var"
}
}
```

```js
import { getPlatformProxy } from "wrangler";

const { env } = await getPlatformProxy();

if (env.ASSETS) {
const text = await (
await p.env.ASSETS.fetch("http://0.0.0.0/file.txt")
).text();
console.log(text); // logs the content of file.txt
}

p.dispose();
```
1 change: 1 addition & 0 deletions fixtures/get-platform-proxy/public/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a test text file!
18 changes: 17 additions & 1 deletion fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import path from "path";
import { D1Database, R2Bucket } from "@cloudflare/workers-types";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getPlatformProxy } from "./shared";
import type { Hyperdrive, KVNamespace } from "@cloudflare/workers-types";
import type {
Fetcher,
Hyperdrive,
KVNamespace,
} from "@cloudflare/workers-types";
import type { Unstable_DevWorker } from "wrangler";

type Env = {
Expand All @@ -15,6 +19,7 @@ type Env = {
MY_BUCKET: R2Bucket;
MY_D1: D1Database;
MY_HYPERDRIVE: Hyperdrive;
ASSETS: Fetcher;
};

const wranglerTomlFilePath = path.join(__dirname, "..", "wrangler.toml");
Expand Down Expand Up @@ -115,6 +120,17 @@ describe("getPlatformProxy - env", () => {
}
});

it("correctly obtains functioning ASSETS bindings", async () => {
const { env, dispose } = await getPlatformProxy<Env>({
configPath: wranglerTomlFilePath,
});
const { ASSETS } = env;
const res = await ASSETS.fetch("https://0.0.0.0/test.txt");
const text = await res.text();
expect(text).toEqual("this is a test text file!\n");
await dispose();
});

it("correctly obtains functioning KV bindings", async () => {
const { env, dispose } = await getPlatformProxy<Env>({
configPath: wranglerTomlFilePath,
Expand Down
6 changes: 6 additions & 0 deletions fixtures/get-platform-proxy/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ MY_VAR = "my-var-value"
MY_VAR_A = "my-var-a"
MY_JSON_VAR = { test = true }

[assets]
directory = "./public"
binding = "ASSETS"
html_handling = "auto-trailing-slash"
not_found_handling = "none"

[[kv_namespaces]]
binding = "MY_KV"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Expand Down
9 changes: 9 additions & 0 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ async function getMiniflareOptionsFromConfig(
migrations: rawConfig.migrations,
});

const processedAssetOptions = getAssetsOptions(
{ assets: undefined },
rawConfig
);
const assetOptions = processedAssetOptions
? buildAssetOptions({ assets: processedAssetOptions })
: {};

const persistOptions = getMiniflarePersistOptions(options.persist);

const serviceBindings = await getServiceBindings(bindings.services);
Expand All @@ -172,6 +180,7 @@ async function getMiniflareOptionsFromConfig(
...serviceBindings,
...bindingOptions.serviceBindings,
},
...assetOptions,
},
...externalWorkers,
],
Expand Down
Loading