30 lines
858 B
TypeScript
30 lines
858 B
TypeScript
|
import { Streamable } from '../../streamable'
|
||
|
|
||
|
export const config = {
|
||
|
runtime: 'edge',
|
||
|
}
|
||
|
|
||
|
let streamable: ReturnType<typeof Streamable> | undefined
|
||
|
|
||
|
export default async function handler(req: Request): Promise<Response> {
|
||
|
// Consume the entire request body.
|
||
|
// This is so we don't confuse the request close with the connection close.
|
||
|
await req.text()
|
||
|
|
||
|
// 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) {
|
||
|
const old = streamable
|
||
|
streamable = undefined
|
||
|
const i = await old.finished
|
||
|
return new Response(`${i}`)
|
||
|
}
|
||
|
|
||
|
const write = new URL(req.url!, 'http://localhost/').searchParams.get('write')
|
||
|
const s = (streamable = Streamable(+write!))
|
||
|
req.signal.onabort = () => {
|
||
|
s.abort()
|
||
|
}
|
||
|
return new Response(s.stream)
|
||
|
}
|