mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 08:17:40 +00:00
perf(web-fetch): memoize readability dependency loading
This commit is contained in:
@@ -1,5 +1,32 @@
|
|||||||
export type ExtractMode = "markdown" | "text";
|
export type ExtractMode = "markdown" | "text";
|
||||||
|
|
||||||
|
let readabilityDepsPromise:
|
||||||
|
| Promise<{
|
||||||
|
Readability: typeof import("@mozilla/readability").Readability;
|
||||||
|
parseHTML: typeof import("linkedom").parseHTML;
|
||||||
|
}>
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
async function loadReadabilityDeps(): Promise<{
|
||||||
|
Readability: typeof import("@mozilla/readability").Readability;
|
||||||
|
parseHTML: typeof import("linkedom").parseHTML;
|
||||||
|
}> {
|
||||||
|
if (!readabilityDepsPromise) {
|
||||||
|
readabilityDepsPromise = Promise.all([import("@mozilla/readability"), import("linkedom")]).then(
|
||||||
|
([readability, linkedom]) => ({
|
||||||
|
Readability: readability.Readability,
|
||||||
|
parseHTML: linkedom.parseHTML,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return await readabilityDepsPromise;
|
||||||
|
} catch (error) {
|
||||||
|
readabilityDepsPromise = undefined;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function decodeEntities(value: string): string {
|
function decodeEntities(value: string): string {
|
||||||
return value
|
return value
|
||||||
.replace(/ /gi, " ")
|
.replace(/ /gi, " ")
|
||||||
@@ -94,10 +121,7 @@ export async function extractReadableContent(params: {
|
|||||||
return rendered;
|
return rendered;
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const [{ Readability }, { parseHTML }] = await Promise.all([
|
const { Readability, parseHTML } = await loadReadabilityDeps();
|
||||||
import("@mozilla/readability"),
|
|
||||||
import("linkedom"),
|
|
||||||
]);
|
|
||||||
const { document } = parseHTML(params.html);
|
const { document } = parseHTML(params.html);
|
||||||
try {
|
try {
|
||||||
(document as { baseURI?: string }).baseURI = params.url;
|
(document as { baseURI?: string }).baseURI = params.url;
|
||||||
|
|||||||
Reference in New Issue
Block a user