Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
aa32deb4
Unverified
Commit
aa32deb4
authored
Dec 08, 2021
by
Gerald Monaco
Committed by
GitHub
Dec 08, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify custom `Writable` (#32247)
* Simplify custom writable * Fix draining
parent
4ce49b79
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
58 deletions
+38
-58
render.tsx
packages/next/server/render.tsx
+38
-58
No files found.
packages/next/server/render.tsx
View file @
aa32deb4
...
...
@@ -1439,65 +1439,50 @@ function renderToNodeStream(
generateStaticHTML
:
boolean
):
Promise
<
NodeWritablePiper
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
let
underlyingStream
:
{
resolve
:
(
error
?:
Error
)
=>
void
writable
:
WritableType
queuedCallbacks
:
Array
<
()
=>
void
>
}
|
null
=
null
const
stream
=
new
Writable
({
// Use the buffer from the underlying stream
highWaterMark
:
0
,
writev
(
chunks
,
callback
)
{
let
str
=
''
for
(
let
{
chunk
}
of
chunks
)
{
str
+=
chunk
.
toString
()
}
let
underlyingStream
:
WritableType
|
null
=
null
let
queuedCallbacks
:
Array
<
(
error
?:
Error
|
null
)
=>
void
>
=
[]
// Based on the suggestion here:
// https://github.com/reactwg/react-18/discussions/110
class
NextWritable
extends
Writable
{
_write
(
chunk
:
any
,
encoding
:
string
,
callback
:
(
error
?:
Error
|
null
)
=>
void
)
{
if
(
!
underlyingStream
)
{
throw
new
Error
(
'invariant: write called without an underlying stream. This is a bug in Next.js'
)
}
if
(
!
underlyingStream
.
writable
.
write
(
str
))
{
underlyingStream
.
queuedCallbacks
.
push
(()
=>
callback
())
// The compression module (https://github.com/expressjs/compression) doesn't
// support callbacks, so we have to wait for a drain event.
if
(
!
underlyingStream
.
write
(
chunk
,
encoding
))
{
queuedCallbacks
.
push
(
callback
)
}
else
{
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
)
})
// 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
:
()
=>
{
flush
()
{
if
(
!
underlyingStream
)
{
throw
new
Error
(
'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
...
...
@@ -1505,21 +1490,16 @@ function renderToNodeStream(
if
(
!
resolved
)
{
resolved
=
true
resolve
((
res
,
next
)
=>
{
const
drainHandler
=
()
=>
{
const
prevCallbacks
=
underlyingStream
!
.
queuedCallbacks
underlyingStream
!
.
queuedCallbacks
=
[]
prevCallbacks
.
forEach
((
callback
)
=>
callback
())
}
res
.
on
(
'drain'
,
drainHandler
)
underlyingStream
=
{
resolve
:
(
err
)
=>
{
underlyingStream
=
null
res
.
removeListener
(
'drain'
,
drainHandler
)
next
(
err
)
},
writable
:
res
,
queuedCallbacks
:
[],
const
doNext
=
(
err
?:
Error
)
=>
{
underlyingStream
=
null
queuedCallbacks
=
[]
next
(
err
)
}
stream
.
once
(
'error'
,
(
err
)
=>
doNext
(
err
))
stream
.
once
(
'finish'
,
()
=>
doNext
())
underlyingStream
=
res
startWriting
()
})
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment