forked from vercel/next.js
/
render-result.ts
66 lines (55 loc) · 1.47 KB
/
render-result.ts
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
59
60
61
62
63
64
65
66
import type { ServerResponse } from 'http'
export default class RenderResult {
_result: string | ReadableStream<Uint8Array>
constructor(response: string | ReadableStream<Uint8Array>) {
this._result = response
}
toUnchunkedString(): string {
if (typeof this._result !== 'string') {
throw new Error(
'invariant: dynamic responses cannot be unchunked. This is a bug in Next.js'
)
}
return this._result
}
pipe(res: ServerResponse): Promise<void> {
if (typeof this._result === 'string') {
throw new Error(
'invariant: static responses cannot be piped. This is a bug in Next.js'
)
}
const response = this._result
const flush =
typeof (res as any).flush === 'function'
? () => (res as any).flush()
: () => {}
return (async () => {
const reader = response.getReader()
let fatalError = false
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
res.end()
return
}
fatalError = true
res.write(value)
flush()
}
} catch (err) {
if (fatalError) {
res.destroy(err as any)
}
throw err
}
})()
}
isDynamic(): boolean {
return typeof this._result !== 'string'
}
static fromStatic(value: string): RenderResult {
return new RenderResult(value)
}
static empty = RenderResult.fromStatic('')
}