From c502aa43312eeec19e01946b4543df6f19cf64f7 Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Mon, 23 Sep 2024 14:23:21 +0900 Subject: [PATCH] refactor(jsx/streaming): Clarified the type of renderToReadableStream. (#3434) --- src/jsx/streaming.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/jsx/streaming.ts b/src/jsx/streaming.ts index 9c475b506..cbdb4fd55 100644 --- a/src/jsx/streaming.ts +++ b/src/jsx/streaming.ts @@ -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, + content: HtmlEscapedString | JSXNode | Promise, onError: (e: unknown) => string | void = console.trace ): ReadableStream => { const reader = new ReadableStream({ 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 and string is already escaped + content = content.toString() as HtmlEscapedString | Promise + } + const context = typeof content === 'object' ? content : {} const resolved = await resolveCallback( - tmp, + content, HtmlEscapedCallbackPhase.BeforeStream, true, context