Skip to content

Commit

Permalink
Add versions secret put
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Jun 5, 2024
1 parent 21573f4 commit 694464d
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 88 deletions.
7 changes: 7 additions & 0 deletions .changeset/little-rice-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": minor
---

feat: add `versions secret put`

This allows for you to add a new secret even if the latest version is not fully deployed. A new version with this secret will be created, the existing secrets and config are copied from the latest version.
140 changes: 72 additions & 68 deletions packages/wrangler/src/__tests__/versions/versions.help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,24 @@ describe("versions --help", () => {
await expect(result).resolves.toBeUndefined();

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
});

Expand All @@ -96,23 +97,24 @@ describe("versions subhelp", () => {
await setImmediate(); // wait for subhelp

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});

test("shows implicit subhelp with --x-versions flag", async () => {
Expand All @@ -122,23 +124,24 @@ describe("versions subhelp", () => {
await setImmediate(); // wait for subhelp

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});

test("shows implicit subhelp with --experimental-gradual-rollouts flag", async () => {
Expand All @@ -148,22 +151,23 @@ describe("versions subhelp", () => {
await setImmediate(); // wait for subhelp

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
});
1 change: 1 addition & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,7 @@ const validateUnsafeBinding: ValidatorFn = (diagnostics, field, value) => {
if (isRequiredProperty(value, "type", "string")) {
const safeBindings = [
"plain_text",
"secret_text",
"json",
"wasm_module",
"data_blob",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,42 @@ import type {
} from "./worker.js";
import type { Json } from "miniflare";

const moduleTypeMimeType: { [type in CfModuleType]: string | undefined } = {
esm: "application/javascript+module",
commonjs: "application/javascript",
"compiled-wasm": "application/wasm",
buffer: "application/octet-stream",
text: "text/plain",
python: "text/x-python",
"python-requirement": "text/x-python-requirement",
"nodejs-compat-module": undefined,
};

export function toMimeType(type: CfModuleType): string {
switch (type) {
case "esm":
return "application/javascript+module";
case "commonjs":
return "application/javascript";
case "compiled-wasm":
return "application/wasm";
case "buffer":
return "application/octet-stream";
case "text":
return "text/plain";
case "python":
return "text/x-python";
case "python-requirement":
return "text/x-python-requirement";
default:
throw new TypeError("Unsupported module: " + type);
const mimeType = moduleTypeMimeType[type];
if (mimeType === undefined) {
throw new TypeError("Unsupported module: " + type);
}

return mimeType;
}

export function fromMimeType(mimeType: string): CfModuleType {
const moduleType = Object.keys(moduleTypeMimeType).find(
(type) => moduleTypeMimeType[type as CfModuleType] === mimeType
) as CfModuleType | undefined;
if (moduleType === undefined) {
throw new TypeError("Unsupported module: " + moduleType);
}

return moduleType;
}

export type WorkerMetadataBinding =
// If you add any new binding types here, also add it to safeBindings
// under validateUnsafeBinding in config/validation.ts
| { type: "plain_text"; name: string; text: string }
| { type: "secret_text"; name: string; text: string }
| { type: "json"; name: string; json: Json }
| { type: "wasm_module"; name: string; part: string }
| { type: "text_blob"; name: string; part: string }
Expand Down Expand Up @@ -143,6 +154,7 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
main,
sourceMaps,
bindings,
rawBindings,
migrations,
usage_model,
compatibility_date,
Expand All @@ -158,7 +170,7 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {

let { modules } = worker;

const metadataBindings: WorkerMetadata["bindings"] = [];
const metadataBindings: WorkerMetadataBinding[] = rawBindings ?? [];

Object.entries(bindings.vars || {})?.forEach(([key, value]) => {
if (typeof value === "string") {
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/deployment-bundle/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Route } from "../config/environment";
import type { WorkerMetadataBinding } from "./create-worker-upload-form";
import type { Json } from "miniflare";

/**
Expand Down Expand Up @@ -314,6 +315,13 @@ export interface CfWorkerInit {
logfwdr: CfLogfwdr | undefined;
unsafe: CfUnsafe | undefined;
};
/**
* The raw bindings - this is basically never provided and it'll be the bindings above
* but if we're just taking from the api and re-putting then this is how we can do that
* without going between the different types
*/
rawBindings?: WorkerMetadataBinding[];

migrations: CfDurableObjectMigrations | undefined;
compatibility_date: string | undefined;
compatibility_flags: string[] | undefined;
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/secret/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export const secret = (secretYargs: CommonYargsArgv) => {
* Remove trailing white space from inputs.
* Matching Wrangler legacy behavior with handling inputs
*/
function trimTrailingWhitespace(str: string) {
export function trimTrailingWhitespace(str: string) {
return str.trimEnd();
}

Expand All @@ -327,7 +327,7 @@ function trimTrailingWhitespace(str: string) {
* This function can be used to grab the incoming stream of data from, say,
* piping the output of another process into the wrangler process.
*/
function readFromStdin(): Promise<string> {
export function readFromStdin(): Promise<string> {
return new Promise((resolve, reject) => {
const stdin = process.stdin;
const chunks: string[] = [];
Expand Down
6 changes: 6 additions & 0 deletions packages/wrangler/src/versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { requireAuth } from "../user";
import { collectKeyValues } from "../utils/collectKeyValues";
import { versionsDeployHandler, versionsDeployOptions } from "./deploy";
import { versionsListHandler, versionsListOptions } from "./list";
import { versionsSecrets } from "./secrets";
import versionsUpload from "./upload";
import { versionsViewHandler, versionsViewOptions } from "./view";
import type { Config } from "../config";
Expand Down Expand Up @@ -254,5 +255,10 @@ export default function registerVersionsSubcommands(
"Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]",
versionsDeployOptions,
versionsDeployHandler
)
.command(
"secret",
"🤫 Generate a secret that can be referenced in a Worker",
versionsSecrets
);
}
34 changes: 34 additions & 0 deletions packages/wrangler/src/versions/secrets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { versionsSecretPutHandler } from "./put";
import type { CommonYargsArgv } from "../../yargs-types";

export function versionsSecretsPutOptions(yargs: CommonYargsArgv) {
return yargs
.positional("key", {
describe: "The variable name to be accessible in the Worker",
type: "string",
})
.option("name", {
describe: "Name of the Worker",
type: "string",
requiresArg: true,
})
.option("message", {
describe: "Description of this deployment (optional)",
type: "string",
requiresArg: true,
})
.option("tag", {
describe: "A tag for this version (optional)",
type: "string",
requiresArg: true,
});
}

export const versionsSecrets = (secretYargs: CommonYargsArgv) => {
return secretYargs.command(
"put <key>",
"Create or update a secret variable for a Worker",
versionsSecretsPutOptions,
versionsSecretPutHandler
);
};
Loading

0 comments on commit 694464d

Please sign in to comment.