diff --git a/.changeset/thick-dots-sit.md b/.changeset/thick-dots-sit.md new file mode 100644 index 000000000000..9e4c2a7aaa6d --- /dev/null +++ b/.changeset/thick-dots-sit.md @@ -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(); +``` diff --git a/fixtures/get-platform-proxy/public/test.txt b/fixtures/get-platform-proxy/public/test.txt new file mode 100644 index 000000000000..2dd981b4d688 --- /dev/null +++ b/fixtures/get-platform-proxy/public/test.txt @@ -0,0 +1 @@ +this is a test text file! diff --git a/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts b/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts index 4fb22fb13e35..8bb8246f887a 100644 --- a/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts +++ b/fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts @@ -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 = { @@ -15,6 +19,7 @@ type Env = { MY_BUCKET: R2Bucket; MY_D1: D1Database; MY_HYPERDRIVE: Hyperdrive; + ASSETS: Fetcher; }; const wranglerTomlFilePath = path.join(__dirname, "..", "wrangler.toml"); @@ -115,6 +120,17 @@ describe("getPlatformProxy - env", () => { } }); + it("correctly obtains functioning ASSETS bindings", async () => { + const { env, dispose } = await getPlatformProxy({ + 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({ configPath: wranglerTomlFilePath, diff --git a/fixtures/get-platform-proxy/wrangler.toml b/fixtures/get-platform-proxy/wrangler.toml index 1b94a0034f1e..fff309658ab9 100644 --- a/fixtures/get-platform-proxy/wrangler.toml +++ b/fixtures/get-platform-proxy/wrangler.toml @@ -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" diff --git a/packages/wrangler/src/api/integrations/platform/index.ts b/packages/wrangler/src/api/integrations/platform/index.ts index d6b78c38f7bd..2f19687b9cbc 100644 --- a/packages/wrangler/src/api/integrations/platform/index.ts +++ b/packages/wrangler/src/api/integrations/platform/index.ts @@ -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); @@ -172,6 +180,7 @@ async function getMiniflareOptionsFromConfig( ...serviceBindings, ...bindingOptions.serviceBindings, }, + ...assetOptions, }, ...externalWorkers, ],