Skip to content

Commit d702a2f

Browse files
pmiguelpenalosa
andauthored
feat(wrangler): Drop worker prefix when creating KV namespaces (#7759)
* feat(wrangler): Drop worker prefix when creating KV namespaces * Remove crypto polyfill --------- Co-authored-by: Samuel Macleod <[email protected]>
1 parent 5cb917d commit d702a2f

File tree

7 files changed

+30
-35
lines changed

7 files changed

+30
-35
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"wrangler": major
3+
---
4+
5+
Remove worker name prefix from KV namespace create
6+
7+
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.
8+
After this change, KV namespaces will no longer get prefixed, and the name used is the name supplied, verbatim.

packages/workers-shared/asset-worker/crypto-polyfill.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/workers-shared/asset-worker/tests/assets-manifest.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFileSync } from "fs";
2+
import crypto from "node:crypto";
23
import { join } from "path";
34
import {
45
CONTENT_HASH_OFFSET,
@@ -12,6 +13,13 @@ import { binarySearch, compare, hashPath } from "../src/assets-manifest";
1213

1314
const encoder = new TextEncoder();
1415

16+
beforeAll(() => {
17+
if (!("crypto" in globalThis)) {
18+
// @ts-expect-error will go away once Node.js is bumped
19+
globalThis.crypto = crypto;
20+
}
21+
});
22+
1523
async function SHA_256(value: string, length: number) {
1624
const data = encoder.encode(value);
1725
const hashBuffer = await crypto.subtle.digest(

packages/workers-shared/asset-worker/vitest.config.mts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default mergeConfig(
77
test: {
88
include: ["tests/**.{test,spec}.{ts,js}"],
99
globals: true,
10-
setupFiles: [import.meta.resolve("./crypto-polyfill.ts")],
1110
},
1211
})
1312
);

packages/wrangler/src/__tests__/__snapshots__/kv.test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

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

1717
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace in an environment if configured to do so 1`] = `
18-
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
18+
"🌀 Creating namespace with title \\"customEnv-UnitTestNamespace\\"
1919
✨ Success!
2020
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
2121
{
@@ -29,7 +29,7 @@ Add the following to your configuration file in your kv_namespaces array under [
2929
`;
3030

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

4545
exports[`wrangler > kv namespace > create > wrangler.json > should create a preview namespace if configured to do so 1`] = `
46-
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
46+
"🌀 Creating namespace with title \\"UnitTestNamespace_preview\\"
4747
✨ Success!
4848
Add the following to your configuration file in your kv_namespaces array:
4949
{
@@ -57,7 +57,7 @@ Add the following to your configuration file in your kv_namespaces array:
5757
`;
5858

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

6969
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace in an environment if configured to do so 1`] = `
70-
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
70+
"🌀 Creating namespace with title \\"customEnv-UnitTestNamespace\\"
7171
✨ Success!
7272
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
7373
[[kv_namespaces]]
@@ -77,7 +77,7 @@ id = \\"some-namespace-id\\"
7777
`;
7878

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

8989
exports[`wrangler > kv namespace > create > wrangler.toml > should create a preview namespace if configured to do so 1`] = `
90-
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
90+
"🌀 Creating namespace with title \\"UnitTestNamespace_preview\\"
9191
✨ Success!
9292
Add the following to your configuration file in your kv_namespaces array:
9393
[[kv_namespaces]]

packages/wrangler/src/__tests__/kv.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,23 @@ describe("wrangler", () => {
189189
describe.each(["wrangler.json", "wrangler.toml"])("%s", (configPath) => {
190190
it("should create a namespace", async () => {
191191
writeWranglerConfig({ name: "worker" }, configPath);
192-
mockCreateRequest("worker-UnitTestNamespace");
192+
mockCreateRequest("UnitTestNamespace");
193193
await runWrangler("kv namespace create UnitTestNamespace");
194194
expect(std.out).toMatchSnapshot();
195195
});
196196

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

200-
mockCreateRequest("worker-UnitTestNamespace_preview");
200+
mockCreateRequest("UnitTestNamespace_preview");
201201
await runWrangler("kv namespace create UnitTestNamespace --preview");
202202
expect(std.out).toMatchSnapshot();
203203
});
204204

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

208-
mockCreateRequest("other-worker-UnitTestNamespace");
208+
mockCreateRequest("UnitTestNamespace");
209209
await runWrangler("kv namespace create UnitTestNamespace");
210210
expect(std.out).toMatchSnapshot();
211211
});
@@ -223,7 +223,7 @@ describe("wrangler", () => {
223223
configPath
224224
);
225225

226-
mockCreateRequest("worker-customEnv-UnitTestNamespace");
226+
mockCreateRequest("customEnv-UnitTestNamespace");
227227
await runWrangler(
228228
"kv namespace create UnitTestNamespace --env customEnv"
229229
);

packages/wrangler/src/kv/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,9 @@ export const kvNamespaceCreateCommand = createCommand({
8383

8484
async handler(args) {
8585
const config = readConfig(args);
86-
if (!config.name) {
87-
logger.warn(
88-
"No configured name present, using `worker` as a prefix for the title"
89-
);
90-
}
91-
92-
const name = config.name || "worker";
93-
const environment = args.env ? `-${args.env}` : "";
86+
const environment = args.env ? `${args.env}-` : "";
9487
const preview = args.preview ? "_preview" : "";
95-
const title = `${name}${environment}-${args.namespace}${preview}`;
88+
const title = `${environment}${args.namespace}${preview}`;
9689

9790
const accountId = await requireAuth(config);
9891

0 commit comments

Comments
 (0)