Files
Justin RidgewellandGitHub 3906374740 Try to fix flakey socket hang up failures in stream cancel tests (#53318)
### What?

I think the flakiness from the new stream cancel tests is due to a uncaught error thrown from the priming. If it is during the priming, then we need to also explicitly check if we restart the test and reset the `streamable`.

### Why?

Flakey tests suck.

### How?

- Adds a `on('error', reject)` to catch the socket error and associate it with the test
- Explicitly checks for the `?write=` param to see if we're priming or getting results
2023-07-28 22:59:24 +00:00

40 lines
1.0 KiB
TypeScript

import { IncomingMessage, ServerResponse } from 'http'
import { pipeline } from 'stream'
import { Readable } from '../../readable'
export const config = {
runtime: 'nodejs',
}
let readable: ReturnType<typeof Readable> | undefined
export default function handler(
req: IncomingMessage,
res: ServerResponse
): Promise<void> {
// Pages API requests have already consumed the body.
// This is so we don't confuse the request close with the connection close.
const write = new URL(req.url!, 'http://localhost/').searchParams.get('write')
// 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 (write) {
const r = (readable = Readable(+write!))
res.on('close', () => {
r.abort()
})
return new Promise((resolve) => {
pipeline(r.stream, res, () => {
resolve()
res.end()
})
})
}
const old = readable!
readable = undefined
return old.finished.then((i) => {
res.end(`${i}`)
})
}