From 699e18c86ca39a01702e28239d604a7cbe7d1f0c Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sun, 19 Jan 2025 23:01:37 +0000 Subject: [PATCH 1/5] add support for assets bindings to `getPlatformProxy` --- .changeset/thick-dots-sit.md | 34 +++++++++++++++++++ fixtures/get-platform-proxy/public/test.txt | 1 + .../tests/get-platform-proxy.env.test.ts | 18 +++++++++- fixtures/get-platform-proxy/wrangler.jsonc | 6 ++++ .../src/api/integrations/platform/index.ts | 24 +++++++++++-- packages/wrangler/src/assets.ts | 4 ++- 6 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 .changeset/thick-dots-sit.md create mode 100644 fixtures/get-platform-proxy/public/test.txt diff --git a/.changeset/thick-dots-sit.md b/.changeset/thick-dots-sit.md new file mode 100644 index 000000000000..466fce066ac8 --- /dev/null +++ b/.changeset/thick-dots-sit.md @@ -0,0 +1,34 @@ +--- +"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" + } +} +``` + +```js +import { getPlatformProxy } from "wrangler"; + +const { env, dispose } = await getPlatformProxy(); + +if (env.ASSETS) { + const text = await (await env.ASSETS.fetch("http://0.0.0.0/file.txt")).text(); + console.log(text); // logs the content of file.txt +} + +await 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 74f7f17646b0..88d00601f704 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 @@ -10,7 +10,11 @@ import { 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 = { @@ -23,6 +27,7 @@ type Env = { MY_BUCKET: R2Bucket; MY_D1: D1Database; MY_HYPERDRIVE: Hyperdrive; + ASSETS: Fetcher; }; const wranglerConfigFilePath = path.join(__dirname, "..", "wrangler.jsonc"); @@ -123,6 +128,17 @@ describe("getPlatformProxy - env", () => { } }); + it("correctly obtains functioning ASSETS bindings", async () => { + const { env, dispose } = await getPlatformProxy({ + configPath: wranglerConfigFilePath, + }); + 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: wranglerConfigFilePath, diff --git a/fixtures/get-platform-proxy/wrangler.jsonc b/fixtures/get-platform-proxy/wrangler.jsonc index 95f6f63b2427..545d04d6b578 100644 --- a/fixtures/get-platform-proxy/wrangler.jsonc +++ b/fixtures/get-platform-proxy/wrangler.jsonc @@ -2,6 +2,12 @@ "name": "get-bindings-proxy-fixture", "main": "src/index.ts", "compatibility_date": "2023-11-21", + "assets": { + "directory": "./public", + "binding": "ASSETS", + "html_handling": "auto-trailing-slash", + "not_found_handling": "none", + }, "vars": { "MY_VAR": "my-var-value", "MY_VAR_A": "my-var-a", diff --git a/packages/wrangler/src/api/integrations/platform/index.ts b/packages/wrangler/src/api/integrations/platform/index.ts index a3232441d267..88ec38601403 100644 --- a/packages/wrangler/src/api/integrations/platform/index.ts +++ b/packages/wrangler/src/api/integrations/platform/index.ts @@ -1,5 +1,5 @@ import { kCurrentWorker, Miniflare } from "miniflare"; -import { getAssetsOptions } from "../../../assets"; +import { getAssetsOptions, NonExistentAssetsDirError } from "../../../assets"; import { readConfig } from "../../../config"; import { partitionDurableObjectBindings } from "../../../deployment-bundle/entry"; import { DEFAULT_MODULE_RULES } from "../../../deployment-bundle/rules"; @@ -18,6 +18,7 @@ import { dedent } from "../../../utils/dedent"; import { CacheStorage } from "./caches"; import { ExecutionContext } from "./executionContext"; import { getServiceBindings } from "./services"; +import type { AssetsOptions } from "../../../assets"; import type { Config, RawConfig, RawEnvironment } from "../../../config"; import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental"; import type { MiniflareOptions, ModuleRule, WorkerOptions } from "miniflare"; @@ -171,10 +172,28 @@ async function getMiniflareOptionsFromConfig( imagesLocalMode: false, }); - const persistOptions = getMiniflarePersistOptions(options.persist); + let processedAssetOptions: AssetsOptions | undefined; + + try { + processedAssetOptions = getAssetsOptions({ assets: undefined }, rawConfig); + } catch (e) { + const isNonExistentError = e instanceof NonExistentAssetsDirError; + // we want to loosen up the assets directory existence restriction here, + // since `getPlatformProxy` can be run when the assets directory doesn't actual + // exist, but all other exceptions should still be thrown + if (!isNonExistentError) { + throw e; + } + } + + const assetOptions = processedAssetOptions + ? buildAssetOptions({ assets: processedAssetOptions }) + : {}; const serviceBindings = await getServiceBindings(bindings.services); + const persistOptions = getMiniflarePersistOptions(options.persist); + const miniflareOptions: MiniflareOptions = { workers: [ { @@ -186,6 +205,7 @@ async function getMiniflareOptionsFromConfig( ...serviceBindings, ...bindingOptions.serviceBindings, }, + ...assetOptions, }, ...externalWorkers, ], diff --git a/packages/wrangler/src/assets.ts b/packages/wrangler/src/assets.ts index 514d815272c2..565e06319e07 100644 --- a/packages/wrangler/src/assets.ts +++ b/packages/wrangler/src/assets.ts @@ -354,6 +354,8 @@ export type AssetsOptions = { _headers?: string; }; +export class NonExistentAssetsDirError extends UserError {} + export function getAssetsOptions( args: { assets: string | undefined; script?: string }, config: Config @@ -387,7 +389,7 @@ export function getAssetsOptions( ? '"--assets" command line argument' : '"assets.directory" field in your configuration file'; - throw new UserError( + throw new NonExistentAssetsDirError( `The directory specified by the ${sourceOfTruthMessage} does not exist:\n` + `${resolvedAssetsPath}`, From b9a859fb9cceb9fdd5ffef07f474de6ce78f1e86 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 20 Jun 2025 13:35:21 +0100 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Victor Berchet --- .changeset/thick-dots-sit.md | 10 ++++------ .../tests/get-platform-proxy.env.test.ts | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/.changeset/thick-dots-sit.md b/.changeset/thick-dots-sit.md index 466fce066ac8..2ac3789ab4e6 100644 --- a/.changeset/thick-dots-sit.md +++ b/.changeset/thick-dots-sit.md @@ -9,8 +9,8 @@ file contains an assets field, correctly returns the appropriate asset binding p example: -```json -// wrangler.json +```jsonc +// wrangler.jsonc { "name": "my-worker", "assets": { @@ -25,10 +25,8 @@ import { getPlatformProxy } from "wrangler"; const { env, dispose } = await getPlatformProxy(); -if (env.ASSETS) { - const text = await (await env.ASSETS.fetch("http://0.0.0.0/file.txt")).text(); - console.log(text); // logs the content of file.txt -} +const text = await (await env.ASSETS.fetch("http://0.0.0.0/file.txt")).text(); +console.log(text); // logs the content of file.txt await dispose(); ``` 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 88d00601f704..2fc44412ffa4 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 @@ -132,8 +132,7 @@ describe("getPlatformProxy - env", () => { const { env, dispose } = await getPlatformProxy({ configPath: wranglerConfigFilePath, }); - const { ASSETS } = env; - const res = await ASSETS.fetch("https://0.0.0.0/test.txt"); + const res = await env.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(); From 2a2c5be6406e007a7f100336e4133ae6f886db4b Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 20 Jun 2025 13:44:22 +0100 Subject: [PATCH 3/5] fix formatting --- .changeset/thick-dots-sit.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/thick-dots-sit.md b/.changeset/thick-dots-sit.md index 2ac3789ab4e6..9170ed557086 100644 --- a/.changeset/thick-dots-sit.md +++ b/.changeset/thick-dots-sit.md @@ -15,8 +15,8 @@ example: "name": "my-worker", "assets": { "directory": "./public/", - "binding": "ASSETS" - } + "binding": "ASSETS", + }, } ``` From ed29b4562f4c5b93e0972fb98cfb7f85d2edd7ac Mon Sep 17 00:00:00 2001 From: Carmen Popoviciu Date: Wed, 25 Jun 2025 13:34:31 +0200 Subject: [PATCH 4/5] fix CI --- packages/wrangler/src/api/integrations/platform/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wrangler/src/api/integrations/platform/index.ts b/packages/wrangler/src/api/integrations/platform/index.ts index 88ec38601403..9d753a541269 100644 --- a/packages/wrangler/src/api/integrations/platform/index.ts +++ b/packages/wrangler/src/api/integrations/platform/index.ts @@ -175,7 +175,7 @@ async function getMiniflareOptionsFromConfig( let processedAssetOptions: AssetsOptions | undefined; try { - processedAssetOptions = getAssetsOptions({ assets: undefined }, rawConfig); + processedAssetOptions = getAssetsOptions({ assets: undefined }, config); } catch (e) { const isNonExistentError = e instanceof NonExistentAssetsDirError; // we want to loosen up the assets directory existence restriction here, From 3b4e973fc42284457f1686e371742dbf9618ef34 Mon Sep 17 00:00:00 2001 From: Carmen Popoviciu Date: Wed, 25 Jun 2025 13:57:13 +0200 Subject: [PATCH 5/5] Update packages/wrangler/src/api/integrations/platform/index.ts --- packages/wrangler/src/api/integrations/platform/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wrangler/src/api/integrations/platform/index.ts b/packages/wrangler/src/api/integrations/platform/index.ts index 9d753a541269..88ec38601403 100644 --- a/packages/wrangler/src/api/integrations/platform/index.ts +++ b/packages/wrangler/src/api/integrations/platform/index.ts @@ -175,7 +175,7 @@ async function getMiniflareOptionsFromConfig( let processedAssetOptions: AssetsOptions | undefined; try { - processedAssetOptions = getAssetsOptions({ assets: undefined }, config); + processedAssetOptions = getAssetsOptions({ assets: undefined }, rawConfig); } catch (e) { const isNonExistentError = e instanceof NonExistentAssetsDirError; // we want to loosen up the assets directory existence restriction here,