Skip to content

Commit

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

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

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

const isFile = (value: unknown): value is File => {
return value instanceof 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";
};

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

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

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

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

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

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

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

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

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

expect(storageHandler).toBeCalledTimes(3);
expect(actualFieldValue1).toBeInstanceOf(File);
expect(actualFieldValue1).toBeInstanceOf(Blob);
expect(actualFieldValue2).toHaveLength(2);
expect((actualFieldValue2 as unknown as FieldValue[])[0]).toBeInstanceOf(
File,
Blob,
);
});
});
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, File>;
buffer: Map<string, Blob>;
key: HMSFunction;

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

const formData = new FormData();

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

const formData = new FormData();

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 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 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(File);
expect(actualFile).toBeInstanceOf(Blob);
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(File);
expect(actualFile).toBeInstanceOf(Blob);
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 File),
actualFiles.every((v) => v instanceof Blob),
);
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 File),
actualFiles.every((v) => v instanceof Blob),
);
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(File);
expect(actualImage).toBeInstanceOf(Blob);
expect(actualImages).toBeInstanceOf(Array);
expect(actualImages).toHaveLength(2);
assert(
Array.isArray(actualImages) &&
actualImages.every((v) => v instanceof File),
actualImages.every((v) => v instanceof Blob),
);
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(File);
expect(actualImage).toBeInstanceOf(Blob);
expect(actualImages).toBeInstanceOf(Array);
expect(actualImages).toHaveLength(2);
assert(
Array.isArray(actualImages) &&
actualImages.every((v) => v instanceof File),
actualImages.every((v) => v instanceof Blob),
);
expect(actualSignedURL).toBeInstanceOf(Object);
expect(actualSignedURL).toMatchObject({
Expand Down
Loading

0 comments on commit dc9cc36

Please sign in to comment.