Skip to content

Commit dceb550

Browse files
update unstable_convertConfigBindingsToStartWorkerBindings to prioritize preview config values (#10534)
1 parent dfce01f commit dceb550

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed

.changeset/fine-days-mix.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Ensure that preview config values are used for remote bindings
6+
7+
Ensure that if in some remote binding configuration provides a preview value (e.g. `preview_database_id` for D1 bindings) such value gets used instead of the standard ones

.changeset/many-carrots-boil.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
update `unstable_convertConfigBindingsToStartWorkerBindings` to prioritize preview config values
6+
7+
Ensure that if some bindings include preview values (e.g. `preview_database_id` for D1 bindings) those get used instead of the standard ones (since these are the ones that start worker should be using)
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import assert from "node:assert";
2+
import { describe, it } from "vitest";
3+
import { convertConfigBindingsToStartWorkerBindings } from "../../../api/startDevWorker/utils";
4+
5+
describe("convertConfigBindingsToStartWorkerBindings", () => {
6+
it("converts config bindings into startWorker bindings", async () => {
7+
const result = convertConfigBindingsToStartWorkerBindings({
8+
kv_namespaces: [
9+
{
10+
id: "<kv_id>",
11+
binding: "MY_KV",
12+
},
13+
],
14+
ai: { binding: "AI" },
15+
browser: { binding: "BROWSER" },
16+
d1_databases: [
17+
{
18+
database_id: "<database_id>",
19+
database_name: "my-database",
20+
binding: "MY_DB",
21+
},
22+
],
23+
dispatch_namespaces: [
24+
{
25+
binding: "MY_DISPATCH_NAMESPACE",
26+
namespace: "namespace",
27+
},
28+
],
29+
durable_objects: {
30+
bindings: [
31+
{
32+
class_name: "MyDo",
33+
name: "MY_DO",
34+
},
35+
],
36+
},
37+
queues: {
38+
producers: [
39+
{
40+
binding: "MY_QUEUE_PRODUCER",
41+
queue: "my-queue",
42+
},
43+
],
44+
consumers: undefined,
45+
},
46+
r2_buckets: [
47+
{
48+
binding: "MY_R2",
49+
bucket_name: "my-bucket",
50+
},
51+
],
52+
services: [
53+
{
54+
binding: "MY_SERVICE",
55+
service: "my-service",
56+
},
57+
],
58+
mtls_certificates: [
59+
{
60+
binding: "MTLS",
61+
certificate_id: "123",
62+
},
63+
],
64+
vectorize: [
65+
{
66+
binding: "MY_VECTORIZE",
67+
index_name: "idx",
68+
},
69+
],
70+
workflows: [
71+
{
72+
binding: "MY_WORKFLOW",
73+
name: "workflow",
74+
class_name: "MyWorkflow",
75+
},
76+
],
77+
});
78+
expect(result).toEqual({
79+
AI: {
80+
type: "ai",
81+
},
82+
BROWSER: {
83+
type: "browser",
84+
},
85+
MTLS: {
86+
certificate_id: "123",
87+
type: "mtls_certificate",
88+
},
89+
MY_DB: {
90+
database_id: "<database_id>",
91+
database_name: "my-database",
92+
type: "d1",
93+
},
94+
MY_DISPATCH_NAMESPACE: {
95+
namespace: "namespace",
96+
type: "dispatch_namespace",
97+
},
98+
MY_DO: {
99+
class_name: "MyDo",
100+
type: "durable_object_namespace",
101+
},
102+
MY_KV: {
103+
id: "<kv_id>",
104+
type: "kv_namespace",
105+
},
106+
MY_QUEUE_PRODUCER: {
107+
queue: "my-queue",
108+
queue_name: "my-queue",
109+
type: "queue",
110+
},
111+
MY_R2: {
112+
bucket_name: "my-bucket",
113+
type: "r2_bucket",
114+
},
115+
MY_SERVICE: {
116+
service: "my-service",
117+
type: "service",
118+
},
119+
MY_VECTORIZE: {
120+
index_name: "idx",
121+
type: "vectorize",
122+
},
123+
MY_WORKFLOW: {
124+
class_name: "MyWorkflow",
125+
name: "workflow",
126+
type: "workflow",
127+
},
128+
});
129+
});
130+
131+
it("prioritizes preview values compared to their standard counterparts", async () => {
132+
const result = convertConfigBindingsToStartWorkerBindings({
133+
ai: undefined,
134+
browser: undefined,
135+
vectorize: [],
136+
d1_databases: [
137+
{
138+
binding: "MY_DB",
139+
database_id: "production-db-id",
140+
preview_database_id: "staging-db-id",
141+
},
142+
],
143+
dispatch_namespaces: [],
144+
durable_objects: {
145+
bindings: [],
146+
},
147+
queues: {
148+
producers: undefined,
149+
consumers: undefined,
150+
},
151+
r2_buckets: [
152+
{
153+
binding: "MY_R2",
154+
bucket_name: "production-bucket-name",
155+
preview_bucket_name: "staging-bucket-name",
156+
},
157+
],
158+
services: undefined,
159+
kv_namespaces: [
160+
{
161+
binding: "MY_KV",
162+
id: "production-kv-id",
163+
preview_id: "staging-kv-id",
164+
},
165+
],
166+
mtls_certificates: [],
167+
workflows: [],
168+
});
169+
170+
assert(result);
171+
assert(result.MY_KV.type === "kv_namespace");
172+
expect(result.MY_KV.id).toBe("staging-kv-id");
173+
174+
assert(result);
175+
assert(result.MY_R2.type === "r2_bucket");
176+
expect(result.MY_R2.bucket_name).toBe("staging-bucket-name");
177+
178+
assert(result);
179+
assert(result.MY_DB.type === "d1");
180+
expect(result.MY_DB.database_id).toBe("staging-db-id");
181+
});
182+
});

packages/wrangler/src/api/startDevWorker/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ export function convertConfigBindingsToStartWorkerBindings(
8181

8282
return convertCfWorkerInitBindingsToBindings({
8383
...bindings,
84+
kv_namespaces: bindings.kv_namespaces.map((kv) => ({
85+
...kv,
86+
id: kv.preview_id ?? kv.id,
87+
})),
88+
d1_databases: bindings.d1_databases.map((d1) => ({
89+
...d1,
90+
database_id: d1.preview_database_id ?? d1.database_id,
91+
})),
92+
r2_buckets: bindings.r2_buckets.map((r2) => ({
93+
...r2,
94+
bucket_name: r2.preview_bucket_name ?? r2.bucket_name,
95+
})),
8496
queues: queues.producers?.map((q) => ({ ...q, queue_name: q.queue })),
8597
});
8698
}

0 commit comments

Comments
 (0)