Skip to content

Commit

Permalink
fix: remove File polyfill from core
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Jun 8, 2024
1 parent 5328858 commit 06142b8
Show file tree
Hide file tree
Showing 9 changed files with 4,261 additions and 3,470 deletions.
3 changes: 0 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
},
"author": "sor4chi",
"license": "MIT",
"dependencies": {
"@web-std/file": "^3.0.3"
},
"devDependencies": {
"hono": "^4.3.2"
},
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { File } from "@web-std/file";

type Field = {
name: string;
type: "single" | "multiple";
Expand All @@ -8,7 +6,7 @@ type Field = {
export class HonoStorageFile extends File {
field: Field;

constructor(file: Blob | File, field: Field) {
constructor(file: File, field: Field) {
super([file], file.name);
this.field = field;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export type FieldSchema = SingleFieldSchema | MultipleFieldSchema;
export type FieldValue = string | File;

const isFile = (value: unknown): value is File => {
if (typeof value !== "object" || value === null) return false;
// HELP ME: instanceof File is not working because node <= 20 doesn't have File :(
return value instanceof Blob && value.constructor.name === "File";
return value instanceof File;
};

export const FILES_KEY = "files";
Expand Down
2 changes: 0 additions & 2 deletions packages/core/tests/file.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { File } from "@web-std/file";

import { HonoStorageFile } from "../src/file";

describe("HonoStorageFile", () => {
Expand Down
11 changes: 5 additions & 6 deletions packages/core/tests/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { File } from "@web-std/file";
import { Hono } from "hono";

import { HonoStorage, FieldValue, Errors } from "../src";
Expand Down Expand Up @@ -71,7 +70,7 @@ describe("HonoStorage", () => {

expect(res.status).toBe(200);
expect(storageHandler).toBeCalledTimes(1);
expect(actualFieldValue).toBeInstanceOf(Blob);
expect(actualFieldValue).toBeInstanceOf(File);
});

it("should through if no form data is provided", async () => {
Expand Down Expand Up @@ -127,7 +126,7 @@ describe("HonoStorage", () => {

expect(res.status).toBe(200);
expect(storageHandler).toBeCalledTimes(1); // 2 file, but 1 is string
expect(actualFieldValue1).toBeInstanceOf(Blob);
expect(actualFieldValue1).toBeInstanceOf(File);
expect(actualFieldValue2).toBe(file2);
});
});
Expand Down Expand Up @@ -265,7 +264,7 @@ describe("HonoStorage", () => {
});

expect(storageHandler).toBeCalledTimes(1);
expect(actualFieldValue).toBeInstanceOf(Blob);
expect(actualFieldValue).toBeInstanceOf(File);
});

it("should work with a multiple field", async () => {
Expand Down Expand Up @@ -363,10 +362,10 @@ describe("HonoStorage", () => {
});

expect(storageHandler).toBeCalledTimes(3);
expect(actualFieldValue1).toBeInstanceOf(Blob);
expect(actualFieldValue1).toBeInstanceOf(File);
expect(actualFieldValue2).toHaveLength(2);
expect((actualFieldValue2 as unknown as FieldValue[])[0]).toBeInstanceOf(
Blob,
File,
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/memory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type HonoMemoryStorageOptions = {
};

export class HonoMemoryStorage extends HonoStorage {
buffer: Map<string, Blob>;
buffer: Map<string, File>;
key: HMSFunction;

constructor(options: HonoMemoryStorageOptions = {}) {
Expand Down
36 changes: 24 additions & 12 deletions packages/memory/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ describe("HonoMemoryStorage", () => {

const formData = new FormData();

const file1 = new Blob(["Hello World 1"], { type: "text/plain" });
const file2 = new Blob(["Hello World 2"], { type: "text/plain" });
const file3 = new Blob(["Hello World 3"], { type: "text/plain" });
formData.append("file", file1, "sample1.txt");
formData.append("file2", file2, "sample2.txt");
formData.append("file2", file3, "sample1.txt");
const file1 = new File(["Hello World 1"], "sample1.txt", {
type: "text/plain",
});
const file2 = new File(["Hello World 2"], "sample2.txt", {
type: "text/plain",
});
const file3 = new File(["Hello World 3"], "sample1.txt", {
type: "text/plain",
});
formData.append("file", file1);
formData.append("file2", file2);
formData.append("file2", file3);

const res = await app.request("http://localhost/upload", {
method: "POST",
Expand Down Expand Up @@ -63,12 +69,18 @@ describe("HonoMemoryStorage", () => {

const formData = new FormData();

const file1 = new Blob(["Hello World 1"], { type: "text/plain" });
const file2 = new Blob(["Hello World 2"], { type: "text/plain" });
const file3 = new Blob(["Hello World 3"], { type: "text/plain" });
formData.append("file", file1, "sample1.txt");
formData.append("file2", file2, "sample2.txt");
formData.append("file2", file3, "sample1.txt");
const file1 = new File(["Hello World 1"], "sample1.txt", {
type: "text/plain",
});
const file2 = new File(["Hello World 2"], "sample2.txt", {
type: "text/plain",
});
const file3 = new File(["Hello World 3"], "sample1.txt", {
type: "text/plain",
});
formData.append("file", file1);
formData.append("file2", file2);
formData.append("file2", file3);

const res = await app.request("http://localhost/upload?store=1", {
method: "POST",
Expand Down
16 changes: 8 additions & 8 deletions packages/s3/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("HonoS3Storage", () => {
});

expect(putToS3).toHaveBeenCalledTimes(1);
expect(actualFile).toBeInstanceOf(Blob);
expect(actualFile).toBeInstanceOf(File);
expect(actualSignedURL).toBe(undefined);
});

Expand Down Expand Up @@ -121,7 +121,7 @@ describe("HonoS3Storage", () => {
expect(getSignedURLFromS3.mock.calls[0][1]).toMatchObject({
expiresIn: 60,
});
expect(actualFile).toBeInstanceOf(Blob);
expect(actualFile).toBeInstanceOf(File);
expect(actualSignedURL).toBe("https://example.com");
});
});
Expand Down Expand Up @@ -163,7 +163,7 @@ describe("HonoS3Storage", () => {
expect(actualFiles).toHaveLength(2);
assert(
Array.isArray(actualFiles) &&
actualFiles.every((v) => v instanceof Blob),
actualFiles.every((v) => v instanceof File),
);
expect(actualSignedURL).toBe(undefined);
});
Expand Down Expand Up @@ -214,7 +214,7 @@ describe("HonoS3Storage", () => {
expect(actualFiles).toHaveLength(2);
assert(
Array.isArray(actualFiles) &&
actualFiles.every((v) => v instanceof Blob),
actualFiles.every((v) => v instanceof File),
);
expect(actualSignedURL).toBeInstanceOf(Array);
expect(actualSignedURL).toHaveLength(2);
Expand Down Expand Up @@ -274,12 +274,12 @@ describe("HonoS3Storage", () => {

expect(putToS3).toHaveBeenCalledTimes(3);
expect(getSignedURLFromS3).toHaveBeenCalledTimes(0);
expect(actualImage).toBeInstanceOf(Blob);
expect(actualImage).toBeInstanceOf(File);
expect(actualImages).toBeInstanceOf(Array);
expect(actualImages).toHaveLength(2);
assert(
Array.isArray(actualImages) &&
actualImages.every((v) => v instanceof Blob),
actualImages.every((v) => v instanceof File),
);
expect(actualSignedURL).toBeInstanceOf(Object);
});
Expand Down Expand Up @@ -342,12 +342,12 @@ describe("HonoS3Storage", () => {
expect(getSignedURLFromS3.mock.calls[1][1]).toMatchObject({
expiresIn: 60,
});
expect(actualImage).toBeInstanceOf(Blob);
expect(actualImage).toBeInstanceOf(File);
expect(actualImages).toBeInstanceOf(Array);
expect(actualImages).toHaveLength(2);
assert(
Array.isArray(actualImages) &&
actualImages.every((v) => v instanceof Blob),
actualImages.every((v) => v instanceof File),
);
expect(actualSignedURL).toBeInstanceOf(Object);
expect(actualSignedURL).toMatchObject({
Expand Down
Loading

0 comments on commit 06142b8

Please sign in to comment.