Skip to content

Commit

Permalink
fix partialrenderer variants
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoff committed Jun 7, 2022
1 parent 2e78547 commit 0438912
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 90 deletions.
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export {
renderToString,
renderToStaticMarkup,
version,
} from './ReactDOMServerLegacyPartialRendererBrowser';

export {
renderToNodeStream,
renderToStaticNodeStream,
} from './ReactDOMLegacyServerNodeStream';
94 changes: 4 additions & 90 deletions packages/react-dom/src/server/ReactDOMLegacyServerNode.js
Expand Up @@ -9,102 +9,16 @@

import type {ReactNodeList} from 'shared/ReactTypes';

import type {Request} from 'react-server/src/ReactFizzServer';

import {
createRequest,
startWork,
startFlowing,
abort,
} from 'react-server/src/ReactFizzServer';

import {
createResponseState,
createRootFormatContext,
} from './ReactDOMServerLegacyFormatConfig';

import {version, renderToStringImpl} from './ReactDOMLegacyServerImpl';

import {Readable} from 'stream';
import {
renderToNodeStream,
renderToStaticNodeStream,
} from './ReactDOMLegacyServerNodeStream';

type ServerOptions = {
identifierPrefix?: string,
};

class ReactMarkupReadableStream extends Readable {
request: Request;
startedFlowing: boolean;
constructor() {
// Calls the stream.Readable(options) constructor. Consider exposing built-in
// features like highWaterMark in the future.
super({});
this.request = (null: any);
this.startedFlowing = false;
}

_destroy(err, callback) {
abort(this.request);
// $FlowFixMe: The type definition for the callback should allow undefined and null.
callback(err);
}

_read(size) {
if (this.startedFlowing) {
startFlowing(this.request, this);
}
}
}

function onError() {
// Non-fatal errors are ignored.
}

function renderToNodeStreamImpl(
children: ReactNodeList,
options: void | ServerOptions,
generateStaticMarkup: boolean,
): Readable {
function onAllReady() {
// We wait until everything has loaded before starting to write.
// That way we only end up with fully resolved HTML even if we suspend.
destination.startedFlowing = true;
startFlowing(request, destination);
}
const destination = new ReactMarkupReadableStream();
const request = createRequest(
children,
createResponseState(false, options ? options.identifierPrefix : undefined),
createRootFormatContext(),
Infinity,
onError,
onAllReady,
undefined,
undefined,
);
destination.request = request;
startWork(request);
return destination;
}

function renderToNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
if (__DEV__) {
console.error(
'renderToNodeStream is deprecated. Use renderToPipeableStream instead.',
);
}
return renderToNodeStreamImpl(children, options, false);
}

function renderToStaticNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
return renderToNodeStreamImpl(children, options, true);
}

function renderToString(
children: ReactNodeList,
options?: ServerOptions,
Expand Down
106 changes: 106 additions & 0 deletions packages/react-dom/src/server/ReactDOMLegacyServerNodeStream.js
@@ -0,0 +1,106 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {ReactNodeList} from 'shared/ReactTypes';

import type {Request} from 'react-server/src/ReactFizzServer';

import {
createRequest,
startWork,
startFlowing,
abort,
} from 'react-server/src/ReactFizzServer';

import {
createResponseState,
createRootFormatContext,
} from './ReactDOMServerLegacyFormatConfig';

import {Readable} from 'stream';

type ServerOptions = {
identifierPrefix?: string,
};

class ReactMarkupReadableStream extends Readable {
request: Request;
startedFlowing: boolean;
constructor() {
// Calls the stream.Readable(options) constructor. Consider exposing built-in
// features like highWaterMark in the future.
super({});
this.request = (null: any);
this.startedFlowing = false;
}

_destroy(err, callback) {
abort(this.request);
// $FlowFixMe: The type definition for the callback should allow undefined and null.
callback(err);
}

_read(size) {
if (this.startedFlowing) {
startFlowing(this.request, this);
}
}
}

function onError() {
// Non-fatal errors are ignored.
}

function renderToNodeStreamImpl(
children: ReactNodeList,
options: void | ServerOptions,
generateStaticMarkup: boolean,
): Readable {
function onAllReady() {
// We wait until everything has loaded before starting to write.
// That way we only end up with fully resolved HTML even if we suspend.
destination.startedFlowing = true;
startFlowing(request, destination);
}
const destination = new ReactMarkupReadableStream();
const request = createRequest(
children,
createResponseState(false, options ? options.identifierPrefix : undefined),
createRootFormatContext(),
Infinity,
onError,
onAllReady,
undefined,
undefined,
);
destination.request = request;
startWork(request);
return destination;
}

function renderToNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
if (__DEV__) {
console.error(
'renderToNodeStream is deprecated. Use renderToPipeableStream instead.',
);
}
return renderToNodeStreamImpl(children, options, false);
}

function renderToStaticNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
return renderToNodeStreamImpl(children, options, true);
}

export {renderToNodeStream, renderToStaticNodeStream};
1 change: 1 addition & 0 deletions scripts/shared/inlinedHostConfigs.js
Expand Up @@ -72,6 +72,7 @@ module.exports = [
'react-dom/src/server/ReactDOMLegacyServerImpl.js', // not an entrypoint, but only usable in *Brower and *Node files
'react-dom/src/server/ReactDOMLegacyServerBrowser.js', // react-dom/server.browser
'react-dom/src/server/ReactDOMLegacyServerNode.js', // react-dom/server.node
'react-dom/src/server/ReactDOMLegacyServerNodeStream.js', // file indirection to support partial forking of some methods in *Node
'react-client/src/ReactFlightClientStream.js', // We can only type check this in streaming configurations.
],
isFlowTyped: true,
Expand Down

0 comments on commit 0438912

Please sign in to comment.