Skip to content

[Flight] Use Web Streams APIs for 3rd-party component in Flight fixture #33481

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

Merged
merged 1 commit into from
Jun 8, 2025
Merged
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
52 changes: 21 additions & 31 deletions fixtures/flight/src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import {renderToPipeableStream} from 'react-server-dom-webpack/server';
import {createFromNodeStream} from 'react-server-dom-webpack/client';
import {renderToReadableStream} from 'react-server-dom-webpack/server';
import {createFromReadableStream} from 'react-server-dom-webpack/client';
import {PassThrough, Readable} from 'stream';

import Container from './Container.js';
Expand Down Expand Up @@ -46,43 +46,33 @@ async function ThirdPartyComponent() {
return delay('hello from a 3rd party', 30);
}

// Using Web streams for tee'ing convenience here.
let cachedThirdPartyReadableWeb;
let cachedThirdPartyStream;

// We create the Component outside of AsyncLocalStorage so that it has no owner.
// That way it gets the owner from the call to createFromNodeStream.
const thirdPartyComponent = <ThirdPartyComponent />;

function fetchThirdParty(noCache) {
if (cachedThirdPartyReadableWeb && !noCache) {
const [readableWeb1, readableWeb2] = cachedThirdPartyReadableWeb.tee();
cachedThirdPartyReadableWeb = readableWeb1;

return createFromNodeStream(Readable.fromWeb(readableWeb2), {
// We're using the Web Streams APIs for tee'ing convenience.
const stream =
cachedThirdPartyStream && !noCache
? cachedThirdPartyStream
: renderToReadableStream(
thirdPartyComponent,
{},
{environmentName: 'third-party'}
);

const [stream1, stream2] = stream.tee();
cachedThirdPartyStream = stream1;

return createFromReadableStream(stream2, {
serverConsumerManifest: {
moduleMap: {},
moduleLoading: {},
});
}

const stream = renderToPipeableStream(
thirdPartyComponent,
{},
{environmentName: 'third-party'}
);

const readable = new PassThrough();
// React currently only supports piping to one stream, so we convert, tee, and
// convert back again.
// TODO: Switch to web streams without converting when #33442 has landed.
const [readableWeb1, readableWeb2] = Readable.toWeb(readable).tee();
cachedThirdPartyReadableWeb = readableWeb1;
const result = createFromNodeStream(Readable.fromWeb(readableWeb2), {
moduleMap: {},
moduleLoading: {},
serverModuleMap: null,
moduleLoading: null,
},
});
stream.pipe(readable);

return result;
}

async function ServerComponent({noCache}) {
Expand Down
Loading