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
9b1b1b3a
Unverified
Commit
9b1b1b3a
authored
May 11, 2024
by
Ethan Arrowood
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite without promises
parent
fb389c97
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
34 deletions
+39
-34
stream-utils.node.ts
packages/next/src/server/stream-utils/stream-utils.node.ts
+39
-34
No files found.
packages/next/src/server/stream-utils/stream-utils.node.ts
View file @
9b1b1b3a
...
...
@@ -10,7 +10,7 @@ import {
pipeline
,
}
from
'node:stream'
import
type
{
Options
as
RenderToPipeableStreamOptions
}
from
'react-dom/server.node'
import
{
DetachedPromise
}
from
'../../lib/detached-promise
'
import
isError
from
'../../lib/is-error
'
export
*
from
'./stream-utils.edge'
...
...
@@ -107,47 +107,52 @@ export function streamFromString(string: string): Readable {
return
Readable
.
from
(
string
)
}
/**
* This utility function buffers all of the chunks it receives from the input
* during a single "macro-task". The transform function schedules a
* `setImmediate` callback that will push the buffered chunks to the readable.
* The transform also ensures not to execute the final callback too early. The
* overall timing of this utility is very specific and must match that of the
* edge based version.
*/
export
function
createBufferedTransformStream
():
Transform
{
let
buffered
:
Uint8Array
[]
=
[]
let
byteLength
=
0
let
pending
:
DetachedPromise
<
void
>
|
undefined
const
flush
=
(
transform
:
Transform
)
=>
{
if
(
pending
)
return
const
detached
=
new
DetachedPromise
<
void
>
()
pending
=
detached
setImmediate
(()
=>
{
try
{
const
chunk
=
new
Uint8Array
(
byteLength
)
let
copiedBytes
=
0
for
(
let
i
=
0
;
i
<
buffered
.
length
;
i
++
)
{
chunk
.
set
(
buffered
[
i
],
copiedBytes
)
copiedBytes
+=
buffered
[
i
].
byteLength
}
buffered
.
length
=
0
byteLength
=
0
transform
.
push
(
chunk
)
}
catch
{
}
finally
{
pending
=
undefined
detached
.
resolve
()
}
})
}
let
bufferedChunks
:
Uint8Array
[]
=
[]
let
bufferedChunksByteLength
=
0
let
pending
=
false
return
new
Transform
({
transform
(
chunk
,
_
,
callback
)
{
buffered
.
push
(
chunk
)
byteLength
+=
chunk
.
byteLength
flush
(
this
)
callback
()
bufferedChunks
.
push
(
chunk
)
bufferedChunksByteLength
+=
chunk
.
byteLength
if
(
pending
)
callback
()
pending
=
true
setImmediate
(()
=>
{
try
{
const
bufferedChunk
=
new
Uint8Array
(
bufferedChunksByteLength
)
let
copiedBytes
=
0
for
(
let
i
=
0
;
i
<
bufferedChunks
.
length
;
i
++
)
{
bufferedChunk
.
set
(
bufferedChunks
[
i
],
copiedBytes
)
copiedBytes
+=
bufferedChunks
[
i
].
byteLength
}
bufferedChunks
.
length
=
0
bufferedChunksByteLength
=
0
callback
(
null
,
bufferedChunk
)
}
catch
(
err
:
unknown
)
{
if
(
isError
(
err
))
callback
(
err
)
}
finally
{
pending
=
false
}
})
},
final
(
callback
)
{
if
(
!
pending
)
callback
()
pending
?.
promise
.
then
(()
=>
callback
())
process
.
nextTick
(()
=>
{
callback
()
})
},
})
}
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