Skip to content

Commit

Permalink
feat: support blob responses (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 authored Jul 10, 2023
1 parent 091f326 commit 5c4dcd1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ export function createAppEventHandler(stack: Stack, options: AppOptions) {
return send(event, val);
}

// Blob
if (val.arrayBuffer && typeof val.arrayBuffer === "function") {
return send(
event,
Buffer.from(await (val as Blob).arrayBuffer()),
val.type
);
}

// Error
if (val instanceof Error) {
throw createError(val);
Expand Down
20 changes: 18 additions & 2 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
createError,
} from "../src";

const readableStreamSupported =
typeof ReadableStream !== "undefined"; /* Node.js 16 */
// Node.js 16 limitations
const readableStreamSupported = typeof ReadableStream !== "undefined";
const blobSupported = typeof Blob !== "undefined";

describe("app", () => {
let app: App;
Expand Down Expand Up @@ -55,6 +56,21 @@ describe("app", () => {
}
});

it.runIf(blobSupported)("can return Blob directly", async () => {
app.use(
eventHandler(
() =>
new Blob(["<h1>Hello World</h1>"], {
type: "text/html",
})
)
);
const res = await request.get("/");

expect(res.headers["content-type"]).toBe("text/html");
expect(res.text).toBe("<h1>Hello World</h1>");
});

it("can return Buffer directly", async () => {
app.use(eventHandler(() => Buffer.from("<h1>Hello world!</h1>", "utf8")));
const res = await request.get("/");
Expand Down

0 comments on commit 5c4dcd1

Please sign in to comment.