-
Notifications
You must be signed in to change notification settings - Fork 26k
/
render-result.ts
46 lines (39 loc) · 1.14 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
import { ServerResponse } from 'http'
import Observable from 'next/dist/compiled/zen-observable'
export default class RenderResult {
_response: string | Observable<string>
constructor(response: string | Observable<string>) {
this._response = response
}
toUnchunkedString(): string {
if (typeof this._response !== 'string') {
throw new Error(
'invariant: dynamic responses cannot be unchunked. This is a bug in Next.js'
)
}
return this._response
}
async pipe(res: ServerResponse): Promise<void> {
if (typeof this._response === 'string') {
throw new Error(
'invariant: static responses cannot be piped. This is a bug in Next.js'
)
}
const maybeFlush =
typeof (res as any).flush === 'function'
? () => (res as any).flush()
: () => {}
await this._response.forEach((chunk) => {
res.write(chunk)
maybeFlush()
})
res.end()
}
isDynamic(): boolean {
return typeof this._response !== 'string'
}
static fromStatic(value: string): RenderResult {
return new RenderResult(value)
}
static empty = RenderResult.fromStatic('')
}