0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

refactor(jsx/streaming): Clarified the type of renderToReadableStream. (#3434)

This commit is contained in:
Taku Amano 2024-09-23 14:23:21 +09:00 committed by GitHub
parent 66df4e1296
commit c502aa4331
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,6 +6,7 @@
import { raw } from '../helper/html'
import { HtmlEscapedCallbackPhase, resolveCallback } from '../utils/html'
import type { HtmlEscapedString } from '../utils/html'
import { JSXNode } from './base'
import { childrenToString } from './components'
import { DOM_RENDERER, DOM_STASH } from './constants'
import { Suspense as SuspenseDomRenderer } from './dom/components'
@ -121,16 +122,19 @@ const textEncoder = new TextEncoder()
* The API might be changed.
*/
export const renderToReadableStream = (
str: HtmlEscapedString | Promise<HtmlEscapedString>,
content: HtmlEscapedString | JSXNode | Promise<HtmlEscapedString>,
onError: (e: unknown) => string | void = console.trace
): ReadableStream<Uint8Array> => {
const reader = new ReadableStream<Uint8Array>({
async start(controller) {
try {
const tmp = str instanceof Promise ? await str : await str.toString()
const context = typeof tmp === 'object' ? tmp : {}
if (content instanceof JSXNode) {
// aJSXNode.toString() returns a string or Promise<string> and string is already escaped
content = content.toString() as HtmlEscapedString | Promise<HtmlEscapedString>
}
const context = typeof content === 'object' ? content : {}
const resolved = await resolveCallback(
tmp,
content,
HtmlEscapedCallbackPhase.BeforeStream,
true,
context