deno.land / x / hono@v4.2.5 / jsx / components.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { raw } from '../helper/html/index.ts'import type { HtmlEscapedString, HtmlEscapedCallback } from '../utils/html.ts'import { HtmlEscapedCallbackPhase, resolveCallback } from '../utils/html.ts'import { DOM_RENDERER } from './constants.ts'import { ErrorBoundary as ErrorBoundaryDomRenderer } from './dom/components.ts'import type { HasRenderToDom } from './dom/render.ts'import type { FC, PropsWithChildren, Child } from './index.ts'
let errorBoundaryCounter = 0
export const childrenToString = async (children: Child[]): Promise<HtmlEscapedString[]> => { try { return children .flat() .map((c) => (c == null || typeof c === 'boolean' ? '' : c.toString())) as HtmlEscapedString[] } catch (e) { if (e instanceof Promise) { await e return childrenToString(children) } else { throw e } }}
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] }
let fallbackStr: string | undefined const fallbackRes = (error: Error): HtmlEscapedString => { onError?.(error) return (fallbackStr || fallbackRender?.(error) || '').toString() as HtmlEscapedString } let resArray: HtmlEscapedString[] | Promise<HtmlEscapedString[]>[] = [] try { resArray = children.map((c) => c == null || typeof c === 'boolean' ? '' : c.toString() ) as HtmlEscapedString[] } catch (e) { fallbackStr = await fallback?.toString() if (e instanceof Promise) { resArray = [ e.then(() => childrenToString(children as Child[])).catch((e) => fallbackRes(e)), ] as Promise<HtmlEscapedString[]>[] } else { resArray = [fallbackRes(e as Error)] } }
if (resArray.some((res) => (res as {}) instanceof Promise)) { fallbackStr ||= await fallback?.toString() const index = errorBoundaryCounter++ const replaceRe = RegExp(`(<template id="E:${index}"></template>.*?)(.*?)(<!--E:${index}-->)`) const caught = false const catchCallback = ({ error, buffer }: { error: Error; buffer?: [string] }) => { if (caught) { return '' }
const fallbackResString = fallbackRes(error) if (buffer) { buffer[0] = buffer[0].replace(replaceRe, fallbackResString) } return buffer ? '' : `<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>` } return raw(`<template id="E:${index}"></template><!--E:${index}-->`, [ ({ phase, buffer, context }) => { if (phase === HtmlEscapedCallbackPhase.BeforeStream) { return } return Promise.all(resArray) .then(async (htmlArray) => { htmlArray = htmlArray.flat() const content = htmlArray.join('') let html = buffer ? '' : `<template data-hono-target="E:${index}">${content}</template><script>((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 raw(resArray.join('')) }};(ErrorBoundary as HasRenderToDom)[DOM_RENDERER] = ErrorBoundaryDomRenderer
hono

Version Info

Tagged at
a month ago