Skip to content

Commit 831f892

Browse files
andyjessopoliy
andauthored
Add pipeline binding to wrangler.toml (#6674)
* Add [[pipelines]] binding in wrangler. * chore: fix lint and formatting errors * chore: fix test * chore: remove only * chore: update wording * chore: fix tests --------- Co-authored-by: Oli Yu <[email protected]> Co-authored-by: Andy Jessop <[email protected]>
1 parent 7579bd8 commit 831f892

File tree

18 files changed

+271
-2
lines changed

18 files changed

+271
-2
lines changed

.changeset/angry-keys-dance.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Added new [[pipelines]] bindings. This creates a new binding that allows sending events to
6+
the specified pipeline.
7+
8+
Example:
9+
10+
[[pipelines]]
11+
binding = "MY_PIPELINE"
12+
pipeline = "my-pipeline"

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ packages/create-cloudflare/templates/**/*.*
2424
# but still exclude the worker-configuration.d.ts file, since it's generated
2525
!packages/create-cloudflare/templates/hello-world/**/*.*
2626
packages/create-cloudflare/templates/hello-world/**/worker-configuration.d.ts
27+
# dist-functions are generated in the fixtures/vitest-pool-workers-examples/pages-functions-unit-integration-self folder
28+
dist-functions
2729

2830
vscode.d.ts
2931
vscode.*.d.ts

packages/wrangler/src/__tests__/configuration.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ describe("normalizeAndValidateConfig()", () => {
130130
upload_source_maps: undefined,
131131
placement: undefined,
132132
tail_consumers: undefined,
133+
pipelines: [],
133134
});
134135
expect(diagnostics.hasErrors()).toBe(false);
135136
expect(diagnostics.hasWarnings()).toBe(false);
@@ -3181,6 +3182,114 @@ describe("normalizeAndValidateConfig()", () => {
31813182
});
31823183
});
31833184

3185+
describe("[pipelines]", () => {
3186+
it("should error if pipelines is an object", () => {
3187+
const { diagnostics } = normalizeAndValidateConfig(
3188+
// @ts-expect-error purposely using an invalid value
3189+
{ pipelines: {} },
3190+
undefined,
3191+
{ env: undefined }
3192+
);
3193+
3194+
expect(diagnostics.hasWarnings()).toBe(false);
3195+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3196+
"Processing wrangler configuration:
3197+
- The field \\"pipelines\\" should be an array but got {}."
3198+
`);
3199+
});
3200+
3201+
it("should error if pipelines is a string", () => {
3202+
const { diagnostics } = normalizeAndValidateConfig(
3203+
// @ts-expect-error purposely using an invalid value
3204+
{ pipelines: "BAD" },
3205+
undefined,
3206+
{ env: undefined }
3207+
);
3208+
3209+
expect(diagnostics.hasWarnings()).toBe(false);
3210+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3211+
"Processing wrangler configuration:
3212+
- The field \\"pipelines\\" should be an array but got \\"BAD\\"."
3213+
`);
3214+
});
3215+
3216+
it("should error if pipelines is a number", () => {
3217+
const { diagnostics } = normalizeAndValidateConfig(
3218+
// @ts-expect-error purposely using an invalid value
3219+
{ pipelines: 999 },
3220+
undefined,
3221+
{ env: undefined }
3222+
);
3223+
3224+
expect(diagnostics.hasWarnings()).toBe(false);
3225+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3226+
"Processing wrangler configuration:
3227+
- The field \\"pipelines\\" should be an array but got 999."
3228+
`);
3229+
});
3230+
3231+
it("should error if pipelines is null", () => {
3232+
const { diagnostics } = normalizeAndValidateConfig(
3233+
// @ts-expect-error purposely using an invalid value
3234+
{ pipelines: null },
3235+
undefined,
3236+
{ env: undefined }
3237+
);
3238+
3239+
expect(diagnostics.hasWarnings()).toBe(false);
3240+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3241+
"Processing wrangler configuration:
3242+
- The field \\"pipelines\\" should be an array but got null."
3243+
`);
3244+
});
3245+
3246+
it("should accept valid bindings", () => {
3247+
const { diagnostics } = normalizeAndValidateConfig(
3248+
{
3249+
pipelines: [
3250+
{
3251+
binding: "VALID",
3252+
pipeline: "343cd4f1d58c42fbb5bd082592fd7143",
3253+
},
3254+
],
3255+
} as unknown as RawConfig,
3256+
undefined,
3257+
{ env: undefined }
3258+
);
3259+
3260+
expect(diagnostics.hasErrors()).toBe(false);
3261+
});
3262+
3263+
it("should error if pipelines.bindings are not valid", () => {
3264+
const { diagnostics } = normalizeAndValidateConfig(
3265+
{
3266+
pipelines: [
3267+
{},
3268+
{
3269+
binding: "VALID",
3270+
pipeline: "343cd4f1d58c42fbb5bd082592fd7143",
3271+
},
3272+
{ binding: 2000, project: 2111 },
3273+
],
3274+
} as unknown as RawConfig,
3275+
undefined,
3276+
{ env: undefined }
3277+
);
3278+
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
3279+
"Processing wrangler configuration:
3280+
- Unexpected fields found in pipelines[2] field: \\"project\\""
3281+
`);
3282+
expect(diagnostics.hasWarnings()).toBe(true);
3283+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
3284+
"Processing wrangler configuration:
3285+
- \\"pipelines[0]\\" bindings must have a string \\"binding\\" field but got {}.
3286+
- \\"pipelines[0]\\" bindings must have a string \\"pipeline\\" field but got {}.
3287+
- \\"pipelines[2]\\" bindings must have a string \\"binding\\" field but got {\\"binding\\":2000,\\"project\\":2111}.
3288+
- \\"pipelines[2]\\" bindings must have a string \\"pipeline\\" field but got {\\"binding\\":2000,\\"project\\":2111}."
3289+
`);
3290+
});
3291+
});
3292+
31843293
describe("[unsafe.bindings]", () => {
31853294
it("should error if unsafe is an array", () => {
31863295
const { diagnostics } = normalizeAndValidateConfig(

packages/wrangler/src/__tests__/deploy.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10615,6 +10615,43 @@ export default{
1061510615
});
1061610616
});
1061710617

10618+
describe("pipelines", () => {
10619+
it("should upload pipelines bindings", async () => {
10620+
writeWranglerToml({
10621+
pipelines: [
10622+
{
10623+
binding: "MY_PIPELINE",
10624+
pipeline: "0123456789ABCDEF0123456789ABCDEF",
10625+
},
10626+
],
10627+
});
10628+
await fs.promises.writeFile("index.js", `export default {};`);
10629+
mockSubDomainRequest();
10630+
mockUploadWorkerRequest({
10631+
expectedBindings: [
10632+
{
10633+
type: "pipelines",
10634+
name: "MY_PIPELINE",
10635+
id: "0123456789ABCDEF0123456789ABCDEF",
10636+
},
10637+
],
10638+
});
10639+
10640+
await runWrangler("deploy index.js");
10641+
expect(std.out).toMatchInlineSnapshot(`
10642+
"Total Upload: xx KiB / gzip: xx KiB
10643+
Worker Startup Time: 100 ms
10644+
Your worker has access to the following bindings:
10645+
- Pipelines:
10646+
- MY_PIPELINE: 0123456789ABCDEF0123456789ABCDEF
10647+
Uploaded test-name (TIMINGS)
10648+
Deployed test-name triggers (TIMINGS)
10649+
https://test-name.test-sub-domain.workers.dev
10650+
Current Version ID: Galaxy-Class"
10651+
`);
10652+
});
10653+
});
10654+
1061810655
describe("--keep-vars", () => {
1061910656
it("should send keepVars when keep-vars is passed in", async () => {
1062010657
vi.stubEnv("CLOUDFLARE_API_TOKEN", "hunter2");

packages/wrangler/src/__tests__/type-generation.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ const bindingsConfigMock: Omit<
217217
},
218218
{ type: "CompiledWasm", globs: ["**/*.wasm"], fallthrough: true },
219219
],
220+
pipelines: [],
220221
};
221222

222223
describe("generateTypes()", () => {

packages/wrangler/src/api/pages/create-worker-bundle-contents.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function createWorkerBundleFormData(
6666
text_blobs: undefined,
6767
data_blobs: undefined,
6868
dispatch_namespaces: undefined,
69+
pipelines: undefined,
6970
logfwdr: undefined,
7071
unsafe: undefined,
7172
experimental_assets: undefined,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
CfLogfwdrBinding,
1717
CfModule,
1818
CfMTlsCertificate,
19+
CfPipeline,
1920
CfQueue,
2021
CfR2Bucket,
2122
CfScriptFormat,
@@ -261,6 +262,7 @@ export type Binding =
261262
| ({ type: "analytics_engine" } & Omit<CfAnalyticsEngineDataset, "binding">)
262263
| ({ type: "dispatch_namespace" } & Omit<CfDispatchNamespace, "binding">)
263264
| ({ type: "mtls_certificate" } & Omit<CfMTlsCertificate, "binding">)
265+
| ({ type: "pipeline" } & Omit<CfPipeline, "binding">)
264266
| ({ type: "logfwdr" } & Omit<CfLogfwdrBinding, "name">)
265267
| { type: `unsafe_${string}` }
266268
| { type: "assets" };

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ export function convertCfWorkerInitBindingstoBindings(
244244
output[info["binding"]] = { type: "assets" };
245245
break;
246246
}
247+
case "pipelines": {
248+
for (const { binding, ...x } of info) {
249+
output[binding] = { type: "pipeline", ...x };
250+
}
251+
break;
252+
}
247253
default: {
248254
assertNever(type);
249255
}
@@ -282,6 +288,7 @@ export async function convertBindingsToCfWorkerInitBindings(
282288
logfwdr: undefined,
283289
unsafe: undefined,
284290
experimental_assets: undefined,
291+
pipelines: undefined,
285292
};
286293

287294
const fetchers: Record<string, ServiceFetch> = {};
@@ -354,6 +361,9 @@ export async function convertBindingsToCfWorkerInitBindings(
354361
} else if (binding.type === "mtls_certificate") {
355362
bindings.mtls_certificates ??= [];
356363
bindings.mtls_certificates.push({ ...binding, binding: name });
364+
} else if (binding.type === "pipeline") {
365+
bindings.pipelines ??= [];
366+
bindings.pipelines.push({ ...binding, binding: name });
357367
} else if (binding.type === "logfwdr") {
358368
bindings.logfwdr ??= { bindings: [] };
359369
bindings.logfwdr.bindings.push({ ...binding, name: name });

packages/wrangler/src/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,5 @@ export const defaultWranglerConfig: Config = {
402402
},
403403
mtls_certificates: [],
404404
tail_consumers: undefined,
405+
pipelines: [],
405406
};

packages/wrangler/src/config/environment.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,23 @@ export interface EnvironmentNonInheritable {
754754
/** Details about the outbound Worker which will handle outbound requests from your namespace */
755755
outbound?: DispatchNamespaceOutbound;
756756
}[];
757+
758+
/**
759+
* Specifies list of Pipelines bound to this Worker environment
760+
*
761+
* NOTE: This field is not automatically inherited from the top level environment,
762+
* and so must be specified in every named environment.
763+
*
764+
* @default `[]`
765+
* @nonInheritable
766+
*/
767+
pipelines: {
768+
/** The binding name used to refer to the bound service. */
769+
binding: string;
770+
771+
/** Name of the Pipeline to bind */
772+
pipeline: string;
773+
}[];
757774
}
758775

759776
/**

0 commit comments

Comments
 (0)