Skip to content

Commit

Permalink
feat(wrangler): Drop worker prefix when creating KV namespaces (#7759)
Browse files Browse the repository at this point in the history
* feat(wrangler): Drop worker prefix when creating KV namespaces

* Remove crypto polyfill

---------

Co-authored-by: Samuel Macleod <[email protected]>
  • Loading branch information
pmiguel and penalosa authored Feb 19, 2025
1 parent 5cb917d commit d702a2f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 35 deletions.
8 changes: 8 additions & 0 deletions .changeset/serious-teachers-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": major
---

Remove worker name prefix from KV namespace create

BREAKING CHANGE: when running `wrangler kv namespace create <name>`, previously the name of the namespace was automatically prefixed with the worker title, or `worker-` when running outside the context of a worker.
After this change, KV namespaces will no longer get prefixed, and the name used is the name supplied, verbatim.
13 changes: 0 additions & 13 deletions packages/workers-shared/asset-worker/crypto-polyfill.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFileSync } from "fs";
import crypto from "node:crypto";
import { join } from "path";
import {
CONTENT_HASH_OFFSET,
Expand All @@ -12,6 +13,13 @@ import { binarySearch, compare, hashPath } from "../src/assets-manifest";

const encoder = new TextEncoder();

beforeAll(() => {
if (!("crypto" in globalThis)) {
// @ts-expect-error will go away once Node.js is bumped
globalThis.crypto = crypto;
}
});

async function SHA_256(value: string, length: number) {
const data = encoder.encode(value);
const hashBuffer = await crypto.subtle.digest(
Expand Down
1 change: 0 additions & 1 deletion packages/workers-shared/asset-worker/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default mergeConfig(
test: {
include: ["tests/**.{test,spec}.{ts,js}"],
globals: true,
setupFiles: [import.meta.resolve("./crypto-polyfill.ts")],
},
})
);
16 changes: 8 additions & 8 deletions packages/wrangler/src/__tests__/__snapshots__/kv.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace 1`] = `
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{
Expand All @@ -15,7 +15,7 @@ Add the following to your configuration file in your kv_namespaces array:
`;

exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace in an environment if configured to do so 1`] = `
"🌀 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]:
{
Expand All @@ -29,7 +29,7 @@ Add the following to your configuration file in your kv_namespaces array under [
`;

exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace using configured worker name 1`] = `
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{
Expand All @@ -43,7 +43,7 @@ Add the following to your configuration file in your kv_namespaces array:
`;

exports[`wrangler > kv namespace > create > wrangler.json > should create a preview namespace if configured to do so 1`] = `
"🌀 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:
{
Expand All @@ -57,7 +57,7 @@ Add the following to your configuration file in your kv_namespaces array:
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace 1`] = `
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
Expand All @@ -67,7 +67,7 @@ id = \\"some-namespace-id\\"
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace in an environment if configured to do so 1`] = `
"🌀 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]:
[[kv_namespaces]]
Expand All @@ -77,7 +77,7 @@ id = \\"some-namespace-id\\"
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace using configured worker name 1`] = `
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
Expand All @@ -87,7 +87,7 @@ id = \\"some-namespace-id\\"
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a preview namespace if configured to do so 1`] = `
"🌀 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:
[[kv_namespaces]]
Expand Down
8 changes: 4 additions & 4 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,23 @@ describe("wrangler", () => {
describe.each(["wrangler.json", "wrangler.toml"])("%s", (configPath) => {
it("should create a namespace", async () => {
writeWranglerConfig({ name: "worker" }, configPath);
mockCreateRequest("worker-UnitTestNamespace");
mockCreateRequest("UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toMatchSnapshot();
});

it("should create a preview namespace if configured to do so", async () => {
writeWranglerConfig({ name: "worker" }, configPath);

mockCreateRequest("worker-UnitTestNamespace_preview");
mockCreateRequest("UnitTestNamespace_preview");
await runWrangler("kv namespace create UnitTestNamespace --preview");
expect(std.out).toMatchSnapshot();
});

it("should create a namespace using configured worker name", async () => {
writeWranglerConfig({ name: "other-worker" }, configPath);

mockCreateRequest("other-worker-UnitTestNamespace");
mockCreateRequest("UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toMatchSnapshot();
});
Expand All @@ -223,7 +223,7 @@ describe("wrangler", () => {
configPath
);

mockCreateRequest("worker-customEnv-UnitTestNamespace");
mockCreateRequest("customEnv-UnitTestNamespace");
await runWrangler(
"kv namespace create UnitTestNamespace --env customEnv"
);
Expand Down
11 changes: 2 additions & 9 deletions packages/wrangler/src/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,9 @@ export const kvNamespaceCreateCommand = createCommand({

async handler(args) {
const config = readConfig(args);
if (!config.name) {
logger.warn(
"No configured name present, using `worker` as a prefix for the title"
);
}

const name = config.name || "worker";
const environment = args.env ? `-${args.env}` : "";
const environment = args.env ? `${args.env}-` : "";
const preview = args.preview ? "_preview" : "";
const title = `${name}${environment}-${args.namespace}${preview}`;
const title = `${environment}${args.namespace}${preview}`;

const accountId = await requireAuth(config);

Expand Down

0 comments on commit d702a2f

Please sign in to comment.