Skip to content

Commit 20c081e

Browse files
sebmarkbagezhengjitf
authored andcommitted
[Fizz/Flight] pipeToNodeWritable(..., writable).startWriting() -> renderToPipeableStream(...).pipe(writable) (facebook#22450)
* Rename pipeToNodeWritable to renderToNodePipe * Add startWriting API to Flight We don't really need it in this case because there's way less reason to delay the stream in Flight. * Pass the destination to startWriting instead of renderToNode * Rename startWriting to pipe This mirrors the ReadableStream API in Node * Error codes * Rename to renderToPipeableStream This mimics the renderToReadableStream API for the browser.
1 parent 83d50bc commit 20c081e

File tree

11 files changed

+169
-178
lines changed

11 files changed

+169
-178
lines changed

fixtures/flight/server/handler.server.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const {pipeToNodeWritable} = require('react-server-dom-webpack/writer');
3+
const {renderToPipeableStream} = require('react-server-dom-webpack/writer');
44
const {readFile} = require('fs');
55
const {resolve} = require('path');
66
const React = require('react');
@@ -20,7 +20,11 @@ module.exports = function(req, res) {
2020
const App = m.default.default || m.default;
2121
res.setHeader('Access-Control-Allow-Origin', '*');
2222
const moduleMap = JSON.parse(data);
23-
pipeToNodeWritable(React.createElement(App), res, moduleMap);
23+
const {pipe} = renderToPipeableStream(
24+
React.createElement(App),
25+
moduleMap
26+
);
27+
pipe(res);
2428
}
2529
);
2630
});

fixtures/ssr/server/render.js

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import {pipeToNodeWritable} from 'react-dom/server';
2+
import {renderToPipeableStream} from 'react-dom/server';
33

44
import App from '../src/components/App';
55

@@ -20,22 +20,18 @@ export default function render(url, res) {
2020
console.error('Fatal', error);
2121
});
2222
let didError = false;
23-
const {startWriting, abort} = pipeToNodeWritable(
24-
<App assets={assets} />,
25-
res,
26-
{
27-
onCompleteShell() {
28-
// If something errored before we started streaming, we set the error code appropriately.
29-
res.statusCode = didError ? 500 : 200;
30-
res.setHeader('Content-type', 'text/html');
31-
startWriting();
32-
},
33-
onError(x) {
34-
didError = true;
35-
console.error(x);
36-
},
37-
}
38-
);
23+
const {pipe, abort} = renderToPipeableStream(<App assets={assets} />, {
24+
onCompleteShell() {
25+
// If something errored before we started streaming, we set the error code appropriately.
26+
res.statusCode = didError ? 500 : 200;
27+
res.setHeader('Content-type', 'text/html');
28+
pipe(res);
29+
},
30+
onError(x) {
31+
didError = true;
32+
console.error(x);
33+
},
34+
});
3935
// Abandon and switch to client rendering after 5 seconds.
4036
// Try lowering this to see the client recover.
4137
setTimeout(abort, 5000);

fixtures/ssr2/server/render.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import * as React from 'react';
1010
// import {renderToString} from 'react-dom/server';
11-
import {pipeToNodeWritable} from 'react-dom/server';
11+
import {renderToPipeableStream} from 'react-dom/server';
1212
import App from '../src/App';
1313
import {DataProvider} from '../src/data';
1414
import {API_DELAY, ABORT_DELAY} from './delays';
@@ -37,17 +37,16 @@ module.exports = function render(url, res) {
3737
});
3838
let didError = false;
3939
const data = createServerData();
40-
const {startWriting, abort} = pipeToNodeWritable(
40+
const {pipe, abort} = renderToPipeableStream(
4141
<DataProvider data={data}>
4242
<App assets={assets} />
4343
</DataProvider>,
44-
res,
4544
{
4645
onCompleteShell() {
4746
// If something errored before we started streaming, we set the error code appropriately.
4847
res.statusCode = didError ? 500 : 200;
4948
res.setHeader('Content-type', 'text/html');
50-
startWriting();
49+
pipe(res);
5150
},
5251
onError(x) {
5352
didError = true;

packages/react-dom/npm/server.node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ exports.renderToString = l.renderToString;
1414
exports.renderToStaticMarkup = l.renderToStaticMarkup;
1515
exports.renderToNodeStream = l.renderToNodeStream;
1616
exports.renderToStaticNodeStream = l.renderToStaticNodeStream;
17-
exports.pipeToNodeWritable = s.pipeToNodeWritable;
17+
exports.renderToPipeableStream = s.renderToPipeableStream;

packages/react-dom/server.node.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export function renderToStaticNodeStream() {
3636
);
3737
}
3838

39-
export function pipeToNodeWritable() {
40-
return require('./src/server/ReactDOMFizzServerNode').pipeToNodeWritable.apply(
39+
export function renderToPipeableStream() {
40+
return require('./src/server/ReactDOMFizzServerNode').renderToPipeableStream.apply(
4141
this,
4242
arguments,
4343
);

0 commit comments

Comments
 (0)