Unverified Commit aa32deb4 authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Simplify custom `Writable` (#32247)

* Simplify custom writable

* Fix draining
parent 4ce49b79
...@@ -1439,65 +1439,50 @@ function renderToNodeStream( ...@@ -1439,65 +1439,50 @@ function renderToNodeStream(
generateStaticHTML: boolean generateStaticHTML: boolean
): Promise<NodeWritablePiper> { ): Promise<NodeWritablePiper> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let underlyingStream: { let underlyingStream: WritableType | null = null
resolve: (error?: Error) => void let queuedCallbacks: Array<(error?: Error | null) => void> = []
writable: WritableType
queuedCallbacks: Array<() => void> // Based on the suggestion here:
} | null = null // https://github.com/reactwg/react-18/discussions/110
class NextWritable extends Writable {
const stream = new Writable({ _write(
// Use the buffer from the underlying stream chunk: any,
highWaterMark: 0, encoding: string,
writev(chunks, callback) { callback: (error?: Error | null) => void
let str = '' ) {
for (let { chunk } of chunks) {
str += chunk.toString()
}
if (!underlyingStream) { if (!underlyingStream) {
throw new Error( throw new Error(
'invariant: write called without an underlying stream. This is a bug in Next.js' 'invariant: write called without an underlying stream. This is a bug in Next.js'
) )
} }
// The compression module (https://github.com/expressjs/compression) doesn't
if (!underlyingStream.writable.write(str)) { // support callbacks, so we have to wait for a drain event.
underlyingStream.queuedCallbacks.push(() => callback()) if (!underlyingStream.write(chunk, encoding)) {
queuedCallbacks.push(callback)
} else { } else {
callback() callback()
} }
},
})
stream.once('finish', () => {
if (!underlyingStream) {
throw new Error(
'invariant: finish called without an underlying stream. This is a bug in Next.js'
)
}
underlyingStream.resolve()
})
stream.once('error', (err) => {
if (!underlyingStream) {
throw new Error(
'invariant: error called without an underlying stream. This is a bug in Next.js'
)
} }
underlyingStream.resolve(err)
}) flush() {
// React uses `flush` to prevent stream middleware like gzip from buffering to the
// point of harming streaming performance, so we make sure to expose it and forward it.
// See: https://github.com/reactwg/react-18/discussions/91
Object.defineProperty(stream, 'flush', {
value: () => {
if (!underlyingStream) { if (!underlyingStream) {
throw new Error( throw new Error(
'invariant: flush called without an underlying stream. This is a bug in Next.js' 'invariant: flush called without an underlying stream. This is a bug in Next.js'
) )
} }
if (typeof (underlyingStream.writable as any).flush === 'function') {
;(underlyingStream.writable as any).flush() const anyWritable = underlyingStream as any
if (typeof anyWritable.flush === 'function') {
anyWritable.flush()
} }
}, }
enumerable: true, }
const stream = new NextWritable()
stream.on('drain', () => {
const callbacks = queuedCallbacks
queuedCallbacks = []
callbacks.forEach((callback) => callback())
}) })
let resolved = false let resolved = false
...@@ -1505,21 +1490,16 @@ function renderToNodeStream( ...@@ -1505,21 +1490,16 @@ function renderToNodeStream(
if (!resolved) { if (!resolved) {
resolved = true resolved = true
resolve((res, next) => { resolve((res, next) => {
const drainHandler = () => { const doNext = (err?: Error) => {
const prevCallbacks = underlyingStream!.queuedCallbacks underlyingStream = null
underlyingStream!.queuedCallbacks = [] queuedCallbacks = []
prevCallbacks.forEach((callback) => callback()) next(err)
}
res.on('drain', drainHandler)
underlyingStream = {
resolve: (err) => {
underlyingStream = null
res.removeListener('drain', drainHandler)
next(err)
},
writable: res,
queuedCallbacks: [],
} }
stream.once('error', (err) => doNext(err))
stream.once('finish', () => doNext())
underlyingStream = res
startWriting() startWriting()
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment