-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "Fix stream cancellation in
RenderResult.pipe()
and …
- Loading branch information
1 parent
cee0599
commit 67b96e2
Showing
14 changed files
with
333 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Streamable } from '../../streamable' | ||
|
||
export const runtime = 'edge' | ||
|
||
let streamable | ||
let requestAborted = false | ||
|
||
export function GET(req: Request): Response { | ||
// The 2nd request should render the stats. We don't use a query param | ||
// because edge rendering will create a different bundle for that. | ||
if (streamable) { | ||
return new Response( | ||
JSON.stringify({ | ||
requestAborted, | ||
i: streamable.i, | ||
streamCleanedUp: streamable.streamCleanedUp, | ||
}) | ||
) | ||
} | ||
|
||
streamable = Streamable() | ||
req.signal.onabort = () => { | ||
requestAborted = true | ||
} | ||
return new Response(streamable.stream) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Streamable } from '../../streamable' | ||
|
||
export const runtime = 'nodejs' | ||
// Next thinks it can statically compile this route, which breaks the test. | ||
export const dynamic = 'force-dynamic' | ||
|
||
let streamable | ||
let requestAborted = false | ||
|
||
export function GET(req: Request): Response { | ||
// The 2nd request should render the stats. We don't use a query param | ||
// because edge rendering will create a different bundle for that. | ||
if (streamable) { | ||
return new Response( | ||
JSON.stringify({ | ||
requestAborted, | ||
i: streamable.i, | ||
streamCleanedUp: streamable.streamCleanedUp, | ||
}) | ||
) | ||
} | ||
|
||
streamable = Streamable() | ||
req.signal.onabort = () => { | ||
requestAborted = true | ||
} | ||
return new Response(streamable.stream) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Streamable } from './streamable' | ||
|
||
export const config = { | ||
matcher: '/middleware', | ||
} | ||
|
||
let streamable | ||
let requestAborted = false | ||
|
||
export default function handler(req: Request): Response { | ||
// The 2nd request should render the stats. We don't use a query param | ||
// because edge rendering will create a different bundle for that. | ||
if (streamable) { | ||
return new Response( | ||
JSON.stringify({ | ||
requestAborted, | ||
i: streamable.i, | ||
streamCleanedUp: streamable.streamCleanedUp, | ||
}) | ||
) | ||
} | ||
|
||
streamable = Streamable() | ||
req.signal.onabort = () => { | ||
requestAborted = true | ||
} | ||
return new Response(streamable.stream) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Streamable } from '../../streamable' | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
let streamable | ||
let requestAborted = false | ||
|
||
export default function handler(req: Request): Response { | ||
// The 2nd request should render the stats. We don't use a query param | ||
// because edge rendering will create a different bundle for that. | ||
if (streamable) { | ||
return new Response( | ||
JSON.stringify({ | ||
requestAborted, | ||
i: streamable.i, | ||
streamCleanedUp: streamable.streamCleanedUp, | ||
}) | ||
) | ||
} | ||
|
||
streamable = Streamable() | ||
req.signal.onabort = () => { | ||
requestAborted = true | ||
} | ||
return new Response(streamable.stream) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { IncomingMessage, ServerResponse } from 'http' | ||
import { pipeline } from 'stream' | ||
import { Readable } from '../../readable' | ||
|
||
export const config = { | ||
runtime: 'nodejs', | ||
} | ||
|
||
let readable | ||
let requestAborted = false | ||
|
||
export default function handler( | ||
_req: IncomingMessage, | ||
res: ServerResponse | ||
): void { | ||
// The 2nd request should render the stats. We don't use a query param | ||
// because edge rendering will create a different bundle for that. | ||
if (readable) { | ||
res.end( | ||
JSON.stringify({ | ||
requestAborted, | ||
i: readable.i, | ||
streamCleanedUp: readable.streamCleanedUp, | ||
}) | ||
) | ||
return | ||
} | ||
|
||
readable = Readable() | ||
res.on('close', () => { | ||
requestAborted = true | ||
}) | ||
pipeline(readable.stream, res, () => { | ||
res.end() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as stream from 'stream' | ||
import { sleep } from './sleep' | ||
|
||
export function Readable() { | ||
const encoder = new TextEncoder() | ||
const readable = { | ||
i: 0, | ||
streamCleanedUp: false, | ||
stream: new stream.Readable({ | ||
async read() { | ||
await sleep(100) | ||
this.push(encoder.encode(String(readable.i++))) | ||
|
||
if (readable.i >= 25) this.push(null) | ||
}, | ||
destroy() { | ||
readable.streamCleanedUp = true | ||
}, | ||
}), | ||
} | ||
return readable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function sleep(ms: number) { | ||
return new Promise((res) => setTimeout(res, ms)) | ||
} |
Oops, something went wrong.