Skip to content

Commit

Permalink
fix(wrangler) wrangler kv:key put does not work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
gmemstr committed Apr 17, 2024
1 parent 9a46e03 commit 307178a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
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: "dKey",
value: buf,
metadata: {
mKey: "mValue",
},
});
await runWrangler(
`"kv:key put my-key --namespace-id another-namespace-id --path test.png" '{"mKey":"mValue"}'`
);
expect(requests.count).toEqual(1);
expect(std.out).toMatchInlineSnapshot(
`"Writing the contents of test.png to the key \\"my-key\\" on namespace another-namespace-id."`
);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should error if no key is provided", async () => {
await expect(
runWrangler("kv:key put")
Expand Down
5 changes: 4 additions & 1 deletion packages/wrangler/src/kv/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ export function unexpectedKVKeyValueProps(keyValue: KeyValue): string[] {
function asFormData(fields: Record<string, unknown>): FormData {
const formData = new FormData();

for (const [name, value] of Object.entries(fields)) {
for (let [name, value] of Object.entries(fields)) {

Check failure on line 202 in packages/wrangler/src/kv/helpers.ts

View workflow job for this annotation

GitHub Actions / Checks (ubuntu-latest)

'name' is never reassigned. Use 'const' instead
if (Buffer.isBuffer(value)) {
value = new Blob([value]);
}
formData.append(name, value);
}

Expand Down

0 comments on commit 307178a

Please sign in to comment.