Skip to content

Commit 4db1fb5

Browse files
Add Worker Pipelines Binding to Miniflare (#7950)
* Add Worker Pipeline binding support Adding support to Miniflare to reference bindings during `wrangler dev` or during tests via vitest. This binding uses newer JSRPC method instead of propagating changes through `workerd.capnp`. * Enable Workers Pipelines test for wrangler dev * Add vitest example for Pipelines * Wire Pipelines from Miniflare to Wrangler dev * Update packages/wrangler/e2e/dev-with-resources.test.ts Pipelines does not support remote bindings, only run this test for local e2e tests. * Add id to service name * Use correct string interpolation Co-authored-by: emily-shen <[email protected]> * Make pipelines not backed by DO resources building off of Emily's feedback on PR #7950 and example commit 56c1e43 * Mark wrangler and miniflare as patch, update README and test output in e2e * Read output until pipeline message is logged --------- Co-authored-by: emily-shen <[email protected]>
1 parent d4d5987 commit 4db1fb5

File tree

20 files changed

+242
-3
lines changed

20 files changed

+242
-3
lines changed

.changeset/slimy-otters-rush.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"miniflare": patch
3+
"wrangler": patch
4+
---
5+
6+
Add local binding support for Worker Pipelines

fixtures/vitest-pool-workers-examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This directory contains example projects tested with `@cloudflare/vitest-pool-wo
1111
| [📚 d1](d1) | Isolated tests using D1 with migrations |
1212
| [📌 durable-objects](durable-objects) | Isolated tests using Durable Objects with direct access |
1313
| [🚥 queues](queues) | Tests using Queue producers and consumers |
14+
| [🚰 pipelines](pipelines) | Tests using Pipelines |
1415
| [🚀 hyperdrive](hyperdrive) | Tests using Hyperdrive with a Vitest managed TCP server |
1516
| [🤹 request-mocking](request-mocking) | Tests using declarative/imperative outbound request mocks |
1617
| [🔌 multiple-workers](multiple-workers) | Tests using multiple auxiliary workers and request mocks |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 🚰 pipelines
2+
3+
This Worker implements endpoint that send details of the incoming HTTP request to a Pipeline.
4+
5+
| Test | Overview |
6+
| ------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
7+
| [pipeline-send-integration-self.test.ts](test/pipeline-send-integration-self.test.ts) | Integration tests for endpoints using `SELF` |
8+
| [pipeline-send-unit.test.ts](test/pipeline-send-unit.test.ts) | Unit tests calling `worker.fetch()` directly mocking the Pipeline |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Env {
2+
PIPELINE: Pipeline;
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
async fetch(request, env, ctx) {
3+
await env.PIPELINE.send([
4+
{
5+
method: request.method.toUpperCase(),
6+
url: request.url,
7+
},
8+
]);
9+
return new Response("Accepted", { status: 202 });
10+
},
11+
} satisfies ExportedHandler<Env>;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.workerd.json",
3+
"include": ["./**/*.ts"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "cloudflare:test" {
2+
// Controls the type of `import("cloudflare:test").env`
3+
interface ProvidedEnv extends Env {}
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { SELF } from "cloudflare:test";
2+
import { expect, it } from "vitest";
3+
4+
it("sends message to pipeline", async () => {
5+
// Send data to the Pipeline
6+
let response = await SELF.fetch("https://example.com/ingest", {
7+
method: "POST",
8+
body: "value",
9+
});
10+
expect(response.status).toBe(202);
11+
expect(await response.text()).toBe("Accepted");
12+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
createExecutionContext,
3+
env,
4+
waitOnExecutionContext,
5+
} from "cloudflare:test";
6+
import { afterEach, expect, it, vi } from "vitest";
7+
import worker from "../src/index";
8+
9+
// This will improve in the next major version of `@cloudflare/workers-types`,
10+
// but for now you'll need to do something like this to get a correctly-typed
11+
// `Request` to pass to `worker.fetch()`.
12+
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
13+
14+
afterEach(() => {
15+
vi.restoreAllMocks();
16+
});
17+
18+
it("produces message to pipeline", async () => {
19+
const mockPipeline = {
20+
send: vi.fn().mockResolvedValue(undefined),
21+
};
22+
23+
const testEnv = {
24+
...env,
25+
PIPELINE: mockPipeline,
26+
};
27+
28+
// Send data to pipeline
29+
const request = new IncomingRequest("https://example.com/ingest", {
30+
method: "POST",
31+
body: "value",
32+
});
33+
const ctx = createExecutionContext();
34+
const response = await worker.fetch(request, testEnv, ctx);
35+
await waitOnExecutionContext(ctx);
36+
37+
expect(response.status).toBe(202);
38+
expect(await response.text()).toBe("Accepted");
39+
40+
// Check `PIPELINE.send()` was called
41+
expect(mockPipeline.send).toBeCalledTimes(1);
42+
expect(mockPipeline.send).toBeCalledWith([
43+
{ method: "POST", url: "https://example.com/ingest" },
44+
]);
45+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.workerd-test.json",
3+
"include": ["./**/*.ts", "../src/env.d.ts"]
4+
}

0 commit comments

Comments
 (0)