Skip to content

Commit

Permalink
feat: web responses with streaming support (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jul 10, 2023
1 parent 5c4dcd1 commit a5c8c9f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import {
H3Event,
} from "./event";
import { createError } from "./error";
import { send, sendStream, isStream, MIMES } from "./utils";
import {
send,
sendStream,
isStream,
MIMES,
sendWebResponse,
isWebResponse,
} from "./utils";
import type { EventHandler, LazyEventHandler } from "./types";

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

if (val) {
// Web Response
if (isWebResponse(val)) {
return sendWebResponse(event, val);
}

// Stream
if (isStream(val)) {
return sendStream(event, val);
Expand Down
26 changes: 26 additions & 0 deletions src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ export function isStream(data: any): data is Readable | ReadableStream {
return false;
}

export function isWebResponse(data: any): data is Response {
return typeof Response !== "undefined" && data instanceof Response;
}

export function sendStream(
event: H3Event,
stream: Readable | ReadableStream
Expand Down Expand Up @@ -270,3 +274,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!", {
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

0 comments on commit a5c8c9f

Please sign in to comment.