components.ts
This file
Path
src/jsx/components.ts
257 lines 路 8.8 KiB 路 TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258import { raw } from '../helper/html'import type { HtmlEscapedCallback, HtmlEscapedString } from '../utils/html'import { HtmlEscapedCallbackPhase, resolveCallback } from '../utils/html'import type { Child, FC, PropsWithChildren } from './'import { jsx, Fragment, isUntrustedObject, renderChildren, renderUntrustedObject } from './base'import { DOM_RENDERER } from './constants'import { captureRenderContext, useContext } from './context'import { ErrorBoundary as ErrorBoundaryDomRenderer } from './dom/components'import type { HasRenderToDom } from './dom/render'import { StreamingContext } from './streaming'
let errorBoundaryCounter = 0
export const childrenToString = async (children: Child[]): Promise<HtmlEscapedString[]> => { try { return children.flat().map(resolveChildEarly) as HtmlEscapedString[] } catch (e) { if (e instanceof Promise) { // Capture before `await`: on the fallback path the render context is // only observable during this synchronous window. const resume = captureRenderContext() await e return resume(() => childrenToString(children)) } else { throw e } }}
const resolveChildEarly = (child: Child): HtmlEscapedString | Promise<HtmlEscapedString> => { if (child == null || typeof child === 'boolean') { return '' as HtmlEscapedString } else if (typeof child === 'string' || Array.isArray(child)) { return renderChildren([child]) } else if (isUntrustedObject(child)) { return renderUntrustedObject(child) } else { const str = child.toString() return str instanceof Promise ? (str as Promise<HtmlEscapedString>) : raw(str) }}
export type ErrorHandler = (error: Error) => voidexport type FallbackRender = (error: Error) => Child
/** * @experimental * `ErrorBoundary` is an experimental feature. * The API might be changed. */export const ErrorBoundary: FC< PropsWithChildren<{ fallback?: Child fallbackRender?: FallbackRender onError?: ErrorHandler }>> = async ({ children, fallback, fallbackRender, onError }) => { if (!children) { return raw('') }
if (!Array.isArray(children)) { children = [children] }
const nonce = useContext(StreamingContext)?.scriptNonce
let resume: ReturnType<typeof captureRenderContext> | undefined const getResume = () => (resume ||= captureRenderContext())
let fallbackStrPromise: Promise<HtmlEscapedString | string | undefined> | undefined const resolveFallbackStr = (): Promise<HtmlEscapedString | string | undefined> => (fallbackStrPromise ||= (async () => { const awaitedFallback = await fallback if (awaitedFallback === null || awaitedFallback === undefined) { return } if (typeof awaitedFallback === 'string' || Array.isArray(awaitedFallback)) { return getResume()(() => renderChildren([awaitedFallback])) } if (isUntrustedObject(awaitedFallback)) { return getResume()(() => renderUntrustedObject(awaitedFallback)) } const fallbackResult = await getResume()(() => awaitedFallback.toString()) return raw( fallbackResult, (fallbackResult as HtmlEscapedString).callbacks || (awaitedFallback as unknown as HtmlEscapedString).callbacks ) })()) const renderFallback = async (error: Error): Promise<HtmlEscapedString> => { const fallbackStr = await resolveFallbackStr() return getResume()(async () => { onError?.(error) const fallbackRes = ( fallbackStr !== undefined ? fallbackStr : (fallbackRender && jsx(Fragment, {}, fallbackRender(error) as HtmlEscapedString)) || '' ) as HtmlEscapedString const fallbackResString = await Fragment({ children: fallbackRes }).toString() return raw( fallbackResString, (fallbackResString as HtmlEscapedString).callbacks || fallbackRes.callbacks ) }) } let resArray: HtmlEscapedString[] | Promise<HtmlEscapedString[]>[] = [] try { resArray = children.map(resolveChildEarly) as unknown as HtmlEscapedString[] } catch (e) { const resume = getResume() if (e instanceof Promise) { resArray = [ e .then(() => resume(() => childrenToString(children as Child[]))) .catch((e) => renderFallback(e)), ] as Promise<HtmlEscapedString[]>[] } else { resArray = [await renderFallback(e as Error)] } }
if (resArray.some((res) => (res as {}) instanceof Promise)) { // Prime the context capture while still synchronous: a child that returned // a Promise from `resolveChildEarly` skipped the `catch`, so the deferred // `catchCallback` would otherwise capture too late. getResume() const index = errorBoundaryCounter++ const replaceRe = RegExp(`(<template id="E:${index}"></template>)(.*?)(<!--E:${index}-->)`, 's') let caught = false const catchCallback = async ({ error, buffer }: { error: Error; buffer?: [string] }) => { if (caught) { return '' } caught = true
const fallbackResString = await renderFallback(error) const fallbackCallbacks = fallbackResString.callbacks if (buffer) { buffer[0] = buffer[0].replace(replaceRe, () => fallbackResString) return fallbackCallbacks?.length ? raw('', fallbackCallbacks) : '' } return raw( `<template data-hono-target="E:${index}">${fallbackResString}</template><script>((d,c,n) => {c=d.currentScript.previousSiblingd=d.getElementById('E:${index}')if(!d)returndo{n=d.nextSibling;n.remove()}while(n.nodeType!=8||n.nodeValue!='E:${index}')d.replaceWith(c.content)})(document)</script>`, fallbackCallbacks ) }
let error: unknown const promiseAll = Promise.all(resArray).catch((e) => (error = e)) return raw(`<template id="E:${index}"></template><!--E:${index}-->`, [ ({ phase, buffer, context }) => { if (phase === HtmlEscapedCallbackPhase.BeforeStream) { return } return promiseAll .then(async (htmlArray: HtmlEscapedString[]) => { if (error) { throw error } htmlArray = htmlArray.flat() const content = htmlArray.join('') let html = buffer ? '' : `<template data-hono-target="E:${index}">${content}</template><script${ nonce ? ` nonce="${nonce}"` : '' }>((d,c) => {c=d.currentScript.previousSiblingd=d.getElementById('E:${index}')if(!d)returnd.parentElement.insertBefore(c.content,d.nextSibling)})(document)</script>`
if (htmlArray.every((html) => !(html as HtmlEscapedString).callbacks?.length)) { if (buffer) { buffer[0] = buffer[0].replace(replaceRe, () => content) } return html }
if (buffer) { buffer[0] = buffer[0].replace( replaceRe, (_all, pre, _, post) => `${pre}${content}${post}` ) }
const callbacks = htmlArray .map((html) => (html as HtmlEscapedString).callbacks || []) .flat()
if (phase === HtmlEscapedCallbackPhase.Stream) { html = await resolveCallback( html, HtmlEscapedCallbackPhase.BeforeStream, true, context ) }
let resolvedCount = 0 const promises = callbacks.map<HtmlEscapedCallback>( (c) => (...args) => c(...args) ?.then((content) => { resolvedCount++
if (buffer) { if (resolvedCount === callbacks.length) { buffer[0] = buffer[0].replace(replaceRe, (_all, _pre, content) => content) } buffer[0] += content return raw('', (content as HtmlEscapedString).callbacks) }
return raw( content + (resolvedCount !== callbacks.length ? '' : `<script>((d,c,n) => {d=d.getElementById('E:${index}')if(!d)returnn=d.nextSiblingwhile(n.nodeType!=8||n.nodeValue!='E:${index}'){n=n.nextSibling}n.remove()d.remove()})(document)</script>`), (content as HtmlEscapedString).callbacks ) }) .catch((error) => catchCallback({ error, buffer })) )
// eslint-disable-next-line @typescript-eslint/no-explicit-any return raw(html, promises as any) }) .catch((error) => catchCallback({ error, buffer })) }, ]) } else { return Fragment({ children: resArray as Child[] }) }};(ErrorBoundary as HasRenderToDom)[DOM_RENDERER] = ErrorBoundaryDomRenderer