Skip to content

Commit

Permalink
Fix TS error in entry-server - make Passthrough a ReadableStream (#7361)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Sep 11, 2023
1 parent 5311c88 commit d281439
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-suits-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

[REMOVE] Fix TS error in entry.server.tsx to make PassThrough body a ReadableStream
9 changes: 7 additions & 2 deletions docs/guides/migrating-react-router-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type {
AppLoadContext,
EntryContext,
} from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -96,11 +97,13 @@ function handleBotRequest(
{
onAllReady() {
const body = new PassThrough();
const stream =
createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down Expand Up @@ -138,11 +141,13 @@ function handleBrowserRequest(
{
onShellReady() {
const body = new PassThrough();
const stream =
createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down
7 changes: 5 additions & 2 deletions integration/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ test.describe("aborted", () => {
"app/entry.server.tsx": js`
import { PassThrough } from "node:stream";
import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -1029,11 +1030,12 @@ test.describe("aborted", () => {
{
onAllReady() {
let body = new PassThrough();
let stream = createReadableStreamFromReadable(body);
responseHeaders.set("Content-Type", "text/html");
resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
Expand Down Expand Up @@ -1074,11 +1076,12 @@ test.describe("aborted", () => {
{
onShellReady() {
let body = new PassThrough();
let stream = createReadableStreamFromReadable(body);
responseHeaders.set("Content-Type", "text/html");
resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
Expand Down
7 changes: 5 additions & 2 deletions packages/remix-dev/config/defaults/entry.server.node.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PassThrough } from "node:stream";

import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -47,11 +48,12 @@ function handleBotRequest(
onAllReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down Expand Up @@ -96,11 +98,12 @@ function handleBrowserRequest(
onShellReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down
11 changes: 7 additions & 4 deletions packages/remix-express/__tests__/server-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Readable } from "node:stream";
import { createRequestHandler as createRemixRequestHandler } from "@remix-run/node";
import {
createReadableStreamFromReadable,
createRequestHandler as createRemixRequestHandler,
} from "@remix-run/node";
import express from "express";
import { createRequest, createResponse } from "node-mocks-http";
import supertest from "supertest";
Expand Down Expand Up @@ -99,9 +102,9 @@ describe("express createRequestHandler", () => {
// https://github.com/node-fetch/node-fetch/blob/4ae35388b078bddda238277142bf091898ce6fda/test/response.js#L142-L148
it("handles body as stream", async () => {
mockedCreateRequestHandler.mockImplementation(() => async () => {
let stream = Readable.from("hello world");
let webStream = Readable.toWeb(stream);
return new Response(webStream as ReadableStream, { status: 200 });
let readable = Readable.from("hello world");
let stream = createReadableStreamFromReadable(readable);
return new Response(stream, { status: 200 });
});

let request = supertest(createApp());
Expand Down
4 changes: 3 additions & 1 deletion scripts/playground/template/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PassThrough } from "node:stream";
import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -29,11 +30,12 @@ export default function handleRequest(
{
[callbackName]: () => {
let body = new PassThrough();
let stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
Expand Down
7 changes: 5 additions & 2 deletions templates/arc/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { PassThrough } from "node:stream";

import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -53,11 +54,12 @@ function handleBotRequest(
onAllReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down Expand Up @@ -102,11 +104,12 @@ function handleBrowserRequest(
onShellReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down
7 changes: 5 additions & 2 deletions templates/express/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { PassThrough } from "node:stream";

import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -53,11 +54,12 @@ function handleBotRequest(
onAllReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down Expand Up @@ -102,11 +104,12 @@ function handleBrowserRequest(
onShellReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down
7 changes: 5 additions & 2 deletions templates/fly/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { PassThrough } from "node:stream";

import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -53,11 +54,12 @@ function handleBotRequest(
onAllReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down Expand Up @@ -102,11 +104,12 @@ function handleBrowserRequest(
onShellReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down
7 changes: 5 additions & 2 deletions templates/remix-javascript/app/entry.server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { PassThrough } from "node:stream";

import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -49,11 +50,12 @@ function handleBotRequest(
{
onAllReady() {
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down Expand Up @@ -91,11 +93,12 @@ function handleBrowserRequest(
{
onShellReady() {
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down
7 changes: 5 additions & 2 deletions templates/remix/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { PassThrough } from "node:stream";

import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
Expand Down Expand Up @@ -53,11 +54,12 @@ function handleBotRequest(
onAllReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down Expand Up @@ -102,11 +104,12 @@ function handleBrowserRequest(
onShellReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down

0 comments on commit d281439

Please sign in to comment.