Skip to content

Commit 251b90c

Browse files
refactor: unify stream types by introducing PipeableOrReadableStream
1 parent ad4dbe1 commit 251b90c

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

node_package/src/injectRSCPayload.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { PipeableStream } from 'react-dom/server';
21
import { PassThrough, Transform } from 'stream';
32
import { finished } from 'stream/promises';
4-
import { RailsContextWithServerComponentCapabilities } from './types/index.ts';
3+
import { RailsContextWithServerComponentCapabilities, PipeableOrReadableStream } from './types/index.ts';
54

65
// In JavaScript, when an escape sequence with a backslash (\) is followed by a character
76
// that isn't a recognized escape character, the backslash is ignored, and the character
@@ -50,7 +49,7 @@ function writeChunk(chunk: string, transform: Transform, cacheKey: string) {
5049
* @returns A combined stream with embedded RSC payloads
5150
*/
5251
export default function injectRSCPayload(
53-
pipeableHtmlStream: NodeJS.ReadableStream | PipeableStream,
52+
pipeableHtmlStream: PipeableOrReadableStream,
5453
railsContext: RailsContextWithServerComponentCapabilities,
5554
) {
5655
const htmlStream = new PassThrough();

node_package/src/streamServerRenderedReactComponent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import createReactOutput from './createReactOutput.ts';
66
import { isPromise, isServerRenderHash } from './isServerRenderResult.ts';
77
import buildConsoleReplay from './buildConsoleReplay.ts';
88
import handleError from './handleError.ts';
9-
import { renderToPipeableStream, PipeableStream } from './ReactDOMServer.cts';
9+
import { renderToPipeableStream } from './ReactDOMServer.cts';
1010
import { createResultObject, convertToError, validateComponent } from './serverRenderUtils.ts';
1111
import {
1212
assertRailsContextWithServerComponentCapabilities,
1313
RenderParams,
1414
StreamRenderState,
1515
StreamableComponentResult,
16+
PipeableOrReadableStream,
1617
} from './types/index.ts';
1718
import injectRSCPayload from './injectRSCPayload.ts';
1819
import { notifySSREnd } from './postSSRHooks.ts';
@@ -112,8 +113,8 @@ export const transformRenderStreamChunksToResultObject = (renderState: StreamRen
112113
},
113114
});
114115

115-
let pipedStream: PipeableStream | NodeJS.ReadableStream | null = null;
116-
const pipeToTransform = (pipeableStream: PipeableStream | NodeJS.ReadableStream) => {
116+
let pipedStream: PipeableOrReadableStream | null = null;
117+
const pipeToTransform = (pipeableStream: PipeableOrReadableStream) => {
117118
pipeableStream.pipe(transformStream);
118119
pipedStream = pipeableStream;
119120
};

node_package/src/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference types="react/experimental" />
22

33
import type { ReactElement, ReactNode, Component, ComponentType } from 'react';
4+
import type { PipeableStream } from 'react-dom/server';
45
import type { Readable } from 'stream';
56

67
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -176,6 +177,8 @@ interface RenderFunction {
176177

177178
type ReactComponentOrRenderFunction = ReactComponent | RenderFunction;
178179

180+
type PipeableOrReadableStream = PipeableStream | NodeJS.ReadableStream;
181+
179182
export type {
180183
ReactComponentOrRenderFunction,
181184
ReactComponent,
@@ -192,6 +195,7 @@ export type {
192195
RenderFunctionSyncResult,
193196
RenderFunctionAsyncResult,
194197
StreamableComponentResult,
198+
PipeableOrReadableStream,
195199
};
196200

197201
export interface RegisteredComponent {

0 commit comments

Comments
 (0)