Skip to content

Commit

Permalink
node-web-streams: remove tee shim, use ReadableStream.tee (#54079)
Browse files Browse the repository at this point in the history
### What?
~The `setTimeout(() => {}, 0)` was used in place of `setImmediate` due to lack of support in the Edge Runtime. This is no longer a problem.~ Not working like I expected. 

`ReadableStream.tee` should be widely supported as well as of [Node 16.5](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/tee#browser_compatibility)

### Why?
Use the platform!

### How?
Use the built-in `ReadableStream.tee()`
  • Loading branch information
MaxLeiter authored Aug 17, 2023
1 parent 93259ca commit 6767454
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 25 deletions.
3 changes: 1 addition & 2 deletions packages/next/src/server/app-render/use-flight-response.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin'
import type { FlightResponseRef } from './flight-response-ref'
import { readableStreamTee } from '../stream-utils/node-web-streams-helper'
import { encodeText, decodeText } from '../stream-utils/encode-decode'
import { htmlEscapeJsonString } from '../htmlescape'

Expand All @@ -26,7 +25,7 @@ export function useFlightResponse(
createFromReadableStream,
} = require(`react-server-dom-webpack/client.edge`)

const [renderStream, forwardStream] = readableStreamTee(req)
const [renderStream, forwardStream] = req.tee()
const res = createFromReadableStream(renderStream, {
moduleMap: isEdgeRuntime
? clientReferenceManifest.edgeSSRModuleMapping
Expand Down
23 changes: 0 additions & 23 deletions packages/next/src/server/stream-utils/node-web-streams-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,6 @@ export function cloneTransformStream(source: TransformStream) {
return clone
}

export function readableStreamTee<T = any>(
readable: ReadableStream<T>
): [ReadableStream<T>, ReadableStream<T>] {
const transformStream = new TransformStream()
const transformStream2 = new TransformStream()
const writer = transformStream.writable.getWriter()
const writer2 = transformStream2.writable.getWriter()

const reader = readable.getReader()
async function read() {
const { done, value } = await reader.read()
if (done) {
await Promise.all([writer.close(), writer2.close()])
return
}
await Promise.all([writer.write(value), writer2.write(value)])
await read()
}
read()

return [transformStream.readable, transformStream2.readable]
}

export function chainStreams<T>(
streams: ReadableStream<T>[]
): ReadableStream<T> {
Expand Down

0 comments on commit 6767454

Please sign in to comment.