Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/open-points-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

Allow users to provide an `account_id` as part of the `WorkerConfigObject` they pass to `maybeStartOrUpdateRemoteProxySession`
5 changes: 5 additions & 0 deletions .changeset/tough-crabs-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/vite-plugin": patch
---

Make sure that the `account_id` present in the user's config file is used for remote bindings
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@cloudflare/vite-plugin-e2e-remote-bindings-config-account-id",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"build": "vite build",
"buildAndPreview": "vite build && vite preview",
"dev": "vite",
"lint": "eslint .",
"preview": "vite preview"
},
"devDependencies": {
"@cloudflare/vite-plugin": "*",
"@cloudflare/workers-types": "^4.20250204.0",
"@eslint/js": "^9.19.0",
"eslint": "^9.19.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.18",
"globals": "^15.14.0",
"miniflare": "*",
"typescript": "~5.7.2",
"typescript-eslint": "^8.22.0",
"vite": "^6.1.0",
"wrangler": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch() {
return new Response("Hello world");
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.node.json" },
{ "path": "./tsconfig.worker.json" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"target": "ES2022",
"lib": ["ES2023"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": ["vite.config.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.node.json",
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.worker.tsbuildinfo",
"types": ["@cloudflare/workers-types/2023-07-01", "vite/client"]
},
"include": ["src"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { cloudflare } from "@cloudflare/vite-plugin";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [
cloudflare({
configPath: "./wrangler.jsonc",
inspectorPort: false,
persistState: false,
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "cloudflare-vite-e2e-remote-bindings-config-account-id-worker",
"main": "./src/index.ts",
"compatibility_date": "2024-12-30",
"compatibility_flags": ["nodejs_compat"],
"account_id": "not-a-valid-account-id-abc",
"services": [
{
"binding": "REMOTE_WORKER",
"service": "MY_REMOTE_WORKER",
"remote": true,
},
],
}
8 changes: 6 additions & 2 deletions packages/vite-plugin-cloudflare/e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
onTestFinished,
vi,
} from "vitest";
import wranglerPackage from "../../wrangler/package.json";
import vitePluginPackage from "../package.json";

const debuglog = util.debuglog("vite-plugin:test");
Expand Down Expand Up @@ -52,7 +53,7 @@ export function seed(
errorOnExist: true,
});
debuglog("Fixture copied to " + projectPath);
await updateVitePluginVersion(projectPath);
await updateVitePluginVersionAndAddWrangler(projectPath);
debuglog("Fixing up replacements in seeded files");
await fixupReplacements(projectPath, replacements);
debuglog("Updated vite-plugin version in package.json");
Expand Down Expand Up @@ -169,7 +170,7 @@ function wrap(proc: childProcess.ChildProcess): Process {
return wrappedProc;
}

async function updateVitePluginVersion(projectPath: string) {
async function updateVitePluginVersionAndAddWrangler(projectPath: string) {
const pkg = JSON.parse(
await fs.readFile(path.resolve(projectPath, "package.json"), "utf8")
);
Expand All @@ -178,6 +179,9 @@ async function updateVitePluginVersion(projectPath: string) {
if (pkg[field]?.["@cloudflare/vite-plugin"]) {
pkg[field]["@cloudflare/vite-plugin"] = vitePluginPackage.version;
}
if (pkg[field]?.["wrangler"]) {
pkg[field]["wrangler"] = wranglerPackage.version;
}
}
await fs.writeFile(
path.resolve(projectPath, "package.json"),
Expand Down
22 changes: 22 additions & 0 deletions packages/vite-plugin-cloudflare/e2e/remote-bindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@ if (!process.env.CLOUDFLARE_ACCOUNT_ID || !process.env.CLOUDFLARE_API_TOKEN) {
});
});
}

describe("remote bindings without actually establishing a remote connection", () => {
const projectPath = seed("remote-bindings-config-account-id", "pnpm");

test("for connection to remote bindings during dev the account_id present in the wrangler config file is used", async ({
expect,
}) => {
const proc = await runLongLived("pnpm", "dev", projectPath);
await vi.waitFor(
async () => {
expect(proc.stderr).toMatch(
// Note: this error message shows that we're attempting to establish the remote proxy connection
// using the "not-a-valid-account-id-abc" account id
/A request to the Cloudflare API \(\/accounts\/not-a-valid-account-id-abc\/.*?\) failed/
);
},
{
timeout: 10_000,
}
);
});
});
2 changes: 2 additions & 0 deletions packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ export async function getDevMiniflareOptions(config: {
{
name: workerConfig.name,
bindings: bindings ?? {},
account_id: workerConfig.account_id,
},
preExistingRemoteProxySession ?? null
)
Expand Down Expand Up @@ -775,6 +776,7 @@ export async function getPreviewMiniflareOptions(config: {
{
name: workerConfig.name,
bindings: bindings ?? {},
account_id: workerConfig.account_id,
},
preExistingRemoteProxySessionData ?? null
)
Expand Down
22 changes: 19 additions & 3 deletions packages/wrangler/src/api/remoteBindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type WorkerConfigObject = {
bindings: NonNullable<StartDevWorkerInput["bindings"]>;
/** If running in a non-public compliance region, set this here. */
complianceRegion?: Config["compliance_region"];
/** Id of the account owning the worker */
account_id?: Config["account_id"];
};

/**
Expand Down Expand Up @@ -110,7 +112,14 @@ export async function maybeStartOrUpdateRemoteProxySession(
remoteProxySession = await startRemoteProxySession(remoteBindings, {
workerName: workerConfigObject.name,
complianceRegion: workerConfigObject.complianceRegion,
auth: getAuthHook(auth, config),
auth: getAuthHook(
auth,
workerConfigObject.account_id
? {
account_id: workerConfigObject.account_id,
}
: config
),
});
} else {
// The auth values haven't changed so we can reuse the pre-existing session
Expand All @@ -127,7 +136,14 @@ export async function maybeStartOrUpdateRemoteProxySession(
remoteProxySession = await startRemoteProxySession(remoteBindings, {
workerName: workerConfigObject.name,
complianceRegion: workerConfigObject.complianceRegion,
auth: getAuthHook(auth, config),
auth: getAuthHook(
auth,
workerConfigObject.account_id
? {
account_id: workerConfigObject.account_id,
}
: config
),
});
}
} else {
Expand Down Expand Up @@ -160,7 +176,7 @@ export async function maybeStartOrUpdateRemoteProxySession(
*/
function getAuthHook(
auth: CfAccount | undefined,
config: Config | undefined
config: Pick<Config, "account_id"> | undefined
): AsyncHook<CfAccount, [Pick<Config, "account_id">]> | undefined {
if (auth) {
return auth;
Expand Down
Loading