Skip to content

Commit

Permalink
fix(server): avoid decoding raw request body (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlexLichter authored May 15, 2024
1 parent d3b773b commit f70104e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ tmp
__*
.vercel
.netlify
test/fs-storage/**
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function createH3StorageHandler(
const isRaw =
getRequestHeader(event, "content-type") === "application/octet-stream";
if (isRaw) {
const value = await readRawBody(event);
const value = await readRawBody(event, false);
await storage.setItemRaw(key, value);
} else {
const value = await readRawBody(event, "utf8");
Expand Down
34 changes: 34 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { readFile } from "node:fs/promises";
import { describe, it, expect } from "vitest";
import { listen } from "listhen";
import { $fetch } from "ofetch";
import { createStorage } from "../src";
import { createStorageServer } from "../src/server";
import fs from "../src/drivers/fs.ts";

describe("server", () => {
it("basic", async () => {
Expand Down Expand Up @@ -56,4 +58,36 @@ describe("server", () => {

await close();
});

it("properly encodes raw items", async () => {
const storage = createStorage({
driver: fs({ base: "./test/fs-storage" }),
});
const storageServer = createStorageServer(storage);
const { close, url: serverURL } = await listen(storageServer.handle, {
port: { random: true },
});

const fetchStorage = (url: string, options?: any) =>
$fetch(url, { baseURL: serverURL, ...options });

const file = await readFile("./test/test.png");

await storage.setItemRaw("1.png", file);
await fetchStorage("2.png", {
method: "PUT",
body: file,
headers: {
"content-type": "application/octet-stream",
},
});
const storedFileNode = await readFile("./test/fs-storage/1.png");
const storedFileFetch = await readFile("./test/fs-storage/2.png");

expect(storedFileNode).toStrictEqual(file);
expect(storedFileFetch).toStrictEqual(file);
expect(storedFileFetch).toStrictEqual(storedFileNode);

await close();
});
});
Binary file added test/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f70104e

Please sign in to comment.