Skip to content

Commit 7d58c2c

Browse files
oliyandyjessop
authored andcommitted
Add [[pipelines]] binding in wrangler.
1 parent 8527675 commit 7d58c2c

File tree

16 files changed

+284
-14
lines changed

16 files changed

+284
-14
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"

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 should have a string \\"binding\\" field but got {}.
3286+
- \\"pipelines[0]\\" bindings must have a \\"pipeline\\" field but got {}.
3287+
- \\"pipelines[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"project\\":2111}.
3288+
- \\"pipelines[2]\\" bindings must have a \\"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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10524,6 +10524,47 @@ export default{
1052410524
});
1052510525
});
1052610526

10527+
describe("pipelines", () => {
10528+
it("should upload pipelines bindings", async () => {
10529+
writeWranglerToml({
10530+
pipelines: [
10531+
{
10532+
binding: "MY_PIPELINE",
10533+
pipeline: "0123456789ABCDEF0123456789ABCDEF",
10534+
},
10535+
],
10536+
});
10537+
await fs.promises.writeFile("index.js", `export default {};`);
10538+
mockSubDomainRequest();
10539+
mockUploadWorkerRequest({
10540+
expectedBindings: [
10541+
{
10542+
type: "pipelines",
10543+
name: "MY_PIPELINE",
10544+
id: "0123456789ABCDEF0123456789ABCDEF",
10545+
},
10546+
],
10547+
});
10548+
10549+
await runWrangler("deploy index.js");
10550+
expect(std.out).toMatchInlineSnapshot(`
10551+
"Total Upload: xx KiB / gzip: xx KiB
10552+
Worker Startup Time: 100 ms
10553+
Your worker has access to the following bindings:
10554+
- Pipelines:
10555+
- MY_PIPELINE: 0123456789ABCDEF0123456789ABCDEF
10556+
Uploaded test-name (TIMINGS)
10557+
Published test-name (TIMINGS)
10558+
https://test-name.test-sub-domain.workers.dev
10559+
Current Deployment ID: Galaxy-Class
10560+
Current Version ID: Galaxy-Class
10561+
10562+
10563+
Note: Deployment ID has been renamed to Version ID. Deployment ID is present to maintain compatibility with the previous behavior of this command. This output will change in a future version of Wrangler. To learn more visit: https://developers.cloudflare.com/workers/configuration/versions-and-deployments"
10564+
`);
10565+
});
10566+
});
10567+
1052710568
describe("--keep-vars", () => {
1052810569
it("should send keepVars when keep-vars is passed in", async () => {
1052910570
vi.stubEnv("CLOUDFLARE_API_TOKEN", "hunter2");

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
/**

packages/wrangler/src/config/index.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import fs from "node:fs";
21
import dotenv from "dotenv";
32
import { findUpSync } from "find-up";
3+
import fs from "node:fs";
4+
import type { CfWorkerInit } from "../deployment-bundle/worker";
45
import { FatalError, UserError } from "../errors";
56
import { getFlag } from "../experimental-flags";
67
import { logger } from "../logger";
78
import { EXIT_CODE_INVALID_PAGES_CONFIG } from "../pages/errors";
89
import { parseJSONC, parseTOML, readFileSync } from "../parse";
9-
import { isPagesConfig, normalizeAndValidateConfig } from "./validation";
10-
import { validatePagesConfig } from "./validation-pages";
11-
import type { CfWorkerInit } from "../deployment-bundle/worker";
1210
import type { CommonYargsOptions } from "../yargs-types";
1311
import type { Config, OnlyCamelCase, RawConfig } from "./config";
1412
import type { NormalizeAndValidateConfigArgs } from "./validation";
13+
import { isPagesConfig, normalizeAndValidateConfig } from "./validation";
14+
import { validatePagesConfig } from "./validation-pages";
1515

1616
export type {
17-
Config,
18-
RawConfig,
19-
ConfigFields,
20-
DevConfig,
21-
RawDevConfig,
17+
Config, ConfigFields,
18+
DevConfig, RawConfig, RawDevConfig
2219
} from "./config";
2320
export type {
24-
Environment,
25-
RawEnvironment,
26-
ConfigModuleRuleType,
21+
ConfigModuleRuleType, Environment,
22+
RawEnvironment
2723
} from "./environment";
2824

2925
type ReadConfigCommandArgs = NormalizeAndValidateConfigArgs & {
@@ -232,6 +228,7 @@ export function printBindings(bindings: CfWorkerInit["bindings"]) {
232228
wasm_modules,
233229
dispatch_namespaces,
234230
mtls_certificates,
231+
pipelines,
235232
} = bindings;
236233

237234
if (data_blobs !== undefined && Object.keys(data_blobs).length > 0) {
@@ -443,6 +440,16 @@ export function printBindings(bindings: CfWorkerInit["bindings"]) {
443440
});
444441
}
445442

443+
if (pipelines?.length) {
444+
output.push({
445+
type: "Pipelines",
446+
entries: pipelines.map(({ binding, pipeline }) => ({
447+
key: binding,
448+
value: pipeline,
449+
}))
450+
})
451+
}
452+
446453
if (version_metadata !== undefined) {
447454
output.push({
448455
type: "Worker Version Metadata",

0 commit comments

Comments
 (0)