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: kv:key put upload binary files fix #1255

Merged
merged 5 commits into from
Jun 14, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/curly-lizards-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: kv:key put binary file upload

As raised in https://github.com/cloudflare/wrangler2/issues/1254, it was discovered that binary uploads were being mangled by wrangler 2, whereas they worked in wrangler 1. This is because they were read into a string by providing an explicit encoding of `utf-8`. This fix reads provided files into a node `Buffer` that is then passed directly to the request.
25 changes: 23 additions & 2 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,11 @@ describe("wrangler", () => {
});

it("should put a key with a value loaded from a given path", async () => {
writeFileSync("foo.txt", "file-contents", "utf-8");
const buf = Buffer.from("file-contents", "utf-8");
writeFileSync("foo.txt", buf);
const requests = mockKeyPutRequest("some-namespace-id", {
key: "my-key",
value: "file-contents",
value: buf,
});
await runWrangler(
"kv:key put my-key --namespace-id some-namespace-id --path foo.txt"
Expand All @@ -463,6 +464,26 @@ describe("wrangler", () => {
expect(requests.count).toEqual(1);
});

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

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/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
parsePackageJSON,
parseTOML,
readFileSync,
readFileSyncToBuffer,
} from "./parse";
import publish from "./publish";
import { createR2Bucket, deleteR2Bucket, listR2Buckets } from "./r2";
Expand Down Expand Up @@ -2260,7 +2261,7 @@ function createCLIParser(argv: string[]) {
const namespaceId = getKVNamespaceId(args, config);
// One of `args.path` and `args.value` must be defined
const value = args.path
? readFileSync(args.path)
? readFileSyncToBuffer(args.path)
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
args.value!;

Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function deleteKVNamespace(
*/
export interface KeyValue {
key: string;
value: string;
value: string | Buffer;
expiration?: number;
expiration_ttl?: number;
metadata?: object;
Expand Down
19 changes: 19 additions & 0 deletions packages/wrangler/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ export function parseJSON<T>(input: string, file?: string): T {
}
}

/**
* Reads a file into a node Buffer.
*/
export function readFileSyncToBuffer(file: string): Buffer {
try {
return fs.readFileSync(file);
} catch (err) {
const { message } = err as Error;
throw new ParseError({
text: `Could not read file: ${file}`,
notes: [
{
text: message.replace(file, resolve(file)),
},
],
});
}
}

/**
* Reads a file and parses it based on its type.
*/
Expand Down