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

feat: web responses with streaming support #436

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
H3Event,
} from "./event";
import { createError } from "./error";
import { send, sendStream, isStream, MIMES } from "./utils";
import { send, sendStream, isStream, MIMES, sendWebResponse } from "./utils";
import type { EventHandler, LazyEventHandler } from "./types";

export interface Layer {
Expand Down Expand Up @@ -125,6 +125,11 @@ export function createAppEventHandler(stack: Stack, options: AppOptions) {
}

if (val) {
// Web Response
if (typeof Response !== undefined && val instanceof Response) {
return sendWebResponse(event, val);
}

// Stream
if (isStream(val)) {
return sendStream(event, val);
Expand Down
22 changes: 22 additions & 0 deletions src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,25 @@ export function writeEarlyHints(
cb();
}
}

export function sendWebResponse(event: H3Event, response: Response) {
for (const [key, value] of response.headers.entries()) {
event.node.res.setHeader(key, value);
}
if (response.status) {
event.node.res.statusCode = sanitizeStatusCode(
response.status,
event.node.res.statusCode
);
}
if (response.statusText) {
event.node.res.statusMessage = sanitizeStatusMessage(response.statusText);
}
if (response.redirected) {
event.node.res.setHeader("location", response.url);
}
if (!response.body) {
return event.node.res.end();
}
return sendStream(event, response.body);
}
18 changes: 18 additions & 0 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
// Node.js 16 limitations
const readableStreamSupported = typeof ReadableStream !== "undefined";
const blobSupported = typeof Blob !== "undefined";
const responseSupported = typeof Response !== "undefined";

describe("app", () => {
let app: App;
Expand All @@ -33,6 +34,23 @@ describe("app", () => {
expect(res.body).toEqual({ url: "/" });
});

it.runIf(responseSupported)("can return Response directly", async () => {
app.use(
"/",
eventHandler(
() =>
new Response("Hello World!", {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document this feature demo in readme.

status: 201,
headers: { "x-test": "test" },
})
)
);

const res = await request.get("/");
expect(res.statusCode).toBe(201);
expect(res.text).toBe("Hello World!");
});

it("can return a 204 response", async () => {
app.use(
"/api",
Expand Down