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: support react pipeable streams #494

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"listhen": "^1.2.2",
"node-fetch-native": "^1.2.0",
"prettier": "^3.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"supertest": "^6.3.3",
"typescript": "^5.1.6",
"unbuild": "^1.2.1",
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 30 additions & 11 deletions src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,15 @@ export function isStream(data: any): data is Readable | ReadableStream {
if (!data || typeof data !== "object") {
return false;
}
// Node.js Readable Streams
if (typeof data.pipe === "function" && typeof data._read === "function") {
return true;
if (typeof data.pipe === "function") {
// Node.js Readable Streams
if (typeof data._read === "function") {
return true;
}
// React Pipeable Streams
if (typeof data.abort === "function") {
return true;
}
}
// Web Streams
if (typeof data.pipeTo === "function") {
Expand Down Expand Up @@ -225,17 +231,30 @@ export function sendStream(
});
}

// Node.js Readable streams
// Node.js Readable Streams
// https://nodejs.org/api/stream.html#readable-streams
if ("pipe" in stream && "_read" in stream) {
if (typeof stream.pipe === "function") {
return new Promise<void>((resolve, reject) => {
// Pipe stream to response
stream.pipe(event.node.res);
stream.on("end", () => {
event.node.res.end();
resolve();
});
stream.on("error", (error: Error) => {
reject(error);

// Handle stream events (if supported)
if (stream.on) {
stream.on("end", () => {
event.node.res.end();
resolve();
});
stream.on("error", (error: Error) => {
reject(error);
});
}

// Handle request aborts
event.node.res.on("close", () => {
// https://react.dev/reference/react-dom/server/renderToPipeableStream
if ((stream as any).abort) {
(stream as any).abort();
}
});
});
}
Expand Down
46 changes: 44 additions & 2 deletions test/integrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,51 @@ import express from "express";
import createConnectApp from "connect";
import { describe, it, expect, beforeEach } from "vitest";
import supertest, { SuperTest, Test } from "supertest";
import { createApp, App, toNodeListener, fromNodeMiddleware } from "../src";
import { createElement } from "react";
import { renderToString, renderToPipeableStream } from "react-dom/server";
import {
createApp,
App,
toNodeListener,
fromNodeMiddleware,
eventHandler,
} from "../src";

describe("integrations with other frameworks", () => {
describe("integration with react", () => {
let app: App;
let request: SuperTest<Test>;

beforeEach(() => {
app = createApp({ debug: true });
request = supertest(toNodeListener(app));
});

it("renderToString", async () => {
app.use(
"/",
eventHandler(() => {
const el = createElement("h1", null, `Hello`);
return renderToString(el);
}),
);
const res = await request.get("/");
expect(res.text).toBe("<h1>Hello</h1>");
});

it("renderToPipeableStream", async () => {
app.use(
"/",
eventHandler(() => {
const el = createElement("h1", null, `Hello`);
return renderToPipeableStream(el);
}),
);
const res = await request.get("/");
expect(res.text).toBe("<h1>Hello</h1>");
});
});

describe("integration with express", () => {
let app: App;
let request: SuperTest<Test>;

Expand Down