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

fix(wrangler) wrangler kv:key put does not work as expected #5619

Merged
merged 1 commit into from
Apr 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/purple-cars-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: correctly handle non-text based files for kv put

The current version of the kv:key put command with the --path argument will treat file contents as a string because it is not one of Blob or File when passed to the form helper library. We should turn it into a Blob so it's not mangling inputs.
23 changes: 23 additions & 0 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,29 @@ describe("wrangler", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should put a key with a binary value and metadata", async () => {
const buf = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAiSURBVHgB7coxEQAACMPAgH/PgAM6dGwu49fA/deIBXrgAj2cAhIFT4QxAAAAAElFTkSuQmCC",
"base64"
);
writeFileSync("test.png", buf);
const requests = mockKeyPutRequest("some-namespace-id", {
key: "another-my-key",
value: buf,
metadata: {
mKey: "mValue",
},
});
await runWrangler(
`kv:key put another-my-key --namespace-id some-namespace-id --path test.png --metadata '{"mKey":"mValue"}'`
);
expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(
`"Writing the contents of test.png to the key \\"another-my-key\\" on namespace some-namespace-id with metadata \\"{\\"mKey\\":\\"mValue\\"}\\"."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should error if no key is provided", async () => {
await expect(
runWrangler("kv:key put")
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/kv/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Blob } from "node:buffer";
import { URLSearchParams } from "node:url";
import { Miniflare } from "miniflare";
import { FormData } from "undici";
Expand Down Expand Up @@ -200,7 +201,7 @@ function asFormData(fields: Record<string, unknown>): FormData {
const formData = new FormData();

for (const [name, value] of Object.entries(fields)) {
formData.append(name, value);
formData.append(name, Buffer.isBuffer(value) ? new Blob([value]) : value);
}

return formData;
Expand Down