Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KV] Remove worker- prefix from kv:namespace create when using wrangler in a pages project or without a name property #5007

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/tall-planets-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"wrangler": major
---

feat: remove the default 'worker' prefix from the `kv:namespace create` command

Previously, if there was no Worker defined by a local `wrangler.toml` or that Worker had no `name` property
then any KV namespace that was created by Wrangler would use `worker_` as a prefix, rather than the Worker's
name.

This is a minor breaking change to the name given to a newly created KV namespace, which is unlikely to affect
many, if any, developers.
18 changes: 9 additions & 9 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,46 +113,46 @@ describe("wrangler", () => {
});

it("should create a namespace", async () => {
mockCreateRequest("worker-UnitTestNamespace");
mockCreateRequest("UnitTestNamespace");
await runWrangler("kv:namespace create UnitTestNamespace");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
`);
});

it("should create a preview namespace if configured to do so", async () => {
mockCreateRequest("worker-UnitTestNamespace_preview");
mockCreateRequest("UnitTestNamespace_preview");
await runWrangler("kv:namespace create UnitTestNamespace --preview");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
"🌀 Creating namespace with title \\"UnitTestNamespace_preview\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = \\"UnitTestNamespace\\", preview_id = \\"some-namespace-id\\" }"
`);
});

it("should create a namespace using configured worker name", async () => {
writeFileSync("./wrangler.toml", 'name = "other-worker"', "utf-8");
mockCreateRequest("other-worker-UnitTestNamespace");
writeFileSync("./wrangler.toml", 'name = "other"', "utf-8");
mockCreateRequest("other-UnitTestNamespace");
await runWrangler("kv:namespace create UnitTestNamespace");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"other-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
`);
});

it("should create a namespace in an environment if configured to do so", async () => {
mockCreateRequest("worker-customEnv-UnitTestNamespace");
mockCreateRequest("customEnv-UnitTestNamespace");
await runWrangler(
"kv:namespace create UnitTestNamespace --env customEnv"
);
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"customEnv-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
{ binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
Expand Down
11 changes: 11 additions & 0 deletions packages/wrangler/src/kv/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ export function getKVNamespaceId(
return namespaceId;
}

/**
* KV namespace binding names must be valid JS identifiers.
*/
export function isValidKVNamespaceBinding(
binding: string | undefined
): binding is string {
return (
typeof binding === "string" && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(binding)
);
}

// TODO(soon): once we upgrade to TypeScript 5.2, this should actually use `using`:
// https://devblogs.microsoft.com/typescript/announcing-typescript-5-2/#using-declarations-and-explicit-resource-management
export async function usingLocalNamespace<T>(
Expand Down
15 changes: 12 additions & 3 deletions packages/wrangler/src/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
getKVKeyValue,
getKVNamespaceId,
isKVKeyValue,
isValidKVNamespaceBinding,
listKVNamespaceKeys,
listKVNamespaces,
putKVBulkKeyValue,
Expand Down Expand Up @@ -60,10 +61,18 @@
);
}

const name = config.name || "worker";
const environment = args.env ? `-${args.env}` : "";
if (!isValidKVNamespaceBinding(args.namespace)) {
throw new CommandLineArgsError(

Check warning on line 65 in packages/wrangler/src/kv/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/kv/index.ts#L65

Added line #L65 was not covered by tests
`The namespace binding name "${args.namespace}" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.`
);
}

const name = config.name || "";
petebacondarwin marked this conversation as resolved.
Show resolved Hide resolved
const environment = args.env ? `${name && "-"}${args.env}` : "";
const preview = args.preview ? "_preview" : "";
const title = `${name}${environment}-${args.namespace}${preview}`;
const title = `${name}${environment}${(name || environment) && "-"}${
args.namespace
}${preview}`;

const accountId = await requireAuth(config);

Expand Down
Loading