Skip to content

Commit df04528

Browse files
authored
Add tests for AE, and support rate limiting in the Vite plugin (#9803)
* Add tests for AE, and support rate limiting in the Vite plugin * changeset
1 parent 2f4b891 commit df04528

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed

.changeset/lemon-bobcats-lose.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": minor
3+
---
4+
5+
Support Workers Analytics Engine & Rate Limiting bindings

packages/vite-plugin-cloudflare/playground/bindings/__tests__/worker.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ test("unsafe_hello_world support", async () => {
1010
const response = await getTextResponse("/hello-world");
1111
expect(response).toBe("Hello World binding works");
1212
});
13+
14+
test("analytics_engine support", async () => {
15+
const response = await getTextResponse("/ae");
16+
expect(response).toBe("AE binding works");
17+
});
18+
19+
test("ratelimit support", async () => {
20+
const response = await getTextResponse("/rate-limit");
21+
expect(response).toBe("Rate limit binding works: first: true, second: false");
22+
});

packages/vite-plugin-cloudflare/playground/bindings/src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ export default {
1818
status: 200,
1919
});
2020
}
21+
case "/ae": {
22+
await env.WAE.writeDataPoint({ doubles: [2, 3] });
23+
24+
return new Response("AE binding works", {
25+
status: 200,
26+
});
27+
}
28+
case "/rate-limit": {
29+
const { success: first } = await env.RATE_LIMITER.limit({
30+
key: "shared-key",
31+
});
32+
const { success: second } = await env.RATE_LIMITER.limit({
33+
key: "shared-key",
34+
});
35+
36+
return new Response(
37+
`Rate limit binding works: first: ${first}, second: ${second}`,
38+
{
39+
status: 200,
40+
}
41+
);
42+
}
2143
case "/hello-world": {
2244
const value = Math.floor(Date.now() * Math.random()).toString(36);
2345
await env.HELLO_WORLD.set(value);
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* eslint-disable */
2-
// Generated by Wrangler by running `wrangler types --include-runtime=false` (hash: 2aff1a83eda1c659fd494a8ca649c27a)
2+
// Generated by Wrangler by running `wrangler types --no-include-runtime` (hash: c9b4a08f8a0baf679382de06c1dd7d1e)
33
declare namespace Cloudflare {
44
interface Env {
55
KV: KVNamespace;
66
HELLO_WORLD: HelloWorldBinding;
7+
WAE: AnalyticsEngineDataset;
8+
RATE_LIMITER: RateLimit;
79
}
810
}
911
interface Env extends Cloudflare.Env {}

packages/vite-plugin-cloudflare/playground/bindings/wrangler.jsonc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "./node_modules/wrangler/config-schema.json",
23
"name": "worker",
34
"main": "./src/index.ts",
45
"compatibility_date": "2024-12-30",
@@ -13,4 +14,23 @@
1314
"binding": "HELLO_WORLD",
1415
},
1516
],
17+
"analytics_engine_datasets": [
18+
{
19+
"dataset": "test",
20+
"binding": "WAE",
21+
},
22+
],
23+
"unsafe": {
24+
"bindings": [
25+
{
26+
"name": "RATE_LIMITER",
27+
"type": "ratelimit",
28+
"namespace_id": "1001",
29+
"simple": {
30+
"limit": 1,
31+
"period": 60,
32+
},
33+
},
34+
],
35+
},
1636
}

packages/vite-plugin-cloudflare/src/miniflare-options.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ export async function getDevMiniflareOptions(
398398

399399
const { externalWorkers } = miniflareWorkerOptions;
400400

401-
const { ratelimits, ...workerOptions } =
402-
miniflareWorkerOptions.workerOptions;
401+
const workerOptions = miniflareWorkerOptions.workerOptions;
403402

404403
return {
405404
externalWorkers,
@@ -700,7 +699,7 @@ export async function getPreviewMiniflareOptions(
700699

701700
const { externalWorkers } = miniflareWorkerOptions;
702701

703-
const { ratelimits, modulesRules, ...workerOptions } =
702+
const { modulesRules, ...workerOptions } =
704703
miniflareWorkerOptions.workerOptions;
705704

706705
logUnknownTails(

0 commit comments

Comments
 (0)