File size: 6,096 Bytes
edaa6b9 | 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 | import { HtmlEscapedCallbackPhase, raw, resolveCallback } from "../utils/html.js";
import { DOM_RENDERER } from "./constants.js";
import { captureRenderContext, useContext } from "./context.js";
import { Fragment, isUntrustedObject, jsx, renderChildren, renderUntrustedObject } from "./base.js";
import { ErrorBoundary as ErrorBoundary$1 } from "./dom/components.js";
import { StreamingContext } from "./streaming.js";
//#region src/jsx/components.ts
let errorBoundaryCounter = 0;
const childrenToString = async (children) => {
try {
return children.flat().map(resolveChildEarly);
} catch (e) {
if (e instanceof Promise) {
const resume = captureRenderContext();
await e;
return resume(() => childrenToString(children));
} else throw e;
}
};
const resolveChildEarly = (child) => {
if (child == null || typeof child === "boolean") return "";
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 : raw(str);
}
};
/**
* @experimental
* `ErrorBoundary` is an experimental feature.
* The API might be changed.
*/
const ErrorBoundary = async ({ children, fallback, fallbackRender, onError }) => {
if (!children) return raw("");
if (!Array.isArray(children)) children = [children];
const nonce = useContext(StreamingContext)?.scriptNonce;
let resume;
const getResume = () => resume ||= captureRenderContext();
let fallbackStrPromise;
const resolveFallbackStr = () => fallbackStrPromise ||= (async () => {
const awaitedFallback = await fallback;
if (awaitedFallback === null || awaitedFallback === void 0) 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.callbacks || awaitedFallback.callbacks);
})();
const renderFallback = async (error) => {
const fallbackStr = await resolveFallbackStr();
return getResume()(async () => {
onError?.(error);
const fallbackRes = fallbackStr !== void 0 ? fallbackStr : fallbackRender && jsx(Fragment, {}, fallbackRender(error)) || "";
const fallbackResString = await Fragment({ children: fallbackRes }).toString();
return raw(fallbackResString, fallbackResString.callbacks || fallbackRes.callbacks);
});
};
let resArray = [];
try {
resArray = children.map(resolveChildEarly);
} catch (e) {
const resume = getResume();
if (e instanceof Promise) resArray = [e.then(() => resume(() => childrenToString(children))).catch((e) => renderFallback(e))];
else resArray = [await renderFallback(e)];
}
if (resArray.some((res) => res instanceof Promise)) {
getResume();
const index = errorBoundaryCounter++;
const replaceRe = RegExp(`(<template id="E:${index}"></template>)(.*?)(<!--E:${index}-->)`, "s");
let caught = false;
const catchCallback = async ({ error, buffer }) => {
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${nonce ? ` nonce="${nonce}"` : ""}>
((d,c,n) => {
c=d.currentScript.previousSibling
d=d.getElementById('E:${index}')
if(!d)return
do{n=d.nextSibling;n.remove()}while(n.nodeType!=8||n.nodeValue!='E:${index}')
d.replaceWith(c.content)
})(document)
<\/script>`, fallbackCallbacks);
};
let error;
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) => {
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.previousSibling
d=d.getElementById('E:${index}')
if(!d)return
d.parentElement.insertBefore(c.content,d.nextSibling)
})(document)
<\/script>`;
if (htmlArray.every((html) => !html.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.callbacks || []).flat();
if (phase === HtmlEscapedCallbackPhase.Stream) html = await resolveCallback(html, HtmlEscapedCallbackPhase.BeforeStream, true, context);
let resolvedCount = 0;
const promises = callbacks.map((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.callbacks);
}
return raw(content + (resolvedCount !== callbacks.length ? "" : `<script${nonce ? ` nonce="${nonce}"` : ""}>
((d,c,n) => {
d=d.getElementById('E:${index}')
if(!d)return
n=d.nextSibling
while(n.nodeType!=8||n.nodeValue!='E:${index}'){n=n.nextSibling}
n.remove()
d.remove()
})(document)
<\/script>`), content.callbacks);
}).catch((error) => catchCallback({
error,
buffer
})));
return raw(html, promises);
}).catch((error) => catchCallback({
error,
buffer
}));
}]);
} else return Fragment({ children: resArray });
};
ErrorBoundary[DOM_RENDERER] = ErrorBoundary$1;
//#endregion
export { ErrorBoundary, childrenToString };
|