Skip to content

Commit

Permalink
feat: support for raw "unsafe bindings"
Browse files Browse the repository at this point in the history
  • Loading branch information
ObsidianMinor committed Feb 8, 2022
1 parent 40d9553 commit 34c814a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/wrangler/src/api/form_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface WorkerMetadata {
service: string;
environment: string;
}
| object
)[];
}

Expand Down Expand Up @@ -105,6 +106,10 @@ export function toFormData(worker: CfWorkerInit): FormData {
});
});

if (bindings.raw) {
metadataBindings.push(bindings.raw);
}

const metadata: WorkerMetadata = {
...(main.type !== "commonjs"
? { main_module: main.name }
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/api/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export interface CfWorkerInit {
durable_objects?: { bindings: CfDurableObject[] };
vars?: CfVars;
services?: CfService[];
raw?: object[];
};
migrations: undefined | CfDurableObjectMigrations;
compatibility_date: string | undefined;
Expand Down
17 changes: 17 additions & 0 deletions packages/wrangler/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ export type Config = {
environment: string;
}[];

/**
* "Unsafe" tables for features that aren't directly supported by wrangler.
* NB: these are not inherited, and HAVE to be duplicated across all environments.
*
* @default `[]`
* @optional
* @inherited false
*/
unsafe?: {
/**
* A set of bindings that should be put into a Worker's upload metadata without changes. These
* can be used to implement bindings for features that haven't released and aren't supported
* directly by wrangler or miniflare.
*/
bindings?: object[];
};

/**
* A list of migrations that should be uploaded with your Worker.
* These define changes in your Durable Object declarations.
Expand Down
21 changes: 21 additions & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ import type Yargs from "yargs";
const resetColor = "\x1b[0m";
const fgGreenColor = "\x1b[32m";

// a set of binding types that are known to be supported by wrangler
const knownBindings = [
"plain_text",
"kv_namespace",
"durable_object_namespace",
];

async function readConfig(configPath?: string): Promise<Config> {
const config: Config = {};
if (!configPath) {
Expand All @@ -71,6 +78,19 @@ async function readConfig(configPath?: string): Promise<Config> {
// todo: validate, add defaults
// let's just do some basics for now

for (const binding of config.unsafe?.bindings ?? []) {
if ("type" in binding && typeof binding["type"] == "string") {
const type = binding["type"];
if (knownBindings.includes(type)) {
console.warn(
`Raw '${type}' bindings are not directly supported by wrangler. Consider migrating to a ` +
`format for '${type}' bindings that is supported by wrangler for optimal support: ` +
"https://developers.cloudflare.com/workers/cli-wrangler/configuration"
);
}
}
}

// @ts-expect-error we're being sneaky here for now
config.__path__ = configPath;

Expand Down Expand Up @@ -681,6 +701,7 @@ export async function main(argv: string[]): Promise<void> {
vars: envRootObj.vars,
durable_objects: envRootObj.durable_objects,
services: envRootObj.experimental_services,
raw: envRootObj.unsafe?.bindings,
}}
/>
);
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export default async function publish(props: Props): Promise<void> {
vars: envRootObj.vars,
durable_objects: envRootObj.durable_objects,
services: envRootObj.experimental_services,
raw: envRootObj.unsafe?.bindings,
};

const workerType = bundle.type === "esm" ? "esm" : "commonjs";
Expand Down

0 comments on commit 34c814a

Please sign in to comment.