File size: 1,358 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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 async function handler(
req: IncomingMessage,
res: ServerResponse
): Promise<void> {
const url = new URL(req.url!, 'http://localhost/')
if (url.searchParams.has('compile')) {
// The request just wants to trigger compilation.
res.statusCode = 204
res.end()
return
}
// Pages API requests have already consumed the body.
// This is so we don't confuse the request close with the connection close.
const write = url.searchParams.get('write')
if (write) {
const r = (readable = Readable(+write))
res.on('close', () => {
r.abort()
})
return new Promise((resolve) => {
pipeline(r.stream, res, () => {
resolve()
res.end()
})
})
}
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
const old = readable
if (!old) {
res.statusCode = 500
res.end(
'The streamable from the prime request is unexpectedly not available'
)
return
}
readable = undefined
const i = await old.finished
res.end(`${i}`)
}
|