2021-10-26 18:50:56 +02:00
|
|
|
import type { ServerResponse } from 'http'
|
2022-10-06 21:43:23 +02:00
|
|
|
import { Writable } from 'stream'
|
2021-09-11 02:17:56 +02:00
|
|
|
|
2022-09-18 22:49:05 +02:00
|
|
|
type ContentTypeOption = string | undefined
|
|
|
|
|
2021-09-04 16:41:06 +02:00
|
|
|
export default class RenderResult {
|
2022-09-18 22:49:05 +02:00
|
|
|
private _result: string | ReadableStream<Uint8Array>
|
|
|
|
private _contentType: ContentTypeOption
|
2021-09-04 16:41:06 +02:00
|
|
|
|
2022-09-18 22:49:05 +02:00
|
|
|
constructor(
|
|
|
|
response: string | ReadableStream<Uint8Array>,
|
|
|
|
{ contentType }: { contentType?: ContentTypeOption } = {}
|
|
|
|
) {
|
2021-09-11 02:17:56 +02:00
|
|
|
this._result = response
|
2022-09-18 22:49:05 +02:00
|
|
|
this._contentType = contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType(): ContentTypeOption {
|
|
|
|
return this._contentType
|
2021-09-04 16:41:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 18:56:31 +02:00
|
|
|
toUnchunkedString(): string {
|
2021-09-11 02:17:56 +02:00
|
|
|
if (typeof this._result !== 'string') {
|
2021-09-08 18:56:31 +02:00
|
|
|
throw new Error(
|
|
|
|
'invariant: dynamic responses cannot be unchunked. This is a bug in Next.js'
|
|
|
|
)
|
2021-09-04 16:41:06 +02:00
|
|
|
}
|
2021-09-11 02:17:56 +02:00
|
|
|
return this._result
|
2021-09-04 16:41:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 21:43:23 +02:00
|
|
|
pipe(res: ServerResponse | Writable): Promise<void> {
|
2021-09-11 02:17:56 +02:00
|
|
|
if (typeof this._result === 'string') {
|
2021-09-08 18:56:31 +02:00
|
|
|
throw new Error(
|
|
|
|
'invariant: static responses cannot be piped. This is a bug in Next.js'
|
|
|
|
)
|
2021-09-04 16:41:06 +02:00
|
|
|
}
|
2021-09-11 02:17:56 +02:00
|
|
|
const response = this._result
|
2022-02-04 18:52:53 +01:00
|
|
|
const flush =
|
|
|
|
typeof (res as any).flush === 'function'
|
|
|
|
? () => (res as any).flush()
|
|
|
|
: () => {}
|
|
|
|
|
2022-02-05 02:13:02 +01:00
|
|
|
return (async () => {
|
|
|
|
const reader = response.getReader()
|
2022-02-04 18:52:53 +01:00
|
|
|
let fatalError = false
|
2022-02-05 02:13:02 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read()
|
|
|
|
|
|
|
|
if (done) {
|
2022-02-04 18:52:53 +01:00
|
|
|
res.end()
|
2022-02-05 02:13:02 +01:00
|
|
|
return
|
2022-02-04 18:52:53 +01:00
|
|
|
}
|
2022-02-05 02:13:02 +01:00
|
|
|
|
|
|
|
fatalError = true
|
|
|
|
res.write(value)
|
|
|
|
flush()
|
2022-02-04 18:52:53 +01:00
|
|
|
}
|
2022-02-05 02:13:02 +01:00
|
|
|
} catch (err) {
|
|
|
|
if (fatalError) {
|
|
|
|
res.destroy(err as any)
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
})()
|
2021-09-04 16:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isDynamic(): boolean {
|
2021-09-11 02:17:56 +02:00
|
|
|
return typeof this._result !== 'string'
|
2021-09-04 16:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromStatic(value: string): RenderResult {
|
2021-09-08 18:56:31 +02:00
|
|
|
return new RenderResult(value)
|
2021-09-04 16:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static empty = RenderResult.fromStatic('')
|
|
|
|
}
|