Skip to content

Commit

Permalink
fix: KV not setting correctly (#964)
Browse files Browse the repository at this point in the history
The KV has URL inputs, which in the case of `/` would get collapsed and lost. To handle special characters `encodeURIComponent` is implemented.

resolves #961
  • Loading branch information
JacobMGEvans committed May 11, 2022
1 parent 5b03eb8 commit 0dfd95f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .changeset/shy-owls-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: KV not setting correctly
The KV has URL inputs, which in the case of `/` would get collapsed and lost.
T:o handle special characters `encodeURIComponent` is implemented.

resolves #961
33 changes: 33 additions & 0 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ describe("wrangler", () => {
expect(requests.count).toEqual(1);
});

it("should encode URI id properly for deleting namespace", async () => {
const requests = mockDeleteRequest("%2Fvoyager");
await runWrangler(`kv:namespace delete --namespace-id /voyager`);
expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should delete a namespace specified by binding name", async () => {
writeWranglerConfig();
const requests = mockDeleteRequest("bound-id");
Expand Down Expand Up @@ -381,6 +389,21 @@ describe("wrangler", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should encode URI key's properly for putting in a key request", async () => {
const requests = mockKeyPutRequest("DS9", {
key: "%2Fmy-key",
value: "my-value",
});

await runWrangler("kv:key put /my-key my-value --namespace-id DS9");

expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(
`"Writing the value \\"my-value\\" to key \\"/my-key\\" on namespace DS9."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should put a key in a given namespace specified by binding", async () => {
writeWranglerConfig();
const requests = mockKeyPutRequest("bound-id", {
Expand Down Expand Up @@ -1078,6 +1101,16 @@ describe("wrangler", () => {
expect(requests.count).toEqual(1);
});

it("should encode the URI properly for deleting a key requests", async () => {
const requests = mockDeleteRequest("voyager", "%2FNCC-74656");
await runWrangler(`kv:key delete --namespace-id voyager /NCC-74656`);
expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(
`"Deleting the key \\"/NCC-74656\\" on namespace voyager."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should delete a key in a namespace specified by binding name", async () => {
writeWranglerConfig();
const requests = mockDeleteRequest("bound-id", "someKey");
Expand Down
8 changes: 6 additions & 2 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,9 @@ export async function main(argv: string[]): Promise<void> {
const accountId = await requireAuth(config);

await fetchResult<{ id: string }>(
`/accounts/${accountId}/storage/kv/namespaces/${id}`,
`/accounts/${accountId}/storage/kv/namespaces/${encodeURIComponent(
id
)}`,
{ method: "DELETE" }
);

Expand Down Expand Up @@ -2270,7 +2272,9 @@ export async function main(argv: string[]): Promise<void> {
const accountId = await requireAuth(config);

await fetchResult(
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`,
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(
key
)}`,
{ method: "DELETE" }
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ export async function putKeyValue(
}
}
return await fetchResult(
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${keyValue.key}`,
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(
keyValue.key
)}`,
{ method: "PUT", body: keyValue.value },
searchParams
);
Expand Down

0 comments on commit 0dfd95f

Please sign in to comment.